Search in sources :

Example 36 with HeliosClient

use of com.spotify.helios.client.HeliosClient in project helios by spotify.

the class CliHostListTest method testHostListJson.

@Test
public void testHostListJson() throws Exception {
    final String jsonOutput = cli("hosts", "-f", "--json");
    final Map<String, HostStatus> statuses = Json.readUnchecked(jsonOutput, new TypeReference<Map<String, HostStatus>>() {
    });
    final HeliosClient client = defaultClient();
    final Map<String, HostStatus> expectedStatuses = client.hostStatuses(ImmutableList.of(hostname1, hostname2)).get();
    assertThat(expectedStatuses, equalTo(statuses));
}
Also used : HostStatus(com.spotify.helios.common.descriptors.HostStatus) HeliosClient(com.spotify.helios.client.HeliosClient) Map(java.util.Map) Test(org.junit.Test)

Example 37 with HeliosClient

use of com.spotify.helios.client.HeliosClient 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 38 with HeliosClient

use of com.spotify.helios.client.HeliosClient 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 39 with HeliosClient

use of com.spotify.helios.client.HeliosClient 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)

Example 40 with HeliosClient

use of com.spotify.helios.client.HeliosClient in project helios by spotify.

the class ImageMissingTest method test.

@Test
public void test() throws Exception {
    startDefaultMaster();
    startDefaultAgent(testHost());
    final HeliosClient client = defaultClient();
    awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    final JobId jobId = createJob(testJobName, testJobVersion, "this_sould_not_exist", ImmutableList.of("/bin/true"));
    deployJob(jobId, testHost());
    awaitJobThrottle(client, testHost(), jobId, IMAGE_MISSING, LONG_WAIT_SECONDS, SECONDS);
    final HostStatus hostStatus = client.hostStatus(testHost()).get();
    final TaskStatus taskStatus = hostStatus.getStatuses().get(jobId);
    assertEquals(TaskStatus.State.FAILED, taskStatus.getState());
}
Also used : HostStatus(com.spotify.helios.common.descriptors.HostStatus) HeliosClient(com.spotify.helios.client.HeliosClient) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Aggregations

HeliosClient (com.spotify.helios.client.HeliosClient)57 Test (org.junit.Test)53 JobId (com.spotify.helios.common.descriptors.JobId)35 Job (com.spotify.helios.common.descriptors.Job)25 Deployment (com.spotify.helios.common.descriptors.Deployment)19 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)14 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)13 Matchers.containsString (org.hamcrest.Matchers.containsString)11 DockerClient (com.spotify.docker.client.DockerClient)10 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)10 HostStatus (com.spotify.helios.common.descriptors.HostStatus)9 AgentMain (com.spotify.helios.agent.AgentMain)8 JobStatus (com.spotify.helios.common.descriptors.JobStatus)6 PortMapping (com.spotify.helios.common.descriptors.PortMapping)5 Map (java.util.Map)5 ExecHealthCheck (com.spotify.helios.common.descriptors.ExecHealthCheck)4 HealthCheck (com.spotify.helios.common.descriptors.HealthCheck)4 HttpHealthCheck (com.spotify.helios.common.descriptors.HttpHealthCheck)4 ServiceEndpoint (com.spotify.helios.common.descriptors.ServiceEndpoint)4 TcpHealthCheck (com.spotify.helios.common.descriptors.TcpHealthCheck)4