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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
/*-
* ============LICENSE_START=======================================================
* ONAP - SO
* ================================================================================
* Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.so.openstack.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.so.db.request.beans.CloudApiRequests;
import org.onap.so.db.request.beans.InfraActiveRequests;
import org.onap.so.db.request.client.RequestsDbClient;
import org.onap.so.openstack.beans.CreateStackRequest;
import org.onap.so.openstack.exceptions.MsoException;
import org.onap.so.openstack.exceptions.MsoOpenstackException;
import org.onap.so.openstack.exceptions.MsoStackAlreadyExists;
import org.springframework.core.env.Environment;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woorea.openstack.base.client.OpenStackResponseException;
import com.woorea.openstack.heat.Heat;
import com.woorea.openstack.heat.StackResource;
import com.woorea.openstack.heat.StackResource.CreateStack;
import com.woorea.openstack.heat.StackResource.DeleteStack;
import com.woorea.openstack.heat.model.CreateStackParam;
import com.woorea.openstack.heat.model.Resources;
import com.woorea.openstack.heat.model.Stack;
@RunWith(MockitoJUnitRunner.class)
public class MsoHeatUtilsTest extends MsoHeatUtils {
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Spy
@InjectMocks
private MsoHeatUtils heatUtils;
@Mock
private RequestsDbClient requestDbClient;
@Mock
private Heat heatClient;
@Mock
private StackStatusHandler stackStatusHandler;
@Mock
private Environment env;
@Mock
private StackResource stackResource;
@Mock
private NovaClientImpl novaClient;
@Mock
private DeleteStack mockDeleteStack;
@Mock
private Resources mockResources;
@Mock
private CreateStack mockCreateStack;
private String cloudSiteId = "cloudSiteId";
private String tenantId = "tenantId";
@Before
public void setup() {
doReturn("15").when(env).getProperty("org.onap.so.adapters.po.pollInterval", "15");
doReturn("1").when(env).getProperty("org.onap.so.adapters.po.pollMultiplier", "60");
}
@Test
public final void pollStackForStatus_Create_Complete_Test() throws MsoException, IOException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_IN_PROGRESS");
stack.setStackStatusReason("Stack Finished");
Stack latestStack = new Stack();
latestStack.setId("id");
latestStack.setStackName("stackName");
latestStack.setStackStatus("CREATE_COMPLETE");
latestStack.setStackStatusReason("Stack Finished");
doReturn(latestStack).when(heatUtils).queryHeatStack(isA(Heat.class), eq("stackName/id"));
doReturn(heatClient).when(heatUtils).getHeatClient(cloudSiteId, tenantId);
Stack actual = heatUtils.pollStackForStatus(1, stack, "CREATE_IN_PROGRESS", cloudSiteId, tenantId);
Mockito.verify(stackStatusHandler, times(1)).updateStackStatus(latestStack);
Mockito.verify(heatUtils, times(1)).queryHeatStack(isA(Heat.class), eq("stackName/id"));
assertEquals(true, actual != null);
}
@Test
public final void pollStackForStatus_Polling_Exhausted_Test() throws MsoException, IOException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_IN_PROGRESS");
stack.setStackStatusReason("Stack Finished");
doNothing().when(stackStatusHandler).updateStackStatus(stack);
doReturn(stack).when(heatUtils).queryHeatStack(isA(Heat.class), eq("stackName/id"));
doReturn(heatClient).when(heatUtils).getHeatClient(cloudSiteId, tenantId);
Stack actual = heatUtils.pollStackForStatus(1, stack, "CREATE_IN_PROGRESS", cloudSiteId, tenantId);
Mockito.verify(stackStatusHandler, times(1)).updateStackStatus(stack);
Mockito.verify(heatUtils, times(1)).queryHeatStack(isA(Heat.class), eq("stackName/id"));
assertEquals(true, actual != null);
}
@Test
public final void postProcessStackCreate_CREATE_IN_PROGRESS_Test() throws MsoException, IOException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_IN_PROGRESS");
stack.setStackStatusReason("Stack Finished");
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
exceptionRule.expect(StackCreationException.class);
exceptionRule.expectMessage("Stack rollback suppressed, stack not deleted");
heatUtils.postProcessStackCreate(stack, false, 120, false, cloudSiteId, tenantId, createStackParam);
}
@Test
public final void postProcessStackCreate_Backout_True_Test() throws MsoException, IOException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_IN_PROGRESS");
stack.setStackStatusReason("Stack Finished");
Stack deletedStack = new Stack();
deletedStack.setId("id");
deletedStack.setStackName("stackName");
deletedStack.setStackStatus("DELETE_COMPLETE");
deletedStack.setStackStatusReason("Stack Deleted");
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
doReturn(deletedStack).when(heatUtils).handleUnknownCreateStackFailure(stack, 120, cloudSiteId, tenantId);
exceptionRule.expect(StackCreationException.class);
exceptionRule.expectMessage(
"Stack Creation Failed Openstack Status: CREATE_IN_PROGRESS Status Reason: Stack Finished , Rollback of Stack Creation completed with status: DELETE_COMPLETE Status Reason: Stack Deleted");
heatUtils.postProcessStackCreate(stack, true, 120, false, cloudSiteId, tenantId, createStackParam);
Mockito.verify(heatUtils, times(1)).handleUnknownCreateStackFailure(stack, 120, cloudSiteId, tenantId);
}
@Test
public final void postProcessStackCreate_Backout_True_Delete_Fail_Test() throws MsoException, IOException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_IN_PROGRESS");
stack.setStackStatusReason("Stack Finished");
Stack deletedStack = new Stack();
deletedStack.setId("id");
deletedStack.setStackName("stackName");
deletedStack.setStackStatus("DELETE_COMPLETE");
deletedStack.setStackStatusReason("Stack Deleted");
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
doThrow(new MsoOpenstackException(500, "Error In Delete", "Error In Delete")).when(heatUtils)
.handleUnknownCreateStackFailure(stack, 120, cloudSiteId, tenantId);
exceptionRule.expect(MsoException.class);
exceptionRule.expectMessage(
"Stack Creation Failed Openstack Status: CREATE_IN_PROGRESS Status Reason: Stack Finished , Rollback of Stack Creation failed with sync error: Error In Delete");
heatUtils.postProcessStackCreate(stack, true, 120, false, cloudSiteId, tenantId, createStackParam);
Mockito.verify(heatUtils, times(1)).handleUnknownCreateStackFailure(stack, 120, cloudSiteId, tenantId);
}
@Test
public final void postProcessStackCreate_Keypair_True_Test() throws MsoException, IOException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_IN_PROGRESS");
stack.setStackStatusReason(
"Resource CREATE failed: Conflict: resources.my_keypair: Key pair 'hst3bbfnm0011vm001' already exists. (HTTP 409) (Request-ID: req-941b0af6-63ae-4d6a-afbc-90b728bacf82");
Stack createdStack = new Stack();
createdStack.setId("id");
createdStack.setStackName("stackName");
createdStack.setStackStatus("CREATE_COMPLETE");
createdStack.setStackStatusReason("Stack Created");
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
doReturn(createdStack).when(heatUtils).handleKeyPairConflict(cloudSiteId, tenantId, createStackParam, 120, true,
stack);
heatUtils.postProcessStackCreate(stack, true, 120, true, cloudSiteId, tenantId, createStackParam);
Mockito.verify(heatUtils, times(1)).handleKeyPairConflict(cloudSiteId, tenantId, createStackParam, 120, true,
stack);
}
@Test
public final void handleUnknownCreateStackFailure_Test() throws MsoException, IOException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_FAILED");
stack.setStackStatusReason(
"Resource CREATE failed: Conflict: resources.my_keypair: Key pair 'hst3bbfnm0011vm001' already exists. (HTTP 409) (Request-ID: req-941b0af6-63ae-4d6a-afbc-90b728bacf82");
Stack deletedStack = new Stack();
deletedStack.setId("id");
deletedStack.setStackName("stackName");
deletedStack.setStackStatus("DELETE_COMPLETE");
deletedStack.setStackStatusReason("Stack Deleted");
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
doReturn(heatClient).when(heatUtils).getHeatClient(cloudSiteId, tenantId);
doNothing().when(heatUtils).postProcessStackDelete(deletedStack);
doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(mockDeleteStack);
doReturn(stackResource).when(heatClient).getStacks();
doReturn(mockDeleteStack).when(stackResource).deleteByName("stackName/id");
doReturn(deletedStack).when(heatUtils).pollStackForStatus(120, stack, "DELETE_IN_PROGRESS", cloudSiteId,
tenantId);
heatUtils.handleUnknownCreateStackFailure(stack, 120, cloudSiteId, tenantId);
Mockito.verify(heatUtils, times(1)).executeAndRecordOpenstackRequest(mockDeleteStack);
Mockito.verify(heatUtils, times(1)).pollStackForStatus(120, stack, "DELETE_IN_PROGRESS", cloudSiteId, tenantId);
Mockito.verify(heatUtils, times(1)).postProcessStackDelete(deletedStack);
}
@Test
public final void handleUnknownCreateStackFailure_Null_Stack_Test() throws MsoException, IOException {
Stack stack = null;
exceptionRule.expect(StackCreationException.class);
exceptionRule.expectMessage("Cannot Find Stack Name or Id");
heatUtils.handleUnknownCreateStackFailure(stack, 120, cloudSiteId, tenantId);
}
@Test
public final void postProcessStackDelete_Stack_Test() throws MsoException, IOException {
Stack deletedStack = new Stack();
deletedStack.setId("id");
deletedStack.setStackName("stackName");
deletedStack.setStackStatus("DELETE_FAILED");
deletedStack.setStackStatusReason("Stack DID NOT DELETE");
exceptionRule.expect(StackRollbackException.class);
exceptionRule.expectMessage(
"Stack Deletion completed with status: DELETE_FAILED Status Reason: Stack DID NOT DELETE");
heatUtils.postProcessStackDelete(deletedStack);
}
@Test
public final void postProcessStackDelete__Null_Stack_Test() throws MsoException, IOException {
Stack stack = null;
exceptionRule.expect(StackRollbackException.class);
exceptionRule.expectMessage("Cannot Find Stack Name or Id");
heatUtils.postProcessStackDelete(stack);
}
@Test
public final void isKeyPairFailure_Test() throws MsoException, IOException {
boolean actual = heatUtils.isKeyPairFailure(
"Exception during create VF 0 : Stack error (CREATE_FAILED): Resource CREATE failed: Conflict: resources.bfnm_my_keypair: Key pair 'hst3bbfnm0011vm001' already exists. (HTTP 409) (Request-ID:req-941b0af6-63ae-4d6a-afbc-90b728bacf82) - stack successfully deleted'rolledBack='true'");
assertEquals(true, actual);
}
@Test
public final void handleKeyPairConflict_Test() throws MsoException, IOException, NovaClientException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_FAILED");
stack.setStackStatusReason(
"Resource CREATE failed: Conflict: resources.my_keypair: Key pair 'hst3bbfnm0011vm001' already exists. (HTTP 409) (Request-ID: req-941b0af6-63ae-4d6a-afbc-90b728bacf82");
Stack createdStack = new Stack();
createdStack.setId("id");
createdStack.setStackName("stackName");
createdStack.setStackStatus("CREATE_COMPLETE");
createdStack.setStackStatusReason("Stack Created");
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
// doReturn(mockResources).when(heatUtils).queryStackResources(cloudSiteId, tenantId, "stackName", 2);
// doNothing().when(novaClient).deleteKeyPair(cloudSiteId, tenantId, "KeypairName");
doReturn(null).when(heatUtils).handleUnknownCreateStackFailure(stack, 120, cloudSiteId, tenantId);
doReturn(createdStack).when(heatUtils).createStack(createStackParam, cloudSiteId, tenantId);
doReturn(createdStack).when(heatUtils).processCreateStack(cloudSiteId, tenantId, 120, true, createdStack,
createStackParam, false);
heatUtils.handleKeyPairConflict(cloudSiteId, tenantId, createStackParam, 120, true, stack);
Mockito.verify(novaClient, times(1)).deleteKeyPair(cloudSiteId, tenantId, "hst3bbfnm0011vm001");
Mockito.verify(heatUtils, times(1)).handleUnknownCreateStackFailure(stack, 120, cloudSiteId, tenantId);
Mockito.verify(heatUtils, times(1)).createStack(createStackParam, cloudSiteId, tenantId);
Mockito.verify(heatUtils, times(1)).processCreateStack(cloudSiteId, tenantId, 120, true, createdStack,
createStackParam, false);
}
@Test
public final void processCreateStack_Test() throws MsoException, IOException, NovaClientException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_FAILED");
stack.setStackStatusReason(
"Resource CREATE failed: Conflict: resources.my_keypair: Key pair 'hst3bbfnm0011vm001' already exists. (HTTP 409) (Request-ID: req-941b0af6-63ae-4d6a-afbc-90b728bacf82");
Stack createdStack = new Stack();
createdStack.setId("id");
createdStack.setStackName("stackName");
createdStack.setStackStatus("CREATE_COMPLETE");
createdStack.setStackStatusReason("Stack Created");
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
doReturn(createdStack).when(heatUtils).pollStackForStatus(120, stack, "CREATE_IN_PROGRESS", cloudSiteId,
tenantId);
doReturn(createdStack).when(heatUtils).postProcessStackCreate(createdStack, true, 120, true, cloudSiteId,
tenantId, createStackParam);
heatUtils.processCreateStack(cloudSiteId, tenantId, 120, true, stack, createStackParam, true);
Mockito.verify(heatUtils, times(1)).pollStackForStatus(120, stack, "CREATE_IN_PROGRESS", cloudSiteId, tenantId);
Mockito.verify(heatUtils, times(1)).postProcessStackCreate(createdStack, true, 120, true, cloudSiteId, tenantId,
createStackParam);
}
@Test
public final void processCreateStack_Exception_Backout_Test()
throws MsoException, IOException, NovaClientException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_FAILED");
stack.setStackStatusReason(
"Resource CREATE failed: Conflict: resources.my_keypair: Key pair 'hst3bbfnm0011vm001' already exists. (HTTP 409) (Request-ID: req-941b0af6-63ae-4d6a-afbc-90b728bacf82");
Stack deletedStack = new Stack();
deletedStack.setId("id");
deletedStack.setStackName("stackName");
deletedStack.setStackStatus("DELETE_COMPLETE");
deletedStack.setStackStatusReason("Stack Deleted");
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
doThrow(new StackCreationException("Error")).when(heatUtils).pollStackForStatus(120, stack,
"CREATE_IN_PROGRESS", cloudSiteId, tenantId);
exceptionRule.expect(MsoException.class);
exceptionRule.expectMessage("Error");
heatUtils.processCreateStack(cloudSiteId, tenantId, 120, true, stack, createStackParam, true);
Mockito.verify(heatUtils, times(1)).pollStackForStatus(120, stack, "CREATE_IN_PROGRESS", cloudSiteId, tenantId);
Mockito.verify(heatUtils, times(1)).postProcessStackCreate(stack, true, 120, true, cloudSiteId, tenantId,
createStackParam);
}
@Test
public final void createStack_Test() throws MsoException, IOException, NovaClientException {
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
doReturn(heatClient).when(heatUtils).getHeatClient(cloudSiteId, tenantId);
doReturn(stackResource).when(heatClient).getStacks();
doReturn(mockCreateStack).when(stackResource).create(createStackParam);
doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(mockCreateStack);
heatUtils.createStack(createStackParam, cloudSiteId, tenantId);
Mockito.verify(stackResource, times(1)).create(createStackParam);
Mockito.verify(heatUtils, times(1)).saveStackRequest(eq(createStackParam), isNull(), eq("stackName"));
Mockito.verify(heatClient, times(1)).getStacks();
Mockito.verify(stackResource, times(1)).create(createStackParam);
}
@Test
public final void saveStack_Test() throws MsoException, IOException, NovaClientException {
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("test", "value");
String environment =
"parameters:\n mmn_volume_name_1: \"data-mn-v-vdb\"\n mmn_volume_name_2: \"arch-mn-v-vdc\"\n";
createStackParam.setParameters(parameters);
createStackParam.setEnvironment(environment);
CreateStackRequest createStackRequest = new CreateStackRequest();
createStackRequest.setEnvironment(environment);
createStackRequest.setParameters(parameters);
ObjectMapper mapper = new ObjectMapper();
String stackRequest = mapper.writeValueAsString(createStackRequest);
InfraActiveRequests request = new InfraActiveRequests();
request.setRequestId("requestId");
doReturn(request).when(requestDbClient).getInfraActiveRequestbyRequestId("requestId");
doNothing().when(requestDbClient).updateInfraActiveRequests(request);
heatUtils.saveStackRequest(createStackParam, "requestId", "stackName");
Mockito.verify(requestDbClient, times(1)).updateInfraActiveRequests(request);
assertNotNull(request.getCloudApiRequests().get(0));
assertEquals("requestId", request.getCloudApiRequests().get(0).getRequestId());
assertEquals(stackRequest, request.getCloudApiRequests().get(0).getRequestBody());
}
@Test
public final void saveStack__Exists_Test() throws MsoException, IOException, NovaClientException {
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("test", "value");
createStackParam.setParameters(parameters);
InfraActiveRequests request = new InfraActiveRequests();
request.setRequestId("requestId");
CloudApiRequests cloudRequest = new CloudApiRequests();
cloudRequest.setCloudIdentifier("stackName");
cloudRequest.setRequestBody("testMe");
cloudRequest.setRequestId("requestId");
request.getCloudApiRequests().add(cloudRequest);
doReturn(request).when(requestDbClient).getInfraActiveRequestbyRequestId("requestId");
doNothing().when(requestDbClient).updateInfraActiveRequests(request);
heatUtils.saveStackRequest(createStackParam, "requestId", "stackName");
Mockito.verify(requestDbClient, times(1)).updateInfraActiveRequests(request);
assertNotNull(request.getCloudApiRequests().get(0));
assertEquals("requestId", request.getCloudApiRequests().get(0).getRequestId());
assertNotEquals("testMe", request.getCloudApiRequests().get(0).getRequestBody());
}
@Test
public final void createStack_Error_Test() throws MsoException, IOException, NovaClientException {
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
doReturn(heatClient).when(heatUtils).getHeatClient(cloudSiteId, tenantId);
doReturn(stackResource).when(heatClient).getStacks();
doReturn(mockCreateStack).when(stackResource).create(createStackParam);
doThrow(new OpenStackResponseException("Unknown Error", 500)).when(heatUtils)
.executeAndRecordOpenstackRequest(mockCreateStack);
exceptionRule.expect(MsoOpenstackException.class);
exceptionRule.expectMessage("Unknown Error");
heatUtils.createStack(createStackParam, cloudSiteId, tenantId);
Mockito.verify(stackResource, times(1)).create(createStackParam);
Mockito.verify(heatUtils, times(1)).saveStackRequest(eq(createStackParam), isNull(), eq("stackName"));
Mockito.verify(heatClient, times(1)).getStacks();
Mockito.verify(stackResource, times(1)).create(createStackParam);
}
@Test
public final void createStack_Error_404_Test() throws MsoException, IOException, NovaClientException {
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
doReturn(heatClient).when(heatUtils).getHeatClient(cloudSiteId, tenantId);
doReturn(stackResource).when(heatClient).getStacks();
doReturn(mockCreateStack).when(stackResource).create(createStackParam);
doThrow(new OpenStackResponseException("Not Found", 409)).when(heatUtils)
.executeAndRecordOpenstackRequest(mockCreateStack);
exceptionRule.expect(MsoStackAlreadyExists.class);
exceptionRule.expectMessage("Stack stackName already exists in Tenant tenantId in Cloud cloudSiteId");
heatUtils.createStack(createStackParam, cloudSiteId, tenantId);
Mockito.verify(stackResource, times(1)).create(createStackParam);
Mockito.verify(heatUtils, times(1)).saveStackRequest(eq(createStackParam), isNull(), eq("stackName"));
Mockito.verify(heatClient, times(1)).getStacks();
Mockito.verify(stackResource, times(1)).create(createStackParam);
}
@Test
public final void processCreateStack_Exception_No_Backout_Test()
throws MsoException, IOException, NovaClientException {
Stack stack = new Stack();
stack.setId("id");
stack.setStackName("stackName");
stack.setStackStatus("CREATE_FAILED");
stack.setStackStatusReason(
"Resource CREATE failed: Conflict: resources.my_keypair: Key pair 'hst3bbfnm0011vm001' already exists. (HTTP 409) (Request-ID: req-941b0af6-63ae-4d6a-afbc-90b728bacf82");
Stack deletedStack = new Stack();
deletedStack.setId("id");
deletedStack.setStackName("stackName");
deletedStack.setStackStatus("DELETE_COMPLETE");
deletedStack.setStackStatusReason("Stack Deleted");
CreateStackParam createStackParam = new CreateStackParam();
createStackParam.setStackName("stackName");
doThrow(new StackCreationException("Error")).when(heatUtils).pollStackForStatus(120, stack,
"CREATE_IN_PROGRESS", cloudSiteId, tenantId);
exceptionRule.expect(MsoException.class);
exceptionRule.expectMessage("Error");
heatUtils.processCreateStack(cloudSiteId, tenantId, 120, false, stack, createStackParam, true);
Mockito.verify(heatUtils, times(1)).pollStackForStatus(120, stack, "CREATE_IN_PROGRESS", cloudSiteId, tenantId);
Mockito.verify(heatUtils, times(0)).handleUnknownCreateStackFailure(stack, 120, cloudSiteId, tenantId);
}
}
|