Skip to content

Commit

Permalink
Merge pull request #98 from hsaraogi/feature/kill-a-container
Browse files Browse the repository at this point in the history
Add method to kill a container
  • Loading branch information
iamdanfox authored Aug 31, 2016
2 parents d929d35 + 23c0c75 commit 0d6237e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public void stop() throws IOException, InterruptedException {
dockerComposeProcess.stop(this);
}

public void kill() throws IOException, InterruptedException {
dockerComposeProcess.kill(this);
}

public State state() throws IOException, InterruptedException {
return dockerComposeProcess.state(containerName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public void stop(Container container) throws IOException, InterruptedException {
command.execute(Command.throwingOnError(), "stop", container.getContainerName());
}

@Override
public void kill(Container container) throws IOException, InterruptedException {
command.execute(Command.throwingOnError(), "kill", container.getContainerName());
}

@Override
public String exec(DockerComposeExecOption dockerComposeExecOption, String containerName,
DockerComposeExecArgument dockerComposeExecArgument) throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public void stop(Container container) throws IOException, InterruptedException {
dockerCompose.stop(container);
}

@Override
public void kill(Container container) throws IOException, InterruptedException {
dockerCompose.kill(container);
}

@Override
public String exec(DockerComposeExecOption dockerComposeExecOption, String containerName,
DockerComposeExecArgument dockerComposeExecArgument) throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface DockerCompose {
void up(Container container) throws IOException, InterruptedException;
void start(Container container) throws IOException, InterruptedException;
void stop(Container container) throws IOException, InterruptedException;
void kill(Container container) throws IOException, InterruptedException;
String exec(DockerComposeExecOption dockerComposeExecOption, String containerName, DockerComposeExecArgument dockerComposeExecArgument) throws IOException, InterruptedException;
String run(DockerComposeRunOption dockerComposeRunOption, String containerName, DockerComposeRunArgument dockerComposeRunArgument) throws IOException, InterruptedException;
ContainerNames ps() throws IOException, InterruptedException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,39 @@ public void start_can_be_run_on_running_container() {
});
}

@Test
public void can_kill_and_start_containers() {
forEachContainer(containerName -> {
try {
Container container = docker.containers().container(containerName);

container.kill();
assertThat(container.state(), is(State.Exit));

container.start();
assertThat(container.state(), is(State.Up));
} catch (IOException | InterruptedException e) {
propagate(e);
}
});
}

@Test
public void kill_can_be_run_on_killed_container() {
forEachContainer(containerName -> {
try {
Container container = docker.containers().container(containerName);

container.kill();
assertThat(container.state(), is(State.Exit));

container.kill();
} catch (IOException | InterruptedException e) {
propagate(e);
}
});
}

@Ignore // This test will not run on Circle CI because it does not currently support docker-compose exec.
@Test
public void exec_returns_output() throws Exception {
Expand Down

0 comments on commit 0d6237e

Please sign in to comment.