aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/restapiv2/requests/SetNodeStateRequestTest.java
blob: 1bd17b1175537472942f887f8bea489399153a4a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.clustercontroller.core.restapiv2.requests;

import com.yahoo.vdslib.state.ClusterState;
import com.yahoo.vdslib.state.Node;
import com.yahoo.vdslib.state.NodeState;
import com.yahoo.vdslib.state.NodeType;
import com.yahoo.vdslib.state.State;
import com.yahoo.vespa.clustercontroller.core.ContentCluster;
import com.yahoo.vespa.clustercontroller.core.NodeInfo;
import com.yahoo.vespa.clustercontroller.core.NodeStateChangeChecker;
import com.yahoo.vespa.clustercontroller.core.listeners.NodeListener;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.StateRestApiException;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.SetUnitStateRequest;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.SetResponse;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class SetNodeStateRequestTest {
    private static final String REASON = "operator";
    private final ContentCluster cluster = mock(ContentCluster.class);
    private final SetUnitStateRequest.Condition condition = SetUnitStateRequest.Condition.SAFE;
    private final Map<String, UnitState> newStates = new HashMap<>();
    private final UnitState unitState = mock(UnitState.class);
    private final int NODE_INDEX = 2;
    private final Node storageNode = new Node(NodeType.STORAGE, NODE_INDEX);
    private final NodeListener stateListener = mock(NodeListener.class);
    private final ClusterState currentClusterState = mock(ClusterState.class);
    private boolean inMasterMoratorium = false;
    private boolean probe = false;

    @BeforeEach
    public void setUp() {
        newStates.put("user", unitState);
    }

    @Test
    void testUpToMaintenance() throws StateRestApiException {
        testSetStateRequest(
                "maintenance",
                State.UP, State.UP,
                NodeStateChangeChecker.Result.allowSettingOfWantedState(),
                Optional.of(State.MAINTENANCE), Optional.of(State.DOWN));
    }

    @Test
    void testProbingDoesntChangeState() throws StateRestApiException {
        probe = true;
        testSetStateRequest(
                "maintenance",
                State.UP, State.UP,
                NodeStateChangeChecker.Result.allowSettingOfWantedState(),
                Optional.empty(), Optional.empty());
    }

    @Test
    void testUpToDown() throws StateRestApiException {
        testSetStateRequest(
                "down",
                State.UP, State.UP,
                NodeStateChangeChecker.Result.allowSettingOfWantedState(),
                Optional.of(State.DOWN), Optional.of(State.DOWN));
    }

    @Test
    void testMaintenanceToUp() throws StateRestApiException {
        testSetStateRequest(
                "up",
                State.MAINTENANCE, State.DOWN,
                NodeStateChangeChecker.Result.allowSettingOfWantedState(),
                Optional.of(State.UP), Optional.of(State.UP));
    }

    @Test
    void testDownToUp() throws StateRestApiException {
        testSetStateRequest(
                "up",
                State.DOWN, State.DOWN,
                NodeStateChangeChecker.Result.allowSettingOfWantedState(),
                Optional.of(State.UP), Optional.of(State.UP));
    }

    @Test
    void testOnlyStorageInMaintenaceToMaintenance() throws StateRestApiException {
        testSetStateRequest(
                "maintenance",
                State.MAINTENANCE, State.UP,
                NodeStateChangeChecker.Result.createAlreadySet(),
                Optional.empty(), Optional.of(State.DOWN));
    }

    @Test
    void testNoOpMaintenaceToMaintenance() throws StateRestApiException {
        testSetStateRequest(
                "maintenance",
                State.MAINTENANCE, State.DOWN,
                NodeStateChangeChecker.Result.createAlreadySet(),
                Optional.empty(), Optional.empty());
    }

    private void testSetStateRequest(
            String wantedStateString,
            State storageWantedState,
            State distributorWantedState,
            NodeStateChangeChecker.Result result,
            Optional<State> expectedNewStorageWantedState,
            Optional<State> expectedNewDistributorWantedState) throws StateRestApiException {
        when(cluster.hasConfiguredNode(NODE_INDEX)).thenReturn(true);

        NodeInfo storageNodeInfo = mock(NodeInfo.class);
        when(cluster.getNodeInfo(storageNode)).thenReturn(storageNodeInfo);
        NodeState storageNodeState = new NodeState(NodeType.STORAGE, storageWantedState);
        when(storageNodeInfo.getUserWantedState()).thenReturn(storageNodeState);

        when(unitState.getId()).thenReturn(wantedStateString);
        when(unitState.getReason()).thenReturn(REASON);

        when(cluster.calculateEffectOfNewState(any(), any(), any(), any(), any(), anyBoolean())).thenReturn(result);

        when(storageNodeInfo.isStorage()).thenReturn(storageNode.getType() == NodeType.STORAGE);
        when(storageNodeInfo.getNodeIndex()).thenReturn(storageNode.getIndex());

        NodeInfo distributorNodeInfo = mock(NodeInfo.class);
        Node distributorNode = new Node(NodeType.DISTRIBUTOR, NODE_INDEX);
        when(cluster.getNodeInfo(distributorNode)).thenReturn(distributorNodeInfo);

        NodeState distributorNodeState = new NodeState(distributorNode.getType(), distributorWantedState);
        if (distributorWantedState != State.UP) {
            distributorNodeState.setDescription(REASON);
        }
        when(distributorNodeInfo.getUserWantedState()).thenReturn(distributorNodeState);

        setWantedState();

        if (expectedNewStorageWantedState.isPresent()) {
            NodeState expectedNewStorageNodeState =
                    new NodeState(NodeType.STORAGE, expectedNewStorageWantedState.get());
            verify(storageNodeInfo).setWantedState(expectedNewStorageNodeState);
            verify(stateListener).handleNewWantedNodeState(storageNodeInfo, expectedNewStorageNodeState);
        } else {
            verify(storageNodeInfo, times(0)).setWantedState(any());
            verify(stateListener, times(0)).handleNewWantedNodeState(eq(storageNodeInfo), any());
        }

        if (expectedNewDistributorWantedState.isPresent()) {
            NodeState expectedNewDistributorNodeState =
                    new NodeState(NodeType.DISTRIBUTOR, expectedNewDistributorWantedState.get());
            verify(distributorNodeInfo).setWantedState(expectedNewDistributorNodeState);
            verify(stateListener).handleNewWantedNodeState(distributorNodeInfo, expectedNewDistributorNodeState);
        } else {
            verify(distributorNodeInfo, times(0)).setWantedState(any());
            verify(stateListener, times(0)).handleNewWantedNodeState(eq(distributorNodeInfo), any());
        }
    }

    private SetResponse setWantedState() throws StateRestApiException {
        return SetNodeStateRequest.setWantedState(
                cluster,
                condition,
                newStates,
                storageNode,
                stateListener,
                currentClusterState,
                inMasterMoratorium,
                probe);
    }
}