Skip to content

Commit 96d01bf

Browse files
Marcus LinkeKostyaSha
authored andcommitted
Improved volume API tests
1 parent 2f82aab commit 96d01bf

File tree

7 files changed

+94
-8
lines changed

7 files changed

+94
-8
lines changed

src/main/java/com/github/dockerjava/netty/InvocationBuilder.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,6 @@ private void sendRequest(HttpRequestProvider requestProvider, Channel channel) {
378378
channelFuture.addListener(new ChannelFutureListener() {
379379
@Override
380380
public void operationComplete(ChannelFuture future) throws Exception {
381-
if (future.isSuccess()) {
382-
System.err.println("Request success");
383-
}
384-
385381
}
386382
});
387383
}

src/test/java/com/github/dockerjava/client/AbstractDockerClientTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ public void afterMethod(ITestResult result) {
109109
}
110110
}
111111

112+
for (String volume : dockerCmdExecFactory.getVolumeNames()) {
113+
LOG.info("Cleaning up temporary volume with {}", volume);
114+
try {
115+
dockerClient.removeVolumeCmd(volume).exec();
116+
} catch (DockerException ignore) {
117+
// ignore.printStackTrace();
118+
}
119+
}
120+
112121
LOG.info("################################## END OF {} ##################################\n", result.getName());
113122
}
114123

