Search in sources :

Example 76 with Job

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

the class SupervisorTest method verifySupervisorStopsDockerContainerWithConfiguredKillTime.

@Test
public void verifySupervisorStopsDockerContainerWithConfiguredKillTime() throws Exception {
    final String containerId = "deadbeef";
    final Job longKillTimeJob = Job.newBuilder().setName(NAME).setCommand(COMMAND).setImage(IMAGE).setVersion(VERSION).setSecondsToWaitBeforeKill(30).build();
    mockTaskStatus(longKillTimeJob.getId());
    final Supervisor longKillTimeSupervisor = createSupervisor(longKillTimeJob);
    when(docker.createContainer(any(ContainerConfig.class), any(String.class))).thenReturn(ContainerCreation.builder().id(containerId).build());
    final ImageInfo imageInfo = mock(ImageInfo.class);
    when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);
    // Have waitContainer wait forever.
    final SettableFuture<ContainerExit> waitFuture = SettableFuture.create();
    when(docker.waitContainer(containerId)).thenAnswer(futureAnswer(waitFuture));
    // Start the job (so that a runner exists)
    longKillTimeSupervisor.setGoal(START);
    when(docker.inspectContainer(eq(containerId))).thenReturn(runningResponse);
    // This is already verified above, but it works as a hack to wait for the model/docker state
    // to converge in such a way that a setGoal(STOP) will work. :|
    verify(docker, timeout(30000)).waitContainer(containerId);
    // Stop the job
    longKillTimeSupervisor.setGoal(STOP);
    verify(docker, timeout(30000)).stopContainer(eq(containerId), eq(longKillTimeJob.getSecondsToWaitBeforeKill()));
    // Change docker container state to stopped now that it was killed
    when(docker.inspectContainer(eq(containerId))).thenReturn(stoppedResponse);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerExit(com.spotify.docker.client.messages.ContainerExit) Job(com.spotify.helios.common.descriptors.Job) ImageInfo(com.spotify.docker.client.messages.ImageInfo) Test(org.junit.Test)

Example 77 with Job

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

the class TaskConfigTest method testRegistrationWithHttpHealthCheck.

@Test
public void testRegistrationWithHttpHealthCheck() throws Exception {
    final String path = "/health";
    final Job job = JOB.toBuilder().setHealthCheck(HealthCheck.newHttpHealthCheck().setPath(path).setPort(PORT_NAME).build()).build();
    final TaskConfig taskConfig = TaskConfig.builder().namespace("test").host(HOST).job(job).build();
    final ServiceRegistration.Endpoint endpoint = taskConfig.registration().getEndpoints().get(0);
    assertEquals(path, endpoint.getHealthCheck().getPath());
    assertEquals(EndpointHealthCheck.HTTP, endpoint.getHealthCheck().getType());
    assertEquals(EXTERNAL_PORT, endpoint.getPort());
}
Also used : Job(com.spotify.helios.common.descriptors.Job) ServiceRegistration(com.spotify.helios.serviceregistration.ServiceRegistration) Test(org.junit.Test)

Example 78 with Job

use of com.spotify.helios.common.descriptors.Job 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 79 with Job

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

the class ApiTest method testHashLessJobCreation.

/**
   * Verify that the Helios master generates and returns a hash if the submitted job creation
   * request does not include one.
   */
@Test
public void testHashLessJobCreation() throws Exception {
    startDefaultMaster();
    final Job job = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(IDLE_COMMAND).setCreatingUser(TEST_USER).build();
    // Remove the hash from the id in the json serialized job
    final ObjectNode json = (ObjectNode) Json.reader().readTree(Json.asString(job));
    json.set("id", TextNode.valueOf(testJobName + ":" + testJobVersion));
    final HttpURLConnection req = post("/jobs?user=" + TEST_USER, Json.asBytes(json));
    assertEquals(req.getResponseCode(), 200);
    final CreateJobResponse res = Json.read(toByteArray(req.getInputStream()), CreateJobResponse.class);
    assertEquals(OK, res.getStatus());
    assertTrue(res.getErrors().isEmpty());
    assertEquals(job.getId().toString(), res.getId());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CreateJobResponse(com.spotify.helios.common.protocol.CreateJobResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Job(com.spotify.helios.common.descriptors.Job) Test(org.junit.Test)

Aggregations

Job (com.spotify.helios.common.descriptors.Job)79 Test (org.junit.Test)57 JobId (com.spotify.helios.common.descriptors.JobId)38 HeliosClient (com.spotify.helios.client.HeliosClient)25 Deployment (com.spotify.helios.common.descriptors.Deployment)21 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)16 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)15 JobStatus (com.spotify.helios.common.descriptors.JobStatus)12 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)11 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)10 KeeperException (org.apache.zookeeper.KeeperException)10 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)10 PortMapping (com.spotify.helios.common.descriptors.PortMapping)9 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)8 ServiceEndpoint (com.spotify.helios.common.descriptors.ServiceEndpoint)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 DockerClient (com.spotify.docker.client.DockerClient)7 ZooKeeperOperation (com.spotify.helios.servicescommon.coordination.ZooKeeperOperation)7 IOException (java.io.IOException)7 ServicePorts (com.spotify.helios.common.descriptors.ServicePorts)6