Skip to content

Commit e8d9896

Browse files
author
Andrey Klimachev
committed
Adding tests for Exec-start and Exec-create.
1 parent ab7aa66 commit e8d9896

10 files changed

Lines changed: 136 additions & 23 deletions

File tree

src/main/java/com/github/dockerjava/api/DockerClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public CreateImageCmd createImageCmd(String repository,
6464
*/
6565
public StartContainerCmd startContainerCmd(String containerId);
6666

67-
public ExecCreateCmd execCmd(String containerId);
67+
public ExecCreateCmd execCreateCmd(String containerId);
6868

6969
public InspectContainerCmd inspectContainerCmd(String containerId);
7070

src/main/java/com/github/dockerjava/api/command/ExecCreateCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.dockerjava.api.command;
22

3-
public interface ExecCreateCmd extends DockerCmd<ExecCreateCmdResponce> {
3+
public interface ExecCreateCmd extends DockerCmd<ExecCreateCmdResponse> {
44

55
public String getContainerId();
66

@@ -14,6 +14,6 @@ public interface ExecCreateCmd extends DockerCmd<ExecCreateCmdResponce> {
1414

1515
public ExecCreateCmd tty(boolean tty);
1616

17-
public static interface Exec extends DockerCmdExec<ExecCreateCmd, ExecCreateCmdResponce> {
17+
public static interface Exec extends DockerCmdExec<ExecCreateCmd, ExecCreateCmdResponse> {
1818
}
1919
}

src/main/java/com/github/dockerjava/api/command/ExecCreateCmdResponce.java renamed to src/main/java/com/github/dockerjava/api/command/ExecCreateCmdResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.fasterxml.jackson.annotation.JsonProperty;
55

66
@JsonIgnoreProperties(ignoreUnknown = true)
7-
public class ExecCreateCmdResponce {
7+
public class ExecCreateCmdResponse {
88

99
@JsonProperty("Id")
1010
private String id;

src/main/java/com/github/dockerjava/api/command/ExecStartCmd.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ public interface ExecStartCmd extends DockerCmd<InputStream>{
1212

1313
public boolean isTty();
1414

15-
public ExecStartCmd withContainerId(String containerId);
16-
1715
/**
1816
* @throws com.github.dockerjava.api.NotFoundException No such container
1917
*/

src/main/java/com/github/dockerjava/core/DockerClientImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public InspectContainerCmd inspectContainerCmd(String containerId) {
194194
}
195195

196196
@Override
197-
public ExecCreateCmd execCmd(String containerId) {
197+
public ExecCreateCmd execCreateCmd(String containerId) {
198198
return new ExecCreateCmdImpl(getDockerCmdExecFactory().createExecCmdExec(), containerId);
199199
}
200200

src/main/java/com/github/dockerjava/core/command/ExecCreateCmdImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.github.dockerjava.api.NotFoundException;
55
import com.github.dockerjava.api.command.ExecCreateCmd;
6-
import com.github.dockerjava.api.command.ExecCreateCmdResponce;
6+
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
77

8-
public class ExecCreateCmdImpl extends AbstrDockerCmd<ExecCreateCmd, ExecCreateCmdResponce> implements ExecCreateCmd {
8+
public class ExecCreateCmdImpl extends AbstrDockerCmd<ExecCreateCmd, ExecCreateCmdResponse> implements ExecCreateCmd {
99

1010
private String containerId;
1111

@@ -63,7 +63,7 @@ public String getContainerId() {
6363
* @throws NotFoundException No such container
6464
*/
6565
@Override
66-
public ExecCreateCmdResponce exec() throws NotFoundException {
66+
public ExecCreateCmdResponse exec() throws NotFoundException {
6767
return super.exec();
6868
}
6969
}

src/main/java/com/github/dockerjava/core/command/ExecStartCmdImpl.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
import java.io.InputStream;
88

9-
public class ExecStartCmdImpl extends AbstrDockerCmd<ExecStartCmd, InputStream> implements ExecStartCmd {
9+
public class ExecStartCmdImpl extends AbstrDockerCmd<ExecStartCmd, InputStream> implements ExecStartCmd {
1010

1111
private String containerId;
1212

1313
private boolean detach, tty;
1414

1515
public ExecStartCmdImpl(ExecStartCmd.Exec exec, String containerId) {
1616
super(exec);
17-
withContainerId(containerId);
17+
Preconditions.checkNotNull(containerId, "containerId was not specified");
18+
this.containerId = containerId;
1819
}
1920

2021
@Override
@@ -32,13 +33,6 @@ public boolean isTty() {
3233
return tty;
3334
}
3435

35-
@Override
36-
public ExecStartCmdImpl withContainerId(String containerId) {
37-
Preconditions.checkNotNull(containerId, "containerId was not specified");
38-
this.containerId = containerId;
39-
return this;
40-
}
41-
4236
/**
4337
* @throws com.github.dockerjava.api.NotFoundException No such container
4438
*/
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.dockerjava.jaxrs;
22

33
import com.github.dockerjava.api.command.ExecCreateCmd;
4-
import com.github.dockerjava.api.command.ExecCreateCmdResponce;
4+
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

@@ -10,7 +10,7 @@
1010

1111
import static javax.ws.rs.client.Entity.entity;
1212

13-
public class ExecCreateCmdExec extends AbstrDockerCmdExec<ExecCreateCmd, ExecCreateCmdResponce> implements ExecCreateCmd.Exec {
13+
public class ExecCreateCmdExec extends AbstrDockerCmdExec<ExecCreateCmd, ExecCreateCmdResponse> implements ExecCreateCmd.Exec {
1414

1515
private static final Logger LOGGER = LoggerFactory
1616
.getLogger(VersionCmdExec.class);
@@ -20,11 +20,11 @@ public ExecCreateCmdExec(WebTarget baseResource) {
2020
}
2121

2222
@Override
23-
protected ExecCreateCmdResponce execute(ExecCreateCmd command) {
23+
protected ExecCreateCmdResponse execute(ExecCreateCmd command) {
2424
WebTarget webResource = getBaseResource().path("/containers/{id}/exec").resolveTemplate("id", command.getContainerId());
2525

2626
LOGGER.trace("POST: {}", webResource);
2727

28-
return webResource.request().accept(MediaType.APPLICATION_JSON).post(entity(command, MediaType.APPLICATION_JSON), ExecCreateCmdResponce.class);
28+
return webResource.request().accept(MediaType.APPLICATION_JSON).post(entity(command, MediaType.APPLICATION_JSON), ExecCreateCmdResponse.class);
2929
}
3030
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.github.dockerjava.core.command;
2+
3+
import com.github.dockerjava.api.DockerException;
4+
import com.github.dockerjava.api.command.CreateContainerResponse;
5+
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
6+
import com.github.dockerjava.client.AbstractDockerClientTest;
7+
import org.testng.ITestResult;
8+
import org.testng.annotations.*;
9+
10+
import java.lang.reflect.Method;
11+
import java.security.SecureRandom;
12+
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
import static org.hamcrest.Matchers.isEmptyString;
15+
import static org.hamcrest.Matchers.not;
16+
17+
@Test(groups = "integration")
18+
public class ExecCreateCmdImplTest extends AbstractDockerClientTest {
19+
@BeforeTest
20+
public void beforeTest() throws DockerException {
21+
super.beforeTest();
22+
}
23+
24+
@AfterTest
25+
public void afterTest() {
26+
super.afterTest();
27+
}
28+
29+
@BeforeMethod
30+
public void beforeMethod(Method method) {
31+
super.beforeMethod(method);
32+
}
33+
34+
@AfterMethod
35+
public void afterMethod(ITestResult result) {
36+
super.afterMethod(result);
37+
}
38+
39+
@Test
40+
public void execCreateTest() {
41+
String containerName = "generated_" + new SecureRandom().nextInt();
42+
43+
CreateContainerResponse container = dockerClient
44+
.createContainerCmd("busybox").withCmd("env")
45+
.withName(containerName).exec();
46+
47+
LOG.info("Created container {}", container.toString());
48+
49+
assertThat(container.getId(), not(isEmptyString()));
50+
51+
dockerClient.startContainerCmd(container.getId()).exec();
52+
53+
dockerClient.waitContainerCmd(container.getId()).exec();
54+
55+
ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId()).withCmd("touch file.log").exec();
56+
57+
assertThat(execCreateCmdResponse.getId(), not(isEmptyString()));
58+
}
59+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.github.dockerjava.core.command;
2+
3+
import com.github.dockerjava.api.DockerException;
4+
import com.github.dockerjava.api.command.CreateContainerResponse;
5+
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
6+
import com.github.dockerjava.client.AbstractDockerClientTest;
7+
import org.testng.ITestResult;
8+
import org.testng.annotations.*;
9+
10+
import java.io.InputStream;
11+
import java.lang.reflect.Method;
12+
import java.security.SecureRandom;
13+
14+
import static org.hamcrest.MatcherAssert.assertThat;
15+
import static org.hamcrest.Matchers.isEmptyString;
16+
import static org.hamcrest.Matchers.not;
17+
18+
public class ExecStartCmdImplTest extends AbstractDockerClientTest {
19+
@BeforeTest
20+
public void beforeTest() throws DockerException {
21+
super.beforeTest();
22+
}
23+
24+
@AfterTest
25+
public void afterTest() {
26+
super.afterTest();
27+
}
28+
29+
@BeforeMethod
30+
public void beforeMethod(Method method) {
31+
super.beforeMethod(method);
32+
}
33+
34+
@AfterMethod
35+
public void afterMethod(ITestResult result) {
36+
super.afterMethod(result);
37+
}
38+
39+
@Test
40+
public void execStartTest() throws Exception {
41+
String containerName = "generated_" + new SecureRandom().nextInt();
42+
43+
CreateContainerResponse container = dockerClient
44+
.createContainerCmd("busybox").withCmd("env")
45+
.withName(containerName).exec();
46+
47+
LOG.info("Created container {}", container.toString());
48+
49+
assertThat(container.getId(), not(isEmptyString()));
50+
51+
dockerClient.startContainerCmd(container.getId()).exec();
52+
53+
dockerClient.waitContainerCmd(container.getId()).exec();
54+
55+
ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId()).withCmd("touch","file.log").exec();
56+
57+
dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec();
58+
59+
InputStream response = dockerClient.copyFileFromContainerCmd(container.getId(), "file.log").exec();
60+
assertTrue(response.available() > 0, "The file was not copied from the container.");
61+
}
62+
}

0 commit comments

Comments
 (0)