43
43
import lombok .extern .slf4j .Slf4j ;
44
44
import org .junit .jupiter .api .BeforeEach ;
45
45
import org .junit .jupiter .api .Test ;
46
+ import org .junit .jupiter .api .extension .ExtendWith ;
46
47
import org .junit .jupiter .api .io .TempDir ;
47
48
import org .junit .jupiter .params .ParameterizedTest ;
48
49
import org .junit .jupiter .params .provider .MethodSource ;
50
+ import org .junit .jupiter .params .provider .ValueSource ;
49
51
import org .mockito .ArgumentCaptor ;
50
52
import org .mockito .InjectMocks ;
51
53
import org .mockito .Mock ;
52
- import org .mockito .MockitoAnnotations ;
54
+ import org .mockito .junit . jupiter . MockitoExtension ;
53
55
54
56
import java .io .File ;
55
57
import java .io .IOException ;
67
69
import static org .mockito .Mockito .*;
68
70
69
71
@ Slf4j
72
+ @ ExtendWith (MockitoExtension .class )
70
73
class PostComputeServiceTests {
71
74
72
75
private static final String CHAIN_TASK_ID = "CHAIN_TASK_ID" ;
73
76
private static final String DATASET_URI = "DATASET_URI" ;
74
77
private static final String WORKER_NAME = "WORKER_NAME" ;
75
78
private static final String TEE_POST_COMPUTE_IMAGE = "TEE_POST_COMPUTE_IMAGE" ;
76
- private static final long TEE_POST_COMPUTE_HEAP = 1024 ;
77
79
private static final String TEE_POST_COMPUTE_ENTRYPOINT = "postComputeEntrypoint" ;
78
80
private static final TeeSessionGenerationResponse SECURE_SESSION = mock (TeeSessionGenerationResponse .class );
79
81
private static final long MAX_EXECUTION_TIME = 1000 ;
80
82
83
+ private final TeeAppProperties postComputeProperties = TeeAppProperties .builder ()
84
+ .image (TEE_POST_COMPUTE_IMAGE )
85
+ .entrypoint (TEE_POST_COMPUTE_ENTRYPOINT )
86
+ .build ();
87
+
81
88
@ TempDir
82
89
public File jUnitTemporaryFolder ;
83
90
private TaskDescription taskDescription = TaskDescription .builder ()
@@ -97,10 +104,6 @@ class PostComputeServiceTests {
97
104
@ Mock
98
105
private TeeServicesManager teeServicesManager ;
99
106
@ Mock
100
- private TeeAppProperties preComputeProperties ;
101
- @ Mock
102
- private TeeAppProperties postComputeProperties ;
103
- @ Mock
104
107
private TeeServicesProperties properties ;
105
108
@ Mock
106
109
private DockerClientInstance dockerClientInstanceMock ;
@@ -112,19 +115,11 @@ class PostComputeServiceTests {
112
115
private TeeServicesPropertiesService teeServicesPropertiesService ;
113
116
@ Mock
114
117
private ComputeDurationsService postComputeDurationsService ;
115
-
116
118
@ Mock
117
119
private TeeService teeMockedService ;
118
120
119
121
@ BeforeEach
120
122
void beforeEach () {
121
- MockitoAnnotations .openMocks (this );
122
- when (dockerService .getClient ()).thenReturn (dockerClientInstanceMock );
123
- when (teeServicesManager .getTeeService (any ())).thenReturn (teeMockedService );
124
- when (properties .getPreComputeProperties ()).thenReturn (preComputeProperties );
125
- when (properties .getPostComputeProperties ()).thenReturn (postComputeProperties );
126
- when (teeServicesPropertiesService .getTeeServicesProperties (CHAIN_TASK_ID )).thenReturn (properties );
127
-
128
123
output = jUnitTemporaryFolder .getAbsolutePath ();
129
124
iexecOut = output + IexecFileHelper .SLASH_IEXEC_OUT ;
130
125
computedJson = iexecOut + IexecFileHelper .SLASH_COMPUTED_JSON ;
@@ -209,35 +204,36 @@ void shouldFailResultFilesNameCheckWhenFileNameTooLong() throws IOException {
209
204
// endregion
210
205
211
206
//region runTeePostCompute
207
+ void prepareMocksForTeePostCompute (DockerRunResponse dockerRunResponse ) {
208
+ List <String > env = Arrays .asList ("var0" , "var1" );
209
+ when (dockerService .getClient ()).thenReturn (dockerClientInstanceMock );
210
+ when (teeServicesManager .getTeeService (any ())).thenReturn (teeMockedService );
211
+ when (teeServicesPropertiesService .getTeeServicesProperties (CHAIN_TASK_ID )).thenReturn (properties );
212
+ when (properties .getPostComputeProperties ()).thenReturn (postComputeProperties );
213
+ when (dockerClientInstanceMock .isImagePresent (TEE_POST_COMPUTE_IMAGE )).thenReturn (true );
214
+ when (teeMockedService .buildPostComputeDockerEnv (taskDescription , SECURE_SESSION )).thenReturn (env );
215
+ String iexecOutBind = iexecOut + ":" + IexecFileHelper .SLASH_IEXEC_OUT ;
216
+ when (dockerService .getIexecOutBind (CHAIN_TASK_ID )).thenReturn (iexecOutBind );
217
+ when (workerConfigService .getWorkerName ()).thenReturn (WORKER_NAME );
218
+ when (workerConfigService .getDockerNetworkName ()).thenReturn ("lasNetworkName" );
219
+ when (sgxService .getSgxDriverMode ()).thenReturn (SgxDriverMode .LEGACY );
220
+ when (dockerService .run (any ())).thenReturn (dockerRunResponse );
221
+ }
222
+
212
223
@ Test
213
224
void shouldRunTeePostComputeAndConnectToLasNetwork () {
214
- String lasNetworkName = "networkName " ;
225
+ String lasNetworkName = "lasNetworkName " ;
215
226
taskDescription = TaskDescription .builder ()
216
227
.chainTaskId (CHAIN_TASK_ID )
217
228
.datasetUri (DATASET_URI )
218
229
.maxExecutionTime (MAX_EXECUTION_TIME )
219
230
.build ();
220
- List <String > env = Arrays .asList ("var0" , "var1" );
221
- when (postComputeProperties .getImage ()).thenReturn (TEE_POST_COMPUTE_IMAGE );
222
- when (postComputeProperties .getHeapSizeInBytes ()).thenReturn (TEE_POST_COMPUTE_HEAP );
223
- when (postComputeProperties .getEntrypoint ()).thenReturn (TEE_POST_COMPUTE_ENTRYPOINT );
224
- when (dockerClientInstanceMock .isImagePresent (TEE_POST_COMPUTE_IMAGE ))
225
- .thenReturn (true );
226
- when (teeMockedService .buildPostComputeDockerEnv (taskDescription , SECURE_SESSION ))
227
- .thenReturn (env );
228
- String iexecOutBind = iexecOut + ":" + IexecFileHelper .SLASH_IEXEC_OUT ;
229
- when (dockerService .getIexecOutBind (CHAIN_TASK_ID )).thenReturn (iexecOutBind );
230
- when (workerConfigService .getTaskOutputDir (CHAIN_TASK_ID )).thenReturn (output );
231
- when (workerConfigService .getTaskIexecOutDir (CHAIN_TASK_ID )).thenReturn (iexecOut );
232
- when (workerConfigService .getWorkerName ()).thenReturn (WORKER_NAME );
233
- when (workerConfigService .getDockerNetworkName ()).thenReturn (lasNetworkName );
234
231
DockerRunResponse expectedDockerRunResponse = DockerRunResponse
235
232
.builder ()
236
233
.finalStatus (DockerRunFinalStatus .SUCCESS )
237
234
.executionDuration (Duration .ofSeconds (10 ))
238
235
.build ();
239
- when (dockerService .run (any ())).thenReturn (expectedDockerRunResponse );
240
- when (sgxService .getSgxDriverMode ()).thenReturn (SgxDriverMode .LEGACY );
236
+ prepareMocksForTeePostCompute (expectedDockerRunResponse );
241
237
List <Device > devices = List .of (Device .parse ("/dev/isgx" ));
242
238
when (sgxService .getSgxDevices ()).thenReturn (devices );
243
239
@@ -251,10 +247,12 @@ void shouldRunTeePostComputeAndConnectToLasNetwork() {
251
247
verify (dockerService ).run (argumentCaptor .capture ());
252
248
DockerRunRequest dockerRunRequest =
253
249
argumentCaptor .getAllValues ().get (0 );
250
+ String iexecOutBind = iexecOut + ":" + IexecFileHelper .SLASH_IEXEC_OUT ;
254
251
HostConfig hostConfig = HostConfig .newHostConfig ()
255
252
.withBinds (Bind .parse (iexecOutBind ))
256
253
.withDevices (devices )
257
254
.withNetworkMode (lasNetworkName );
255
+ List <String > env = Arrays .asList ("var0" , "var1" );
258
256
assertThat (dockerRunRequest ).isEqualTo (
259
257
DockerRunRequest .builder ()
260
258
.hostConfig (hostConfig )
@@ -276,9 +274,9 @@ void shouldNotRunTeePostComputeSinceDockerImageNotFoundLocally() {
276
274
.datasetUri (DATASET_URI )
277
275
.maxExecutionTime (MAX_EXECUTION_TIME )
278
276
.build ();
279
- when (postComputeProperties . getImage ()).thenReturn (TEE_POST_COMPUTE_IMAGE );
280
- when (postComputeProperties . getHeapSizeInBytes ( )).thenReturn (TEE_POST_COMPUTE_HEAP );
281
- when (postComputeProperties . getEntrypoint ()).thenReturn (TEE_POST_COMPUTE_ENTRYPOINT );
277
+ when (dockerService . getClient ()).thenReturn (dockerClientInstanceMock );
278
+ when (teeServicesPropertiesService . getTeeServicesProperties ( CHAIN_TASK_ID )).thenReturn (properties );
279
+ when (properties . getPostComputeProperties ()).thenReturn (postComputeProperties );
282
280
when (dockerClientInstanceMock .isImagePresent (TEE_POST_COMPUTE_IMAGE ))
283
281
.thenReturn (false );
284
282
@@ -297,29 +295,17 @@ void shouldRunTeePostComputeWithFailDockerResponse(Map.Entry<Integer, ReplicateS
297
295
.datasetUri (DATASET_URI )
298
296
.maxExecutionTime (MAX_EXECUTION_TIME )
299
297
.build ();
300
- List <String > env = Arrays .asList ("var0" , "var1" );
301
- when (postComputeProperties .getImage ()).thenReturn (TEE_POST_COMPUTE_IMAGE );
302
- when (postComputeProperties .getHeapSizeInBytes ()).thenReturn (TEE_POST_COMPUTE_HEAP );
303
- when (postComputeProperties .getEntrypoint ()).thenReturn (TEE_POST_COMPUTE_ENTRYPOINT );
304
- when (dockerClientInstanceMock .isImagePresent (TEE_POST_COMPUTE_IMAGE ))
305
- .thenReturn (true );
306
- when (teeMockedService .buildPostComputeDockerEnv (taskDescription , SECURE_SESSION ))
307
- .thenReturn (env );
308
- String iexecOutBind = iexecOut + ":" + IexecFileHelper .SLASH_IEXEC_OUT ;
309
- when (dockerService .getIexecOutBind (CHAIN_TASK_ID )).thenReturn (iexecOutBind );
310
- when (workerConfigService .getTaskOutputDir (CHAIN_TASK_ID )).thenReturn (output );
311
- when (workerConfigService .getTaskIexecOutDir (CHAIN_TASK_ID )).thenReturn (iexecOut );
312
- when (workerConfigService .getWorkerName ()).thenReturn (WORKER_NAME );
313
- when (workerConfigService .getDockerNetworkName ()).thenReturn ("lasNetworkName" );
314
298
DockerRunResponse expectedDockerRunResponse =
315
299
DockerRunResponse .builder ()
316
300
.finalStatus (DockerRunFinalStatus .FAILED )
317
301
.containerExitCode (exitCodeKeyToExpectedCauseValue .getKey ())
318
302
.build ();
319
- when (dockerService .run (any ())).thenReturn (expectedDockerRunResponse );
320
- when (sgxService .getSgxDriverMode ()).thenReturn (SgxDriverMode .LEGACY );
321
- when (computeExitCauseService .getExitCausesAndPruneForGivenComputeStage (CHAIN_TASK_ID , ComputeStage .POST , POST_COMPUTE_FAILED_UNKNOWN_ISSUE ))
322
- .thenReturn (List .of (exitCodeKeyToExpectedCauseValue .getValue ()));
303
+ prepareMocksForTeePostCompute (expectedDockerRunResponse );
304
+ // Only stub computeExitCauseService for exitCode == 1
305
+ if (exitCodeKeyToExpectedCauseValue .getKey () == 1 ) {
306
+ when (computeExitCauseService .getExitCausesAndPruneForGivenComputeStage (CHAIN_TASK_ID , ComputeStage .POST , POST_COMPUTE_FAILED_UNKNOWN_ISSUE ))
307
+ .thenReturn (List .of (exitCodeKeyToExpectedCauseValue .getValue ()));
308
+ }
323
309
324
310
PostComputeResponse postComputeResponse =
325
311
postComputeService .runTeePostCompute (taskDescription , SECURE_SESSION );
@@ -345,24 +331,11 @@ void shouldNotRunTeePostComputeSinceTimeout() {
345
331
.datasetUri (DATASET_URI )
346
332
.maxExecutionTime (MAX_EXECUTION_TIME )
347
333
.build ();
348
- List <String > env = Arrays .asList ("var0" , "var1" );
349
- when (postComputeProperties .getImage ()).thenReturn (TEE_POST_COMPUTE_IMAGE );
350
- when (postComputeProperties .getHeapSizeInBytes ()).thenReturn (TEE_POST_COMPUTE_HEAP );
351
- when (postComputeProperties .getEntrypoint ()).thenReturn (TEE_POST_COMPUTE_ENTRYPOINT );
352
- when (dockerClientInstanceMock .isImagePresent (TEE_POST_COMPUTE_IMAGE ))
353
- .thenReturn (true );
354
- when (teeMockedService .buildPostComputeDockerEnv (taskDescription , SECURE_SESSION ))
355
- .thenReturn (env );
356
- when (dockerService .getIexecOutBind (CHAIN_TASK_ID )).thenReturn ("/iexec_out:/iexec_out" );
357
- when (workerConfigService .getTaskOutputDir (CHAIN_TASK_ID )).thenReturn (output );
358
- when (workerConfigService .getWorkerName ()).thenReturn (WORKER_NAME );
359
- when (workerConfigService .getDockerNetworkName ()).thenReturn ("lasNetworkName" );
360
334
DockerRunResponse expectedDockerRunResponse =
361
335
DockerRunResponse .builder ()
362
336
.finalStatus (DockerRunFinalStatus .TIMEOUT )
363
337
.build ();
364
- when (dockerService .run (any ())).thenReturn (expectedDockerRunResponse );
365
- when (sgxService .getSgxDriverMode ()).thenReturn (SgxDriverMode .LEGACY );
338
+ prepareMocksForTeePostCompute (expectedDockerRunResponse );
366
339
367
340
PostComputeResponse postComputeResponse =
368
341
postComputeService .runTeePostCompute (taskDescription , SECURE_SESSION );
@@ -372,5 +345,26 @@ void shouldNotRunTeePostComputeSinceTimeout() {
372
345
.containsExactly (ReplicateStatusCause .POST_COMPUTE_TIMEOUT );
373
346
verify (dockerService ).run (any ());
374
347
}
348
+
349
+ // region getExitCauses
350
+ @ ParameterizedTest
351
+ @ ValueSource (ints = {4 , 5 , 10 , 42 , 127 , 255 })
352
+ void shouldReturnUnknownIssueForUnmappedExitCodes (int exitCode ) {
353
+ taskDescription = TaskDescription .builder ()
354
+ .chainTaskId (CHAIN_TASK_ID )
355
+ .datasetUri (DATASET_URI )
356
+ .maxExecutionTime (MAX_EXECUTION_TIME )
357
+ .build ();
358
+ final DockerRunResponse dockerResponse = DockerRunResponse .builder ()
359
+ .finalStatus (DockerRunFinalStatus .FAILED )
360
+ .containerExitCode (exitCode )
361
+ .build ();
362
+ prepareMocksForTeePostCompute (dockerResponse );
363
+ final PostComputeResponse response = postComputeService .runTeePostCompute (taskDescription , SECURE_SESSION );
364
+ assertThat (response .isSuccessful ()).isFalse ();
365
+ assertThat (response .getExitCauses ())
366
+ .hasSize (1 )
367
+ .containsExactly (POST_COMPUTE_FAILED_UNKNOWN_ISSUE );
368
+ }
375
369
//endregion
376
370
}
0 commit comments