Search in sources :

Example 61 with Job

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());
}
Also used : CreateJobResponse(com.spotify.helios.common.protocol.CreateJobResponse) Job(com.spotify.helios.common.descriptors.Job) Test(org.junit.Test)

Example 62 with Job

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());
    }
}
Also used : ServicePorts(com.spotify.helios.common.descriptors.ServicePorts) FileOutputStream(java.io.FileOutputStream) PortMapping(com.spotify.helios.common.descriptors.PortMapping) Job(com.spotify.helios.common.descriptors.Job) File(java.io.File) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) Test(org.junit.Test)

Example 63 with Job

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());
}
Also used : Path(java.nio.file.Path) HeliosClient(com.spotify.helios.client.HeliosClient) 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 64 with Job

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);
}
Also used : HttpHealthCheck(com.spotify.helios.common.descriptors.HttpHealthCheck) HealthCheck(com.spotify.helios.common.descriptors.HealthCheck) ExecHealthCheck(com.spotify.helios.common.descriptors.ExecHealthCheck) TcpHealthCheck(com.spotify.helios.common.descriptors.TcpHealthCheck) HeliosClient(com.spotify.helios.client.HeliosClient) Job(com.spotify.helios.common.descriptors.Job) Test(org.junit.Test)

Example 65 with 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());
}
Also used : ExecCreation(com.spotify.docker.client.messages.ExecCreation) DockerClient(com.spotify.docker.client.DockerClient) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) Endpoint(com.spotify.helios.serviceregistration.ServiceRegistration.Endpoint) HttpHealthCheck(com.spotify.helios.common.descriptors.HttpHealthCheck) HealthCheck(com.spotify.helios.common.descriptors.HealthCheck) ExecHealthCheck(com.spotify.helios.common.descriptors.ExecHealthCheck) TcpHealthCheck(com.spotify.helios.common.descriptors.TcpHealthCheck) HeliosClient(com.spotify.helios.client.HeliosClient) Job(com.spotify.helios.common.descriptors.Job) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) ServiceRegistration(com.spotify.helios.serviceregistration.ServiceRegistration) 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