use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.
the class DefaultDockerClient method removeContainer.
@Override
public void removeContainer(final String containerId, final RemoveContainerParam... params) throws DockerException, InterruptedException {
try {
WebTarget resource = resource().path("containers").path(containerId);
for (final RemoveContainerParam param : params) {
resource = resource.queryParam(param.name(), param.value());
}
request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
} catch (DockerRequestException e) {
switch(e.status()) {
case 400:
throw new BadParamException(getQueryParamMap(resource()), e);
case 404:
throw new ContainerNotFoundException(containerId, e);
default:
throw e;
}
}
}
use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.
the class DefaultDockerClient method createService.
@Override
public ServiceCreateResponse createService(final ServiceSpec spec, final RegistryAuth config) throws DockerException, InterruptedException {
assertApiVersionIsAbove("1.24");
final WebTarget resource = resource().path("services").path("create");
try {
return request(POST, ServiceCreateResponse.class, resource, resource.request(APPLICATION_JSON_TYPE).header("X-Registry-Auth", authHeader(config)), Entity.json(spec));
} catch (DockerRequestException e) {
switch(e.status()) {
case 406:
throw new DockerException("Server error or node is not part of swarm.", e);
case 409:
throw new DockerException("Name conflicts with an existing object.", e);
default:
throw e;
}
}
}
use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.
the class DefaultDockerClientTest method testRenameContainer.
@Test
public void testRenameContainer() throws Exception {
sut.pull(BUSYBOX_LATEST);
final String originalName = randomName();
final String newName = randomName();
// Create a container with originalName
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
final ContainerCreation creation = sut.createContainer(config, originalName);
final String id = creation.id();
assertThat(sut.inspectContainer(id).name(), equalToIgnoreLeadingSlash(originalName));
// Rename to newName
sut.renameContainer(id, newName);
assertThat(sut.inspectContainer(id).name(), equalToIgnoreLeadingSlash(newName));
// We should no longer find a container with originalName
try {
sut.inspectContainer(originalName);
fail("There should be no container with name " + originalName);
} catch (ContainerNotFoundException e) {
// Note, even though property in ContainerNotFoundException is named containerId,
// in this case it holds the name, since that is what we passed to inspectContainer.
assertThat(e.getContainerId(), equalToIgnoreLeadingSlash(originalName));
}
// Try to rename to a disallowed name (not matching /?[a-zA-Z0-9_-]+).
// Should get IllegalArgumentException.
final String badName = "abc123.!*";
try {
sut.renameContainer(id, badName);
fail("We should not be able to rename a container " + badName);
} catch (IllegalArgumentException ignored) {
// Pass
}
// Try to rename to null
try {
sut.renameContainer(id, null);
fail("We should not be able to rename a container null");
} catch (IllegalArgumentException ignored) {
// Pass
}
// Create another container with originalName
final ContainerConfig config2 = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
final ContainerCreation creation2 = sut.createContainer(config2, originalName);
final String id2 = creation2.id();
assertThat(sut.inspectContainer(id2).name(), equalToIgnoreLeadingSlash(originalName));
// Try to rename another container to newName. Should get a ContainerRenameConflictException.
try {
sut.renameContainer(id2, newName);
fail("We should not be able to rename container " + id2 + " to " + newName);
} catch (ContainerRenameConflictException e) {
assertThat(e.getContainerId(), equalTo(id2));
assertThat(e.getNewName(), equalToIgnoreLeadingSlash(newName));
} catch (DockerRequestException ignored) {
// This is a docker bug. Docker responds with HTTP 500 when it should be HTTP 409.
// See https://github.com/docker/docker/issues/21016.
// Fixed in version 1.11.0 API version 1.23. So we should see a lower API version here.
// Seems to be a regression in API version 1.3[23] https://github.com/moby/moby/issues/35472
assertThat(dockerApiVersionLessThan("1.23") || dockerApiVersionEquals("1.32") || dockerApiVersionEquals("1.33"), is(true));
}
// Rename a non-existent id. Should get ContainerNotFoundException.
final String badId = "no_container_with_this_id_should_exist_otherwise_things_are_weird";
try {
sut.renameContainer(badId, randomName());
fail("There should be no container with id " + badId);
} catch (ContainerNotFoundException e) {
assertThat(e.getContainerId(), equalTo(badId));
}
}
use of com.spotify.docker.client.exceptions.DockerRequestException in project helios by spotify.
the class SyslogRedirectionTest method test.
@Test
public void test() throws Exception {
final String syslogOutput = "should-be-redirected";
try (final DockerClient docker = getNewDockerClient()) {
// Start a container that will be our "syslog" endpoint (just run netcat and print whatever
// we receive).
final String port = "4711";
final String expose = port + "/udp";
docker.pull(ALPINE);
final HostConfig hostConfig = HostConfig.builder().publishAllPorts(true).build();
final ContainerConfig config = ContainerConfig.builder().image(// includes spotify/busybox:latest with netcat with udp support
ALPINE).cmd(asList("nc", "-p", port, "-l", "-u")).exposedPorts(ImmutableSet.of(expose)).hostConfig(hostConfig).build();
final ContainerCreation creation = docker.createContainer(config, testTag + "_syslog");
final String syslogContainerId = creation.id();
docker.startContainer(syslogContainerId);
final ContainerInfo containerInfo = docker.inspectContainer(syslogContainerId);
assertThat(containerInfo.state().running(), equalTo(true));
final String syslogEndpoint = syslogHost + ":" + containerInfo.networkSettings().ports().get(expose).get(0).hostPort();
// Run a Helios job that logs to syslog.
startDefaultMaster();
startDefaultAgent(testHost(), "--syslog-redirect", syslogEndpoint);
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final List<String> command = Lists.newArrayList();
final JobId jobId = createJob(testJobName, testJobVersion, testImage, command, ImmutableMap.of("SYSLOG_REDIRECTOR", "/syslog-redirector"));
deployJob(jobId, testHost());
final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
{
// Verify the log for the task container
LogStream logs = null;
try {
logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr());
final String log = logs.readFully();
// for old docker versions should be nothing in the docker output log, either error text
// or our message
assertEquals("", log);
} catch (DockerRequestException e) {
// for new docker versions, trying to read logs should throw an error but the syslog
// option should be set
final String logType = docker.inspectContainer(taskStatus.getContainerId()).hostConfig().logConfig().logType();
assertEquals("syslog", logType);
} finally {
if (logs != null) {
logs.close();
}
}
}
// Verify the log for the syslog container
{
final String log;
try (LogStream logs = docker.logs(syslogContainerId, stdout(), stderr())) {
log = logs.readFully();
}
// the output message from the command should appear in the syslog container
assertThat(log, containsString(syslogOutput));
}
}
}
use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.
the class DefaultDockerClient method resizeTty.
@Override
public void resizeTty(final String containerId, final Integer height, final Integer width) throws DockerException, InterruptedException {
checkTtyParams(height, width);
WebTarget resource = resource().path("containers").path(containerId).path("resize");
if (height != null && height > 0) {
resource = resource.queryParam("h", height);
}
if (width != null && width > 0) {
resource = resource.queryParam("w", width);
}
try {
request(POST, resource, resource.request(TEXT_PLAIN_TYPE));
} catch (DockerRequestException e) {
switch(e.status()) {
case 404:
throw new ContainerNotFoundException(containerId, e);
default:
throw e;
}
}
}
Aggregations