Search in sources :

Example 21 with JobStatus

use of com.spotify.helios.common.descriptors.JobStatus in project helios by spotify.

the class CliDeploymentTest method testDeployAndUndeployJob.

@Test
public void testDeployAndUndeployJob() throws Exception {
    startDefaultMaster();
    // Wait for master to come up
    Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<String>() {

        @Override
        public String call() throws Exception {
            final String output = cli("masters");
            return output.contains(masterName()) ? output : null;
        }
    });
    startDefaultAgent(testHost());
    final String image = BUSYBOX;
    final Map<String, PortMapping> ports = ImmutableMap.of("foo", PortMapping.of(4711), "bar", PortMapping.of(5000, externalPort));
    final Map<ServiceEndpoint, ServicePorts> registration = ImmutableMap.of(ServiceEndpoint.of("foo-service", "tcp"), ServicePorts.of("foo"), ServiceEndpoint.of("bar-service", "http"), ServicePorts.of("bar"));
    final Map<String, String> env = ImmutableMap.of("BAD", "f00d");
    // Wait for agent to come up
    awaitHostRegistered(testHost(), LONG_WAIT_SECONDS, SECONDS);
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // Create job
    final JobId jobId = createJob(testJobName, testJobVersion, image, IDLE_COMMAND, env, ports, registration);
    // Query for job
    final Job expected = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(image).setCommand(IDLE_COMMAND).setEnv(env).setPorts(ports).setRegistration(registration).setCreatingUser(TEST_USER).build();
    final String inspectOutput = cli("inspect", "--json", jobId.toString());
    final Job parsed = Json.read(inspectOutput, Job.class);
    assertJobEquals(expected, parsed);
    assertThat(cli("jobs", testJobName, "-q"), containsString(jobId.toString()));
    assertThat(cli("jobs", testJobName + ":" + testJobVersion, "-q"), containsString(jobId.toString()));
    assertEquals("job pattern foozbarz matched no jobs", cli("jobs", "foozbarz").trim());
    assertTrue(cli("jobs", "foozbarz", "-q").isEmpty());
    // Create a new job using the first job as a template
    final Job expectedCloned = expected.toBuilder().setVersion(expected.getId().getVersion() + "-cloned").build();
    final JobId clonedJobId = JobId.parse(WHITESPACE.trimFrom(cli("create", "-q", "-t", testJobName + ":" + testJobVersion, testJobName + ":" + testJobVersion + "-cloned")));
    final String clonedInspectOutput = cli("inspect", "--json", clonedJobId.toString());
    final Job clonedParsed = Json.read(clonedInspectOutput, Job.class);
    assertJobEquals(expectedCloned, clonedParsed);
    // Verify that port mapping and environment variables are correct
    final String statusString = cli("status", "--job", jobId.toString(), "--json");
    final Map<JobId, JobStatus> statuses = Json.read(statusString, STATUSES_TYPE);
    final Job job = statuses.get(jobId).getJob();
    assertEquals(ServicePorts.of("foo"), job.getRegistration().get(ServiceEndpoint.of("foo-service", "tcp")));
    assertEquals(ServicePorts.of("bar"), job.getRegistration().get(ServiceEndpoint.of("bar-service", "http")));
    assertEquals(4711, job.getPorts().get("foo").getInternalPort());
    assertEquals(PortMapping.of(5000, externalPort), job.getPorts().get("bar"));
    assertEquals("f00d", job.getEnv().get("BAD"));
    final String duplicateJob = cli("create", testJobName + ":" + testJobVersion, image, "--", IDLE_COMMAND);
    assertThat(duplicateJob, containsString("JOB_ALREADY_EXISTS"));
    final String prestop = stopJob(jobId, testHost());
    assertThat(prestop, containsString("JOB_NOT_DEPLOYED"));
    // Deploy job
    deployJob(jobId, testHost());
    // Stop job
    final String stop1 = stopJob(jobId, BOGUS_HOST);
    assertThat(stop1, containsString("HOST_NOT_FOUND"));
    final String stop2 = stopJob(BOGUS_JOB, testHost());
    assertThat(stop2, containsString("Unknown job"));
    final String stop3 = stopJob(jobId, testHost());
    assertThat(stop3, containsString(testHost() + ": done"));
    // Verify that undeploying the job from a nonexistent host fails
    assertThat(cli("undeploy", jobId.toString(), BOGUS_HOST), containsString("HOST_NOT_FOUND"));
    // Verify that undeploying a nonexistent job from the host fails
    assertThat(cli("undeploy", BOGUS_JOB.toString(), testHost()), containsString("Unknown job"));
    // Undeploy job
    undeployJob(jobId, testHost());
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) JobStatus(com.spotify.helios.common.descriptors.JobStatus) ServicePorts(com.spotify.helios.common.descriptors.ServicePorts) PortMapping(com.spotify.helios.common.descriptors.PortMapping) Job(com.spotify.helios.common.descriptors.Job) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 22 with JobStatus

