Skip to content

Commit 0699878

Browse files
cdancyKostyaSha
authored andcommitted
Expose 'User' property on ExecCreateCmd (docker-java#707) (docker-java#708)
* Expose 'User' property on ExecCreateCmd (docker-java#707)
1 parent 1ec83d6 commit 0699878

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public interface ExecCreateCmd extends SyncDockerCmd<ExecCreateCmdResponse> {
2020
@CheckForNull
2121
Boolean hasTtyEnabled();
2222

23+
@CheckForNull
24+
String getUser();
25+
2326
ExecCreateCmd withAttachStderr(Boolean attachStderr);
2427

2528
ExecCreateCmd withAttachStdin(Boolean attachStdin);
@@ -32,6 +35,8 @@ public interface ExecCreateCmd extends SyncDockerCmd<ExecCreateCmdResponse> {
3235

3336
ExecCreateCmd withTty(Boolean tty);
3437

38+
ExecCreateCmd withUser(String user);
39+
3540
interface Exec extends DockerCmdSyncExec<ExecCreateCmd, ExecCreateCmdResponse> {
3641
}
3742

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public class ExecCreateCmdImpl extends AbstrDockerCmd<ExecCreateCmd, ExecCreateC
2626
@JsonProperty("Tty")
2727
private Boolean tty;
2828

29+
/**
30+
* @since {@link RemoteApiVersion#VERSION_1_19}
31+
*/
32+
@JsonProperty("User")
33+
private String user;
34+
2935
@JsonProperty("Cmd")
3036
private String[] cmd;
3137

@@ -65,6 +71,12 @@ public ExecCreateCmd withTty(Boolean tty) {
6571
return this;
6672
}
6773

74+
@Override
75+
public ExecCreateCmd withUser(String user) {
76+
this.user = user;
77+
return this;
78+
}
79+
6880
@Override
6981
public ExecCreateCmd withCmd(String... cmd) {
7082
this.cmd = cmd;
@@ -96,6 +108,11 @@ public Boolean hasTtyEnabled() {
96108
return tty;
97109
}
98110

111+
@Override
112+
public String getUser() {
113+
return user;
114+
}
115+
99116
/**
100117
* @throws NotFoundException
101118
* No such container

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void afterMethod(ITestResult result) {
4444
public void execCreateTest() {
4545
String containerName = "generated_" + new SecureRandom().nextInt();
4646

47-
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("top")
47+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withUser("root").withCmd("top")
4848
.withName(containerName).exec();
4949

5050
LOG.info("Created container {}", container.toString());

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
package com.github.dockerjava.core.command;
22

3-
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.Matchers.isEmptyString;
5-
import static org.hamcrest.Matchers.not;
6-
3+
import com.github.dockerjava.api.command.CreateContainerResponse;
4+
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
5+
import com.github.dockerjava.api.exception.NotFoundException;
6+
import com.github.dockerjava.client.AbstractDockerClientTest;
77
import java.io.InputStream;
88
import java.lang.reflect.Method;
99
import java.security.SecureRandom;
10-
10+
import static org.hamcrest.MatcherAssert.assertThat;
11+
import static org.hamcrest.Matchers.isEmptyString;
12+
import static org.hamcrest.Matchers.not;
1113
import org.testng.ITestResult;
1214
import org.testng.annotations.AfterMethod;
1315
import org.testng.annotations.AfterTest;
1416
import org.testng.annotations.BeforeMethod;
1517
import org.testng.annotations.BeforeTest;
1618
import org.testng.annotations.Test;
1719

18-
import com.github.dockerjava.api.command.CreateContainerResponse;
19-
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
20-
import com.github.dockerjava.client.AbstractDockerClientTest;
21-
2220
@Test(groups = "integration")
2321
public class ExecStartCmdImplTest extends AbstractDockerClientTest {
2422
@BeforeTest
@@ -53,7 +51,7 @@ public void execStart() throws Exception {
5351
dockerClient.startContainerCmd(container.getId()).exec();
5452

5553
ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId())
56-
.withAttachStdout(true).withCmd("touch", "/execStartTest.log").exec();
54+
.withAttachStdout(true).withCmd("touch", "/execStartTest.log").withUser("root").exec();
5755
dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec(
5856
new ExecStartResultCallback(System.out, System.err)).awaitCompletion();
5957

@@ -92,4 +90,23 @@ public void execStartAttached() throws Exception {
9290
assertNotNull(responseAsString);
9391
assertTrue(responseAsString.length() > 0);
9492
}
93+
94+
@Test(groups = "ignoreInCircleCi", expectedExceptions = NotFoundException.class)
95+
public void execStartWithNonExistentUser() throws Exception {
96+
String containerName = "generated_" + new SecureRandom().nextInt();
97+
98+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999")
99+
.withName(containerName).exec();
100+
LOG.info("Created container {}", container.toString());
101+
assertThat(container.getId(), not(isEmptyString()));
102+
103+
dockerClient.startContainerCmd(container.getId()).exec();
104+
105+
ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId())
106+
.withAttachStdout(true).withCmd("touch", "/execStartTest.log").withUser("NonExistentUser").exec();
107+
dockerClient.execStartCmd(execCreateCmdResponse.getId()).withDetach(false).withTty(true)
108+
.exec(new ExecStartResultCallback(System.out, System.err)).awaitCompletion();
109+
110+
dockerClient.copyArchiveFromContainerCmd(container.getId(), "/execStartTest.log").exec();
111+
}
95112
}

0 commit comments

Comments
 (0)