use of com.spotify.helios.common.descriptors.TaskStatus in project helios by spotify.
the class JobExpirationTest method test.
@Test
public void test() throws Exception {
startDefaultMaster();
final HeliosClient client = defaultClient();
startDefaultAgent(testHost());
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, IDLE_COMMAND, DateTime.now().plusSeconds(10).toDate());
deployJob(jobId, testHost());
// Make sure the job runs
final TaskStatus taskStatus = awaitJobState(client, testHost(), jobId, RUNNING, WAIT_TIMEOUT_SECONDS, SECONDS);
// Then make sure it expires
Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<JobId>() {
@Override
public JobId call() throws Exception {
if (client.jobs().get().containsKey(jobId)) {
// job still exists, return null to continue polling
return null;
} else {
// job no longer exists, return non-null to exit polling
return jobId;
}
}
});
// Wait for the agent to kill the container
final ContainerExit exit = docker.waitContainer(taskStatus.getContainerId());
assertThat(exit.statusCode(), is(0));
}
use of com.spotify.helios.common.descriptors.TaskStatus in project helios by spotify.
the class MultiplePortJobTest method testPortEnvVars.
@Test
public void testPortEnvVars() throws Exception {
startDefaultMaster();
startDefaultAgent(testHost());
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final Map<String, PortMapping> ports = ImmutableMap.of("bar", staticMapping1);
try (final DockerClient dockerClient = getNewDockerClient()) {
final JobId jobId = createJob(testJobName + 1, testJobVersion, BUSYBOX, asList("sh", "-c", "echo $HELIOS_PORT_bar"), EMPTY_ENV, ports);
deployJob(jobId, testHost());
final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
final String log;
try (final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr())) {
log = logs.readFully();
}
assertEquals(testHost() + ":" + externalPort1, log.trim());
}
}
use of com.spotify.helios.common.descriptors.TaskStatus 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());
}
}
use of com.spotify.helios.common.descriptors.TaskStatus in project helios by spotify.
the class NetworkModeTest method test.
@Test
public void test() throws Exception {
final CreateJobResponse created = client.createJob(job).get();
assertEquals(CreateJobResponse.Status.OK, created.getStatus());
final JobId jobId = job.getId();
// 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 taskStatus = awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
try (final DockerClient docker = getNewDockerClient()) {
final HostConfig hostConfig = docker.inspectContainer(taskStatus.getContainerId()).hostConfig();
assertEquals(NETWORK_MODE, hostConfig.networkMode());
}
}
use of com.spotify.helios.common.descriptors.TaskStatus in project helios by spotify.
the class ResourcesTest method testClient.
@Test
public void testClient() throws Exception {
// Doesn't work on CircleCI because their lxc-driver can't set cpus
// See output of `docker run --cpuset-cpus 0-1 spotify/busybox:latest true`
assumeFalse(isCircleCi());
final CreateJobResponse created = client.createJob(job).get();
assertEquals(CreateJobResponse.Status.OK, created.getStatus());
final JobId jobId = job.getId();
// 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 taskStatus = awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
assertJobEquals(job, taskStatus.getJob());
try (final DockerClient docker = getNewDockerClient()) {
final HostConfig hostConfig = docker.inspectContainer(taskStatus.getContainerId()).hostConfig();
assertEquals(CPU_SHARES, hostConfig.cpuShares());
assertEquals(CPUSET_CPUS, hostConfig.cpusetCpus());
final Info info = docker.info();
final Iterable<String> split = Splitter.on(".").split(docker.version().apiVersion());
//noinspection ConstantConditions
final int major = Integer.parseInt(Iterables.get(split, 0, "0"));
//noinspection ConstantConditions
final int minor = Integer.parseInt(Iterables.get(split, 1, "0"));
// TODO (dxia) This doesn't work on docker < 1.7 ie docker API < 1.19 for some reason.
if (major >= 1 && minor >= 19) {
if (info.memoryLimit()) {
assertEquals(MEMORY, hostConfig.memory());
}
if (info.swapLimit()) {
assertEquals(MEMORY_SWAP, hostConfig.memorySwap());
}
}
}
}
Aggregations