-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_extract_terraform_operation_metadata.py
More file actions
455 lines (390 loc) · 24.4 KB
/
test_extract_terraform_operation_metadata.py
File metadata and controls
455 lines (390 loc) · 24.4 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
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
import unittest
import os
from unittest.mock import patch, mock_open
from terraform.extract_terraform_operation_metadata import (
time_to_seconds,
parse_module_path,
process_terraform_logs,
)
class TestExtractTerraformOperationMetadata(unittest.TestCase):
def test_time_to_seconds_with_hours_minutes_and_seconds(self):
self.assertEqual(time_to_seconds("1h2m30s"), 3750)
def test_time_to_seconds_with_hours_and_seconds(self):
self.assertEqual(time_to_seconds("1h30s"), 3630)
def test_time_to_seconds_with_minutes_and_seconds(self):
self.assertEqual(time_to_seconds("2m30s"), 150)
def test_time_to_seconds_with_seconds_only(self):
self.assertEqual(time_to_seconds("45s"), 45)
def test_time_to_seconds_with_invalid_format(self):
self.assertEqual(time_to_seconds("invalid"), 0)
def test_parse_module_path_with_submodule(self):
module, submodule, resource = parse_module_path("module.aks[\"cas\"].azurerm_kubernetes_cluster.aks")
self.assertEqual(module, "aks[\"cas\"]")
self.assertEqual(submodule, "azurerm_kubernetes_cluster")
self.assertEqual(resource, "aks")
def test_parse_module_path_without_submodule(self):
module, submodule, resource = parse_module_path("module.main.resource")
self.assertEqual(module, "main")
self.assertEqual(submodule, "")
self.assertEqual(resource, "resource")
def test_parse_module_path_with_only_module(self):
module, submodule, resource = parse_module_path("module.main")
self.assertEqual(module, "main")
self.assertEqual(submodule, "")
self.assertEqual(resource, "")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data="module.aks[\"cas\"].azurerm_kubernetes_cluster.aks: Creation complete after 1h2m30s\n")
def test_process_terraform_logs_with_valid_log_line(self, mock_open_file, mock_isfile):
os.environ["RUN_ID"] = "123456789"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="test_scenario_type",
_scenario_name="test_scenario_name",
)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]["run_id"], "123456789")
self.assertEqual(results[0]["module_name"], "aks[\"cas\"]")
self.assertEqual(results[0]["submodule_name"], "azurerm_kubernetes_cluster")
self.assertEqual(results[0]["resource_name"], "aks")
self.assertEqual(results[0]["action"], "apply")
self.assertEqual(results[0]["time_taken_seconds"], 3750)
mock_open_file.assert_called_once_with('/fake/path/terraform_apply.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data="module.main.resource: Destruction complete after 45s\n")
def test_process_terraform_logs_with_destruction_log_line(self, mock_open_file, mock_isfile):
os.environ["RUN_ID"] = "987654321"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="destroy",
_scenario_type="test_scenario_type",
_scenario_name="test_scenario_name",
)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]["run_id"], "987654321")
self.assertEqual(results[0]["module_name"], "main")
self.assertEqual(results[0]["submodule_name"], "")
self.assertEqual(results[0]["resource_name"], "resource")
self.assertEqual(results[0]["action"], "destroy")
self.assertEqual(results[0]["time_taken_seconds"], 45)
mock_open_file.assert_called_once_with('/fake/path/terraform_destroy.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_destroy.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data="module.network.vnet: Creation complete after 1h5m15s\nmodule.network.subnet: Creation complete after 1m15s\nmodule.storage.bucket: Creation complete after 30s\n")
def test_process_terraform_logs_with_multiple_log_lines(self, mock_open_file, mock_isfile):
os.environ["RUN_ID"] = "1122334455"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="perf-eval",
_scenario_name="test_scenario_name",
)
self.assertEqual(len(results), 3)
self.assertEqual(results[0]["run_id"], "1122334455")
self.assertEqual(results[0]["module_name"], "network")
self.assertEqual(results[0]["submodule_name"], "")
self.assertEqual(results[0]["resource_name"], "vnet")
self.assertEqual(results[0]["action"], "apply")
self.assertEqual(results[0]["time_taken_seconds"], 3915)
self.assertEqual(results[0]["scenario_type"], "perf-eval")
self.assertEqual(results[0]["scenario_name"], "test_scenario_name")
self.assertEqual(results[1]["run_id"], "1122334455")
self.assertEqual(results[1]["module_name"], "network")
self.assertEqual(results[1]["submodule_name"], "")
self.assertEqual(results[1]["resource_name"], "subnet")
self.assertEqual(results[1]["action"], "apply")
self.assertEqual(results[1]["time_taken_seconds"], 75)
self.assertEqual(results[1]["scenario_type"], "perf-eval")
self.assertEqual(results[1]["scenario_name"], "test_scenario_name")
self.assertEqual(results[2]["run_id"], "1122334455")
self.assertEqual(results[2]["module_name"], "storage")
self.assertEqual(results[2]["submodule_name"], "")
self.assertEqual(results[2]["resource_name"], "bucket")
self.assertEqual(results[2]["action"], "apply")
self.assertEqual(results[2]["time_taken_seconds"], 30)
self.assertEqual(results[2]["scenario_type"], "perf-eval")
self.assertEqual(results[2]["scenario_name"], "test_scenario_name")
mock_open_file.assert_called_once_with('/fake/path/terraform_apply.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data="module.invalid.log.line: Invalid complete after 10m\n")
def test_process_terraform_logs_with_invalid_logs_format(self, mock_open_file, mock_isfile):
os.environ["RUN_ID"] = "5566778899"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="test_scenario_type",
_scenario_name="test_scenario_name",
)
self.assertEqual(len(results), 0)
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
mock_open_file.assert_called_once_with('/fake/path/terraform_apply.log', 'r', encoding='utf-8')
@patch("os.path.isfile", return_value=False)
def test_process_terraform_logs_with_missing_file(self, mock_isfile):
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="test_scenario_type",
_scenario_name="test_scenario_name",
)
self.assertEqual(results, [])
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data="invalid log line\n")
def test_process_terraform_logs_with_invalid_log_line(self, mock_open_file, mock_isfile):
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="test_scenario_type",
_scenario_name="test_scenario_name",
)
self.assertEqual(results, [])
mock_open_file.assert_called_once_with('/fake/path/terraform_apply.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=(
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Creating...\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [10m0s elapsed]\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... 20m0s elapsed]\n"
"│ Error: Failed to create/update resource\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Creating...\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [15m0s elapsed]\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... 25m30s elapsed]\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Creating...\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [5m0s elapsed]\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... 30m45s elapsed]\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Creation complete after 30m55s [id=/subscriptions/b8ceb4e5-f05b-4562-a9f5-14acb1f24219/resourceGroups/57202-578c08a2/providers/Microsoft.ContainerService/managedClusters/ccp-provisioning-H4]\n"
))
def test_process_terraform_logs_with_failed_run_and_retries(self, mock_open_file, mock_isfile):
os.environ["RUN_ID"] = "9988776655"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="perf-eval",
_scenario_name="test_scenario_name",
)
self.assertEqual(len(results), 3)
# First failed run - last elapsed was 20m0s = 1200s
self.assertEqual(results[0]["module_name"], "azapi[\"ccp\"]")
self.assertEqual(results[0]["resource_name"], "aks_cluster")
self.assertEqual(results[0]["time_taken_seconds"], 1200)
self.assertEqual(results[0]["result"], {"success": False, "timed_out": False})
# Second failed run (retry 1) - last elapsed was 25m30s = 1530s
self.assertEqual(results[1]["module_name"], "azapi[\"ccp\"]")
self.assertEqual(results[1]["resource_name"], "aks_cluster")
self.assertEqual(results[1]["time_taken_seconds"], 1530)
self.assertEqual(results[1]["result"], {"success": False, "timed_out": False})
# Third failed run (retry 2) - last elapsed was 30m45s = 1845s
self.assertEqual(results[2]["module_name"], "azapi[\"ccp\"]")
self.assertEqual(results[2]["resource_name"], "aks_cluster")
self.assertEqual(results[2]["time_taken_seconds"], 1855)
self.assertEqual(results[2]["result"], {"success": True, "timed_out": False})
mock_open_file.assert_called_once_with('/fake/path/terraform_apply.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=(
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Creating...\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [5m0s elapsed]\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [10m0s elapsed]\n"
"creating/updating Resource: (ResourceId\n"
"/subscriptions/b8ceb4e5/resourceGroups/58296/providers/Microsoft.ContainerService/managedClusters/ccp-H8\n"
"│ Error: creating/updating Resource: context deadline exceeded\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Creating...\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [5m0s elapsed]\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Creation complete after 8m30s [id=/subscriptions/b8ceb4e5/resourceGroups/58296/providers/Microsoft.ContainerService/managedClusters/ccp-H8]\n"
))
def test_process_terraform_logs_with_timeout_and_retry(self, mock_open_file, mock_isfile):
os.environ["RUN_ID"] = "1234567890"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="perf-eval",
_scenario_name="test_scenario_name",
)
self.assertEqual(len(results), 2)
# First run timed out
self.assertEqual(results[0]["module_name"], "azapi[\"ccp\"]")
self.assertEqual(results[0]["resource_name"], "aks_cluster")
self.assertEqual(results[0]["time_taken_seconds"], 600)
self.assertEqual(results[0]["result"], {"success": False, "timed_out": True})
# Retry succeeded
self.assertEqual(results[1]["module_name"], "azapi[\"ccp\"]")
self.assertEqual(results[1]["resource_name"], "aks_cluster")
self.assertEqual(results[1]["time_taken_seconds"], 510)
self.assertEqual(results[1]["result"], {"success": True, "timed_out": False})
mock_open_file.assert_called_once_with('/fake/path/terraform_apply.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=(
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Creating...\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [5m0s elapsed]\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [10m0s elapsed]\n"
"│ Error: Failed to create/update resource\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Creating...\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [5m0s elapsed]\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [10m0s elapsed]\n"
"│ Error: creating/updating Resource: context deadline exceeded\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Creating...\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [5m0s elapsed]\n"
"module.azapi[\"ccp\"].azapi_resource.aks_cluster: Still creating... [10m0s elapsed]\n"
"│ Error: creating/updating Resource: context deadline exceeded\n"
))
def test_process_terraform_logs_with_all_retries_failed(self, mock_open_file, mock_isfile):
os.environ["RUN_ID"] = "1112223334"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="perf-eval",
_scenario_name="test_scenario_name",
)
self.assertEqual(len(results), 3)
# First run failed with a non-timeout error
self.assertEqual(results[0]["run_id"], "1112223334")
self.assertEqual(results[0]["module_name"], "azapi[\"ccp\"]")
self.assertEqual(results[0]["resource_name"], "aks_cluster")
self.assertEqual(results[0]["time_taken_seconds"], 600)
self.assertEqual(results[0]["result"], {"success": False, "timed_out": False})
# Second run timed out
self.assertEqual(results[1]["run_id"], "1112223334")
self.assertEqual(results[1]["module_name"], "azapi[\"ccp\"]")
self.assertEqual(results[1]["resource_name"], "aks_cluster")
self.assertEqual(results[1]["time_taken_seconds"], 600)
self.assertEqual(results[1]["result"], {"success": False, "timed_out": True})
# Third run timed out
self.assertEqual(results[2]["run_id"], "1112223334")
self.assertEqual(results[2]["module_name"], "azapi[\"ccp\"]")
self.assertEqual(results[2]["resource_name"], "aks_cluster")
self.assertEqual(results[2]["time_taken_seconds"], 600)
self.assertEqual(results[2]["result"], {"success": False, "timed_out": True})
mock_open_file.assert_called_once_with('/fake/path/terraform_apply.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=(
'module.azapi["ccp-provisioning-H2"].azapi_resource.aks_cluster: Destroying... [id=/subscriptions/b8ceb4e5-f05b-4562-a9f5-14acb1f24219/resourceGroups/59393-51f48219/providers/Microsoft.ContainerService/managedClusters/ccp-provisioning-H2]\n'
'module.azapi["ccp-provisioning-H2"].azapi_resource.aks_cluster: Still destroying... [id=/subscriptions/b8ceb4e5-f05b-4562-a9f5-...ce/managedClusters/ccp-provisioning-H2, 00m10s elapsed]\n'
))
def test_process_terraform_logs_still_destroying_with_id_prefix(self, mock_open_file, mock_isfile):
"""Verify metadata record is created when 'Still destroying' line contains id= prefix before elapsed time."""
os.environ["RUN_ID"] = "4455667788"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="destroy",
_scenario_type="ccp",
_scenario_name="provisioning",
)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]["run_id"], "4455667788")
self.assertEqual(results[0]["module_name"], 'azapi["ccp-provisioning-H2"]')
self.assertEqual(results[0]["submodule_name"], "azapi_resource")
self.assertEqual(results[0]["resource_name"], "aks_cluster")
self.assertEqual(results[0]["action"], "destroy")
self.assertEqual(results[0]["time_taken_seconds"], 10)
self.assertEqual(results[0]["result"], {"success": False, "timed_out": False})
mock_open_file.assert_called_once_with('/fake/path/terraform_destroy.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_destroy.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=(
'module.azapi["ccp-provisioning-H2"].azapi_resource.aks_cluster: Creating... [id=/subscriptions/b8ceb4e5-f05b-4562-a9f5-14acb1f24219/resourceGroups/59393-51f48219/providers/Microsoft.ContainerService/managedClusters/ccp-provisioning-H2]\n'
'module.azapi["ccp-provisioning-H2"].azapi_resource.aks_cluster: Still creating... [id=/subscriptions/b8ceb4e5-f05b-4562-a9f5-...ce/managedClusters/ccp-provisioning-H2, 10m0s elapsed]\n'
'module.azapi["ccp-provisioning-H2"].azapi_resource.aks_cluster: Still creating... [id=/subscriptions/b8ceb4e5-f05b-4562-a9f5-...ce/managedClusters/ccp-provisioning-H2, 20m0s elapsed]\n'
'module.azapi["ccp-provisioning-H2"].azapi_resource.aks_cluster: Creation complete after 25m30s [id=/subscriptions/b8ceb4e5-f05b-4562-a9f5-14acb1f24219/resourceGroups/59393-51f48219/providers/Microsoft.ContainerService/managedClusters/ccp-provisioning-H2]\n'
))
def test_process_terraform_logs_still_creating_with_id_prefix_then_complete(self, mock_open_file, mock_isfile):
"""Verify that 'Still creating' lines with id= prefix are parsed and final completion record is used."""
os.environ["RUN_ID"] = "5566778899"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="ccp",
_scenario_name="provisioning",
)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]["run_id"], "5566778899")
self.assertEqual(results[0]["module_name"], 'azapi["ccp-provisioning-H2"]')
self.assertEqual(results[0]["submodule_name"], "azapi_resource")
self.assertEqual(results[0]["resource_name"], "aks_cluster")
self.assertEqual(results[0]["action"], "apply")
self.assertEqual(results[0]["time_taken_seconds"], 1530)
self.assertEqual(results[0]["result"], {"success": True, "timed_out": False})
mock_open_file.assert_called_once_with('/fake/path/terraform_apply.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=(
'module.azapi["ccp"].azapi_resource.aks_cluster: Destroying... [id=/subscriptions/b8ceb4e5/resourceGroups/59393/providers/Microsoft.ContainerService/managedClusters/ccp-H2]\n'
'│ Error: deleting Resource: unexpected status 409\n'
))
def test_process_terraform_logs_failure_without_elapsed_line(self, mock_open_file, mock_isfile):
"""Verify a failure record with 0s elapsed is created when there is no 'Still destroying' line."""
os.environ["RUN_ID"] = "6677889900"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="destroy",
_scenario_type="ccp",
_scenario_name="provisioning",
)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]["run_id"], "6677889900")
self.assertEqual(results[0]["module_name"], 'azapi["ccp"]')
self.assertEqual(results[0]["submodule_name"], "azapi_resource")
self.assertEqual(results[0]["resource_name"], "aks_cluster")
self.assertEqual(results[0]["action"], "destroy")
self.assertEqual(results[0]["time_taken_seconds"], 0)
self.assertEqual(results[0]["result"], {"success": False, "timed_out": False})
mock_open_file.assert_called_once_with('/fake/path/terraform_destroy.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_destroy.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=(
'module.azapi["ccp"].azapi_resource.aks_cluster: Creating...\n'
'│ Error: creating Resource: context deadline exceeded\n'
))
def test_process_terraform_logs_timeout_without_elapsed_line(self, mock_open_file, mock_isfile):
"""Verify a failure record with timed_out=True and 0s elapsed is created when timeout occurs without elapsed lines."""
os.environ["RUN_ID"] = "7788990011"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="ccp",
_scenario_name="provisioning",
)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]["run_id"], "7788990011")
self.assertEqual(results[0]["module_name"], 'azapi["ccp"]')
self.assertEqual(results[0]["resource_name"], "aks_cluster")
self.assertEqual(results[0]["action"], "apply")
self.assertEqual(results[0]["time_taken_seconds"], 0)
self.assertEqual(results[0]["result"], {"success": False, "timed_out": True})
mock_open_file.assert_called_once_with('/fake/path/terraform_apply.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
@patch("os.path.isfile", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=(
'module.azapi["ccp"].azapi_resource.aks_cluster: Creating...\n'
'│ Error: creating Resource: unexpected status 409\n'
'module.azapi["ccp"].azapi_resource.aks_cluster: Creating...\n'
'module.azapi["ccp"].azapi_resource.aks_cluster: Still creating... [5m0s elapsed]\n'
'module.azapi["ccp"].azapi_resource.aks_cluster: Creation complete after 8m30s [id=/subscriptions/b8ceb4e5/resourceGroups/59393/providers/Microsoft.ContainerService/managedClusters/ccp-H2]\n'
))
def test_process_terraform_logs_retry_after_immediate_failure(self, mock_open_file, mock_isfile):
"""Verify first run records 0s failure when it fails immediately, and retry succeeds normally."""
os.environ["RUN_ID"] = "8899001122"
results = process_terraform_logs(
log_path="/fake/path",
_command_type="apply",
_scenario_type="ccp",
_scenario_name="provisioning",
)
self.assertEqual(len(results), 2)
# First run failed immediately - no elapsed time
self.assertEqual(results[0]["module_name"], 'azapi["ccp"]')
self.assertEqual(results[0]["resource_name"], "aks_cluster")
self.assertEqual(results[0]["time_taken_seconds"], 0)
self.assertEqual(results[0]["result"], {"success": False, "timed_out": False})
# Retry succeeded
self.assertEqual(results[1]["module_name"], 'azapi["ccp"]')
self.assertEqual(results[1]["resource_name"], "aks_cluster")
self.assertEqual(results[1]["time_taken_seconds"], 510)
self.assertEqual(results[1]["result"], {"success": True, "timed_out": False})
mock_open_file.assert_called_once_with('/fake/path/terraform_apply.log', 'r', encoding='utf-8')
mock_isfile.assert_called_once_with("/fake/path/terraform_apply.log")
if __name__ == "__main__":
unittest.main()