Search in sources :

Example 16 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class SyslogRedirectionTest method test.

@Test
public void test() throws Exception {
    final String syslogOutput = "should-be-redirected";
    try (final DockerClient docker = getNewDockerClient()) {
        // Start a container that will be our "syslog" endpoint (just run netcat and print whatever
        // we receive).
        final String port = "4711";
        final String expose = port + "/udp";
        docker.pull(ALPINE);
        final HostConfig hostConfig = HostConfig.builder().publishAllPorts(true).build();
        final ContainerConfig config = ContainerConfig.builder().image(// includes spotify/busybox:latest with netcat with udp support
        ALPINE).cmd(asList("nc", "-p", port, "-l", "-u")).exposedPorts(ImmutableSet.of(expose)).hostConfig(hostConfig).build();
        final ContainerCreation creation = docker.createContainer(config, testTag + "_syslog");
        final String syslogContainerId = creation.id();
        docker.startContainer(syslogContainerId);
        final ContainerInfo containerInfo = docker.inspectContainer(syslogContainerId);
        assertThat(containerInfo.state().running(), equalTo(true));
        final String syslogEndpoint = syslogHost + ":" + containerInfo.networkSettings().ports().get(expose).get(0).hostPort();
        // Run a Helios job that logs to syslog.
        startDefaultMaster();
        startDefaultAgent(testHost(), "--syslog-redirect", syslogEndpoint);
        awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
        final List<String> command = Lists.newArrayList();
        final JobId jobId = createJob(testJobName, testJobVersion, testImage, command, ImmutableMap.of("SYSLOG_REDIRECTOR", "/syslog-redirector"));
        deployJob(jobId, testHost());
        final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
        {
            // Verify the log for the task container
            LogStream logs = null;
            try {
                logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr());
                final String log = logs.readFully();
                // for old docker versions should be nothing in the docker output log, either error text
                // or our message
                assertEquals("", log);
            } catch (DockerRequestException e) {
                // for new docker versions, trying to read logs should throw an error but the syslog
                // option should be set
                final String logType = docker.inspectContainer(taskStatus.getContainerId()).hostConfig().logConfig().logType();
                assertEquals("syslog", logType);
            } finally {
                if (logs != null) {
                    logs.close();
                }
            }
        }
        // Verify the log for the syslog container
        {
            final String log;
            try (LogStream logs = docker.logs(syslogContainerId, stdout(), stderr())) {
                log = logs.readFully();
            }
            // the output message from the command should appear in the syslog container
            assertThat(log, containsString(syslogOutput));
        }
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerClient(com.spotify.docker.client.DockerClient) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Matchers.containsString(org.hamcrest.Matchers.containsString) LogStream(com.spotify.docker.client.LogStream) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 17 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class SystemTestBase method deployJob.

protected void deployJob(final JobId jobId, final String host, final String token) throws Exception {
    final List<String> deployArgs = Lists.newArrayList(jobId.toString(), host);
    if (token != null) {
        deployArgs.addAll(ImmutableList.of("--token", token));
    }
    final String deployOutput = cli("deploy", deployArgs);
    assertThat(deployOutput, containsString(host + ": done"));
    final String output = cli("status", "--host", host, "--json");
    final Map<JobId, JobStatus> statuses = Json.readUnchecked(output, new TypeReference<Map<JobId, JobStatus>>() {
    });
    assertTrue(statuses.keySet().contains(jobId));
}
Also used : JobStatus(com.spotify.helios.common.descriptors.JobStatus) Matchers.containsString(org.hamcrest.Matchers.containsString) Integer.toHexString(java.lang.Integer.toHexString) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) JobId(com.spotify.helios.common.descriptors.JobId)

Example 18 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class TerminationTest method testNoIntOnExit.

