10
10
import org .elasticsearch .action .ActionRequest ;
11
11
import org .elasticsearch .action .ActionResponse ;
12
12
import org .elasticsearch .action .ActionType ;
13
+ import org .elasticsearch .action .admin .indices .refresh .RefreshRequest ;
14
+ import org .elasticsearch .action .support .broadcast .BroadcastResponse ;
13
15
import org .elasticsearch .client .internal .Client ;
14
16
import org .elasticsearch .common .settings .Settings ;
15
17
import org .elasticsearch .common .util .concurrent .EsExecutors ;
20
22
import org .elasticsearch .test .ESTestCase ;
21
23
import org .elasticsearch .test .client .NoOpClient ;
22
24
import org .elasticsearch .threadpool .ThreadPool ;
25
+ import org .elasticsearch .xpack .core .security .action .UpdateIndexMigrationVersionAction ;
23
26
import org .elasticsearch .xpack .core .security .action .UpdateIndexMigrationVersionResponse ;
24
27
import org .elasticsearch .xpack .core .security .support .SecurityMigrationTaskParams ;
25
28
import org .junit .Before ;
26
29
30
+ import java .util .List ;
27
31
import java .util .Map ;
28
32
import java .util .Set ;
29
33
import java .util .TreeMap ;
@@ -40,8 +44,11 @@ public class SecurityMigrationExecutorTests extends ESTestCase {
40
44
private SecurityIndexManager securityIndexManager ;
41
45
42
46
private int updateIndexMigrationVersionActionInvocations ;
47
+ private int refreshActionInvocations ;
43
48
44
- private boolean clientShouldThrowException = false ;
49
+ private boolean updateVersionShouldThrowException = false ;
50
+
51
+ private boolean refreshIndexShouldThrowException = false ;
45
52
46
53
private AllocatedPersistentTask mockTask = mock (AllocatedPersistentTask .class );
47
54
@@ -51,6 +58,7 @@ public void setUpMocks() {
51
58
when (threadPool .getThreadContext ()).thenReturn (new ThreadContext (Settings .EMPTY ));
52
59
when (threadPool .generic ()).thenReturn (EsExecutors .DIRECT_EXECUTOR_SERVICE );
53
60
updateIndexMigrationVersionActionInvocations = 0 ;
61
+ refreshActionInvocations = 0 ;
54
62
client = new NoOpClient (threadPool ) {
55
63
@ Override
56
64
@ SuppressWarnings ("unchecked" )
@@ -59,12 +67,27 @@ protected <Request extends ActionRequest, Response extends ActionResponse> void
59
67
Request request ,
60
68
ActionListener <Response > listener
61
69
) {
62
- if (clientShouldThrowException ) {
63
- listener .onFailure (new IllegalStateException ("Bad client" ));
64
- return ;
70
+ if (request instanceof RefreshRequest ) {
71
+ if (refreshIndexShouldThrowException ) {
72
+ if (randomBoolean ()) {
73
+ listener .onFailure (new IllegalStateException ("Refresh index failed" ));
74
+ } else {
75
+ listener .onResponse ((Response ) new BroadcastResponse (1 , 0 , 1 , List .of ()));
76
+ }
77
+ } else {
78
+ refreshActionInvocations ++;
79
+ listener .onResponse ((Response ) new BroadcastResponse (1 , 1 , 0 , List .of ()));
80
+ }
81
+ } else if (request instanceof UpdateIndexMigrationVersionAction .Request ) {
82
+ if (updateVersionShouldThrowException ) {
83
+ listener .onFailure (new IllegalStateException ("Update version failed" ));
84
+ } else {
85
+ updateIndexMigrationVersionActionInvocations ++;
86
+ listener .onResponse ((Response ) new UpdateIndexMigrationVersionResponse ());
87
+ }
88
+ } else {
89
+ fail ("Unexpected client request" );
65
90
}
66
- updateIndexMigrationVersionActionInvocations ++;
67
- listener .onResponse ((Response ) new UpdateIndexMigrationVersionResponse ());
68
91
69
92
}
70
93
};
@@ -85,6 +108,7 @@ public void testSuccessfulMigration() {
85
108
verify (mockTask , times (1 )).markAsCompleted ();
86
109
verify (mockTask , times (0 )).markAsFailed (any ());
87
110
assertEquals (2 , updateIndexMigrationVersionActionInvocations );
111
+ assertEquals (3 , refreshActionInvocations );
88
112
assertEquals (2 , migrateInvocations [0 ]);
89
113
}
90
114
@@ -111,6 +135,7 @@ public void testNoMigrationMeetsRequirements() {
111
135
verify (mockTask , times (1 )).markAsCompleted ();
112
136
verify (mockTask , times (0 )).markAsFailed (any ());
113
137
assertEquals (0 , updateIndexMigrationVersionActionInvocations );
138
+ assertEquals (1 , refreshActionInvocations );
114
139
assertEquals (0 , migrateInvocationsCounter [0 ]);
115
140
}
116
141
@@ -140,6 +165,7 @@ public void testPartialMigration() {
140
165
securityMigrationExecutor .nodeOperation (mockTask , new SecurityMigrationTaskParams (0 , true ), mock (PersistentTaskState .class ));
141
166
verify (mockTask , times (1 )).markAsCompleted ();
142
167
verify (mockTask , times (0 )).markAsFailed (any ());
168
+ assertEquals (3 , refreshActionInvocations );
143
169
assertEquals (2 , updateIndexMigrationVersionActionInvocations );
144
170
assertEquals (2 , migrateInvocations [0 ]);
145
171
}
@@ -158,6 +184,7 @@ public void testNoMigrationNeeded() {
158
184
verify (mockTask , times (1 )).markAsCompleted ();
159
185
verify (mockTask , times (0 )).markAsFailed (any ());
160
186
assertEquals (0 , updateIndexMigrationVersionActionInvocations );
187
+ assertEquals (1 , refreshActionInvocations );
161
188
assertEquals (0 , migrateInvocations [0 ]);
162
189
}
163
190
@@ -186,14 +213,13 @@ public int minMappingVersion() {
186
213
}))
187
214
);
188
215
189
- assertThrows (
190
- IllegalStateException .class ,
191
- () -> securityMigrationExecutor .nodeOperation (
216
+ securityMigrationExecutor .nodeOperation (
192
217
mockTask ,
193
218
new SecurityMigrationTaskParams (0 , true ),
194
219
mock (PersistentTaskState .class )
195
- )
196
- );
220
+ );
221
+ verify (mockTask , times (1 )).markAsFailed (any ());
222
+ verify (mockTask , times (0 )).markAsCompleted ();
197
223
}
198
224
199
225
public void testUpdateMigrationVersionThrowsException () {
@@ -205,12 +231,27 @@ public void testUpdateMigrationVersionThrowsException() {
205
231
client ,
206
232
new TreeMap <>(Map .of (1 , generateMigration (migrateInvocations , true ), 2 , generateMigration (migrateInvocations , true )))
207
233
);
208
- clientShouldThrowException = true ;
234
+ updateVersionShouldThrowException = true ;
209
235
securityMigrationExecutor .nodeOperation (mockTask , new SecurityMigrationTaskParams (0 , true ), mock (PersistentTaskState .class ));
210
236
verify (mockTask , times (1 )).markAsFailed (any ());
211
237
verify (mockTask , times (0 )).markAsCompleted ();
212
238
}
213
239
240
+ public void testRefreshSecurityIndexThrowsException () {
241
+ final int [] migrateInvocations = new int [1 ];
242
+ SecurityMigrationExecutor securityMigrationExecutor = new SecurityMigrationExecutor (
243
+ "test-task" ,
244
+ threadPool .generic (),
245
+ securityIndexManager ,
246
+ client ,
247
+ new TreeMap <>(Map .of (1 , generateMigration (migrateInvocations , true ), 2 , generateMigration (migrateInvocations , true )))
248
+ );
249
+ refreshIndexShouldThrowException = true ;
250
+ securityMigrationExecutor .nodeOperation (mockTask , new SecurityMigrationTaskParams (0 , true ), mock (PersistentTaskState .class ));
251
+ verify (mockTask , times (0 )).markAsFailed (any ());
252
+ verify (mockTask , times (1 )).markAsCompleted ();
253
+ }
254
+
214
255
private SecurityMigrations .SecurityMigration generateMigration (int [] migrateInvocationsCounter , boolean isEligible ) {
215
256
SecurityMigrations .SecurityMigration migration = new SecurityMigrations .SecurityMigration () {
216
257
@ Override
0 commit comments