aboutsummaryrefslogtreecommitdiffstats
path: root/orchestrator/src/test/java/com/yahoo/vespa/orchestrator/policy/HostedVespaPolicyTest.java
blob: 97adeea983580168b2215fe6abb90b192b0f6d5b (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.orchestrator.policy;


import com.yahoo.config.provision.ApplicationId;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.flags.InMemoryFlagSource;
import com.yahoo.vespa.orchestrator.OrchestrationException;
import com.yahoo.vespa.orchestrator.OrchestratorContext;
import com.yahoo.vespa.orchestrator.controller.ClusterControllerClient;
import com.yahoo.vespa.orchestrator.controller.ClusterControllerClientFactory;
import com.yahoo.vespa.orchestrator.controller.ClusterControllerNodeState;
import com.yahoo.vespa.orchestrator.model.ApplicationApi;
import com.yahoo.vespa.orchestrator.model.ApplicationApiFactory;
import com.yahoo.vespa.orchestrator.model.ClusterApi;
import com.yahoo.vespa.orchestrator.model.StorageNode;
import com.yahoo.vespa.orchestrator.status.HostStatus;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;

import java.util.Arrays;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author oyving
 * @author bakksjo
 */
public class HostedVespaPolicyTest {

    private final ClusterControllerClientFactory clientFactory = mock(ClusterControllerClientFactory.class);
    private final ClusterControllerClient client = mock(ClusterControllerClient.class);
    private final ManualClock clock = new ManualClock();
    private final ApplicationApiFactory applicationApiFactory = new ApplicationApiFactory(3, 5, 1, 0.1, clock);
    private final InMemoryFlagSource flagSource = new InMemoryFlagSource();

    @Before
    public void setUp() {
        when(clientFactory.createClient(any(), any())).thenReturn(client);
    }

    @Test
    public void testGrantSuspension() throws HostStateChangeDeniedException {
        final HostedVespaClusterPolicy clusterPolicy = mock(HostedVespaClusterPolicy.class);
        when(clusterPolicy.verifyGroupGoingDownIsFine(any())).thenReturn(SuspensionReasons.nothingNoteworthy());
        final HostedVespaPolicy policy = new HostedVespaPolicy(clusterPolicy, clientFactory, applicationApiFactory, flagSource);
        final ApplicationApi applicationApi = mock(ApplicationApi.class);
        when(applicationApi.applicationId()).thenReturn(ApplicationId.fromSerializedForm("tenant:app:default"));

        ClusterApi clusterApi1 = mock(ClusterApi.class);
        ClusterApi clusterApi2 = mock(ClusterApi.class);
        ClusterApi clusterApi3 = mock(ClusterApi.class);
        List<ClusterApi> clusterApis = Arrays.asList(clusterApi1, clusterApi2, clusterApi3);
        when(applicationApi.getClusters()).thenReturn(clusterApis);

        StorageNode storageNode1 = mock(StorageNode.class);
        HostName hostName1 = new HostName("storage-1");
        when(storageNode1.hostName()).thenReturn(hostName1);

        HostName hostName2 = new HostName("host-2");

        StorageNode storageNode3 = mock(StorageNode.class);
        HostName hostName3 = new HostName("storage-3");
        when(storageNode1.hostName()).thenReturn(hostName3);

        List<StorageNode> upStorageNodes = Arrays.asList(storageNode1, storageNode3);
        when(applicationApi.getNoRemarksStorageNodesInGroupInClusterOrder()).thenReturn(upStorageNodes);
        // setHostState

        List<HostName> noRemarksHostNames = Arrays.asList(hostName1, hostName2, hostName3);
        when(applicationApi.getNodesInGroupWithStatus(HostStatus.NO_REMARKS)).thenReturn(noRemarksHostNames);

        InOrder order = inOrder(applicationApi, clusterPolicy, storageNode1, storageNode3);

        OrchestratorContext context = mock(OrchestratorContext.class);
        policy.grantSuspensionRequest(context, applicationApi);

        order.verify(applicationApi).getClusters();
        order.verify(clusterPolicy).verifyGroupGoingDownIsFine(clusterApi1);
        order.verify(clusterPolicy).verifyGroupGoingDownIsFine(clusterApi2);
        order.verify(clusterPolicy).verifyGroupGoingDownIsFine(clusterApi3);

        order.verify(applicationApi).getNoRemarksStorageNodesInGroupInClusterOrder();
        order.verify(storageNode1).setStorageNodeState(context, ClusterControllerNodeState.MAINTENANCE);
        order.verify(storageNode3).setStorageNodeState(context, ClusterControllerNodeState.MAINTENANCE);

        order.verify(applicationApi).getNodesInGroupWithStatus(HostStatus.NO_REMARKS);
        order.verify(applicationApi).setHostState(context, hostName1, HostStatus.ALLOWED_TO_BE_DOWN);
        order.verify(applicationApi).setHostState(context, hostName2, HostStatus.ALLOWED_TO_BE_DOWN);
        order.verify(applicationApi).setHostState(context, hostName3, HostStatus.ALLOWED_TO_BE_DOWN);

        order.verifyNoMoreInteractions();
    }

    @Test
    public void testAcquirePermissionToRemove() throws OrchestrationException {
        final HostedVespaClusterPolicy clusterPolicy = mock(HostedVespaClusterPolicy.class);
        final HostedVespaPolicy policy = new HostedVespaPolicy(clusterPolicy, clientFactory, applicationApiFactory, flagSource);
        final ApplicationApi applicationApi = mock(ApplicationApi.class);
        when(applicationApi.applicationId()).thenReturn(ApplicationId.fromSerializedForm("tenant:app:default"));

        ClusterApi clusterApi1 = mock(ClusterApi.class);
        ClusterApi clusterApi2 = mock(ClusterApi.class);
        ClusterApi clusterApi3 = mock(ClusterApi.class);
        List<ClusterApi> clusterApis = Arrays.asList(clusterApi1, clusterApi2, clusterApi3);
        when(applicationApi.getClusters()).thenReturn(clusterApis);

        StorageNode storageNode1 = mock(StorageNode.class);
        HostName hostName1 = new HostName("storage-1");
        when(storageNode1.hostName()).thenReturn(hostName1);

        HostName hostName2 = new HostName("host-2");

        StorageNode storageNode3 = mock(StorageNode.class);
        HostName hostName3 = new HostName("storage-3");
        when(storageNode1.hostName()).thenReturn(hostName3);

        List<StorageNode> upStorageNodes = Arrays.asList(storageNode1, storageNode3);
        when(applicationApi.getStorageNodesInGroupInClusterOrder()).thenReturn(upStorageNodes);

        List<HostName> noRemarksHostNames = Arrays.asList(hostName1, hostName2, hostName3);
        when(applicationApi.getNodesInGroupWith(any())).thenReturn(noRemarksHostNames);

        InOrder order = inOrder(applicationApi, clusterPolicy, storageNode1, storageNode3);

        OrchestratorContext context = mock(OrchestratorContext.class);
        OrchestratorContext probeContext = mock(OrchestratorContext.class);
        when(context.createSubcontextForSingleAppOp(true)).thenReturn(probeContext);
        policy.acquirePermissionToRemove(context, applicationApi);

        order.verify(applicationApi).getClusters();
        order.verify(clusterPolicy).verifyGroupGoingDownPermanentlyIsFine(clusterApi1);
        order.verify(clusterPolicy).verifyGroupGoingDownPermanentlyIsFine(clusterApi2);
        order.verify(clusterPolicy).verifyGroupGoingDownPermanentlyIsFine(clusterApi3);

        order.verify(applicationApi).getStorageNodesInGroupInClusterOrder();
        order.verify(storageNode1).setStorageNodeState(probeContext, ClusterControllerNodeState.DOWN);
        order.verify(storageNode3).setStorageNodeState(probeContext, ClusterControllerNodeState.DOWN);

        order.verify(applicationApi).getNodesInGroupWith(any());
        order.verify(applicationApi).setHostState(context, hostName1, HostStatus.PERMANENTLY_DOWN);
        order.verify(applicationApi).setHostState(context, hostName2, HostStatus.PERMANENTLY_DOWN);
        order.verify(applicationApi).setHostState(context, hostName3, HostStatus.PERMANENTLY_DOWN);

        order.verifyNoMoreInteractions();
    }

    @Test
    public void testAcquirePermissionToRemoveConfigServer() throws OrchestrationException {
        final HostedVespaClusterPolicy clusterPolicy = mock(HostedVespaClusterPolicy.class);
        final HostedVespaPolicy policy = new HostedVespaPolicy(clusterPolicy, clientFactory, applicationApiFactory, flagSource);
        final ApplicationApi applicationApi = mock(ApplicationApi.class);
        when(applicationApi.applicationId()).thenReturn(ApplicationId.fromSerializedForm("tenant:app:default"));

        ClusterApi clusterApi1 = mock(ClusterApi.class);
        ClusterApi clusterApi2 = mock(ClusterApi.class);
        ClusterApi clusterApi3 = mock(ClusterApi.class);
        List<ClusterApi> clusterApis = Arrays.asList(clusterApi1, clusterApi2, clusterApi3);
        when(applicationApi.getClusters()).thenReturn(clusterApis);

        StorageNode storageNode1 = mock(StorageNode.class);
        HostName hostName1 = new HostName("storage-1");
        when(storageNode1.hostName()).thenReturn(hostName1);

        HostName hostName2 = new HostName("host-2");

        StorageNode storageNode3 = mock(StorageNode.class);
        HostName hostName3 = new HostName("storage-3");
        when(storageNode1.hostName()).thenReturn(hostName3);

        List<StorageNode> upStorageNodes = Arrays.asList(storageNode1, storageNode3);
        when(applicationApi.getStorageNodesInGroupInClusterOrder()).thenReturn(upStorageNodes);

        List<HostName> noRemarksHostNames = Arrays.asList(hostName1, hostName2, hostName3);
        when(applicationApi.getNodesInGroupWith(any())).thenReturn(noRemarksHostNames);

        InOrder order = inOrder(applicationApi, clusterPolicy, storageNode1, storageNode3);

        OrchestratorContext context = mock(OrchestratorContext.class);
        OrchestratorContext probeContext = mock(OrchestratorContext.class);
        when(context.createSubcontextForSingleAppOp(true)).thenReturn(probeContext);
        policy.acquirePermissionToRemove(context, applicationApi);

        order.verify(applicationApi).getClusters();
        order.verify(clusterPolicy).verifyGroupGoingDownPermanentlyIsFine(clusterApi1);
        order.verify(clusterPolicy).verifyGroupGoingDownPermanentlyIsFine(clusterApi2);
        order.verify(clusterPolicy).verifyGroupGoingDownPermanentlyIsFine(clusterApi3);

        order.verify(applicationApi).getStorageNodesInGroupInClusterOrder();
        order.verify(storageNode1).setStorageNodeState(probeContext, ClusterControllerNodeState.DOWN);
        order.verify(storageNode3).setStorageNodeState(probeContext, ClusterControllerNodeState.DOWN);

        order.verify(applicationApi).getNodesInGroupWith(any());
        order.verify(applicationApi).setHostState(context, hostName1, HostStatus.PERMANENTLY_DOWN);
        order.verify(applicationApi).setHostState(context, hostName2, HostStatus.PERMANENTLY_DOWN);
        order.verify(applicationApi).setHostState(context, hostName3, HostStatus.PERMANENTLY_DOWN);

        order.verifyNoMoreInteractions();
    }
}