src/test/java/com/github/dockerjava/core/TestDockerCmdExecFactory.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.github.dockerjava.api.command.CreateImageCmd;
2020
import com.github.dockerjava.api.command.CreateImageResponse;
2121
import com.github.dockerjava.api.command.CreateVolumeCmd;
22+
import com.github.dockerjava.api.command.CreateVolumeResponse;
2223
import com.github.dockerjava.api.command.DockerCmdExecFactory;
2324
import com.github.dockerjava.api.command.EventsCmd;
2425
import com.github.dockerjava.api.command.ExecCreateCmd;
@@ -65,6 +66,8 @@ public class TestDockerCmdExecFactory implements DockerCmdExecFactory {
6566

6667
private List<String> imageNames = new ArrayList<String>();
6768

69+
private List<String> volumeNames = new ArrayList<String>();
70+
6871
private DockerCmdExecFactory delegate;
6972

7073
public TestDockerCmdExecFactory(DockerCmdExecFactory delegate) {
@@ -314,7 +317,14 @@ public StatsCmd.Exec createStatsCmdExec() {
314317

315318
@Override
316319
public CreateVolumeCmd.Exec createCreateVolumeCmdExec() {
317-
return delegate.createCreateVolumeCmdExec();
320+
return new CreateVolumeCmd.Exec() {
321+
@Override
322+
public CreateVolumeResponse exec(CreateVolumeCmd command) {
323+
CreateVolumeResponse result = delegate.createCreateVolumeCmdExec().exec(command);
324+
volumeNames.add(command.getName());
325+
return result;
326+
}
327+
};
318328
}
319329

320330
@Override
@@ -324,7 +334,14 @@ public InspectVolumeCmd.Exec createInspectVolumeCmdExec() {
324334

325335
@Override
326336
public RemoveVolumeCmd.Exec createRemoveVolumeCmdExec() {
327-
return delegate.createRemoveVolumeCmdExec();
337+
return new RemoveVolumeCmd.Exec() {
338+
@Override
339+
public Void exec(RemoveVolumeCmd command) {
340+
delegate.createRemoveVolumeCmdExec().exec(command);
341+
volumeNames.remove(command.getName());
342+
return null;
343+
}
344+
};
328345
}
329346

330347
@Override
@@ -339,4 +356,8 @@ public List<String> getContainerNames() {
339356
public List<String> getImageNames() {
340357
return new ArrayList<String>(imageNames);
341358
}
359+
360+
public List<String> getVolumeNames() {
361+
return new ArrayList<String>(volumeNames);
362+
}
342363
}

src/test/java/com/github/dockerjava/core/command/CreateVolumeCmdImplTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,33 @@ public void afterMethod(ITestResult result) {
4343
@Test
4444
public void createVolume() throws DockerException {
4545

46-
CreateVolumeResponse createVolumeResponse = dockerClient.createVolumeCmd().withName("volume1")
46+
String volumeName = "volume1";
47+
48+
CreateVolumeResponse createVolumeResponse = dockerClient.createVolumeCmd().withName(volumeName)
4749
.withDriver("local").exec();
4850

49-
assertThat(createVolumeResponse.getName(), equalTo("volume1"));
51+
assertThat(createVolumeResponse.getName(), equalTo(volumeName));
5052
assertThat(createVolumeResponse.getDriver(), equalTo("local"));
5153
assertThat(createVolumeResponse.getMountpoint(), containsString("/volume1/"));
5254
}
55+
56+
@Test
57+
public void createVolumeWithExistingName() throws DockerException {
58+
59+
String volumeName = "volume1";
60+
61+
CreateVolumeResponse createVolumeResponse1 = dockerClient.createVolumeCmd().withName(volumeName)
62+
.withDriver("local").exec();
63+
64+
assertThat(createVolumeResponse1.getName(), equalTo(volumeName));
65+
assertThat(createVolumeResponse1.getDriver(), equalTo("local"));
66+
assertThat(createVolumeResponse1.getMountpoint(), containsString("/volume1/"));
67+
68+
CreateVolumeResponse createVolumeResponse2 = dockerClient.createVolumeCmd().withName(volumeName)
69+
.withDriver("local").exec();
70+
71+
assertThat(createVolumeResponse2.getName(), equalTo(volumeName));
72+
assertThat(createVolumeResponse2.getDriver(), equalTo("local"));
73+
assertThat(createVolumeResponse2.getMountpoint(), equalTo(createVolumeResponse1.getMountpoint()));
74+
}
5375
}

src/test/java/com/github/dockerjava/core/command/InspectVolumeCmdImplTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.github.dockerjava.api.command.InspectVolumeResponse;
1717
import com.github.dockerjava.api.exception.DockerException;
18+
import com.github.dockerjava.api.exception.NotFoundException;
1819
import com.github.dockerjava.client.AbstractDockerClientTest;
1920

2021
@Test(groups = "integration")
@@ -53,4 +54,12 @@ public void inspectVolume() throws DockerException {
5354
assertThat(inspectVolumeResponse.getDriver(), equalTo("local"));
5455
assertThat(inspectVolumeResponse.getMountpoint(), containsString("/volume1/"));
5556
}
57+
58+
@Test(expectedExceptions = NotFoundException.class)
59+
public void inspectNonExistentVolume() throws DockerException {
60+
61+
String volumeName = "non-existing";
62+
63+
dockerClient.inspectVolumeCmd(volumeName).exec();
64+
}
5665
}

src/test/java/com/github/dockerjava/netty/exec/CreateVolumeCmdExecTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,24 @@ public void createVolume() throws DockerException {
5050
assertThat(createVolumeResponse.getDriver(), equalTo("local"));
5151
assertThat(createVolumeResponse.getMountpoint(), containsString("/volume1/"));
5252
}
53+
54+
@Test
55+
public void createVolumeWithExistingName() throws DockerException {
56+
57+
String volumeName = "volume1";
58+
59+
CreateVolumeResponse createVolumeResponse1 = dockerClient.createVolumeCmd().withName(volumeName)
60+
.withDriver("local").exec();
61+
62+
assertThat(createVolumeResponse1.getName(), equalTo(volumeName));
63+
assertThat(createVolumeResponse1.getDriver(), equalTo("local"));
64+
assertThat(createVolumeResponse1.getMountpoint(), containsString("/volume1/"));
65+
66+
CreateVolumeResponse createVolumeResponse2 = dockerClient.createVolumeCmd().withName(volumeName)
67+
.withDriver("local").exec();
68+
69+
assertThat(createVolumeResponse2.getName(), equalTo(volumeName));
70+
assertThat(createVolumeResponse2.getDriver(), equalTo("local"));
71+
assertThat(createVolumeResponse2.getMountpoint(), equalTo(createVolumeResponse1.getMountpoint()));
72+
}
5373
}

src/test/java/com/github/dockerjava/netty/exec/InspectVolumeCmdExecTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.github.dockerjava.api.command.InspectVolumeResponse;
1717
import com.github.dockerjava.api.exception.DockerException;
18+
import com.github.dockerjava.api.exception.NotFoundException;
1819
import com.github.dockerjava.netty.AbstractNettyDockerClientTest;
1920

2021
@Test(groups = "integration")
@@ -53,4 +54,12 @@ public void inspectVolume() throws DockerException {
5354
assertThat(inspectVolumeResponse.getDriver(), equalTo("local"));
5455
assertThat(inspectVolumeResponse.getMountpoint(), containsString("/volume1/"));
5556
}
57+
58+
@Test(expectedExceptions = NotFoundException.class)
59+
public void inspectNonExistentVolume() throws DockerException {
60+
61+
String volumeName = "non-existing";
62+
63+
dockerClient.inspectVolumeCmd(volumeName).exec();
64+
}
5665
}

0 commit comments

Comments
 (0)