Search in sources :

Example 16 with DockerClient

use of com.spotify.docker.client.DockerClient in project opennms by OpenNMS.

the class SyslogKafkaElasticsearch5OutageIT method testMinionSyslogsOverKafkaToEsRest.

@Test
public void testMinionSyslogsOverKafkaToEsRest() throws Exception {
    Date startOfTest = new Date();
    int numMessages = 10000;
    int packetsPerSecond = 250;
    InetSocketAddress minionSshAddr = testEnvironment.getServiceAddress(ContainerAlias.MINION, 8201);
    InetSocketAddress opennmsSshAddr = testEnvironment.getServiceAddress(ContainerAlias.OPENNMS, 8101);
    InetSocketAddress kafkaAddress = testEnvironment.getServiceAddress(ContainerAlias.KAFKA, 9092);
    InetSocketAddress zookeeperAddress = testEnvironment.getServiceAddress(ContainerAlias.KAFKA, 2181);
    // Install the Kafka syslog and trap handlers on the Minion system
    installFeaturesOnMinion(minionSshAddr, kafkaAddress);
    // Install the Kafka and Elasticsearch features on the OpenNMS system
    installFeaturesOnOpenNMS(opennmsSshAddr, kafkaAddress, zookeeperAddress);
    final String sender = testEnvironment.getContainerInfo(ContainerAlias.SNMPD).networkSettings().ipAddress();
    // Wait for the minion to show up
    await().atMost(90, SECONDS).pollInterval(5, SECONDS).until(DaoUtils.countMatchingCallable(getDaoFactory().getDao(MinionDaoHibernate.class), new CriteriaBuilder(OnmsMinion.class).gt("lastUpdated", startOfTest).eq("location", "MINION").toCriteria()), is(1));
    LOG.info("Warming up syslog routes by sending 100 packets");
    // Warm up the routes
    sendMessage(ContainerAlias.MINION, sender, 100);
    for (int i = 0; i < 10; i++) {
        LOG.info("Slept for " + i + " seconds");
        Thread.sleep(1000);
    }
    LOG.info("Resetting statistics");
    resetRouteStatistics(opennmsSshAddr, minionSshAddr);
    for (int i = 0; i < 20; i++) {
        LOG.info("Slept for " + i + " seconds");
        Thread.sleep(1000);
    }
    // Make sure that this evenly divides into the numMessages
    final int chunk = 250;
    // Make sure that this is an even multiple of chunk
    final int logEvery = 1000;
    int count = 0;
    long start = System.currentTimeMillis();
    AtomicInteger restartCounter = new AtomicInteger();
    // Start a timer that occasionally restarts Elasticsearch
    Timer restarter = new Timer("Elasticsearch-Restarter", true);
    restarter.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            final DockerClient docker = ((AbstractTestEnvironment) testEnvironment).getDockerClient();
            final String id = testEnvironment.getContainerInfo(ContainerAlias.ELASTICSEARCH_5).id();
            try {
                LOG.info("Restarting container: {}", id);
                docker.restartContainer(id);
                restartCounter.incrementAndGet();
                LOG.info("Container restarted: {}", id);
            } catch (DockerException | InterruptedException e) {
                LOG.warn("Unexpected exception while restarting container {}", id, e);
            }
        }
    }, 0L, TimeUnit.SECONDS.toMillis(29));
    // Send ${numMessages} syslog messages
    RateLimiter limiter = RateLimiter.create(packetsPerSecond);
    for (int i = 0; i < (numMessages / chunk); i++) {
        limiter.acquire(chunk);
        sendMessage(ContainerAlias.MINION, sender, chunk);
        count += chunk;
        if (count % logEvery == 0) {
            long mid = System.currentTimeMillis();
            LOG.info(String.format("Sent %d packets in %d milliseconds", logEvery, mid - start));
            start = System.currentTimeMillis();
        }
    }
    // Stop restarting Elasticsearch
    restarter.cancel();
    // 100 warm-up messages plus ${numMessages} messages
    pollForElasticsearchEventsUsingJest(this::getEs5Address, 100 + numMessages);
    assertTrue("Elasticsearch was never restarted", restartCounter.get() > 0);
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) DockerClient(com.spotify.docker.client.DockerClient) OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) InetSocketAddress(java.net.InetSocketAddress) Date(java.util.Date) RateLimiter(com.google.common.util.concurrent.RateLimiter) Timer(java.util.Timer) TimerTask(java.util.TimerTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 17 with DockerClient

use of com.spotify.docker.client.DockerClient in project opennms by OpenNMS.

the class MinionHeartbeatOutageIT method restartContainer.

private void restartContainer(ContainerAlias alias) {
    final DockerClient docker = ((AbstractTestEnvironment) testEnvironment).getDockerClient();
    final String id = testEnvironment.getContainerInfo(alias).id();
    final Logger logger = getLogger();
    try {
        logger.info("Restarting container: {} -> {}", alias, id);
        docker.restartContainer(id);
        logger.info("Container restarted: {} -> {}", alias, id);
    } catch (DockerException | InterruptedException e) {
        logger.warn("Unexpected exception while restarting container {}", id, e);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerClient(com.spotify.docker.client.DockerClient) AbstractTestEnvironment(org.opennms.test.system.api.AbstractTestEnvironment) Logger(org.slf4j.Logger)

Example 18 with DockerClient

use of com.spotify.docker.client.DockerClient in project helios by spotify.

the class ZooKeeperClusterIdTest method testAgent.

@Test
public void testAgent() throws Exception {
    startDefaultMaster("--zk-cluster-id=" + zkClusterId);
    startDefaultAgent(testHost(), "--zk-cluster-id=" + zkClusterId);
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // Create job and deploy it
    final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, IDLE_COMMAND);
    deployJob(jobId, testHost());
    final TaskStatus runningStatus = awaitTaskState(jobId, testHost(), RUNNING);
    final String containerId = runningStatus.getContainerId();
    // Delete the config node which contains the cluster ID and all the job definitions
    zk().curatorWithSuperAuth().delete().deletingChildrenIfNeeded().forPath("/config");
    // Sleep for a second so agent has a chance to react to deletion
    Thread.sleep(1000);
    // Make sure the agent didn't stop the job
    try (final DockerClient docker = getNewDockerClient()) {
        final List<Container> containers = docker.listContainers();
        final CustomTypeSafeMatcher<Container> containerIdMatcher = new CustomTypeSafeMatcher<Container>("Container with id " + containerId) {

            @Override
            protected boolean matchesSafely(Container container) {
                return container.id().equals(containerId);
            }
        };
        assertContainersMatch(containers, containerIdMatcher);
    }
}
Also used : Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher) Matchers.containsString(org.hamcrest.Matchers.containsString) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 19 with DockerClient

use of com.spotify.docker.client.DockerClient in project helios by spotify.

the class AgentService method createDockerClient.

private DockerClient createDockerClient(final AgentConfig config) {
    final DockerClient dockerClient;
    if (isNullOrEmpty(config.getDockerHost().dockerCertPath())) {
        dockerClient = new PollingDockerClient(config.getDockerHost().uri());
    } else {
        final Path dockerCertPath = java.nio.file.Paths.get(config.getDockerHost().dockerCertPath());
        final DockerCertificates dockerCertificates;
        try {
            dockerCertificates = new DockerCertificates(dockerCertPath);
        } catch (DockerCertificateException e) {
            throw Throwables.propagate(e);
        }
        dockerClient = new PollingDockerClient(config.getDockerHost().uri(), dockerCertificates);
    }
    return dockerClient;
}
Also used : Path(java.nio.file.Path) DockerClient(com.spotify.docker.client.DockerClient) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) DockerCertificates(com.spotify.docker.client.DockerCertificates)

