From f5eaf1b0b02efea9b67379e1d1a8b885de39ce38 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Sat, 27 Sep 2014 18:54:07 +0200 Subject: [PATCH 1/3] Parsing serialized Bind with given access mode fails readWrite() and readOnly() both fail due to implementation errors in Bind.parse() --- .../github/dockerjava/api/model/BindTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/test/java/com/github/dockerjava/api/model/BindTest.java diff --git a/src/test/java/com/github/dockerjava/api/model/BindTest.java b/src/test/java/com/github/dockerjava/api/model/BindTest.java new file mode 100644 index 000000000..b77744520 --- /dev/null +++ b/src/test/java/com/github/dockerjava/api/model/BindTest.java @@ -0,0 +1,38 @@ +package com.github.dockerjava.api.model; + +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +public class BindTest { + + @Test + public void parseUsingDefaultAccessMode() { + Bind bind = Bind.parse("/host:/container"); + assertEquals(bind.getPath(), "/host"); + assertEquals(bind.getVolume().getPath(), "/container"); + assertEquals(bind.isReadOnly(), false); + } + + @Test + public void parseReadWrite() { + Bind bind = Bind.parse("/host:/container:rw"); + assertEquals(bind.getPath(), "/host"); + assertEquals(bind.getVolume().getPath(), "/container"); + assertEquals(bind.isReadOnly(), false); + } + + @Test + public void parseReadOnly() { + Bind bind = Bind.parse("/host:/container:ro"); + assertEquals(bind.getPath(), "/host"); + assertEquals(bind.getVolume().getPath(), "/container"); + assertEquals(bind.isReadOnly(), true); + } + + @Test(expectedExceptions = RuntimeException.class) + public void parseInvalidAccessMode() { + Bind.parse("/host:/container:xx"); + } + +} From 1b74f6cca3ffab7b525a315aa74233388bc9def8 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Sat, 27 Sep 2014 19:14:25 +0200 Subject: [PATCH 2/3] Fix and improve parsing of serialized Bind --- .../java/com/github/dockerjava/api/model/Bind.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/dockerjava/api/model/Bind.java b/src/main/java/com/github/dockerjava/api/model/Bind.java index 6779e2580..e654c7d5c 100644 --- a/src/main/java/com/github/dockerjava/api/model/Bind.java +++ b/src/main/java/com/github/dockerjava/api/model/Bind.java @@ -33,6 +33,12 @@ public boolean isReadOnly() { return readOnly; } + /** + * Parses a bind mount specification to a {@link Bind}. + * + * @param serialized the specification, e.g. /host:/container:ro + * @return a {@link Bind} matching the specification + */ public static Bind parse(String serialized) { try { String[] parts = serialized.split(":"); @@ -41,7 +47,9 @@ public static Bind parse(String serialized) { return new Bind(parts[0], Volume.parse(parts[1])); } case 3: { - if ("rw".equals(parts[3].toLowerCase())) + if ("rw".equals(parts[2].toLowerCase())) + return new Bind(parts[0], Volume.parse(parts[1]), false); + else if ("ro".equals(parts[2].toLowerCase())) return new Bind(parts[0], Volume.parse(parts[1]), true); else throw new RuntimeException("Error parsing Bind '" From ed92136784dfa31477392e4f4f61d63d072e4b23 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Sun, 28 Sep 2014 13:58:38 +0200 Subject: [PATCH 3/3] Parsing invalid serialized Bind throws IllegalArgumentException --- .../com/github/dockerjava/api/model/Bind.java | 9 ++++----- .../github/dockerjava/api/model/BindTest.java | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/github/dockerjava/api/model/Bind.java b/src/main/java/com/github/dockerjava/api/model/Bind.java index e654c7d5c..0c4dd3150 100644 --- a/src/main/java/com/github/dockerjava/api/model/Bind.java +++ b/src/main/java/com/github/dockerjava/api/model/Bind.java @@ -38,6 +38,7 @@ public boolean isReadOnly() { * * @param serialized the specification, e.g. /host:/container:ro * @return a {@link Bind} matching the specification + * @throws IllegalArgumentException if the specification cannot be parsed */ public static Bind parse(String serialized) { try { @@ -52,16 +53,14 @@ public static Bind parse(String serialized) { else if ("ro".equals(parts[2].toLowerCase())) return new Bind(parts[0], Volume.parse(parts[1]), true); else - throw new RuntimeException("Error parsing Bind '" - + serialized + "'"); + throw new IllegalArgumentException(); } default: { - throw new RuntimeException("Error parsing Bind '" + serialized - + "'"); + throw new IllegalArgumentException(); } } } catch (Exception e) { - throw new RuntimeException("Error parsing Bind '" + serialized + throw new IllegalArgumentException("Error parsing Bind '" + serialized + "'"); } } diff --git a/src/test/java/com/github/dockerjava/api/model/BindTest.java b/src/test/java/com/github/dockerjava/api/model/BindTest.java index b77744520..412c537fa 100644 --- a/src/test/java/com/github/dockerjava/api/model/BindTest.java +++ b/src/test/java/com/github/dockerjava/api/model/BindTest.java @@ -30,9 +30,22 @@ public void parseReadOnly() { assertEquals(bind.isReadOnly(), true); } - @Test(expectedExceptions = RuntimeException.class) + @Test(expectedExceptions = IllegalArgumentException.class, + expectedExceptionsMessageRegExp = "Error parsing Bind.*") public void parseInvalidAccessMode() { Bind.parse("/host:/container:xx"); } - + + @Test(expectedExceptions = IllegalArgumentException.class, + expectedExceptionsMessageRegExp = "Error parsing Bind 'nonsense'") + public void parseInvalidInput() { + Bind.parse("nonsense"); + } + + @Test(expectedExceptions = IllegalArgumentException.class, + expectedExceptionsMessageRegExp = "Error parsing Bind 'null'") + public void parseNull() { + Bind.parse(null); + } + }