Search in sources :

Example 26 with DockerClient

use of com.spotify.docker.client.DockerClient in project helios by spotify.

the class AddExtraHostTest method test.

@Test
public void test() throws Exception {
    try (final DockerClient docker = getNewDockerClient()) {
        // Start Helios agent, configured to bind host /etc/hostname into container /mnt/hostname
        startDefaultMaster();
        startDefaultAgent(testHost(), "--add-host", "secrethost:169.254.169.254");
        awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
        // a job that cat's /etc/hosts
        final List<String> command = ImmutableList.of("cat", "/etc/hosts");
        final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, command);
        deployJob(jobId, testHost());
        final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
        final String log;
        try (LogStream logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr())) {
            log = logs.readFully();
            assertThat(log, containsString("169.254.169.254\tsecrethost"));
        }
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) Matchers.containsString(org.hamcrest.Matchers.containsString) LogStream(com.spotify.docker.client.LogStream) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 27 with DockerClient

use of com.spotify.docker.client.DockerClient in project helios by spotify.

the class AgentZooKeeperDownTolerationTest method test.

@Test
public void test() throws Exception {
    startDefaultMaster();
    final DockerClient dockerClient = getNewDockerClient();
    final HeliosClient client = defaultClient();
    final AgentMain agent1 = startDefaultAgent(testHost());
    // Create a job
    final Job job = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).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());
    // 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 firstTaskStatus = awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
    assertJobEquals(job, firstTaskStatus.getJob());
    assertNotNull(dockerClient.inspectContainer(firstTaskStatus.getContainerId()));
    // Stop zookeeper
    zk().stop();
    // Wait for a while and make sure that the container is still running
    Thread.sleep(5000);
    assertTrue(dockerClient.inspectContainer(firstTaskStatus.getContainerId()).state().running());
    // Stop the agent
    agent1.stopAsync().awaitTerminated();
    // Start the agent again
    final AgentMain agent2 = startDefaultAgent(testHost());
    // Wait for a while and make sure that the same container is still running
    Thread.sleep(5000);
    assertTrue(dockerClient.inspectContainer(firstTaskStatus.getContainerId()).state().running());
    // Kill the container
    dockerClient.killContainer(firstTaskStatus.getContainerId());
    assertFalse(dockerClient.inspectContainer(firstTaskStatus.getContainerId()).state().running());
    // Wait for a while and make sure that a new container was spawned
    final String firstRestartedContainerId = Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<String>() {

        @Override
        public String call() throws Exception {
            final List<Container> containers = listContainers(dockerClient, testTag);
            return containers.size() == 1 ? containers.get(0).id() : null;
        }
    });
    // Stop the agent
    agent2.stopAsync().awaitTerminated();
    // Kill the container
    dockerClient.killContainer(firstRestartedContainerId);
    assertFalse(dockerClient.inspectContainer(firstRestartedContainerId).state().running());
    // Start the agent again
    startDefaultAgent(testHost());
    // Wait for a while and make sure that a new container was spawned
    final String secondRestartedContainerId = Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<String>() {

        @Override
        public String call() throws Exception {
            final List<Container> containers = listContainers(dockerClient, testTag);
            return containers.size() == 1 ? containers.get(0).id() : null;
        }
    });
    assertTrue(dockerClient.inspectContainer(secondRestartedContainerId).state().running());
    // Start zookeeper
    zk().start();
    // Verify that the agent is listed as up
    awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // Wait for the new container id to be reflected in the task status
    Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<TaskStatus>() {

        @Override
        public TaskStatus call() throws Exception {
            final JobStatus jobStatus = client.jobStatus(jobId).get();
            final TaskStatus taskStatus = jobStatus.getTaskStatuses().get(testHost());
            return taskStatus != null && Objects.equals(taskStatus.getContainerId(), secondRestartedContainerId) ? taskStatus : null;
        }
    });
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) Deployment(com.spotify.helios.common.descriptors.Deployment) HeliosClient(com.spotify.helios.client.HeliosClient) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) JobStatus(com.spotify.helios.common.descriptors.JobStatus) CreateJobResponse(com.spotify.helios.common.protocol.CreateJobResponse) AgentMain(com.spotify.helios.agent.AgentMain) List(java.util.List) Job(com.spotify.helios.common.descriptors.Job) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 28 with DockerClient

