diff options
Diffstat (limited to 'docker-api/src/test/java/com/yahoo/vespa/hosted/dockerapi/DockerTest.java')
-rw-r--r-- | docker-api/src/test/java/com/yahoo/vespa/hosted/dockerapi/DockerTest.java | 197 |
1 files changed, 0 insertions, 197 deletions
diff --git a/docker-api/src/test/java/com/yahoo/vespa/hosted/dockerapi/DockerTest.java b/docker-api/src/test/java/com/yahoo/vespa/hosted/dockerapi/DockerTest.java deleted file mode 100644 index 18f87e5ae17..00000000000 --- a/docker-api/src/test/java/com/yahoo/vespa/hosted/dockerapi/DockerTest.java +++ /dev/null @@ -1,197 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.hosted.dockerapi; - -import org.apache.commons.io.IOUtils; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.URL; -import java.util.Optional; -import java.util.concurrent.ExecutionException; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeTrue; - -/** - * Requires docker daemon, see {@link com.yahoo.vespa.hosted.dockerapi.DockerTestUtils} for more details. - * - * @author freva - * @author dybdahl - */ -public class DockerTest { - private DockerImpl docker; - private static final DockerImage dockerImage = new DockerImage("simple-ipv6-server:Dockerfile"); - private static final String MANAGER_NAME = "docker-test"; - - // Ignored because the test is very slow (several minutes) when swap is enabled, to disable: (Linux) - // $ sudo swapoff -a - @Ignore - @Test - public void testOutOfMemoryDoesNotAffectOtherContainers() throws InterruptedException, ExecutionException, IOException { - String hostName1 = "docker10.test.yahoo.com"; - String hostName2 = "docker11.test.yahoo.com"; - ContainerName containerName1 = new ContainerName("docker-test-1"); - ContainerName containerName2 = new ContainerName("docker-test-2"); - InetAddress inetAddress1 = InetAddress.getByName("172.18.10.10"); - InetAddress inetAddress2 = InetAddress.getByName("172.18.10.11"); - - docker.createContainerCommand(dockerImage, ContainerResources.from(0, 0.1), containerName1, hostName1) - .withManagedBy(MANAGER_NAME) - .withNetworkMode(DockerImpl.DOCKER_CUSTOM_MACVLAN_NETWORK_NAME) - .withIpAddress(inetAddress1) - .create(); - docker.startContainer(containerName1); - - docker.createContainerCommand(dockerImage, ContainerResources.from(0, 0.1), containerName2, hostName2) - .withManagedBy(MANAGER_NAME) - .withNetworkMode(DockerImpl.DOCKER_CUSTOM_MACVLAN_NETWORK_NAME) - .withIpAddress(inetAddress2) - .create(); - docker.startContainer(containerName2); - - // 137 = 128 + 9 = kill -9 (SIGKILL), doesn't need to be run as "root", but "yahoo" does not exist in this basic image - assertThat(docker.executeInContainerAsRoot(containerName2, "python", "/pysrc/fillmem.py", "90").getExitStatus(), is(137)); - - // Verify that both HTTP servers are still up - testReachabilityFromHost("http://" + inetAddress1.getHostAddress() + "/ping"); - testReachabilityFromHost("http://" + inetAddress2.getHostAddress() + "/ping"); - - docker.stopContainer(containerName1); - docker.deleteContainer(containerName1); - - docker.stopContainer(containerName2); - docker.deleteContainer(containerName2); - } - - @Test - public void testContainerCycle() throws IOException, InterruptedException, ExecutionException { - final ContainerName containerName = new ContainerName("docker-test-foo"); - final String containerHostname = "hostName1"; - - docker.createContainerCommand(dockerImage, ContainerResources.UNLIMITED, containerName, containerHostname) - .withManagedBy(MANAGER_NAME).create(); - Optional<Container> container = docker.getContainer(containerName); - assertTrue(container.isPresent()); - assertEquals(container.get().state, Container.State.CREATED); - - docker.startContainer(containerName); - container = docker.getContainer(containerName); - assertTrue(container.isPresent()); - assertEquals(container.get().state, Container.State.RUNNING); - - docker.dockerClient.pauseContainerCmd(containerName.asString()).exec(); - container = docker.getContainer(containerName); - assertTrue(container.isPresent()); - assertEquals(container.get().state, Container.State.PAUSED); - - docker.dockerClient.unpauseContainerCmd(containerName.asString()).exec(); - docker.stopContainer(containerName); - container = docker.getContainer(containerName); - assertTrue(container.isPresent()); - assertEquals(container.get().state, Container.State.EXITED); - - docker.deleteContainer(containerName); - assertThat(docker.listAllContainersManagedBy(MANAGER_NAME).isEmpty(), is(true)); - } - - /** - * Test the expected behavior for exec when it times out - it should throw an exception when it times out, - * and before the process completes. - * - * The test timeout value is set quite high to avoid noise if screwdriver is slow but lower than the process time. - */ - @Test(expected = DockerExecTimeoutException.class, timeout = 2000) - public void testContainerExecHounorsTimeout() throws IOException, InterruptedException, ExecutionException { - final ContainerName containerName = new ContainerName("docker-test-foo"); - final String containerHostname = "hostName1"; - - docker.createContainerCommand(dockerImage, ContainerResources.UNLIMITED, containerName, containerHostname) - .withManagedBy(MANAGER_NAME).create(); - docker.startContainer(containerName); - docker.executeInContainerAsRoot(containerName, 1L, "sh", "-c", "sleep 5"); - } - - /** - * Test the expected behavior for exec that completes before specified timeout - it should return when the process finishes and not - * wait for the timeout. Some previous tests indicated that this was not behaving correctly. - * - * No timeout implies infinite timeout. - * - * The test timeout value is set quite high to avoid noise if screwdriver is slow - */ - @Test(timeout = 4000) - public void testContainerExecDoesNotBlockUntilTimeoutWhenCommandFinishesBeforeTimeout() throws IOException, InterruptedException, ExecutionException { - final ContainerName containerName = new ContainerName("docker-test-foo"); - final String containerHostname = "hostName1"; - - docker.createContainerCommand(dockerImage, ContainerResources.UNLIMITED, containerName, containerHostname) - .withManagedBy(MANAGER_NAME).create(); - docker.startContainer(containerName); - docker.executeInContainerAsRoot(containerName, 2L, "sh", "-c", "echo hei"); - - // Also test that this is the behavoir when not specifying timeout - docker.executeInContainerAsRoot(containerName,"sh", "-c", "echo hei"); - } - - @Test - public void testDockerNetworking() throws InterruptedException, ExecutionException, IOException { - String hostName1 = "docker10.test.yahoo.com"; - String hostName2 = "docker11.test.yahoo.com"; - ContainerName containerName1 = new ContainerName("docker-test-1"); - ContainerName containerName2 = new ContainerName("docker-test-2"); - InetAddress inetAddress1 = InetAddress.getByName("172.18.10.10"); - InetAddress inetAddress2 = InetAddress.getByName("172.18.10.11"); - - docker.createContainerCommand(dockerImage, ContainerResources.UNLIMITED, containerName1, hostName1) - .withManagedBy(MANAGER_NAME) - .withNetworkMode(DockerImpl.DOCKER_CUSTOM_MACVLAN_NETWORK_NAME).withIpAddress(inetAddress1).create(); - docker.startContainer(containerName1); - - docker.createContainerCommand(dockerImage, ContainerResources.UNLIMITED, containerName2, hostName2) - .withManagedBy(MANAGER_NAME) - .withNetworkMode(DockerImpl.DOCKER_CUSTOM_MACVLAN_NETWORK_NAME).withIpAddress(inetAddress2).create(); - docker.startContainer(containerName2); - - testReachabilityFromHost("http://" + inetAddress1.getHostAddress() + "/ping"); - testReachabilityFromHost("http://" + inetAddress2.getHostAddress() + "/ping"); - - String[] curlFromNodeToNode = new String[]{"curl", "-g", "http://" + inetAddress2.getHostAddress() + "/ping"}; - ProcessResult result = docker.executeInContainerAsRoot(containerName1, curlFromNodeToNode); - assertThat("Could not reach " + containerName2.asString() + " from " + containerName1.asString(), - result.getOutput(), is("pong\n")); - - docker.stopContainer(containerName1); - docker.deleteContainer(containerName1); - - docker.stopContainer(containerName2); - docker.deleteContainer(containerName2); - } - - @Before - public void setup() throws InterruptedException, ExecutionException, IOException { - if (docker == null) { - assumeTrue(DockerTestUtils.dockerDaemonIsPresent()); - - docker = DockerTestUtils.getDocker(); - DockerTestUtils.buildSimpleHttpServerDockerImage(docker, dockerImage); - } - - // Clean up any non deleted containers from previous tests - docker.getAllContainersManagedBy(MANAGER_NAME).forEach(container -> { - if (container.state.isRunning()) docker.stopContainer(container.name); - docker.deleteContainer(container.name); - }); - } - - private void testReachabilityFromHost(String target) throws IOException, InterruptedException { - URL url = new URL(target); - String containerServer = IOUtils.toString(url.openStream()); - assertThat(containerServer, is("pong\n")); - } -} |