-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathOperatorShutdownHandlerTest.java
More file actions
239 lines (198 loc) · 11.1 KB
/
OperatorShutdownHandlerTest.java
File metadata and controls
239 lines (198 loc) · 11.1 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.uid2.operator;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import com.uid2.operator.service.ShutdownService;
import com.uid2.operator.vertx.OperatorShutdownHandler;
import com.uid2.shared.attest.AttestationResponseCode;
import io.vertx.core.Vertx;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.utils.Pair;
import java.security.Permission;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.*;
@ExtendWith(VertxExtension.class)
public class OperatorShutdownHandlerTest {
private AutoCloseable mocks;
@Mock private Clock clock;
@Mock private ShutdownService shutdownService;
private OperatorShutdownHandler operatorShutdownHandler;
@BeforeEach
void beforeEach() {
mocks = MockitoAnnotations.openMocks(this);
when(clock.instant()).thenAnswer(i -> Instant.now());
doThrow(new RuntimeException()).when(shutdownService).Shutdown(1);
this.operatorShutdownHandler = new OperatorShutdownHandler(Duration.ofHours(12), Duration.ofHours(12), Duration.ofHours(168), clock, shutdownService);
}
@AfterEach
void afterEach() throws Exception {
mocks.close();
}
@Test
void shutdownOnAttestFailure(VertxTestContext testContext) {
ListAppender<ILoggingEvent> logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(OperatorShutdownHandler.class)).addAppender(logWatcher);
// Revoke auth
try {
this.operatorShutdownHandler.handleAttestResponse(Pair.of(AttestationResponseCode.AttestationFailure, "Unauthorized"));
} catch (RuntimeException e) {
verify(shutdownService).Shutdown(1);
String message = logWatcher.list.get(0).getFormattedMessage();
Assertions.assertEquals("core attestation failed with AttestationFailure, shutting down operator, core response: Unauthorized", logWatcher.list.get(0).getFormattedMessage());
testContext.completeNow();
}
}
@Test
void shutdownOnAttestFailedTooLong(VertxTestContext testContext) {
ListAppender<ILoggingEvent> logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(OperatorShutdownHandler.class)).addAppender(logWatcher);
this.operatorShutdownHandler.handleAttestResponse(Pair.of(AttestationResponseCode.RetryableFailure, ""));
when(clock.instant()).thenAnswer(i -> Instant.now().plus(12, ChronoUnit.HOURS).plusSeconds(60));
try {
this.operatorShutdownHandler.handleAttestResponse(Pair.of(AttestationResponseCode.RetryableFailure, ""));
} catch (RuntimeException e) {
verify(shutdownService).Shutdown(1);
Assertions.assertTrue(logWatcher.list.get(0).getFormattedMessage().contains("core attestation has been in failed state for too long. shutting down operator"));
testContext.completeNow();
}
}
@Test
void attestRecoverOnSuccess(VertxTestContext testContext) {
ListAppender<ILoggingEvent> logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(OperatorShutdownHandler.class)).addAppender(logWatcher);
this.operatorShutdownHandler.handleAttestResponse(Pair.of(AttestationResponseCode.RetryableFailure, ""));
when(clock.instant()).thenAnswer(i -> Instant.now().plus(6, ChronoUnit.HOURS));
this.operatorShutdownHandler.handleAttestResponse(Pair.of(AttestationResponseCode.Success, ""));
when(clock.instant()).thenAnswer(i -> Instant.now().plus(12, ChronoUnit.HOURS));
assertDoesNotThrow(() -> {
this.operatorShutdownHandler.handleAttestResponse(Pair.of(AttestationResponseCode.RetryableFailure, ""));
});
verify(shutdownService, never()).Shutdown(anyInt());
testContext.completeNow();
}
@Test
void shutdownOnSaltsExpiredTooLong(VertxTestContext testContext) {
ListAppender<ILoggingEvent> logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(OperatorShutdownHandler.class)).addAppender(logWatcher);
this.operatorShutdownHandler.handleSaltRetrievalResponse(true);
Assertions.assertTrue(logWatcher.list.get(0).getFormattedMessage().contains("all salts are expired"));
when(clock.instant()).thenAnswer(i -> Instant.now().plus(12, ChronoUnit.HOURS).plusSeconds(60));
Assertions.assertThrows(RuntimeException.class, () -> {
this.operatorShutdownHandler.handleSaltRetrievalResponse(true);
});
Assertions.assertAll("Expired Salts Log Messages",
() -> verify(shutdownService).Shutdown(1),
() -> Assertions.assertTrue(logWatcher.list.get(1).getFormattedMessage().contains("all salts are expired")),
() -> Assertions.assertTrue(logWatcher.list.get(2).getFormattedMessage().contains("salts have been in expired state for too long. shutting down operator")),
() -> Assertions.assertEquals(3, logWatcher.list.size()));
testContext.completeNow();
}
@Test
void saltsRecoverOnSuccess(VertxTestContext testContext) {
ListAppender<ILoggingEvent> logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(OperatorShutdownHandler.class)).addAppender(logWatcher);
this.operatorShutdownHandler.handleSaltRetrievalResponse(true);
Assertions.assertTrue(logWatcher.list.get(0).getFormattedMessage().contains("all salts are expired"));
when(clock.instant()).thenAnswer(i -> Instant.now().plus(6, ChronoUnit.HOURS));
this.operatorShutdownHandler.handleSaltRetrievalResponse(true);
Assertions.assertTrue(logWatcher.list.get(1).getFormattedMessage().contains("all salts are expired"));
when(clock.instant()).thenAnswer(i -> Instant.now().plus(12, ChronoUnit.HOURS));
assertDoesNotThrow(() -> {
this.operatorShutdownHandler.handleSaltRetrievalResponse(false);
});
Assertions.assertEquals(2, logWatcher.list.size());
verify(shutdownService, never()).Shutdown(anyInt());
testContext.completeNow();
}
@Test
void saltsLogErrorAtInterval(VertxTestContext testContext) {
ListAppender<ILoggingEvent> logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(OperatorShutdownHandler.class)).addAppender(logWatcher);
this.operatorShutdownHandler.handleSaltRetrievalResponse(true);
Assertions.assertTrue(logWatcher.list.get(0).getFormattedMessage().contains("all salts are expired"));
when(clock.instant()).thenAnswer(i -> Instant.now().plus(9, ChronoUnit.MINUTES));
this.operatorShutdownHandler.handleSaltRetrievalResponse(true);
Assertions.assertEquals(1, logWatcher.list.size());
when(clock.instant()).thenAnswer(i -> Instant.now().plus(11, ChronoUnit.MINUTES));
this.operatorShutdownHandler.handleSaltRetrievalResponse(true);
Assertions.assertTrue(logWatcher.list.get(1).getFormattedMessage().contains("all salts are expired"));
Assertions.assertEquals(2, logWatcher.list.size());
testContext.completeNow();
}
@Test
void shutdownOnKeysetKeyFailedTooLong(VertxTestContext testContext) {
ListAppender<ILoggingEvent> logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(OperatorShutdownHandler.class)).addAppender(logWatcher);
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(false);
Assertions.assertTrue(logWatcher.list.get(0).getFormattedMessage().contains("keyset keys sync started failing"));
when(clock.instant()).thenAnswer(i -> Instant.now().plus(7, ChronoUnit.DAYS).plusSeconds(60));
try {
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(false);
} catch (RuntimeException e) {
verify(shutdownService).Shutdown(1);
Assertions.assertTrue(logWatcher.list.stream().anyMatch(log ->
log.getFormattedMessage().contains("keyset keys have been failing to sync for too long")));
testContext.completeNow();
}
}
@Test
void keysetKeyRecoverOnSuccess(VertxTestContext testContext) {
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(false);
when(clock.instant()).thenAnswer(i -> Instant.now().plus(3, ChronoUnit.DAYS));
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(true);
when(clock.instant()).thenAnswer(i -> Instant.now().plus(7, ChronoUnit.DAYS));
assertDoesNotThrow(() -> {
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(false);
});
verify(shutdownService, never()).Shutdown(anyInt());
testContext.completeNow();
}
@Test
void keysetKeyNoShutdownWhenAlwaysSuccessful(VertxTestContext testContext) {
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(true);
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(true);
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(true);
verify(shutdownService, never()).Shutdown(anyInt());
testContext.completeNow();
}
@Test
void keysetKeyLogProgressAtInterval(VertxTestContext testContext) {
ListAppender<ILoggingEvent> logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(OperatorShutdownHandler.class)).addAppender(logWatcher);
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(false);
long warnLogCount1 = logWatcher.list.stream().filter(log ->
log.getFormattedMessage().contains("keyset keys sync still failing")).count();
when(clock.instant()).thenAnswer(i -> Instant.now().plus(5, ChronoUnit.MINUTES));
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(false);
long warnLogCount2 = logWatcher.list.stream().filter(log ->
log.getFormattedMessage().contains("keyset keys sync still failing")).count();
Assertions.assertEquals(warnLogCount1, warnLogCount2);
when(clock.instant()).thenAnswer(i -> Instant.now().plus(11, ChronoUnit.MINUTES));
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(false);
long warnLogCount3 = logWatcher.list.stream().filter(log ->
log.getFormattedMessage().contains("keyset keys sync still failing")).count();
Assertions.assertTrue(warnLogCount3 > warnLogCount2);
testContext.completeNow();
}
}