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);
}
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());
}
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;
}
});
}
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());
}
Aggregations