aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/configserver/orchestrator/OrchestratorImplTest.java
blob: bb9c075ad741eb3d908eb7213594d06f2fecccb4 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.configserver.orchestrator;

import com.yahoo.vespa.hosted.node.admin.configserver.ConfigServerApiImpl;
import com.yahoo.vespa.hosted.node.admin.configserver.HttpException;
import com.yahoo.vespa.orchestrator.restapi.wire.BatchOperationResult;
import com.yahoo.vespa.orchestrator.restapi.wire.HostStateChangeDenialReason;
import com.yahoo.vespa.orchestrator.restapi.wire.UpdateHostResponse;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author freva
 */
public class OrchestratorImplTest {

    private static final String hostName = "host123.yahoo.com";

    private final ConfigServerApiImpl configServerApi = mock(ConfigServerApiImpl.class);
    private final OrchestratorImpl orchestrator = new OrchestratorImpl(configServerApi);

    @Test
    void testSuspendCall() {
        when(configServerApi.put(
                eq(OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_API + "/" + hostName + "/suspended"),
                eq(Optional.empty()),
                eq(UpdateHostResponse.class),
                any()
        )).thenReturn(new UpdateHostResponse(hostName, null));

        orchestrator.suspend(hostName);
    }

    @Test
    void testSuspendCallWithFailureReason() {
        assertThrows(OrchestratorException.class, () -> {
            when(configServerApi.put(
                    eq(OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_API + "/" + hostName + "/suspended"),
                    eq(Optional.empty()),
                    eq(UpdateHostResponse.class),
                    any()
            )).thenReturn(new UpdateHostResponse(hostName, new HostStateChangeDenialReason("hostname", "fail")));

            orchestrator.suspend(hostName);
        });
    }

    @Test
    void testSuspendCallWithNotFound() {
        assertThrows(OrchestratorNotFoundException.class, () -> {
            when(configServerApi.put(any(String.class), any(), any(), any()))
                    .thenThrow(new HttpException.NotFoundException("Not Found"));

            orchestrator.suspend(hostName);
        });
    }

    @Test
    void testSuspendCallWithSomeOtherException() {
        assertThrows(RuntimeException.class, () -> {
            when(configServerApi.put(any(String.class), any(), any(), any()))
                    .thenThrow(new RuntimeException("Some parameter was wrong"));

            orchestrator.suspend(hostName);
        });
    }


    @Test
    void testResumeCall() {
        when(configServerApi.delete(
                OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_API + "/" + hostName + "/suspended",
                UpdateHostResponse.class
        )).thenReturn(new UpdateHostResponse(hostName, null));

        orchestrator.resume(hostName);
    }

    @Test
    void testResumeCallWithFailureReason() {
        assertThrows(OrchestratorException.class, () -> {
            when(configServerApi.delete(
                    OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_API + "/" + hostName + "/suspended",
                    UpdateHostResponse.class
            )).thenReturn(new UpdateHostResponse(hostName, new HostStateChangeDenialReason("hostname", "fail")));

            orchestrator.resume(hostName);
        });
    }

    @Test
    void testResumeCallWithNotFound() {
        assertThrows(OrchestratorNotFoundException.class, () -> {
            when(configServerApi.delete(
                    any(String.class),
                    any()
            )).thenThrow(new HttpException.NotFoundException("Not Found"));

            orchestrator.resume(hostName);
        });
    }

    @Test
    void testResumeCallWithSomeOtherException() {
        assertThrows(RuntimeException.class, () -> {
            when(configServerApi.put(any(String.class), any(), any(), any()))
                    .thenThrow(new RuntimeException("Some parameter was wrong"));

            orchestrator.suspend(hostName);
        });
    }

    @Test
    void testBatchSuspendCall() {
        String parentHostName = "host1.test.yahoo.com";
        List<String> hostNames = List.of("a1.host1.test.yahoo.com", "a2.host1.test.yahoo.com");

        when(configServerApi.put(
                eq("/orchestrator/v1/suspensions/hosts/host1.test.yahoo.com?hostname=a1.host1.test.yahoo.com&hostname=a2.host1.test.yahoo.com"),
                eq(Optional.empty()),
                eq(BatchOperationResult.class),
                any()
        )).thenReturn(BatchOperationResult.successResult());

        orchestrator.suspend(parentHostName, hostNames);
    }

    @Test
    void testBatchSuspendCallWithFailureReason() {
        assertThrows(OrchestratorException.class, () -> {
            String parentHostName = "host1.test.yahoo.com";
            List<String> hostNames = List.of("a1.host1.test.yahoo.com", "a2.host1.test.yahoo.com");
            String failureReason = "Failed to suspend";

            when(configServerApi.put(
                    eq("/orchestrator/v1/suspensions/hosts/host1.test.yahoo.com?hostname=a1.host1.test.yahoo.com&hostname=a2.host1.test.yahoo.com"),
                    eq(Optional.empty()),
                    eq(BatchOperationResult.class),
                    any()
            )).thenReturn(new BatchOperationResult(failureReason));

            orchestrator.suspend(parentHostName, hostNames);
        });
    }

    @Test
    void testBatchSuspendCallWithSomeException() {
        assertThrows(RuntimeException.class, () -> {
            String parentHostName = "host1.test.yahoo.com";
            List<String> hostNames = List.of("a1.host1.test.yahoo.com", "a2.host1.test.yahoo.com");
            String exceptionMessage = "Exception: Something crashed!";

            when(configServerApi.put(
                    eq("/orchestrator/v1/suspensions/hosts/host1.test.yahoo.com?hostname=a1.host1.test.yahoo.com&hostname=a2.host1.test.yahoo.com"),
                    eq(Optional.empty()),
                    eq(BatchOperationResult.class),
                    any()
            )).thenThrow(new RuntimeException(exceptionMessage));

            orchestrator.suspend(parentHostName, hostNames);
        });
    }

}