Search in sources :

Example 1 with ExecCreateParam

use of com.spotify.docker.client.DockerClient.ExecCreateParam in project docker-client by spotify.

the class DefaultDockerClientTest method testExecInspect.

@Test
public void testExecInspect() throws Exception {
    requireDockerApiVersionAtLeast("1.16", "Exec Inspect");
    // CircleCI uses lxc, doesn't support exec - https://circleci.com/docs/docker/#docker-exec
    assumeFalse(CIRCLECI);
    sut.pull(BUSYBOX_LATEST);
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final String containerName = randomName();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    final String containerId = containerCreation.id();
    sut.startContainer(containerId);
    final List<ExecCreateParam> createParams = newArrayList(ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr(), ExecCreateParam.attachStdin(), ExecCreateParam.tty());
    // some functionality in this test depends on API 1.19 (exec user)
    final boolean execUserSupported = dockerApiVersionAtLeast("1.19");
    if (execUserSupported) {
        createParams.add(ExecCreateParam.user("1000"));
    }
    final ExecCreation execCreation = sut.execCreate(containerId, new String[] { "sh", "-c", "exit 2" }, createParams.toArray(new ExecCreateParam[createParams.size()]));
    final String execId = execCreation.id();
    log.info("execId = {}", execId);
    final ExecState notStarted = sut.execInspect(execId);
    assertThat(notStarted.id(), is(execId));
    assertThat(notStarted.running(), is(false));
    if (dockerApiVersionLessThan("1.22")) {
        assertThat(notStarted.exitCode(), is(0));
    } else {
        assertThat(notStarted.exitCode(), nullValue());
    }
    assertThat(notStarted.openStdin(), is(true));
    assertThat(notStarted.openStderr(), is(true));
    assertThat(notStarted.openStdout(), is(true));
    try (final LogStream stream = sut.execStart(execId)) {
        stream.readFully();
    }
    final ExecState started = sut.execInspect(execId);
    assertThat(started.id(), is(execId));
    assertThat(started.running(), is(false));
    assertThat(started.exitCode(), is(2));
    assertThat(started.openStdin(), is(true));
    assertThat(started.openStderr(), is(true));
    assertThat(started.openStdout(), is(true));
    final ProcessConfig processConfig = started.processConfig();
    assertThat(processConfig.privileged(), is(false));
    if (execUserSupported) {
        assertThat(processConfig.user(), is("1000"));
    }
    assertThat(processConfig.tty(), is(true));
    assertThat(processConfig.entrypoint(), is("sh"));
    assertThat(processConfig.arguments(), Matchers.<List<String>>is(ImmutableList.of("-c", "exit 2")));
    if (dockerApiVersionLessThan("1.22")) {
        final ContainerInfo containerInfo = started.container();
        assertThat(containerInfo.path(), is("sh"));
        assertThat(containerInfo.args(), Matchers.<List<String>>is(ImmutableList.of("-c", "while :; do sleep 1; done")));
        assertThat(containerInfo.config().image(), is(BUSYBOX_LATEST));
    } else {
        assertNotNull(started.containerId(), "containerId");
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ExecCreateParam(com.spotify.docker.client.DockerClient.ExecCreateParam) ExecCreation(com.spotify.docker.client.messages.ExecCreation) ProcessConfig(com.spotify.docker.client.messages.ProcessConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ExecState(com.spotify.docker.client.messages.ExecState) Test(org.junit.Test)

Example 2 with ExecCreateParam

use of com.spotify.docker.client.DockerClient.ExecCreateParam in project docker-client by spotify.

the class DefaultDockerClientTest method testExecInspectNoUser.

@Test
public void testExecInspectNoUser() throws Exception {
    requireDockerApiVersionAtLeast("1.16", "Exec Inspect");
    sut.pull(BUSYBOX_LATEST);
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final String containerName = randomName();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    final String containerId = containerCreation.id();
    sut.startContainer(containerId);
    final List<ExecCreateParam> createParams = newArrayList(ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr(), ExecCreateParam.attachStdin(), ExecCreateParam.tty());
    final ExecCreation execCreation = sut.execCreate(containerId, new String[] { "sh", "-c", "exit 2" }, createParams.toArray(new ExecCreateParam[createParams.size()]));
    final String execId = execCreation.id();
    log.info("execId = {}", execId);
    try (final LogStream stream = sut.execStart(execId)) {
        stream.readFully();
    }
    final ExecState state = sut.execInspect(execId);
    assertThat(state.id(), is(execId));
    final ProcessConfig processConfig = state.processConfig();
    assertThat(processConfig.user(), isEmptyOrNullString());
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ExecCreateParam(com.spotify.docker.client.DockerClient.ExecCreateParam) ExecCreation(com.spotify.docker.client.messages.ExecCreation) ProcessConfig(com.spotify.docker.client.messages.ProcessConfig) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ExecState(com.spotify.docker.client.messages.ExecState) Test(org.junit.Test)

Aggregations

ExecCreateParam (com.spotify.docker.client.DockerClient.ExecCreateParam)2 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)2 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)2 ExecCreation (com.spotify.docker.client.messages.ExecCreation)2 ExecState (com.spotify.docker.client.messages.ExecState)2 ProcessConfig (com.spotify.docker.client.messages.ProcessConfig)2 Long.toHexString (java.lang.Long.toHexString)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)2 Test (org.junit.Test)2 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)1