summaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/integrationTests/MultiDockerTest.java
blob: 5619fd1bab327a17ffd4f176a74f42c7ebadfb4e (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
93
94
// 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.node.admin.integrationTests;

import com.yahoo.config.provision.NodeType;
import com.yahoo.vespa.hosted.dockerapi.ContainerName;
import com.yahoo.vespa.hosted.dockerapi.DockerImage;
import com.yahoo.vespa.hosted.node.admin.configserver.noderepository.NodeSpec;
import com.yahoo.vespa.hosted.provision.Node;
import org.junit.Test;

/**
 * @author freva
 */
public class MultiDockerTest {

    @Test
    public void test() throws InterruptedException {
        try (DockerTester dockerTester = new DockerTester()) {
            addAndWaitForNode(dockerTester, "host1.test.yahoo.com", new DockerImage("image1"));
            NodeSpec nodeSpec2 = addAndWaitForNode(
                    dockerTester, "host2.test.yahoo.com", new DockerImage("image2"));

            dockerTester.addNodeRepositoryNode(
                    new NodeSpec.Builder(nodeSpec2)
                            .state(Node.State.dirty)
                            .minCpuCores(1)
                            .minMainMemoryAvailableGb(1)
                            .minDiskAvailableGb(1)
                            .build());

            // Wait until it is marked ready
            while (dockerTester.nodeRepositoryMock.getNode(nodeSpec2.getHostname())
                    .filter(node -> node.getState() != Node.State.ready).isPresent()) {
                Thread.sleep(10);
            }

            addAndWaitForNode(dockerTester, "host3.test.yahoo.com", new DockerImage("image1"));

            dockerTester.callOrderVerifier.assertInOrder(
                    "createContainerCommand with DockerImage { imageId=image1 }, HostName: host1.test.yahoo.com, ContainerName { name=host1 }",
                    "executeInContainerAsRoot with ContainerName { name=host1 }, args: [" + DockerTester.NODE_PROGRAM + ", resume]",

                    "createContainerCommand with DockerImage { imageId=image2 }, HostName: host2.test.yahoo.com, ContainerName { name=host2 }",
                    "executeInContainerAsRoot with ContainerName { name=host2 }, args: [" + DockerTester.NODE_PROGRAM + ", resume]",

                    "stopContainer with ContainerName { name=host2 }",
                    "deleteContainer with ContainerName { name=host2 }",

                    "createContainerCommand with DockerImage { imageId=image1 }, HostName: host3.test.yahoo.com, ContainerName { name=host3 }",
                    "executeInContainerAsRoot with ContainerName { name=host3 }, args: [" + DockerTester.NODE_PROGRAM + ", resume]");

            dockerTester.callOrderVerifier.assertInOrderWithAssertMessage(
                    "Maintainer did not receive call to delete application storage",
                    "deleteContainer with ContainerName { name=host2 }",
                    "DeleteContainerStorage with ContainerName { name=host2 }");

            dockerTester.callOrderVerifier.assertInOrder(
                    "updateNodeAttributes with HostName: host1.test.yahoo.com, NodeAttributes{restartGeneration=1, rebootGeneration=0, dockerImage=image1, hardwareDivergence='null'}",
                    "updateNodeAttributes with HostName: host2.test.yahoo.com, NodeAttributes{restartGeneration=1, rebootGeneration=0, dockerImage=image2, hardwareDivergence='null'}",
                    "setNodeState host2.test.yahoo.com to ready",
                    "updateNodeAttributes with HostName: host3.test.yahoo.com, NodeAttributes{restartGeneration=1, rebootGeneration=0, dockerImage=image1, hardwareDivergence='null'}");
        }
    }

    private NodeSpec addAndWaitForNode(DockerTester tester, String hostName, DockerImage dockerImage) throws InterruptedException {
        NodeSpec nodeSpec = new NodeSpec.Builder()
                .hostname(hostName)
                .wantedDockerImage(dockerImage)
                .wantedVespaVersion("1.2.3")
                .state(Node.State.active)
                .nodeType(NodeType.tenant)
                .flavor("docker")
                .wantedRestartGeneration(1L)
                .currentRestartGeneration(1L)
                .minCpuCores(1)
                .minMainMemoryAvailableGb(1)
                .minDiskAvailableGb(1)
                .build();

        tester.addNodeRepositoryNode(nodeSpec);

        // Wait for node admin to be notified with node repo state and the docker container has been started
        while (tester.nodeAdmin.getListOfHosts().size() != tester.nodeRepositoryMock.getNumberOfContainerSpecs()) {
            Thread.sleep(10);
        }

        ContainerName containerName = ContainerName.fromHostname(hostName);
        tester.callOrderVerifier.assertInOrder(
                "createContainerCommand with " + dockerImage + ", HostName: " + hostName + ", " + containerName,
                "executeInContainerAsRoot with " + containerName + ", args: [" + DockerTester.NODE_PROGRAM + ", resume]");

        return nodeSpec;
    }
}