@Test
public void testNoIntOnExit() throws Exception {
    startDefaultMaster();
    final String host = testHost();
    startDefaultAgent(host);
    final HeliosClient client = defaultClient();
    awaitHostStatus(client, host, UP, LONG_WAIT_SECONDS, SECONDS);
    // Note: signal 2 is SIGINT
    final Job jobToInterrupt = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(asList("/bin/sh", "-c", "trap handle 2; handle() { echo int; exit 0; }; " + "while true; do sleep 1; done")).build();
    final JobId jobId = createJob(jobToInterrupt);
    deployJob(jobId, host);
    awaitTaskState(jobId, host, RUNNING);
    client.setGoal(new Deployment(jobId, Goal.STOP, Deployment.EMTPY_DEPLOYER_USER, Deployment.EMPTY_DEPLOYER_MASTER, Deployment.EMPTY_DEPLOYMENT_GROUP_NAME), host);
    final TaskStatus taskStatus = awaitTaskState(jobId, host, STOPPED);
    final String log;
    try (final DockerClient dockerClient = getNewDockerClient();
        LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout())) {
        log = logs.readFully();
    }
    // No message expected, since SIGINT should not be sent
    assertEquals("", log);
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) Deployment(com.spotify.helios.common.descriptors.Deployment) LogStream(com.spotify.docker.client.LogStream) 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) Test(org.junit.Test)

Example 19 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class UndeployFilteringTest method testAgent.

@Test
public void testAgent() throws Exception {
    final JobId jobId = createAndAwaitJobRunning();
    final byte[] data1 = curator.getData().forPath(Paths.statusHostJob(TEST_HOST, jobId));
    assertNotNull(data1);
    final TaskStatus status = Json.read(data1, TaskStatus.class);
    assertNotNull(status);
    assertEquals(START, status.getGoal());
    assertEquals(RUNNING, status.getState());
    // stop so we can create and maintain the tombstone
    agent.stopAsync().awaitTerminated();
    // create tombstone
    client.undeploy(jobId, TEST_HOST).get();
    final byte[] data2 = curator.getData().forPath(Paths.statusHostJob(TEST_HOST, jobId));
    assertNotNull(data2);
    final TaskStatus status2 = Json.read(data2, TaskStatus.class);
    assertNotNull(status2);
    assertEquals(START, status2.getGoal());
    assertEquals(RUNNING, status2.getState());
}
Also used : TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 20 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class UndeployFilteringTest method createAndAwaitJobRunning.

private JobId createAndAwaitJobRunning() throws Exception {
    final CreateJobResponse jobby = client.createJob(job).get();
    assertEquals(CreateJobResponse.Status.OK, jobby.getStatus());
    final JobId jobId = job.getId();
    final JobDeployResponse deployResponse = client.deploy(Deployment.of(jobId, START), TEST_HOST).get();
    assertEquals(JobDeployResponse.Status.OK, deployResponse.getStatus());
    awaitJobState(client, TEST_HOST, jobId, State.RUNNING, 30, TimeUnit.SECONDS);
    return jobId;
}
Also used : CreateJobResponse(com.spotify.helios.common.protocol.CreateJobResponse) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) JobId(com.spotify.helios.common.descriptors.JobId)

Aggregations

JobId (com.spotify.helios.common.descriptors.JobId)115 Test (org.junit.Test)68 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)41 Job (com.spotify.helios.common.descriptors.Job)37 HeliosClient (com.spotify.helios.client.HeliosClient)35 Deployment (com.spotify.helios.common.descriptors.Deployment)29 Matchers.containsString (org.hamcrest.Matchers.containsString)25 DockerClient (com.spotify.docker.client.DockerClient)19 JobStatus (com.spotify.helios.common.descriptors.JobStatus)19 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)16 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)13 IOException (java.io.IOException)12 HostStatus (com.spotify.helios.common.descriptors.HostStatus)11 Map (java.util.Map)11 LogStream (com.spotify.docker.client.LogStream)10 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)10 KeeperException (org.apache.zookeeper.KeeperException)9 TaskStatusEvent (com.spotify.helios.common.descriptors.TaskStatusEvent)8 AgentMain (com.spotify.helios.agent.AgentMain)7 PortMapping (com.spotify.helios.common.descriptors.PortMapping)7