X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/main/java/com/github/dockerjava/api/model/Bind.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public boolean isReadOnly() {
return readOnly;
}

/**
* Parses a bind mount specification to a {@link Bind}.
*
* @param serialized the specification, e.g. <code>/host:/container:ro</code>
* @return a {@link Bind} matching the specification
* @throws IllegalArgumentException if the specification cannot be parsed
*/
public static Bind parse(String serialized) {
try {
String[] parts = serialized.split(":");
Expand All @@ -41,19 +48,19 @@ 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 '"
+ 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
+ "'");
}
}
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/com/github/dockerjava/api/model/BindTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 = 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);
}

}
X Tutup