blob: 079d68760438deea9c6d1667d28e5d24a15899e9 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
// 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 com.github.dockerjava.api.model.Network;
import com.yahoo.metrics.simple.MetricReceiver;
import com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper;
import java.io.File;
/**
* Helper class for testing full integration with docker daemon, requires running daemon. To run these tests:
*
* MAC:
* 1. Install Docker Toolbox, and start it (Docker Quickstart Terminal) (you can close terminal window afterwards)
* 2. For network test, we need to make docker containers visible for Mac: sudo route add 172.18.0.0/16 192.168.99.100
*
* @author freva
*/
public class DockerTestUtils {
private static final OS operatingSystem = getSystemOS();
private static final String prefix = "/Users/" + System.getProperty("user.name") + "/.docker/machine/machines/default/";
private static final DockerConfig dockerConfig = new DockerConfig(new DockerConfig.Builder()
.caCertPath( operatingSystem == OS.Mac_OS_X ? prefix + "ca.pem" : "")
.clientCertPath(operatingSystem == OS.Mac_OS_X ? prefix + "cert.pem" : "")
.clientKeyPath( operatingSystem == OS.Mac_OS_X ? prefix + "key.pem" : "")
.uri( operatingSystem == OS.Mac_OS_X ? "tcp://192.168.99.100:2376" : "tcp://localhost:2376")
.secondsToWaitBeforeKillingContainer(0));
private static DockerImpl docker;
public static boolean dockerDaemonIsPresent() {
if (docker != null) return true;
if (operatingSystem == OS.Unsupported) {
System.err.println("This test does not support " + System.getProperty("os.name") + " yet, ignoring test.");
return false;
}
try {
getDocker(); // Will throw an exception if docker is not installed/incorrectly configured
return true;
} catch (Exception e) {
System.err.println("Please install Docker Toolbox and start Docker Quick Start Terminal once, ignoring test.");
System.err.println(e.getMessage());
return false;
}
}
public static DockerImpl getDocker() {
if (docker == null) {
docker = new DockerImpl(
dockerConfig,
false, /* fallback to 1.23 on errors */
new MetricReceiverWrapper(MetricReceiver.nullImplementation));
createDockerTestNetworkIfNeeded(docker);
}
return docker;
}
public static void createDockerTestNetworkIfNeeded(DockerImpl docker) {
if (! docker.dockerClient.listNetworksCmd().withNameFilter(DockerImpl.DOCKER_CUSTOM_MACVLAN_NETWORK_NAME).exec().isEmpty()) return;
Network.Ipam ipam = new Network.Ipam().withConfig(new Network.Ipam.Config()
.withSubnet("172.18.0.0/16")
.withGateway("172.18.0.1"));
docker.dockerClient.createNetworkCmd()
.withName(DockerImpl.DOCKER_CUSTOM_MACVLAN_NETWORK_NAME).withDriver("bridge").withIpam(ipam).exec();
}
public static void buildSimpleHttpServerDockerImage(DockerImpl docker, DockerImage dockerImage) {
try {
docker.deleteImage(dockerImage);
} catch (Exception e) {
if (! e.getMessage().equals("Failed to delete docker image " + dockerImage.asString())) {
throw e;
}
}
// Build the image locally
File dockerFileStream = new File("src/test/resources/simple-ipv6-server");
docker.buildImage(dockerFileStream, dockerImage);
}
public enum OS { Linux, Mac_OS_X, Unsupported }
public static OS getSystemOS() {
switch (System.getProperty("os.name").toLowerCase()) {
case "linux": return OS.Linux;
case "mac os x": return OS.Mac_OS_X;
default: return OS.Unsupported;
}
}
}
|