Search in sources :

Example 6 with HeliosClient

use of com.spotify.helios.client.HeliosClient in project helios by spotify.

the class MultiplePortJobTest method test.

@Test
public void test() throws Exception {
    startDefaultMaster();
    // 'Reserve' a 2 port wide range of ports.
    final Range<Integer> portRange = temporaryPorts.localPortRange("agent1", 2);
    // Start an agent using the aforementioned 2 port wide range.
    final AgentMain agent1 = startDefaultAgent(testHost(), "--port-range=" + portRange.lowerEndpoint() + ":" + portRange.upperEndpoint());
    try (final DockerClient dockerClient = getNewDockerClient()) {
        final HeliosClient client = defaultClient();
        awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
        // foo is a mapping of 4711 -> A port dynamically allocated by the agent's PortAllocator
        // bar is a mapping of 4712 -> A static port randomly selected by temporaryPorts
        final Map<String, PortMapping> ports1 = ImmutableMap.of("foo", PortMapping.of(4711), "bar", staticMapping1);
        // foo is a mapping of 4711 -> A port dynamically allocated by the agent's PortAllocator
        // bar is a mapping of 4712 -> A static port randomly selected by temporaryPorts
        final Map<String, PortMapping> ports2 = ImmutableMap.of("foo", PortMapping.of(4711), "bar", staticMapping2);
        final JobId jobId1 = createJob(testJobName + 1, testJobVersion, BUSYBOX, IDLE_COMMAND, EMPTY_ENV, ports1);
        assertNotNull(jobId1);
        deployJob(jobId1, testHost());
        final TaskStatus firstTaskStatus1 = awaitJobState(client, testHost(), jobId1, RUNNING, LONG_WAIT_SECONDS, SECONDS);
        final JobId jobId2 = createJob(testJobName + 2, testJobVersion, BUSYBOX, IDLE_COMMAND, EMPTY_ENV, ports2);
        assertNotNull(jobId2);
        deployJob(jobId2, testHost());
        final TaskStatus firstTaskStatus2 = awaitJobState(client, testHost(), jobId2, RUNNING, LONG_WAIT_SECONDS, SECONDS);
        // Verify we allocated dynamic ports from within the specified range.
        assertTrue(portRange.contains(firstTaskStatus1.getPorts().get("foo").getExternalPort()));
        assertTrue(portRange.contains(firstTaskStatus2.getPorts().get("foo").getExternalPort()));
        // Verify we allocated the static ports we asked for.
        assertEquals(staticMapping1, firstTaskStatus1.getPorts().get("bar"));
        assertEquals(staticMapping2, firstTaskStatus2.getPorts().get("bar"));
        // Verify we didn't allocate the same dynamic port to both jobs.
        assertNotEquals(firstTaskStatus1.getPorts().get("foo"), firstTaskStatus2.getPorts().get("foo"));
        // TODO (dano): the supervisor should report the allocated ports at all times
        // Verify that port allocation is kept across container restarts
        dockerClient.killContainer(firstTaskStatus1.getContainerId());
        final TaskStatus restartedTaskStatus1 = Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<TaskStatus>() {

            @Override
            public TaskStatus call() throws Exception {
                final HostStatus hostStatus = client.hostStatus(testHost()).get();
                final TaskStatus taskStatus = hostStatus.getStatuses().get(jobId1);
                return (taskStatus != null && taskStatus.getState() == RUNNING && !Objects.equals(taskStatus.getContainerId(), firstTaskStatus1.getContainerId())) ? taskStatus : null;
            }
        });
        assertEquals(firstTaskStatus1.getPorts(), restartedTaskStatus1.getPorts());
        // Verify that port allocation is kept across agent restarts
        agent1.stopAsync().awaitTerminated();
        dockerClient.killContainer(firstTaskStatus2.getContainerId());
        startDefaultAgent(testHost());
        final TaskStatus restartedTaskStatus2 = Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<TaskStatus>() {

            @Override
            public TaskStatus call() throws Exception {
                final HostStatus hostStatus = client.hostStatus(testHost()).get();
                final TaskStatus taskStatus = hostStatus.getStatuses().get(jobId2);
                return (taskStatus != null && taskStatus.getState() == RUNNING && !Objects.equals(taskStatus.getContainerId(), firstTaskStatus2.getContainerId())) ? taskStatus : null;
            }
        });
        assertEquals(firstTaskStatus2.getPorts(), restartedTaskStatus2.getPorts());
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) HeliosClient(com.spotify.helios.client.HeliosClient) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) AgentMain(com.spotify.helios.agent.AgentMain) HostStatus(com.spotify.helios.common.descriptors.HostStatus) PortMapping(com.spotify.helios.common.descriptors.PortMapping) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 7 with HeliosClient

