aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/configserver/noderepository/RealNodeRepositoryTest.java
blob: 1ad8a4326e755ec7680435a47c8db66d95481552 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.configserver.noderepository;

import com.yahoo.application.Networking;
import com.yahoo.application.container.JDisc;
import com.yahoo.config.provision.DockerImage;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.host.FlavorOverrides;
import com.yahoo.vespa.hosted.node.admin.configserver.ConfigServerApi;
import com.yahoo.vespa.hosted.node.admin.configserver.ConfigServerApiImpl;
import com.yahoo.vespa.hosted.provision.restapi.NodesV2ApiHandler;
import com.yahoo.vespa.hosted.provision.testutils.ContainerConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.URI;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Tests the NodeRepository class used for talking to the node repository. It uses a mock from the node repository
 * which already contains some data.
 *
 * @author dybdahl
 */
public class RealNodeRepositoryTest {

    private static final double delta = 0.00000001;
    private JDisc container;
    private NodeRepository nodeRepositoryApi;

    private int findRandomOpenPort() throws IOException {
        try (ServerSocket socket = new ServerSocket(0)) {
            socket.setReuseAddress(true);
            return socket.getLocalPort();
        }
    }

    /**
     * Starts NodeRepository with
     *   {@link com.yahoo.vespa.hosted.provision.testutils.MockNodeFlavors}
     *   {@link com.yahoo.vespa.hosted.provision.testutils.MockNodeRepository}
     *   {@link NodesV2ApiHandler}
     * These classes define some test data that is used in these tests.
     */
    @BeforeEach
    public void startContainer() throws Exception {
        Exception lastException = null;

        // This tries to bind a random open port for the node-repo mock, which is a race condition, so try
        // a few times before giving up
        for (int i = 0; i < 3; i++) {
            try {
                int port = findRandomOpenPort();
                container = JDisc.fromServicesXml(ContainerConfig.servicesXmlV2(port), Networking.enable);
                ConfigServerApi configServerApi = ConfigServerApiImpl.createForTesting(
                        List.of(URI.create("http://127.0.0.1:" + port)));
                waitForJdiscContainerToServe(configServerApi);
                return;
            } catch (RuntimeException e) {
                lastException = e;
            }
        }
        throw new RuntimeException("Failed to bind a port in three attempts, giving up", lastException);
    }

    private void waitForJdiscContainerToServe(ConfigServerApi configServerApi) throws InterruptedException {
        Instant start = Instant.now();
        nodeRepositoryApi = new RealNodeRepository(configServerApi);
        while (Instant.now().minusSeconds(120).isBefore(start)) {
            try {
                nodeRepositoryApi.getNodes("foobar");
                return;
            } catch (Exception e) {
                Thread.sleep(100);
            }
        }
        throw new RuntimeException("Could not get answer from container.");
    }

    @AfterEach
    public void stopContainer() {
        if (container != null) {
            container.close();
        }
    }

    @Test
    void testGetContainersToRunApi() {
        String dockerHostHostname = "dockerhost1.yahoo.com";

        List<NodeSpec> containersToRun = nodeRepositoryApi.getNodes(dockerHostHostname);
        assertEquals(1, containersToRun.size());
        NodeSpec node = containersToRun.get(0);
        assertEquals("host4.yahoo.com", node.hostname());
        assertEquals(DockerImage.fromString("docker-registry.domain.tld:8080/dist/vespa:6.42.0"), node.wantedDockerImage().get());
        assertEquals(NodeState.active, node.state());
        assertEquals(Long.valueOf(0), node.wantedRestartGeneration().get());
        assertEquals(Long.valueOf(0), node.currentRestartGeneration().get());
        assertEquals(1, node.vcpu(), delta);
        assertEquals(4, node.memoryGb(), delta);
        assertEquals(100, node.diskGb(), delta);
    }

    @Test
    void testGetContainer() {
        String hostname = "host4.yahoo.com";
        Optional<NodeSpec> node = nodeRepositoryApi.getOptionalNode(hostname);
        assertTrue(node.isPresent());
        assertEquals(hostname, node.get().hostname());
    }

    @Test
    void testGetContainerForNonExistingNode() {
        String hostname = "host-that-does-not-exist";
        Optional<NodeSpec> node = nodeRepositoryApi.getOptionalNode(hostname);
        assertFalse(node.isPresent());
    }

    @Test
    void testUpdateNodeAttributes() {
        String hostname = "host4.yahoo.com";
        nodeRepositoryApi.updateNodeAttributes(
                hostname,
                new NodeAttributes()
                        .withRestartGeneration(1)
                        .withDockerImage(DockerImage.fromString("registry.example.com/repo/image-1:6.2.3")));
    }

    @Test
    void testMarkAsReady() {
        nodeRepositoryApi.setNodeState("host5.yahoo.com", NodeState.dirty);
        nodeRepositoryApi.setNodeState("host5.yahoo.com", NodeState.ready);

        try {
            nodeRepositoryApi.setNodeState("host4.yahoo.com", NodeState.ready);
            fail("Should not be allowed to be marked ready as it is not registered as provisioned, dirty, failed or parked");
        } catch (RuntimeException ignored) {
            // expected
        }

        try {
            nodeRepositoryApi.setNodeState("host101.yahoo.com", NodeState.ready);
            fail("Expected failure because host101 does not exist");
        } catch (RuntimeException ignored) {
            // expected
        }
    }

    @Test
    void testAddNodes() {
        AddNode host = AddNode.forHost("host123.domain.tld",
                "id1",
                "default",
                Optional.of(FlavorOverrides.ofDisk(123)),
                NodeType.confighost,
                Set.of("::1"), Set.of("::2", "::3"));

        NodeResources nodeResources = new NodeResources(1, 2, 3, 4, NodeResources.DiskSpeed.slow, NodeResources.StorageType.local);
        AddNode node = AddNode.forNode("host123-1.domain.tld", "id1", "host123.domain.tld", nodeResources, NodeType.config, Set.of("::2", "::3"));

        assertFalse(nodeRepositoryApi.getOptionalNode("host123.domain.tld").isPresent());
        nodeRepositoryApi.addNodes(List.of(host, node));

        NodeSpec hostSpec = nodeRepositoryApi.getOptionalNode("host123.domain.tld").orElseThrow();
        assertEquals("id1", hostSpec.id());
        assertEquals("default", hostSpec.flavor());
        assertEquals(123, hostSpec.diskGb(), 0);
        assertEquals(NodeType.confighost, hostSpec.type());
        assertEquals(NodeResources.Architecture.x86_64, hostSpec.resources().architecture());

        NodeSpec nodeSpec = nodeRepositoryApi.getOptionalNode("host123-1.domain.tld").orElseThrow();
        assertEquals(nodeResources, nodeSpec.resources());
        assertEquals(NodeType.config, nodeSpec.type());
    }

}