use of com.spotify.docker.client.DockerClient in project helios by spotify.
the class MultiplePortJobTest method testPortEnvVars.
@Test
public void testPortEnvVars() throws Exception {
startDefaultMaster();
startDefaultAgent(testHost());
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final Map<String, PortMapping> ports = ImmutableMap.of("bar", staticMapping1);
try (final DockerClient dockerClient = getNewDockerClient()) {
final JobId jobId = createJob(testJobName + 1, testJobVersion, BUSYBOX, asList("sh", "-c", "echo $HELIOS_PORT_bar"), EMPTY_ENV, ports);
deployJob(jobId, testHost());
final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
final String log;
try (final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr())) {
log = logs.readFully();
}
assertEquals(testHost() + ":" + externalPort1, log.trim());
}
}
use of com.spotify.docker.client.DockerClient in project helios by spotify.
the class NetworkModeTest method test.
@Test
public void test() throws Exception {
final CreateJobResponse created = client.createJob(job).get();
assertEquals(CreateJobResponse.Status.OK, created.getStatus());
final JobId jobId = job.getId();
// Wait for agent to come up
awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS);
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
// Deploy the job on the agent
final Deployment deployment = Deployment.of(jobId, START);
final JobDeployResponse deployed = client.deploy(deployment, testHost()).get();
assertEquals(JobDeployResponse.Status.OK, deployed.getStatus());
// Wait for the job to run
final TaskStatus taskStatus = awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
try (final DockerClient docker = getNewDockerClient()) {
final HostConfig hostConfig = docker.inspectContainer(taskStatus.getContainerId()).hostConfig();
assertEquals(NETWORK_MODE, hostConfig.networkMode());
}
}
use of com.spotify.docker.client.DockerClient in project helios by spotify.
the class ResourcesTest method testClient.
@Test
public void testClient() throws Exception {
// Doesn't work on CircleCI because their lxc-driver can't set cpus
// See output of `docker run --cpuset-cpus 0-1 spotify/busybox:latest true`
assumeFalse(isCircleCi());
final CreateJobResponse created = client.createJob(job).get();
assertEquals(CreateJobResponse.Status.OK, created.getStatus());
final JobId jobId = job.getId();
// Wait for agent to come up
awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS);
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
// Deploy the job on the agent
final Deployment deployment = Deployment.of(jobId, START);
final JobDeployResponse deployed = client.deploy(deployment, testHost()).get();
assertEquals(JobDeployResponse.Status.OK, deployed.getStatus());
// Wait for the job to run
final TaskStatus taskStatus = awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
assertJobEquals(job, taskStatus.getJob());
try (final DockerClient docker = getNewDockerClient()) {
final HostConfig hostConfig = docker.inspectContainer(taskStatus.getContainerId()).hostConfig();
assertEquals(CPU_SHARES, hostConfig.cpuShares());
assertEquals(CPUSET_CPUS, hostConfig.cpusetCpus());
final Info info = docker.info();
final Iterable<String> split = Splitter.on(".").split(docker.version().apiVersion());
// noinspection ConstantConditions
final int major = Integer.parseInt(Iterables.get(split, 0, "0"));
// noinspection ConstantConditions
final int minor = Integer.parseInt(Iterables.get(split, 1, "0"));
// TODO (dxia) This doesn't work on docker < 1.7 ie docker API < 1.19 for some reason.
if (major >= 1 && minor >= 19) {
if (info.memoryLimit()) {
assertEquals(MEMORY, hostConfig.memory());
}
if (info.swapLimit()) {
assertEquals(MEMORY_SWAP, hostConfig.memorySwap());
}
}
}
}
use of com.spotify.docker.client.DockerClient 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.DockerClient in project helios by spotify.
the class SyslogRedirectionTest method setup.
@Before
public void setup() throws Exception {
try (final DockerClient docker = getNewDockerClient()) {
// Build an image with an ENTRYPOINT and CMD prespecified
final String dockerDirectory = Resources.getResource("syslog-test-image").getPath();
docker.build(Paths.get(dockerDirectory), testImage);
// Figure out the host IP from the container's point of view (needed for syslog)
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX).cmd(asList("ip", "route", "show")).build();
final ContainerCreation creation = docker.createContainer(config);
final String containerId = creation.id();
docker.startContainer(containerId);
// Wait for the container to exit.
// If we don't wait, docker.logs() might return an epmty string because the container
// cmd hasn't run yet.
docker.waitContainer(containerId);
final String log;
try (LogStream logs = docker.logs(containerId, stdout(), stderr())) {
log = logs.readFully();
}
final Matcher m = DEFAULT_GATEWAY_PATTERN.matcher(log);
if (m.find()) {
syslogHost = m.group("gateway");
} else {
fail("couldn't determine the host address from '" + log + "'");
}
}
}
Aggregations