use of com.spotify.helios.client.HeliosClient in project helios by spotify.

the class NamespaceTest method test.

@Test
public void test() throws Exception {
    startDefaultMaster();
    final String id = "test-" + Integer.toHexString(new SecureRandom().nextInt());
    final String namespace = "helios-" + id;
    final HeliosClient client = defaultClient();
    startDefaultAgent(testHost(), "--id=" + id);
    // Create a job
    final Job job = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(IDLE_COMMAND).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());
    awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
    try (final DockerClient docker = getNewDockerClient()) {
        final List<Container> containers = docker.listContainers();
        Container jobContainer = null;
        for (final Container container : containers) {
            for (final String name : container.names()) {
                if (name.startsWith("/" + namespace)) {
                    jobContainer = container;
                }
            }
        }
        assertNotNull(jobContainer);
    }
}
Also used : Container(com.spotify.docker.client.messages.Container) CreateJobResponse(com.spotify.helios.common.protocol.CreateJobResponse) DockerClient(com.spotify.docker.client.DockerClient) SecureRandom(java.security.SecureRandom) Deployment(com.spotify.helios.common.descriptors.Deployment) HeliosClient(com.spotify.helios.client.HeliosClient) Job(com.spotify.helios.common.descriptors.Job) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 8 with HeliosClient

use of com.spotify.helios.client.HeliosClient in project helios by spotify.

the class QueryFailureTest method testJobStatusHostFilter.

@Test
public void testJobStatusHostFilter() throws Exception {
    startDefaultMaster();
    final HeliosClient client = defaultClient();
    startDefaultAgent(testHost());
    // Create a job
    final Job job = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(IDLE_COMMAND).setPorts(EMPTY_PORTS).build();
    final CreateJobResponse created = client.createJob(job).get();
    assertEquals(CreateJobResponse.Status.OK, created.getStatus());
    final String result = cli("status", "--host", "framazama");
    assertThat(result, containsString("There are no jobs deployed to hosts with the host pattern"));
}
Also used : CreateJobResponse(com.spotify.helios.common.protocol.CreateJobResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) HeliosClient(com.spotify.helios.client.HeliosClient) Job(com.spotify.helios.common.descriptors.Job) Test(org.junit.Test)

Example 9 with HeliosClient

use of com.spotify.helios.client.HeliosClient in project helios by spotify.

the class ZooKeeperBadNodeTest method testGetJobsWithBadNode.

@Test
public void testGetJobsWithBadNode() throws Exception {
    startDefaultMaster("--zk-cluster-id=" + zkClusterId);
    final HeliosClient client = defaultClient();
    client.jobs().get();
}
Also used : HeliosClient(com.spotify.helios.client.HeliosClient) Test(org.junit.Test)

Example 10 with HeliosClient

use of com.spotify.helios.client.HeliosClient in project helios by spotify.

the class SystemTestBase method client.

protected HeliosClient client(final String user, final String endpoint) {
    final HeliosClient client = HeliosClient.newBuilder().setUser(user).setEndpoints(singletonList(URI.create(endpoint))).build();
    clients.add(client);
    return client;
}
Also used : HeliosClient(com.spotify.helios.client.HeliosClient)

Aggregations

HeliosClient (com.spotify.helios.client.HeliosClient)57 Test (org.junit.Test)53 JobId (com.spotify.helios.common.descriptors.JobId)35 Job (com.spotify.helios.common.descriptors.Job)25 Deployment (com.spotify.helios.common.descriptors.Deployment)19 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)14 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)13 Matchers.containsString (org.hamcrest.Matchers.containsString)11 DockerClient (com.spotify.docker.client.DockerClient)10 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)10 HostStatus (com.spotify.helios.common.descriptors.HostStatus)9 AgentMain (com.spotify.helios.agent.AgentMain)8 JobStatus (com.spotify.helios.common.descriptors.JobStatus)6 PortMapping (com.spotify.helios.common.descriptors.PortMapping)5 Map (java.util.Map)5 ExecHealthCheck (com.spotify.helios.common.descriptors.ExecHealthCheck)4 HealthCheck (com.spotify.helios.common.descriptors.HealthCheck)4 HttpHealthCheck (com.spotify.helios.common.descriptors.HttpHealthCheck)4 ServiceEndpoint (com.spotify.helios.common.descriptors.ServiceEndpoint)4 TcpHealthCheck (com.spotify.helios.common.descriptors.TcpHealthCheck)4