1
+ import pkg .types .types as types
2
+ import pkg .aws_az .types .types as experimentDetails
3
+ import pkg .aws_az .environment .environment as experimentEnv
4
+ import pkg .events .events as events
5
+ import logging
6
+ import chaosLib .litmus .aws_az_chaos .lib .aws_az_chaos as litmusLIB
7
+ import pkg .result .chaosresult as chaosResults
8
+ import pkg .utils .common .common as common
9
+ import pkg .aws_status .status as awsStatus
10
+
11
+ # AwsAzExperiment contains steps to inject chaos
12
+ def AwsAzExperiment (clients ):
13
+
14
+ # Initialising expermentDetails, resultDetails, eventsDetails, chaosDetails, status and result objects
15
+ experimentsDetails = experimentDetails .ExperimentDetails ()
16
+ resultDetails = types .ResultDetails ()
17
+ eventsDetails = types .EventDetails ()
18
+ chaosDetails = types .ChaosDetails ()
19
+ result = chaosResults .ChaosResults ()
20
+ statusAws = awsStatus .AWS_AZ ()
21
+
22
+ #Fetching all the ENV passed from the runner pod
23
+ experimentEnv .GetENV (experimentsDetails )
24
+
25
+ logging .info ("[PreReq]: Initialise Chaos Variables for the %s experiment" , experimentsDetails .ExperimentName )
26
+
27
+ # Intialise the chaos attributes
28
+ experimentEnv .InitialiseChaosVariables (chaosDetails , experimentsDetails )
29
+
30
+ # Intialise Chaos Result Parameters
31
+ types .SetResultAttributes (resultDetails , chaosDetails )
32
+
33
+ #Updating the chaos result in the beginning of experiment
34
+ logging .info ("[PreReq]: Updating the chaos result of %s experiment (SOT)" ,(experimentsDetails .ExperimentName ))
35
+ err = result .ChaosResult (chaosDetails , resultDetails , "SOT" , clients )
36
+ if err != None :
37
+ logging .error ("Unable to Create the Chaos Result, err: %s" ,(err ))
38
+ failStep = "Updating the chaos result of aws-az-chaos experiment (SOT)"
39
+ result .RecordAfterFailure (chaosDetails , resultDetails , failStep , eventsDetails , clients )
40
+ return
41
+
42
+ # Set the chaos result uid
43
+ result .SetResultUID (resultDetails , chaosDetails , clients )
44
+
45
+ # generating the event in chaosresult to marked the verdict as awaited
46
+ msg = "Experiment " + experimentsDetails .ExperimentName + ", Result Awaited"
47
+ types .SetResultEventAttributes (eventsDetails , types .AwaitedVerdict , msg , "Normal" , resultDetails )
48
+ events .GenerateEvents (eventsDetails , chaosDetails , "ChaosResult" , clients )
49
+
50
+ # DISPLAY THE LOADBALANCER INFORMATION
51
+ logging .info ("[Info]: The application information is as follows LoadBalancer Name=%s, LoadBalancer Zones=%s, Ramp Time=%s" ,experimentsDetails .LoadBalancerName ,experimentsDetails .LoadBalancerZones ,experimentsDetails .RampTime )
52
+
53
+ # Calling AbortWatcher, it will continuously watch for the abort signal and generate the required and result
54
+ common .AbortWatcher (experimentsDetails .ExperimentName , resultDetails , chaosDetails , eventsDetails , clients )
55
+
56
+ # PRE-CHAOS APPLICATION STATUS CHECK
57
+ logging .info ("[Status]: Verify that the AUT (Application Under Test) is running (pre-chaos)" )
58
+ err = statusAws .CheckAWSStatus (experimentsDetails )
59
+ if err != None :
60
+ logging .error ("Target available zone status check failed, err: %s" , err )
61
+ failStep = "Verify that the AUT (Application Under Test) is running (pre-chaos)"
62
+ result .RecordAfterFailure (chaosDetails , resultDetails , failStep , eventsDetails , clients )
63
+ return
64
+
65
+ if experimentsDetails .EngineName != "" :
66
+ # marking AUT as running, as we already checked the status of application under test
67
+ msg = "AUT: Running"
68
+ # generating the for the pre-chaos check
69
+ types .SetEngineEventAttributes (eventsDetails , types .PreChaosCheck , msg , "Normal" , chaosDetails )
70
+ events .GenerateEvents (eventsDetails , chaosDetails , "ChaosEngine" , clients )
71
+
72
+ # Including the litmus lib for aws-az-chaos
73
+ if experimentsDetails .ChaosLib == "litmus" :
74
+ err = litmusLIB .PrepareAWSAZExperiment (experimentsDetails , resultDetails , eventsDetails , chaosDetails , clients , statusAws )
75
+ if err != None :
76
+ logging .error ("Chaos injection failed, err: %s" ,(err ))
77
+ failStep = "failed in chaos injection phase"
78
+ result .RecordAfterFailure (chaosDetails , resultDetails , failStep , eventsDetails , clients )
79
+ return
80
+
81
+ else :
82
+ logging .info ("[Invalid]: Please Provide the correct LIB" )
83
+ failStep = "no match found for specified lib"
84
+ result .RecordAfterFailure (chaosDetails , resultDetails , failStep , eventsDetails , clients )
85
+ return
86
+
87
+ logging .info ("[Confirmation]: %s chaos has been injected successfully" , experimentsDetails .ExperimentName )
88
+ resultDetails .Verdict = "Pass"
89
+
90
+ # POST-CHAOS APPLICATION STATUS CHECK
91
+ logging .info ("[Status]: Verify that the AUT (Application Under Test) is running (post-chaos)" )
92
+ err = statusAws .CheckAWSStatus (experimentsDetails )
93
+ if err != None :
94
+ logging .error ("Target aws instance status check failed, err: %s" , err )
95
+ failStep = "Verify that the AUT (Application Under Test) is running (post-chaos)"
96
+ result .RecordAfterFailure (chaosDetails , resultDetails , failStep , eventsDetails , clients )
97
+ return
98
+
99
+
100
+ if experimentsDetails .EngineName != "" :
101
+ # marking AUT as running, as we already checked the status of application under test
102
+ msg = "AUT: Running"
103
+
104
+ # generating post chaos event
105
+ types .SetEngineEventAttributes (eventsDetails , types .PostChaosCheck , msg , "Normal" , chaosDetails )
106
+ events .GenerateEvents (eventsDetails , chaosDetails , "ChaosEngine" , clients )
107
+
108
+
109
+ #Updating the chaosResult in the end of experiment
110
+ logging .info ("[The End]: Updating the chaos result of %s experiment (EOT)" , experimentsDetails .ExperimentName )
111
+ err = result .ChaosResult (chaosDetails , resultDetails , "EOT" , clients )
112
+ if err != None :
113
+ logging .error ("Unable to Update the Chaos Result, err: %s" , err )
114
+ return
115
+
116
+ # generating the event in chaosresult to marked the verdict as pass/fail
117
+ msg = "Experiment " + experimentsDetails .ExperimentName + ", Result " + resultDetails .Verdict
118
+ reason = types .PassVerdict
119
+ eventType = "Normal"
120
+ if resultDetails .Verdict != "Pass" :
121
+ reason = types .FailVerdict
122
+ eventType = "Warning"
123
+
124
+ types .SetResultEventAttributes (eventsDetails , reason , msg , eventType , resultDetails )
125
+ events .GenerateEvents (eventsDetails , chaosDetails , "ChaosResult" , clients )
126
+ if experimentsDetails .EngineName != "" :
127
+ msg = experimentsDetails .ExperimentName + " experiment has been " + resultDetails .Verdict + "ed"
128
+ types .SetEngineEventAttributes (eventsDetails , types .Summary , msg , "Normal" , chaosDetails )
129
+ events .GenerateEvents (eventsDetails , chaosDetails , "ChaosEngine" , clients )
0 commit comments