Search in sources :

Example 6 with Container

use of com.spotify.docker.client.messages.Container in project linuxtools by eclipse.

the class DockerConnectionTest method shouldLoadContainers.

@Test
public void shouldLoadContainers() throws DockerException {
    // given
    final Container fooContainer = MockContainerFactory.id("foo").build();
    final Container barContainer = MockContainerFactory.id("bar").build();
    final DockerClient client = MockDockerClientFactory.container(fooContainer).container(barContainer).build();
    final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
    dockerConnection.open(false);
    // when
    final List<IDockerContainer> containers = dockerConnection.getContainers();
    // then
    assertThat(containers).hasSize(2);
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) Test(org.junit.Test)

Example 7 with Container

use of com.spotify.docker.client.messages.Container in project helios by spotify.

the class ZooKeeperClusterIdTest method testAgent.

@Test
public void testAgent() throws Exception {
    startDefaultMaster("--zk-cluster-id=" + zkClusterId);
    startDefaultAgent(testHost(), "--zk-cluster-id=" + zkClusterId);
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // Create job and deploy it
    final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, IDLE_COMMAND);
    deployJob(jobId, testHost());
    final TaskStatus runningStatus = awaitTaskState(jobId, testHost(), RUNNING);
    final String containerId = runningStatus.getContainerId();
    // Delete the config node which contains the cluster ID and all the job definitions
    zk().curatorWithSuperAuth().delete().deletingChildrenIfNeeded().forPath("/config");
    // Sleep for a second so agent has a chance to react to deletion
    Thread.sleep(1000);
    // Make sure the agent didn't stop the job
    try (final DockerClient docker = getNewDockerClient()) {
        final List<Container> containers = docker.listContainers();
        final CustomTypeSafeMatcher<Container> containerIdMatcher = new CustomTypeSafeMatcher<Container>("Container with id " + containerId) {

            @Override
            protected boolean matchesSafely(Container container) {
                return container.id().equals(containerId);
            }
        };
        assertContainersMatch(containers, containerIdMatcher);
    }
}
Also used : Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher) Matchers.containsString(org.hamcrest.Matchers.containsString) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 8 with Container

use of com.spotify.docker.client.messages.Container in project helios by spotify.

the class SystemTestBase method listContainers.

protected List<Container> listContainers(final DockerClient dockerClient, final String needle) throws DockerException, InterruptedException {
    final List<Container> containers = dockerClient.listContainers();
    final List<Container> matches = Lists.newArrayList();
    for (final Container container : containers) {
        if (container.names() != null) {
            for (final String name : container.names()) {
                if (name.contains(needle)) {
                    matches.add(container);
                    break;
                }
            }
        }
    }
    return matches;
}
Also used : Container(com.spotify.docker.client.messages.Container) Matchers.containsString(org.hamcrest.Matchers.containsString) Integer.toHexString(java.lang.Integer.toHexString)

Example 9 with Container

use of com.spotify.docker.client.messages.Container 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();
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Service(com.google.common.util.concurrent.Service) Matchers.containsString(org.hamcrest.Matchers.containsString) Integer.toHexString(java.lang.Integer.toHexString) HeliosClient(com.spotify.helios.client.HeliosClient) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) After(org.junit.After)

Example 10 with Container

use of com.spotify.docker.client.messages.Container 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);
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) Deployment(com.spotify.helios.common.descriptors.Deployment) Integer.toHexString(java.lang.Integer.toHexString) HeliosClient(com.spotify.helios.client.HeliosClient) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) Container(com.spotify.docker.client.messages.Container) CreateJobResponse(com.spotify.helios.common.protocol.CreateJobResponse) Job(com.spotify.helios.common.descriptors.Job) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Aggregations

Container (com.spotify.docker.client.messages.Container)11 DockerClient (com.spotify.docker.client.DockerClient)9 Test (org.junit.Test)5 Image (com.spotify.docker.client.messages.Image)3 HeliosClient (com.spotify.helios.client.HeliosClient)3 JobId (com.spotify.helios.common.descriptors.JobId)3 Integer.toHexString (java.lang.Integer.toHexString)3 IDockerContainer (org.eclipse.linuxtools.docker.core.IDockerContainer)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)2 DockerTimeoutException (com.spotify.docker.client.exceptions.DockerTimeoutException)2 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)2 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)2 ImageInfo (com.spotify.docker.client.messages.ImageInfo)2 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)2 IOException (java.io.IOException)2 IDockerImage (org.eclipse.linuxtools.docker.core.IDockerImage)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Service (com.google.common.util.concurrent.Service)1 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1