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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public interface ExecCreateCmd extends SyncDockerCmd<ExecCreateCmdResponse> {
@CheckForNull
Boolean hasTtyEnabled();

@CheckForNull
String getUser();

ExecCreateCmd withAttachStderr(Boolean attachStderr);

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

ExecCreateCmd withTty(Boolean tty);

ExecCreateCmd withUser(String user);

interface Exec extends DockerCmdSyncExec<ExecCreateCmd, ExecCreateCmdResponse> {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public class ExecCreateCmdImpl extends AbstrDockerCmd<ExecCreateCmd, ExecCreateC
@JsonProperty("Tty")
private Boolean tty;

/**
* @since {@link RemoteApiVersion#VERSION_1_19}
*/
@JsonProperty("User")
private String user;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since what api version it appeared? existed all the time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged for docker 1.7.0 which is analogous to api version 1.19.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put @since ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.


@JsonProperty("Cmd")
private String[] cmd;

Expand Down Expand Up @@ -65,6 +71,12 @@ public ExecCreateCmd withTty(Boolean tty) {
return this;
}

@Override
public ExecCreateCmd withUser(String user) {
this.user = user;
return this;
}

@Override
public ExecCreateCmd withCmd(String... cmd) {
this.cmd = cmd;
Expand Down Expand Up @@ -96,6 +108,11 @@ public Boolean hasTtyEnabled() {
return tty;
}

@Override
public String getUser() {
return user;
}

/**
* @throws NotFoundException
* No such container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void afterMethod(ITestResult result) {
public void execCreateTest() {
String containerName = "generated_" + new SecureRandom().nextInt();

CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("top")
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withUser("root").withCmd("top")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add user option here to show that creating the command works as expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make separate test, root will be used by default afair.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found, depends on when param was added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I guess my thinking was that setting the user, even though it's the default, would show that it works as expected without having to create a new test for this one option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, let's keep

.withName(containerName).exec();

LOG.info("Created container {}", container.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package com.github.dockerjava.core.command;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.not;

import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.client.AbstractDockerClientTest;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.security.SecureRandom;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.not;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
import com.github.dockerjava.client.AbstractDockerClientTest;

@Test(groups = "integration")
public class ExecStartCmdImplTest extends AbstractDockerClientTest {
@BeforeTest
Expand Down Expand Up @@ -53,7 +51,7 @@ public void execStart() throws Exception {
dockerClient.startContainerCmd(container.getId()).exec();

ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId())
.withAttachStdout(true).withCmd("touch", "/execStartTest.log").exec();
.withAttachStdout(true).withCmd("touch", "/execStartTest.log").withUser("root").exec();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added User here to show that not only creating the command works but that executing with the user does as well.

dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec(
new ExecStartResultCallback(System.out, System.err)).awaitCompletion();

Expand Down Expand Up @@ -92,4 +90,23 @@ public void execStartAttached() throws Exception {
assertNotNull(responseAsString);
assertTrue(responseAsString.length() > 0);
}

@Test(groups = "ignoreInCircleCi", expectedExceptions = NotFoundException.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where expectedExceptions = NotFoundException.class is expected?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not clear because last line in test is assert so exception didn't happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things are not exactly clear here I admit and now I'm scratching my head wondering why I did things this way. Let me clean this up a bit along with the other minor changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so it's the below line that throws the NFE. My thinking was to remove everything below this line and keep the expectedExceptions = NotFoundException.class. This shows that creating with some non-existent-user disallows one to copy anything from the container, in this case /execStartTest.log, because it does not exist because the user itself does not exist.

InputStream response = dockerClient.copyArchiveFromContainerCmd(container.getId(), "/execStartTest.log").exec();

public void execStartWithNonExistentUser() throws Exception {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test to show that using a non-existent name fails.

String containerName = "generated_" + new SecureRandom().nextInt();

CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999")
.withName(containerName).exec();
LOG.info("Created container {}", container.toString());
assertThat(container.getId(), not(isEmptyString()));

dockerClient.startContainerCmd(container.getId()).exec();

ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId())
.withAttachStdout(true).withCmd("touch", "/execStartTest.log").withUser("NonExistentUser").exec();
dockerClient.execStartCmd(execCreateCmdResponse.getId()).withDetach(false).withTty(true)
.exec(new ExecStartResultCallback(System.out, System.err)).awaitCompletion();

dockerClient.copyArchiveFromContainerCmd(container.getId(), "/execStartTest.log").exec();
}
}