use of com.spotify.helios.common.descriptors.JobStatus in project helios by spotify.

the class MultipleJobsTest method jobStatusBulk.

@Test
public void jobStatusBulk() throws Exception {
    startDefaultMaster();
    startDefaultAgent(testHost());
    awaitHostRegistered(testHost(), LONG_WAIT_SECONDS, SECONDS);
    final HeliosClient client = defaultClient();
    final Job job = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(IDLE_COMMAND).setCreatingUser(TEST_USER).build();
    final JobId jobId = job.getId();
    client.createJob(job).get();
    final Job job2 = Job.newBuilder().setName(testJobName + "2").setVersion(testJobVersion).setImage(BUSYBOX).setCommand(IDLE_COMMAND).setCreatingUser(TEST_USER).build();
    final JobId job2Id = job2.getId();
    client.createJob(job2).get();
    final Deployment deployment = Deployment.of(jobId, START, TEST_USER);
    final Deployment deployment2 = Deployment.of(job2Id, START, TEST_USER);
    client.deploy(deployment, testHost()).get();
    awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
    client.deploy(deployment2, testHost()).get();
    awaitJobState(client, testHost(), job2Id, RUNNING, LONG_WAIT_SECONDS, SECONDS);
    final Map<JobId, JobStatus> statuses = client.jobStatuses(ImmutableSet.of(jobId, job2Id)).get();
    assertTrue("should contain job 1 id", statuses.containsKey(jobId));
    assertTrue("should contain job 2 id", statuses.containsKey(job2Id));
}
Also used : JobStatus(com.spotify.helios.common.descriptors.JobStatus) Deployment(com.spotify.helios.common.descriptors.Deployment) HeliosClient(com.spotify.helios.client.HeliosClient) Job(com.spotify.helios.common.descriptors.Job) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 23 with JobStatus

use of com.spotify.helios.common.descriptors.JobStatus 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)

Aggregations

JobStatus (com.spotify.helios.common.descriptors.JobStatus)23 JobId (com.spotify.helios.common.descriptors.JobId)19 Job (com.spotify.helios.common.descriptors.Job)11 Test (org.junit.Test)9 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)8 HeliosClient (com.spotify.helios.client.HeliosClient)6 Map (java.util.Map)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 Deployment (com.spotify.helios.common.descriptors.Deployment)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 IOException (java.io.IOException)3 PortMapping (com.spotify.helios.common.descriptors.PortMapping)2 ServiceEndpoint (com.spotify.helios.common.descriptors.ServiceEndpoint)2 TaskStatusEvent (com.spotify.helios.common.descriptors.TaskStatusEvent)2 JobUndeployResponse (com.spotify.helios.common.protocol.JobUndeployResponse)2 Integer.toHexString (java.lang.Integer.toHexString)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableList (com.google.common.collect.ImmutableList)1