use of com.spotify.docker.client.DockerClient in project opennms by OpenNMS.

the class SyslogKafkaElasticsearch5OutageIT method getEs5Address.

protected InetSocketAddress getEs5Address() {
    try {
        // Fetch an up-to-date ContainerInfo for the ELASTICSEARCH_5 container
        final DockerClient docker = ((AbstractTestEnvironment) testEnvironment).getDockerClient();
        final String id = testEnvironment.getContainerInfo(ContainerAlias.ELASTICSEARCH_5).id();
        ContainerInfo info = docker.inspectContainer(id);
        return testEnvironment.getServiceAddress(info, 9200, "tcp");
    } catch (DockerException | InterruptedException e) {
        LOG.error("Unexpected exception trying to fetch Elassticsearch port", e);
        return null;
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerClient(com.spotify.docker.client.DockerClient) AbstractTestEnvironment(org.opennms.test.system.api.AbstractTestEnvironment) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo)

Example 29 with DockerClient

use of com.spotify.docker.client.DockerClient in project opennms by OpenNMS.

the class SyslogKafkaElasticsearchBufferingIT method stopContainer.

private void stopContainer(ContainerAlias alias) {
    final DockerClient docker = ((AbstractTestEnvironment) testEnvironment).getDockerClient();
    final String id = testEnvironment.getContainerInfo(alias).id();
    try {
        LOG.info("Stopping container: {} -> {}", alias, id);
        docker.stopContainer(id, 60);
        LOG.info("Container stopped: {} -> {}", alias, id);
    } catch (DockerException | InterruptedException e) {
        LOG.warn("Unexpected exception while stopping container {}", id, e);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerClient(com.spotify.docker.client.DockerClient) AbstractTestEnvironment(org.opennms.test.system.api.AbstractTestEnvironment)

Example 30 with DockerClient

use of com.spotify.docker.client.DockerClient in project opennms by OpenNMS.

the class SyslogKafkaElasticsearchBufferingIT method startContainer.

private void startContainer(ContainerAlias alias) {
    final DockerClient docker = ((AbstractTestEnvironment) testEnvironment).getDockerClient();
    final String id = testEnvironment.getContainerInfo(alias).id();
    try {
        LOG.info("Starting container: {} -> {}", alias, id);
        docker.startContainer(id);
        LOG.info("Container started: {} -> {}", alias, id);
    } catch (DockerException | InterruptedException e) {
        LOG.warn("Unexpected exception while starting container {}", id, e);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerClient(com.spotify.docker.client.DockerClient) AbstractTestEnvironment(org.opennms.test.system.api.AbstractTestEnvironment)

Aggregations

DockerClient (com.spotify.docker.client.DockerClient)31 Test (org.junit.Test)21 JobId (com.spotify.helios.common.descriptors.JobId)19 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)18 LogStream (com.spotify.docker.client.LogStream)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 HeliosClient (com.spotify.helios.client.HeliosClient)10 Deployment (com.spotify.helios.common.descriptors.Deployment)8 DockerException (com.spotify.docker.client.exceptions.DockerException)7 Job (com.spotify.helios.common.descriptors.Job)7 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)6 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)6 Container (com.spotify.docker.client.messages.Container)4 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)4 HostConfig (com.spotify.docker.client.messages.HostConfig)4 HostStatus (com.spotify.helios.common.descriptors.HostStatus)4 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)3 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)3 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)3 AgentMain (com.spotify.helios.agent.AgentMain)3