summaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/orchestrator/OrchestratorImplTest.java
blob: 9b2dcb07eb3ab3b718a9b2c87c5da4c6c2b2a904 (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
package com.yahoo.vespa.hosted.node.admin.orchestrator;

import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.hosted.node.admin.util.ConfigServerHttpRequestExecutor;
import com.yahoo.vespa.orchestrator.restapi.wire.BatchHostSuspendRequest;
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.Before;
import org.junit.Test;

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

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

/**
 * @author valerijf
 */
public class OrchestratorImplTest {
    private static final HostName hostName = new HostName("host123.yahoo.com");
    private ConfigServerHttpRequestExecutor requestExecutor;
    private OrchestratorImpl orchestrator;

    @Before
    public void setup() {
        requestExecutor = mock(ConfigServerHttpRequestExecutor.class);
        orchestrator = new OrchestratorImpl(requestExecutor);
    }

    @Test
    public void testSuspendCall() {
        when(requestExecutor.put(
                OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_API + "/" + hostName+ "/suspended",
                OrchestratorImpl.HARDCODED_ORCHESTRATOR_PORT,
                Optional.empty(),
                UpdateHostResponse.class
        )).thenReturn(new UpdateHostResponse(hostName.s(), null));

        boolean response = orchestrator.suspend(hostName);
        assertTrue("Expected Orchestrator to approve", response);
    }

    @Test
    public void testSuspendCallWithFailureReason() {
        when(requestExecutor.put(
                OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_API + "/" + hostName+ "/suspended",
                OrchestratorImpl.HARDCODED_ORCHESTRATOR_PORT,
                Optional.empty(),
                UpdateHostResponse.class
        )).thenReturn(new UpdateHostResponse(hostName.s(), new HostStateChangeDenialReason("hostname", "service", "fail")));

        boolean response = orchestrator.suspend(hostName);
        assertFalse("Expected Orchestrator to deny when presented with HostChangeDenialReason", response);
    }

    @Test
    public void testSuspendCallWithNotFound() {
        when(requestExecutor.put(
                any(String.class),
                any(Integer.class),
                any(),
                any()
        )).thenThrow(requestExecutor.new NotFoundException("Not Found"));

        boolean response = orchestrator.suspend(hostName);
        assertTrue("Expected Orchestrator to respond with true even when NotFoundException is thrown", response);
    }

    @Test
    public void testSuspendCallWithSomeOtherException() {
        when(requestExecutor.put(
                any(String.class),
                any(Integer.class),
                any(),
                any()
        )).thenThrow(new RuntimeException("Some parameter was wrong"));

        boolean response = orchestrator.suspend(hostName);
        assertFalse("Expected Orchestrator to respond with false when some other exception is thrown", response);
    }


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

        boolean response = orchestrator.resume(hostName);
        assertTrue("Expected Orchestrator to approve", response);
    }

    @Test
    public void testResumeCallWithFailureReason() {
        when(requestExecutor.delete(
                OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_API + "/" + hostName+ "/suspended",
                OrchestratorImpl.HARDCODED_ORCHESTRATOR_PORT,
                UpdateHostResponse.class
        )).thenReturn(new UpdateHostResponse(hostName.s(), new HostStateChangeDenialReason("hostname", "service", "fail")));

        boolean response = orchestrator.resume(hostName);
        assertFalse("Expected Orchestrator to deny when presented with HostChangeDenialReason", response);
    }

    @Test
    public void testResumeCallWithNotFound() {
        when(requestExecutor.delete(
                any(String.class),
                any(Integer.class),
                any()
        )).thenThrow(requestExecutor.new NotFoundException("Not Found"));

        boolean response = orchestrator.resume(hostName);
        assertTrue("Expected Orchestrator to respond with true even when NotFoundException is thrown", response);
    }

    @Test
    public void testResumeCallWithSomeOtherException() {
        when(requestExecutor.put(
                any(String.class),
                any(Integer.class),
                any(),
                any()
        )).thenThrow(new RuntimeException("Some parameter was wrong"));

        boolean response = orchestrator.suspend(hostName);
        assertFalse("Expected Orchestrator to respond with false when some other exception is thrown", response);
    }


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

        when(requestExecutor.put(
                OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_SUSPENSION_API,
                OrchestratorImpl.HARDCODED_ORCHESTRATOR_PORT,
                Optional.of(new BatchHostSuspendRequest(parentHostName, hostNames)),
                BatchOperationResult.class
        )).thenReturn(BatchOperationResult.successResult());

        Optional<String> response = orchestrator.suspend(parentHostName, hostNames);
        assertFalse("Expected failureReason to be empty", response.isPresent());
    }

    @Test
    public void testBatchSuspendCallWithFailureReason() {
        String parentHostName = "host1.test.yahoo.com";
        List<String> hostNames = Arrays.asList("a1.host1.test.yahoo.com", "a2.host1.test.yahoo.com");
        String failureReason = "Failed to suspend";

        when(requestExecutor.put(
                OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_SUSPENSION_API,
                OrchestratorImpl.HARDCODED_ORCHESTRATOR_PORT,
                Optional.of(new BatchHostSuspendRequest(parentHostName, hostNames)),
                BatchOperationResult.class
        )).thenReturn(new BatchOperationResult(failureReason));

        Optional<String> response = orchestrator.suspend(parentHostName, hostNames);
        assertEquals("Expected failureReason to be empty", response, Optional.of(failureReason));
    }

    @Test
    public void testBatchSuspendCallWithSomeException() {
        String parentHostName = "host1.test.yahoo.com";
        List<String> hostNames = Arrays.asList("a1.host1.test.yahoo.com", "a2.host1.test.yahoo.com");
        String exceptionMessage = "Exception: Something crashed!";

        when(requestExecutor.put(
                OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_SUSPENSION_API,
                OrchestratorImpl.HARDCODED_ORCHESTRATOR_PORT,
                Optional.of(new BatchHostSuspendRequest(parentHostName, hostNames)),
                BatchOperationResult.class
        )).thenThrow(new RuntimeException(exceptionMessage));

        Optional<String> response = orchestrator.suspend(parentHostName, hostNames);
        assertEquals("Expected failureReason to be empty", response, Optional.of(exceptionMessage));
    }
}