@Before public void setup() throws Exception { // LXC has a bug where the TERM signal isn't sent to containers, so we can only run this test // if docker runs with the native driver. // See: https://github.com/docker/docker/issues/2436 final DockerClient dockerClient = getNewDockerClient(); assumeThat(dockerClient.info().executionDriver(), startsWith("native")); }
envVars.put("PICK_ALL_RANDOM_PORTS", "true"); try { String hostName = dockerClient.info().name(); extraHosts.add(String.format("%s:%s", hostName, "127.0.1.0")); } catch (DockerException | InterruptedException e) {
@Test public void test() throws Exception { startDefaultMaster(); final String id = "test-" + toHexString(new SecureRandom().nextInt()); final String namespace = "helios-" + id; final String intruder1 = intruder(namespace); final String intruder2 = intruder(namespace); // Start a container in the agent namespace startContainer(intruder1); // Start agent final HeliosClient client = defaultClient(); startDefaultAgent(testHost(), "--id=" + id); awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS); awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS); // With LXC, killing a container results in exit code 0. // In docker 1.5 killing a container results in exit code 137, in previous versions it's -1. final String executionDriver = docker.info().executionDriver(); final List<Long> expectedExitCodes = (executionDriver != null && executionDriver.startsWith("lxc-")) ? Collections.singletonList(0L) : asList(-1L, 137L); // Wait for the agent to kill the container final ContainerExit exit1 = docker.waitContainer(intruder1); assertThat(exit1.statusCode(), isIn(expectedExitCodes)); // Start another container in the agent namespace startContainer(intruder2); // Wait for the agent to kill the second container as well final ContainerExit exit2 = docker.waitContainer(intruder2); assertThat(exit2.statusCode(), isIn(expectedExitCodes)); }
public void assertVolumes(final JobId jobId) throws Exception { // Wait for agent to come up awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS); awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS); // Deploy the job on the agent final Deployment deployment = Deployment.of(jobId, START); final JobDeployResponse deployed = client.deploy(deployment, testHost()).get(); assertEquals(JobDeployResponse.Status.OK, deployed.getStatus()); // Wait for the job to run final TaskStatus taskStatus = awaitJobState( client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS); assertJobEquals(job, taskStatus.getJob()); final Integer randomVolPort = taskStatus.getPorts().get("random").getExternalPort(); final Integer namedVolPort = taskStatus.getPorts().get("named").getExternalPort(); final Integer hostnamePort = taskStatus.getPorts().get("hostname").getExternalPort(); assert randomVolPort != null; assert namedVolPort != null; assert hostnamePort != null; // Read "foo" from /volume/bar and /mnt/my-volume/bar assertEquals("foo", recvUtf8(randomVolPort, 3)); assertEquals("foo", recvUtf8(namedVolPort, 3)); // Read hostname from /hostname final String hostname = getNewDockerClient().info().name(); final String mountedHostname = recvUtf8(hostnamePort, hostname.length()); assertEquals(hostname, mountedHostname); }
assertEquals(CPUSET_CPUS, hostConfig.cpusetCpus()); final Info info = docker.info(); final Iterable<String> split = Splitter.on(".").split(docker.version().apiVersion());
@Test public void test() throws Exception { try (final DockerClient docker = getNewDockerClient()) { // Start Helios agent, configured to bind host /etc/hostname into container /mnt/hostname startDefaultMaster(); startDefaultAgent(testHost(), "--bind", "/etc/hostname:/mnt/hostname:ro"); awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS); // Figure out the host kernel version final String hostname = docker.info().name(); // Run a job that cat's /mnt/hostname, which should be the host's name final List<String> command = ImmutableList.of("cat", "/mnt/hostname"); final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, command); deployJob(jobId, testHost()); final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED); final String log; try (LogStream logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr())) { log = logs.readFully(); } // the kernel version from the host should be in the log assertThat(log, containsString(hostname)); } }
private static boolean compatibleDockerVersion(final DockerClient docker) { final String executionDriver; final String apiVersion; try { executionDriver = docker.info().executionDriver(); apiVersion = docker.version().apiVersion(); } catch (DockerException e) { return false; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } if (Strings.isNullOrEmpty(executionDriver) || !executionDriver.startsWith("native")) { return false; } if (Strings.isNullOrEmpty(apiVersion)) { return false; } final Iterable<String> split = Splitter.on(".").split(apiVersion); final int major = Integer.parseInt(Iterables.get(split, 0, "0")); final int minor = Integer.parseInt(Iterables.get(split, 1, "0")); return major == 1 && minor >= 18; } }
DockerRule(DockerRuleBuilder builder) { this.builder = builder; this.imageNameWithTag = imageNameWithTag(builder.imageName()); try { dockerClient = DefaultDockerClient.fromEnv().build(); log.debug("server.info: {}", dockerClient.info()); log.debug("server.version: {}", dockerClient.version()); if (builder.imageAlwaysPull() || ! imageAvaliable(dockerClient, imageNameWithTag)) { dockerClient.pull(imageNameWithTag); } } catch (ImageNotFoundException e) { throw new ImagePullException(String.format("Image '%s' not found", imageNameWithTag), e); } catch (DockerCertificateException | DockerException | InterruptedException e) { throw new IllegalStateException(e); } }