use of com.spotify.docker.client.DockerClient in project helios by spotify.
the class SystemTestBase method baseTeardown.
@After
public void baseTeardown() throws Exception {
for (final HeliosClient client : clients) {
client.close();
}
clients.clear();
for (final Service service : services) {
try {
service.stopAsync();
} catch (Exception e) {
log.error("Uncaught exception", e);
}
}
for (final Service service : services) {
try {
service.awaitTerminated();
} catch (Exception e) {
log.error("Service failed", e);
}
}
services.clear();
// Clean up docker
try (final DockerClient dockerClient = getNewDockerClient()) {
final List<Container> containers = dockerClient.listContainers();
for (final Container container : containers) {
for (final String name : container.names()) {
if (name.contains(testTag)) {
try {
dockerClient.killContainer(container.id());
} catch (DockerException e) {
e.printStackTrace();
}
break;
}
}
}
} catch (Exception e) {
log.error("Docker client exception", e);
}
if (zk != null) {
zk.close();
}
listThreads();
}
use of com.spotify.docker.client.DockerClient in project helios by spotify.
the class TerminationTest method testTermOnExit.
@Test
public void testTermOnExit() throws Exception {
startDefaultMaster();
final String host = testHost();
startDefaultAgent(host);
final HeliosClient client = defaultClient();
awaitHostStatus(client, host, UP, LONG_WAIT_SECONDS, SECONDS);
// Note: signal 15 is SIGTERM
final Job jobToInterrupt = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(asList("/bin/sh", "-c", "trap handle 15; handle() { echo term; exit 0; }; " + "while true; do sleep 1; done")).build();
final JobId jobId = createJob(jobToInterrupt);
deployJob(jobId, host);
awaitTaskState(jobId, host, RUNNING);
client.setGoal(new Deployment(jobId, Goal.STOP, Deployment.EMTPY_DEPLOYER_USER, Deployment.EMPTY_DEPLOYER_MASTER, Deployment.EMPTY_DEPLOYMENT_GROUP_NAME), host);
final TaskStatus taskStatus = awaitTaskState(jobId, host, STOPPED);
final String log;
try (final DockerClient dockerClient = getNewDockerClient();
LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout())) {
log = logs.readFully();
}
// Message expected, because the SIGTERM handler in the script should have run
assertEquals("term\n", log);
}
use of com.spotify.docker.client.DockerClient in project helios by spotify.
the class DeploymentTest method testLotsOfConcurrentJobs.
@Test
public void testLotsOfConcurrentJobs() throws Exception {
startDefaultMaster();
final HeliosClient client = defaultClient();
startDefaultAgent(testHost());
awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS);
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final int numberOfJobs = 40;
final List<JobId> jobIds = Lists.newArrayListWithCapacity(numberOfJobs);
final String jobName = testJobName + "_" + toHexString(ThreadLocalRandom.current().nextInt());
// create and deploy a bunch of jobs
for (Integer i = 0; i < numberOfJobs; i++) {
final Job job = Job.newBuilder().setName(jobName).setVersion(i.toString()).setImage(BUSYBOX).setCommand(IDLE_COMMAND).setCreatingUser(TEST_USER).build();
final JobId jobId = job.getId();
final CreateJobResponse created = client.createJob(job).get();
assertEquals(CreateJobResponse.Status.OK, created.getStatus());
final Deployment deployment = Deployment.of(jobId, START, TEST_USER);
final JobDeployResponse deployed = client.deploy(deployment, testHost()).get();
assertEquals(JobDeployResponse.Status.OK, deployed.getStatus());
jobIds.add(jobId);
}
// get the container ID's for the jobs
final Set<String> containerIds = Sets.newHashSetWithExpectedSize(numberOfJobs);
for (final JobId jobId : jobIds) {
final TaskStatus taskStatus = awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
containerIds.add(taskStatus.getContainerId());
}
try (final DockerClient dockerClient = getNewDockerClient()) {
// kill all the containers for the jobs
for (final String containerId : containerIds) {
dockerClient.killContainer(containerId);
}
// make sure all the containers come back up
final int restartedContainers = Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int matchingContainerCount = 0;
for (final Container c : dockerClient.listContainers()) {
for (final String name : c.names()) {
if (name.contains(jobName)) {
matchingContainerCount++;
}
}
}
if (matchingContainerCount < containerIds.size()) {
return null;
} else {
return matchingContainerCount;
}
}
});
assertEquals(numberOfJobs, restartedContainers);
}
}
use of com.spotify.docker.client.DockerClient in project helios by spotify.
the class DnsServerTest method testDnsParam.
@Test
public void testDnsParam() throws Exception {
final String server1 = "127.0.0.1";
final String server2 = "127.0.0.2";
startDefaultMaster();
startDefaultAgent(testHost(), "--dns", server1, "--dns", server2);
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, asList("cat", "/etc/resolv.conf"));
deployJob(jobId, testHost());
final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
try (final DockerClient dockerClient = getNewDockerClient()) {
final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr());
final String log = logs.readFully();
assertThat(log, containsString(server1));
assertThat(log, containsString(server2));
}
}
use of com.spotify.docker.client.DockerClient in project helios by spotify.
the class HealthCheckTest method testExec.
@Test
public void testExec() throws Exception {
// CircleCI uses lxc which doesn't support exec - https://circleci.com/docs/docker/#docker-exec
assumeFalse(isCircleCi());
final DockerClient dockerClient = getNewDockerClient();
assumeThat(dockerClient.version(), is(execCompatibleDockerVersion()));
startDefaultMaster();
final HeliosClient client = defaultClient();
startDefaultAgent(testHost(), "--service-registry=" + registryAddress);
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
// check if "file" exists in the root directory as a healthcheck
final HealthCheck healthCheck = ExecHealthCheck.of("test", "-e", "file");
// start a container that listens on the service port
final String portName = "service";
final String serviceName = "foo_service";
final String serviceProtocol = "foo_proto";
final Job job = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(asList("sh", "-c", "nc -l -p 4711")).addPort(portName, PortMapping.of(4711)).addRegistration(ServiceEndpoint.of(serviceName, serviceProtocol), ServicePorts.of(portName)).setHealthCheck(healthCheck).build();
final JobId jobId = createJob(job);
deployJob(jobId, testHost());
final TaskStatus jobState = awaitTaskState(jobId, testHost(), HEALTHCHECKING);
// wait a few seconds to see if the service gets registered
Thread.sleep(3000);
// shouldn't be registered, since we haven't created the file yet
verify(registrar, never()).register(any(ServiceRegistration.class));
// create the file in the container to make the healthcheck succeed
final String[] makeFileCmd = new String[] { "touch", "file" };
final ExecCreation execCreation = dockerClient.execCreate(jobState.getContainerId(), makeFileCmd);
final String execId = execCreation.id();
dockerClient.execStart(execId);
awaitTaskState(jobId, testHost(), RUNNING);
dockerClient.close();
verify(registrar, timeout((int) SECONDS.toMillis(LONG_WAIT_SECONDS))).register(registrationCaptor.capture());
final ServiceRegistration serviceRegistration = registrationCaptor.getValue();
assertEquals(1, serviceRegistration.getEndpoints().size());
final Endpoint registeredEndpoint = serviceRegistration.getEndpoints().get(0);
assertEquals("wrong service", serviceName, registeredEndpoint.getName());
assertEquals("wrong protocol", serviceProtocol, registeredEndpoint.getProtocol());
}
Aggregations