@@ -50,6 +50,7 @@ def __init__(self, test_obj, backend: backend_api.ControlPanelAPI, cluster, name
50
50
self .cluster = cluster
51
51
self .scenario_path = scenario_path
52
52
self .namespace = namespace
53
+ self .scenario_key = self .test_scenario
53
54
54
55
def __del__ (self ):
55
56
pass
@@ -100,6 +101,9 @@ def verify_scenario(self):
100
101
101
102
def verify_fix (self ):
102
103
raise Exception ("Not implemented" )
104
+
105
+ def construct_message (self ,message ):
106
+ return f"{ self .scenario_key } : cluster: { self .cluster } , namespace: { self .namespace } , message: { message } "
103
107
104
108
105
109
@@ -244,11 +248,15 @@ class SecurityRisksScenarioManager(ScenarioManager):
244
248
245
249
def __init__ (self , test_obj , backend : backend_api .ControlPanelAPI , cluster , namespace ):
246
250
super ().__init__ (test_obj , backend , cluster , namespace , SCENARIOS_TEST_PATH )
247
-
251
+ self .test_scenario = test_obj ["test_job" ][0 ].get ("test_scenario" , "not defined" )
252
+ self .fix_object = test_obj ["test_job" ][0 ].get ("fix_object" , "control" )
248
253
self .test_security_risk_ids = test_obj ["test_job" ][0 ].get ("security_risks_ids" , [])
254
+ self .scenario_key = self .test_scenario + " fix: " + self .fix_object + " security_risk_ids: " + ',' .join (self .test_security_risk_ids )
249
255
self .with_network_policy = test_obj ["test_job" ][0 ].get ("with_network_policy" , False )
256
+ Logger .logger .info (f"Generated ScenarioManager for { self .test_scenario } scenario on { self .cluster } cluster in { self .namespace } namespace" )
250
257
251
- def verify_scenario (self ):
258
+
259
+ def verify_scenario (self , timeout = 600 ):
252
260
"""
253
261
verify_scenario validate the security risks results on the backend
254
262
validations supported:
@@ -263,34 +271,34 @@ def verify_scenario(self):
263
271
Logger .logger .info ("validating security risks list" )
264
272
res = self .wait_for_report (
265
273
self .verify_security_risks_list ,
266
- timeout = 600 ,
267
- sleep_interval = 10
274
+ timeout = timeout ,
275
+ sleep_interval = 15
268
276
)
269
277
270
278
return res [0 ]
271
279
272
280
273
- def verify_fix (self ):
281
+ def verify_fix (self , timeout = 600 ):
274
282
"""
275
283
verify_fix validate the security risks fix results on the backend
276
284
"""
277
285
Logger .logger .info ("wait for response from BE" )
278
286
isEmpty , t = self .wait_for_report (
279
287
self .is_security_risk_empty ,
280
288
security_risk_ids = self .test_security_risk_ids ,
281
- timeout = 600 ,
282
- sleep_interval = 10
289
+ timeout = timeout ,
290
+ sleep_interval = 15
283
291
)
284
292
285
293
return isEmpty
286
294
287
- def apply_fix (self , fix_type ):
295
+ def apply_fix (self ):
288
296
"""
289
297
apply_fix apply the fix to the cluster
290
298
"""
291
- fix_command = os .path .join (self .scenario_path , self .test_scenario , "delete_deployments" ) + ' --namespace ' + self .namespace
299
+ fix_command = "bash " + os .path .join (self .scenario_path , self .test_scenario , "delete_deployments" ) + ' --namespace ' + self .namespace
292
300
TestUtil .run_command (command_args = fix_command , display_stdout = True , timeout = 300 )
293
- super ().apply_fix (fix_type )
301
+ # super().apply_fix(self.fix_object )
294
302
295
303
def get_exceptions_list (self ):
296
304
"""
@@ -459,7 +467,7 @@ def check_security_risks_resources_results(self, result, expected):
459
467
}
460
468
461
469
if 'total' in result and 'total' in expected :
462
- assert result ['total' ]['value' ] == expected ['total' ]['value' ], f"'Total' value mismatch: result: { result ['total' ]['value' ]} != expected: { expected ['total' ]['value' ]} "
470
+ assert result ['total' ]['value' ] == expected ['total' ]['value' ], self . construct_message ( f"'Total' value mismatch: result: { result ['total' ]['value' ]} != expected: { expected ['total' ]['value' ]} " )
463
471
464
472
465
473
if 'response' in result and 'response' in expected :
@@ -485,7 +493,7 @@ def check_security_risks_results(self, result, expected):
485
493
486
494
if len (result ['response' ]) != len (expected ['response' ]):
487
495
missingSecurityRiskIDs = self .find_missing_security_risks (result , expected )
488
- raise Exception (f"Length mismatch: result: { len (result ['response' ])} != expected: { len (expected ['response' ])} , missing security risks: { missingSecurityRiskIDs } " )
496
+ raise Exception (self . construct_message ( f"Length mismatch: result: { len (result ['response' ])} != expected: { len (expected ['response' ])} , missing security risks: { missingSecurityRiskIDs } " ) )
489
497
490
498
compare_lists (result ['response' ], expected ['response' ], ignore_keys )
491
499
@@ -499,10 +507,9 @@ def check_security_risks_categories(self, result, expected):
499
507
assert "total" in result , f"'Total' key not found in the result"
500
508
501
509
if 'total' in result and 'total' in expected :
502
- assert result ['total' ]['value' ] == expected ['total' ]['value' ], f"'Total' value mismatch: result: { result ['total' ]['value' ]} != expected: { expected ['total' ]['value' ]} "
510
+ assert result ['total' ]['value' ] == expected ['total' ]['value' ], self . construct_message ( f"'Total' value mismatch: result: { result ['total' ]['value' ]} != expected: { expected ['total' ]['value' ]} " )
503
511
504
-
505
- assert result ['response' ] == expected ['response' ], f"Security risks categories response differs from the expected one. Response: { result ['response' ]} , Expected: { expected ['response' ]} "
512
+ assert result ['response' ] == expected ['response' ], self .construct_message (f"Security risks categories response differs from the expected one. Response: { result ['response' ]} , Expected: { expected ['response' ]} " )
506
513
507
514
508
515
def check_security_risks_severities (self , result , expected ):
@@ -517,8 +524,8 @@ def check_security_risks_severities(self, result, expected):
517
524
if 'total' in result and 'total' in expected :
518
525
assert result ['total' ]['value' ] == expected ['total' ]['value' ], f"'Total' value mismatch: result: { result ['total' ]['value' ]} != expected: { expected ['total' ]['value' ]} "
519
526
520
- assert result ['response' ]['severityResourceCounter' ] == expected ['response' ]['severityResourceCounter' ], f"Security risks severities resource counter response differs from the expected one. Response: { result ['response' ]['severityResourceCounter' ]} , Expected: { expected ['response' ]['severityResourceCounter' ]} "
521
- assert result ['response' ]['totalResources' ] == expected ['response' ]['totalResources' ], f"Security risks severities total resources response differs from the expected one. Response: { result ['response' ]['totalResources' ]} , Expected: { expected ['response' ]['totalResources' ]} "
527
+ assert result ['response' ]['severityResourceCounter' ] == expected ['response' ]['severityResourceCounter' ], self . construct_message ( f"Security risks severities resource counter response differs from the expected one. Response: { result ['response' ]['severityResourceCounter' ]} , Expected: { expected ['response' ]['severityResourceCounter' ]} " )
528
+ assert result ['response' ]['totalResources' ] == expected ['response' ]['totalResources' ], self . construct_message ( f"Security risks severities total resources response differs from the expected one. Response: { result ['response' ]['totalResources' ]} , Expected: { expected ['response' ]['totalResources' ]} " )
522
529
523
530
524
531
@@ -548,7 +555,7 @@ def is_security_risk_resources_empty(self, security_risk_ids):
548
555
)
549
556
550
557
response = json .loads (r .text )
551
- assert response ['total' ]['value' ] == 0 , "Security risks resources found, expecting no security risks resources"
558
+ assert response ['total' ]['value' ] == 0 , self . construct_message ( "Security risks resources found, expecting no security risks resources" )
552
559
return True
553
560
554
561
def verify_security_risks_trends (self , expected_n_events_detected , expected_n_events_resolved , expected_current_detected , expected_change_from_beginning_of_period ):
@@ -563,12 +570,12 @@ def verify_security_risks_trends(self, expected_n_events_detected, expected_n_ev
563
570
564
571
Logger .logger .info ('comparing security risks trends result with expected ones' )
565
572
566
- assert len (response ['securityIssuesTrends' ]) == EXPECTED_TRENDS_DAYS_BACK , f"Security risks trends response differs from the expected one. Response: { len (response ['securityIssuesTrends' ])} != Expected: { EXPECTED_TRENDS_DAYS_BACK } "
567
-
568
- assert response ["totalDetectedForPeriod" ] == expected_n_events_detected , f"Security risks trends total detected for period response differs from the expected one. Response: { response ['totalDetectedForPeriod' ]} != Expected: { expected_n_events_detected } "
569
- assert response ["totalResolvedForPeriod" ] == expected_n_events_resolved , f"Security risks trends total resolved for period response differs from the expected one. Response: { response ['totalResolvedForPeriod' ]} != Expected: { expected_n_events_resolved } "
570
- assert response ["currentDetected" ] == expected_current_detected , f"Security risks trends current detected response differs from the expected one. Response: { response ['currentDetected' ]} != Expected: { expected_current_detected } "
571
- assert response ["changeFromBeginningOfPeriod" ] == expected_change_from_beginning_of_period , f"Security risks trends change from beginning of period response differs from the expected one. Response: { response ['changeFromBeginningOfPeriod' ]} != Expected: { expected_change_from_beginning_of_period } "
573
+ assert len (response ['securityIssuesTrends' ]) == EXPECTED_TRENDS_DAYS_BACK , self . construct_message ( f"Security risks trends response differs from the expected one. Response: { len (response ['securityIssuesTrends' ])} != Expected: { EXPECTED_TRENDS_DAYS_BACK } " )
574
+
575
+ assert response ["totalDetectedForPeriod" ] == expected_n_events_detected , self . construct_message ( f"Security risks trends total detected for period response differs from the expected one. Response: { response ['totalDetectedForPeriod' ]} != Expected: { expected_n_events_detected } " )
576
+ assert response ["totalResolvedForPeriod" ] == expected_n_events_resolved , self . construct_message ( f"Security risks trends total resolved for period response differs from the expected one. Response: { response ['totalResolvedForPeriod' ]} != Expected: { expected_n_events_resolved } " )
577
+ assert response ["currentDetected" ] == expected_current_detected , self . construct_message ( f"Security risks trends current detected response differs from the expected one. Response: { response ['currentDetected' ]} != Expected: { expected_current_detected } " )
578
+ assert response ["changeFromBeginningOfPeriod" ] == expected_change_from_beginning_of_period , self . construct_message ( f"Security risks trends change from beginning of period response differs from the expected one. Response: { response ['changeFromBeginningOfPeriod' ]} != Expected: { expected_change_from_beginning_of_period } " )
572
579
573
580
def verify_security_risks_list_uniquevalues (self , list_result ):
574
581
"""
@@ -597,7 +604,7 @@ def verify_security_risks_list_uniquevalues(self, list_result):
597
604
expected = summarize_uniquevalues (list_result , fieldName )
598
605
response = json .loads (r .text )
599
606
600
- assert response == expected , f"verify_security_risks_list_uniquevalues - security risks unique values for '{ fieldName } ' response differs from the expected one. Response: { response } , Expected: { expected } "
607
+ assert response == expected , self . construct_message ( f" security risks unique values for '{ fieldName } ' response differs from the expected one. Response: { response } , Expected: { expected } ")
601
608
602
609
603
610
def verify_security_risks_severities (self , list_result ):
@@ -614,7 +621,10 @@ def verify_security_risks_severities(self, list_result):
614
621
response = json .loads (r .text )
615
622
616
623
Logger .logger .info ('comparing security risks severities result with expected ones' )
617
- self .check_security_risks_severities (response , expected )
624
+ try :
625
+ self .check_security_risks_severities (response , expected )
626
+ except Exception as e :
627
+ raise Exception (self .construct_message (f"Failed to validate security risks severities: { e } , response: { response } , expected: { expected } " ))
618
628
619
629
def verify_security_risks_categories (self , list_result ):
620
630
"""
@@ -630,26 +640,32 @@ def verify_security_risks_categories(self, list_result):
630
640
response = json .loads (r .text )
631
641
632
642
Logger .logger .info ('comparing security risks categories result with expected ones' )
633
- self .check_security_risks_categories (response , expected )
643
+ try :
644
+ self .check_security_risks_categories (response , expected )
645
+ except Exception as e :
646
+ raise Exception (self .construct_message (f"Failed to validate security risks categories: { e } , response: { response } , expected: { expected } " ))
634
647
635
648
636
649
def verify_security_risks_list (self ):
637
650
"""
638
651
verify_security_risks_list validate the security risks results on the backend
639
652
"""
640
653
# current_datetime = datetime.now(timezone.utc)
641
- Logger .logger .info ("wait for response from BE " )
654
+ Logger .logger .info (f"getting security risks list for { self . test_security_risk_ids } , cluster: { self . cluster } , namespace: { self . namespace } " )
642
655
r = self .backend .get_security_risks_list (self .cluster , self .namespace , self .test_security_risk_ids )
643
656
644
- Logger .logger .info (' loading security risks scenario to validate it' )
657
+ Logger .logger .info (f" loading security risks scenario_key { self . scenario_key } to validate it" )
645
658
f = open (os .path .join (SCENARIOS_EXPECTED_VALUES , self .test_scenario + '_security-risks-list.json' ))
646
659
expected = json .load (f )
647
660
response = json .loads (r .text )
648
661
649
662
filtered_expected = filter_security_risks_list (expected , self .test_security_risk_ids )
650
663
651
- Logger .logger .info ('comparing security risks result with expected ones' )
652
- self .check_security_risks_results (response , filtered_expected )
664
+ Logger .logger .info (self .construct_message ("comparing security risks result with expected ones" ))
665
+ try :
666
+ self .check_security_risks_results (response , filtered_expected )
667
+ except Exception as e :
668
+ raise Exception (self .construct_message (f"Failed to validate security risks list: { e } , response: { response } , expected: { filtered_expected } " ))
653
669
654
670
return response
655
671
@@ -661,16 +677,19 @@ def verify_security_risks_resources(self):
661
677
662
678
for security_risk_id , expectedPrefix in SECURITY_RISKS_RESOURCES_PREFIX .items ():
663
679
if security_risk_id in self .test_security_risk_ids :
664
- Logger .logger .info (f"wait for response from BE with filter: { security_risk_id } " )
680
+ Logger .logger .info (self . construct_message ( f"getting security risks resources" ) )
665
681
r = self .backend .get_security_risks_resources (self .cluster , self .namespace , security_risk_id )
666
682
667
683
Logger .logger .info ('loading security risks scenario to validate it' )
668
684
f = open (os .path .join (SCENARIOS_EXPECTED_VALUES , self .test_scenario + expectedPrefix + '.json' ))
669
685
expected = json .load (f )
670
686
response = json .loads (r .text )
671
687
672
- Logger .logger .info ('comparing security risks result with expected ones' )
673
- self .check_security_risks_resources_results (response , expected )
688
+ Logger .logger .info (self .construct_message (f"comparing security risks resources result with expected ones" ))
689
+ try :
690
+ self .check_security_risks_resources_results (response , expected )
691
+ except Exception as e :
692
+ raise Exception (self .construct_message (f"Failed to validate security risks resources for scenario_key : { self .scenario_key } , security risk id: { security_risk_id } : { e } , response: { response } , expected: { expected } " ))
674
693
675
694
676
695
def find_missing_security_risks (self , result , expected ):
0 commit comments