aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/HostInfoUpdaterTest.java
blob: 583800caefad7b83eed25877146a4e3b0da40946 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.maintenance;

import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.ControllerTester;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.Node;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.NodeFilter;
import com.yahoo.vespa.hosted.controller.api.integration.entity.NodeEntity;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author mpolden
 * @author bjormel
 */
public class HostInfoUpdaterTest {

    @Test
    void maintain() {
        ControllerTester tester = new ControllerTester();
        tester.serviceRegistry().configServer().nodeRepository().allowPatching(true);
        addNodeEntities(tester);

        // First iteration patches all hosts
        HostInfoUpdater maintainer = new HostInfoUpdater(tester.controller(), Duration.ofDays(1));
        maintainer.maintain();
        List<Node> nodes = allNodes(tester);
        assertFalse(nodes.isEmpty());
        for (var node : nodes) {
            assertEquals(node.type().isHost(), node.switchHostname().isPresent(), "Node " + node.hostname().value() + (node.type().isHost() ? " has" : " does not have")
                    + " switch hostname");
            if (node.type().isHost()) {
                assertEquals("tor-" + node.hostname().value(), node.switchHostname().get());
            }
        }

        // Second iteration does not patch anything as all switch information is current
        tester.serviceRegistry().configServer().nodeRepository().allowPatching(false);
        maintainer.maintain();

        // One host is moved to a different switch
        Node host = allNodes(tester).stream().filter(node -> node.type().isHost()).findFirst().get();
        String newSwitch = "tor2-" + host.hostname().value();
        NodeEntity nodeEntity = new NodeEntity(host.hostname().value(), "RD350G", "Lenovo", newSwitch);
        tester.serviceRegistry().entityService().addNodeEntity(nodeEntity);

        // Host is updated
        tester.serviceRegistry().configServer().nodeRepository().allowPatching(true);
        maintainer.maintain();
        assertEquals(newSwitch, getNode(host.hostname(), tester).switchHostname().get());

        // Host has updated model
        String newModel = "Quanta q801";
        String manufacturer = "quanta computer";
        nodeEntity = new NodeEntity(host.hostname().value(), newModel, manufacturer, newSwitch);
        tester.serviceRegistry().entityService().addNodeEntity(nodeEntity);

        // Host is updated
        tester.serviceRegistry().configServer().nodeRepository().allowPatching(true);
        maintainer.maintain();
        assertEquals(manufacturer + " " + newModel, getNode(host.hostname(), tester).modelName().get());

        // Host keeps old switch hostname if removed from the node entity
        nodeEntity = new NodeEntity(host.hostname().value(), newModel, manufacturer, "");
        tester.serviceRegistry().entityService().addNodeEntity(nodeEntity);
        maintainer.maintain();
        assertEquals(newSwitch, getNode(host.hostname(), tester).switchHostname().get());

        // Host keeps old model name if removed from the node entity
        nodeEntity = new NodeEntity(host.hostname().value(), "", "", newSwitch);
        tester.serviceRegistry().entityService().addNodeEntity(nodeEntity);
        maintainer.maintain();
        assertEquals(manufacturer + " " + newModel, getNode(host.hostname(), tester).modelName().get());

        // Updates node registered under a different hostname
        ZoneId zone = tester.zoneRegistry().zones().controllerUpgraded().all().ids().get(0);
        String hostnameSuffix = ".prod." + zone.value();
        Node configNode = Node.builder().hostname(HostName.of("cfg3" + hostnameSuffix))
                .type(NodeType.config)
                .build();
        Node configHost = Node.builder().hostname(HostName.of("cfghost3" + hostnameSuffix))
                .type(NodeType.confighost)
                .build();
        tester.serviceRegistry().configServer().nodeRepository().putNodes(zone, List.of(configNode, configHost));
        String switchHostname = switchHostname(configHost);
        NodeEntity configNodeEntity = new NodeEntity("cfg3"  + hostnameSuffix, "RD350G", "Lenovo", switchHostname);
        tester.serviceRegistry().entityService().addNodeEntity(configNodeEntity);
        maintainer.maintain();
        assertEquals(switchHostname, getNode(configHost.hostname(), tester).switchHostname().get());
        assertTrue(getNode(configNode.hostname(), tester).switchHostname().isEmpty(), "Switch hostname is not set for non-host");
    }

    private static Node getNode(HostName hostname, ControllerTester tester) {
        return allNodes(tester).stream()
                               .filter(node -> node.hostname().equals(hostname))
                               .findFirst()
                               .orElseThrow(() -> new IllegalArgumentException("No such node: " + hostname));
    }

    private static List<Node> allNodes(ControllerTester tester) {
        List<Node> nodes = new ArrayList<>();
        for (var zone : tester.zoneRegistry().zones().controllerUpgraded().all().ids()) {
            nodes.addAll(tester.serviceRegistry().configServer().nodeRepository().list(zone, NodeFilter.all()));
        }
        return nodes;
    }

    private static String switchHostname(Node node) {
        return "tor-" + node.hostname().value();
    }

    private static void addNodeEntities(ControllerTester tester) {
        for (var node : allNodes(tester)) {
            if (!node.type().isHost()) continue;
            NodeEntity nodeEntity = new NodeEntity(node.hostname().value(), "RD350G", "Lenovo", switchHostname(node));
            tester.serviceRegistry().entityService().addNodeEntity(nodeEntity);
        }
    }

}