summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/ClusterStateViewTest.java
blob: 1125c431caefdd15d07b802bd6848ceaa6c93584 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import com.yahoo.vdslib.state.*;
import com.yahoo.vespa.clustercontroller.core.hostinfo.HostInfo;
import com.yahoo.vespa.clustercontroller.core.hostinfo.StorageNodeStatsBridge;
import org.junit.Test;

import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

/**
 * @author hakonhall
 */
public class ClusterStateViewTest {
    private final NodeInfo nodeInfo = mock(NodeInfo.class);
    private final ClusterStatsAggregator statsAggregator = mock(ClusterStatsAggregator.class);
    private final ClusterState clusterState = mock(ClusterState.class);
    private final ClusterStateView clusterStateView = new ClusterStateView(clusterState, statsAggregator);

    private HostInfo createHostInfo(String version) {
        return HostInfo.createHostInfo("{ \"cluster-state-version\": " + version + " }");
    }

    @Test
    public void testWrongNodeType() {
        when(nodeInfo.isDistributor()).thenReturn(false);

        clusterStateView.handleUpdatedHostInfo(nodeInfo, createHostInfo("101"));

        verify(statsAggregator, never()).updateForDistributor(anyInt(), any());
    }

    @Test
    public void testStateVersionMismatch() {
        when(nodeInfo.isDistributor()).thenReturn(true);
        when(clusterState.getVersion()).thenReturn(101);

        clusterStateView.handleUpdatedHostInfo(nodeInfo, createHostInfo("22"));

        verify(statsAggregator, never()).updateForDistributor(anyInt(), any());
    }

    @Test
    public void testFailToGetStats() {
        when(nodeInfo.isDistributor()).thenReturn(true);
        when(clusterState.getVersion()).thenReturn(101);

        clusterStateView.handleUpdatedHostInfo(nodeInfo, createHostInfo("22"));

        verify(statsAggregator, never()).updateForDistributor(anyInt(), any());
    }

    @Test
    public void testSuccessCase() {
        when(nodeInfo.isDistributor()).thenReturn(true);
        HostInfo hostInfo = HostInfo.createHostInfo(
                "{" +
                " \"cluster-state-version\": 101," +
                " \"distributor\": {\n" +
                "        \"storage-nodes\": [\n" +
                "            {\n" +
                "                \"node-index\": 3\n" +
                "            }\n" +
                "           ]}}");


        when(nodeInfo.getNodeIndex()).thenReturn(3);
        when(clusterState.getVersion()).thenReturn(101);

        clusterStateView.handleUpdatedHostInfo(nodeInfo, hostInfo);

        verify(statsAggregator).updateForDistributor(3, StorageNodeStatsBridge.generate(hostInfo.getDistributor()));
    }

    @Test
    public void testIndicesOfUpNodes() {
        when(clusterState.getNodeCount(NodeType.DISTRIBUTOR)).thenReturn(7);

        NodeState nodeState = mock(NodeState.class);
        when(nodeState.getState()).
                thenReturn(State.MAINTENANCE).  // 0
                thenReturn(State.RETIRED).  // 1
                thenReturn(State.INITIALIZING).  // 2
                thenReturn(State.DOWN).
                thenReturn(State.STOPPING).
                thenReturn(State.UNKNOWN).
                thenReturn(State.UP);  // 6

        when(clusterState.getNodeState(any())).thenReturn(nodeState);

        Set<Integer> indices = ClusterStateView.getIndicesOfUpNodes(clusterState, NodeType.DISTRIBUTOR);
        assertEquals(4, indices.size());
        assert(indices.contains(0));
        assert(indices.contains(1));
        assert(indices.contains(2));
        assert(indices.contains(6));
    }

}