summaryrefslogtreecommitdiffstats
path: root/docker-api/src/test/java/com/yahoo/vespa/hosted/dockerapi/CreateContainerCommandImplTest.java
blob: 3b8b0a84e64b46147ee210f706ab2f63b8582b87 (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
// 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.yahoo.config.provision.DockerImage;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.stream.Stream;

import static org.junit.Assert.assertEquals;

public class CreateContainerCommandImplTest {

    @Test
    public void testToString() throws UnknownHostException {
        DockerImage dockerImage = DockerImage.fromString("docker.registry.domain.tld/my/image:1.2.3");
        ContainerResources containerResources = new ContainerResources(2.5, 100, 1024);
        String hostname = "docker-1.region.domain.tld";
        ContainerName containerName = ContainerName.fromHostname(hostname);

        Docker.CreateContainerCommand createContainerCommand = new CreateContainerCommandImpl(
                null, dockerImage, containerName)
                .withHostName(hostname)
                .withResources(containerResources)
                .withLabel("my-label", "test-label")
                .withUlimit("nofile", 1, 2)
                .withUlimit("nproc", 10, 20)
                .withEnvironment("env1", "val1")
                .withEnvironment("env2", "val2")
                .withVolume(Paths.get("vol1"), Paths.get("/host/vol1"))
                .withAddCapability("SYS_PTRACE")
                .withAddCapability("SYS_ADMIN")
                .withDropCapability("NET_ADMIN")
                .withNetworkMode("bridge")
                .withIpAddress(InetAddress.getByName("10.0.0.1"))
                .withIpAddress(InetAddress.getByName("::1"))
                .withEntrypoint("/path/to/program", "arg1", "arg2")
                .withPrivileged(true);

        assertEquals("--name docker-1 " +
                "--hostname docker-1.region.domain.tld " +
                "--cpu-shares 100 " +
                "--cpus 2.5 " +
                "--memory 1024 " +
                "--label my-label=test-label " +
                "--ulimit nofile=1:2 " +
                "--ulimit nproc=10:20 " +
                "--pids-limit -1 " +
                "--env env1=val1 " +
                "--env env2=val2 " +
                "--volume vol1:/host/vol1:Z " +
                "--cap-add SYS_ADMIN " +
                "--cap-add SYS_PTRACE " +
                "--cap-drop NET_ADMIN " +
                "--net bridge " +
                "--ip 10.0.0.1 " +
                "--ip6 0:0:0:0:0:0:0:1 " +
                "--entrypoint /path/to/program " +
                "--privileged docker.registry.domain.tld/my/image:1.2.3 " +
                "arg1 " +
                "arg2", createContainerCommand.toString());
    }

    @Test
    public void generateMacAddressTest() {
        String[][] addresses = {
                {"test123.host.yahoo.com",  null,       "abcd:1234::1", "ee:ae:a9:de:ad:c2"},
                {"test123.host.yahoo.com",  null,       "abcd:1234::2", "fa:81:11:1b:ff:fb"},
                {"unique.host.yahoo.com",   null,       "abcd:1234::1", "96:a4:00:77:90:3b"},
                {"test123.host.yahoo.com",  "10.0.0.1", null,           "7e:de:b3:7c:9e:96"},
                {"test123.host.yahoo.com",  "10.0.0.1", "abcd:1234::1", "6a:06:af:16:25:95"}};

        Stream.of(addresses).forEach(address -> {
            String generatedMac = CreateContainerCommandImpl.generateMACAddress(
                    Optional.of(address[0]), Optional.ofNullable(address[1]), Optional.ofNullable(address[2]));
            String expectedMac = address[3];
            assertEquals(expectedMac, generatedMac);
        });
    }
}