Example 20 with DockerClient

use of com.spotify.docker.client.DockerClient in project helios by spotify.

the class SystemTestBase method assertDockerReachable.

private void assertDockerReachable(final int probePort) throws Exception {
    try (final DockerClient docker = getNewDockerClient()) {
        // Pull our base images
        try {
            docker.inspectImage(BUSYBOX);
        } catch (ImageNotFoundException e) {
            docker.pull(BUSYBOX);
        }
        try {
            docker.inspectImage(ALPINE);
        } catch (ImageNotFoundException e) {
            docker.pull(ALPINE);
        }
        // Start a container with an exposed port
        final HostConfig hostConfig = HostConfig.builder().portBindings(ImmutableMap.of("4711/tcp", singletonList(PortBinding.of("0.0.0.0", probePort)))).build();
        final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX).cmd("nc", "-p", "4711", "-lle", "cat").exposedPorts(ImmutableSet.of("4711/tcp")).hostConfig(hostConfig).build();
        final ContainerCreation creation = docker.createContainer(config, testTag + "-probe");
        final String containerId = creation.id();
        docker.startContainer(containerId);
        // Wait for container to come up
        Polling.await(5, SECONDS, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                final ContainerInfo info = docker.inspectContainer(containerId);
                return info.state().running() ? true : null;
            }
        });
        log.info("Verifying that docker containers are reachable");
        try {
            Polling.awaitUnchecked(5, SECONDS, new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    log.info("Probing: {}:{}", DOCKER_HOST.address(), probePort);
                    try (final Socket ignored = new Socket(DOCKER_HOST.address(), probePort)) {
                        return true;
                    } catch (IOException e) {
                        return false;
                    }
                }
            });
        } catch (TimeoutException e) {
            fail("Please ensure that DOCKER_HOST is set to an address that where containers can " + "be reached. If docker is running in a local VM, DOCKER_HOST must be set to the " + "address of that VM. If docker can only be reached on a limited port range, " + "set the environment variable DOCKER_PORT_RANGE=start:end");
        }
        docker.killContainer(containerId);
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Matchers.containsString(org.hamcrest.Matchers.containsString) Integer.toHexString(java.lang.Integer.toHexString) IOException(java.io.IOException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

DockerClient (com.spotify.docker.client.DockerClient)31 Test (org.junit.Test)21 JobId (com.spotify.helios.common.descriptors.JobId)19 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)18 LogStream (com.spotify.docker.client.LogStream)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 HeliosClient (com.spotify.helios.client.HeliosClient)10 Deployment (com.spotify.helios.common.descriptors.Deployment)8 DockerException (com.spotify.docker.client.exceptions.DockerException)7 Job (com.spotify.helios.common.descriptors.Job)7 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)6 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)6 Container (com.spotify.docker.client.messages.Container)4 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)4 HostConfig (com.spotify.docker.client.messages.HostConfig)4 HostStatus (com.spotify.helios.common.descriptors.HostStatus)4 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)3 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)3 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)3 AgentMain (com.spotify.helios.agent.AgentMain)3