use of com.spotify.helios.common.descriptors.Job in project helios by spotify.
the class CliJobCreationTest method testSuccessJsonOutput.
@Test
public void testSuccessJsonOutput() throws Exception {
// Creating a valid job should return JSON with status OK
final String output = cli("create", "--json", testJobNameAndVersion, BUSYBOX);
final CreateJobResponse createJobResponse = Json.read(output, CreateJobResponse.class);
assertEquals(CreateJobResponse.Status.OK, createJobResponse.getStatus());
assertEquals(new ArrayList<String>(), createJobResponse.getErrors());
assertTrue(createJobResponse.getId().startsWith(testJobNameAndVersion));
// Check the master has set the created field
final String output2 = cli("inspect", testJobNameAndVersion, "--json");
final Job job = Json.read(output2, Job.class);
assertNotNull(job.getCreated());
}
use of com.spotify.helios.common.descriptors.Job in project helios by spotify.
the class CliJobCreationTest method testMergeFileAndCliArgsEnvPrecedence.
@Test
public void testMergeFileAndCliArgsEnvPrecedence() throws Exception {
final String redundantEnvKey = "BAD";
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(redundantEnvKey, "f00d");
final Map<String, String> volumes = Maps.newHashMap();
volumes.put("/etc/spotify/secret-keys.yaml:ro", "/etc/spotify/secret-keys.yaml");
// Create a new job, serialize it to JSON
final Job.Builder builder = Job.newBuilder().setCommand(Lists.newArrayList("server", "foo-service.yaml")).setEnv(env).setPorts(ports).setRegistration(registration).setVolumes(volumes).setCreatingUser(TEST_USER);
final Job job = builder.build();
final String jobConfigJsonString = job.toJsonString();
// Create temporary job config file
final File file = temporaryFolder.newFile();
final String absolutePath = file.getAbsolutePath();
// Write JSON config to temp file
try (final FileOutputStream outFile = new FileOutputStream(file)) {
outFile.write(Charsets.UTF_8.encode(jobConfigJsonString).array());
// Create job and specify the temp file.
cli("create", "--file", absolutePath, testJobNameAndVersion, BUSYBOX, "--env", redundantEnvKey + "=FOOD");
// Inspect the job.
final String actualJobConfigJson = cli("inspect", testJobNameAndVersion, "--json");
// Compare to make sure the created job has the expected configuration,
// i.e. the configuration resulting from a merge of the JSON file and CLI args.
final Job actualJob = Json.read(actualJobConfigJson, Job.class);
final Job.Builder actualJobBuilder = actualJob.toBuilder();
builder.setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setEnv(ImmutableMap.of(redundantEnvKey, "FOOD"));
assertJobEquals(builder.build(), actualJobBuilder.build());
}
}
use of com.spotify.helios.common.descriptors.Job in project helios by spotify.
the class ConfigFileJobCreationTest method test.
@Test
public void test() throws Exception {
startDefaultMaster();
final HeliosClient client = defaultClient();
final String name = testJobName;
final String version = "17";
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");
final Map<String, Object> configuration = ImmutableMap.of("id", name + ":" + version, "image", image, "ports", ports, "registration", registration, "env", env);
final Path file = temporaryFolder.newFile().toPath();
Files.write(file, Json.asBytes(configuration));
final String output = cli("create", "-q", "-f", file.toAbsolutePath().toString());
final JobId jobId = JobId.parse(WHITESPACE.trimFrom(output));
final Map<JobId, Job> jobs = client.jobs().get();
final Job job = jobs.get(jobId);
assertEquals(name, job.getId().getName());
assertEquals(version, job.getId().getVersion());
assertEquals(ports, job.getPorts());
assertEquals(env, job.getEnv());
assertEquals(registration, job.getRegistration());
}
use of com.spotify.helios.common.descriptors.Job in project helios by spotify.
the class HealthCheckTest method testHttp.
@Test
public void testHttp() throws Exception {
startDefaultMaster();
final HeliosClient client = defaultClient();
startDefaultAgent(testHost(), "--service-registry=" + registryAddress);
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final HealthCheck healthCheck = HttpHealthCheck.of("http", "/");
// start a container that listens on a poke port, and once poked runs a web server
final Job job = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(NGINX).setCommand(asList("sh", "-c", "nc -l -p 4711 && nginx -g 'daemon off;'")).addPort("poke", PortMapping.of(4711)).addPort("http", PortMapping.of(80)).addRegistration(ServiceEndpoint.of("foo_service", "foo_proto"), ServicePorts.of("http")).setHealthCheck(healthCheck).build();
assertContainerRegistersAfterPoke(client, job);
}
use of com.spotify.helios.common.descriptors.Job in project helios by spotify.
the class HealthCheckTest method testExec.
@Test
public void testExec() throws Exception {
// CircleCI uses lxc which doesn't support exec - https://circleci.com/docs/docker/#docker-exec
assumeFalse(isCircleCi());
final DockerClient dockerClient = getNewDockerClient();
assumeThat(dockerClient.version(), is(execCompatibleDockerVersion()));
startDefaultMaster();
final HeliosClient client = defaultClient();
startDefaultAgent(testHost(), "--service-registry=" + registryAddress);
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
// check if "file" exists in the root directory as a healthcheck
final HealthCheck healthCheck = ExecHealthCheck.of("test", "-e", "file");
// start a container that listens on the service port
final String portName = "service";
final String serviceName = "foo_service";
final String serviceProtocol = "foo_proto";
final Job job = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(asList("sh", "-c", "nc -l -p 4711")).addPort(portName, PortMapping.of(4711)).addRegistration(ServiceEndpoint.of(serviceName, serviceProtocol), ServicePorts.of(portName)).setHealthCheck(healthCheck).build();
final JobId jobId = createJob(job);
deployJob(jobId, testHost());
final TaskStatus jobState = awaitTaskState(jobId, testHost(), HEALTHCHECKING);
// wait a few seconds to see if the service gets registered
Thread.sleep(3000);
// shouldn't be registered, since we haven't created the file yet
verify(registrar, never()).register(any(ServiceRegistration.class));
// create the file in the container to make the healthcheck succeed
final String[] makeFileCmd = new String[] { "touch", "file" };
final ExecCreation execCreation = dockerClient.execCreate(jobState.getContainerId(), makeFileCmd);
final String execId = execCreation.id();
dockerClient.execStart(execId);
awaitTaskState(jobId, testHost(), RUNNING);
dockerClient.close();
verify(registrar, timeout((int) SECONDS.toMillis(LONG_WAIT_SECONDS))).register(registrationCaptor.capture());
final ServiceRegistration serviceRegistration = registrationCaptor.getValue();
assertEquals(1, serviceRegistration.getEndpoints().size());
final Endpoint registeredEndpoint = serviceRegistration.getEndpoints().get(0);
assertEquals("wrong service", serviceName, registeredEndpoint.getName());
assertEquals("wrong protocol", serviceProtocol, registeredEndpoint.getProtocol());
}
Aggregations