Change rb-definition-version identifier
[testsuite/pythonsdk-tests.git] / tests / test_store_state.py
1 from time import sleep
2
3 import pytest
4
5 from onaptests.steps.base import BaseStep
6 from onaptests.utils.exceptions import OnapTestException
7
8
9
10 class TestStep(BaseStep):
11
12     @BaseStep.store_state
13     def execute(self):
14         return super().execute()
15
16     @BaseStep.store_state(cleanup=True)
17     def cleanup(self) -> None:
18         return super().cleanup()
19
20     @property
21     def description(self):
22         return "Test pass step"
23
24     @property
25     def component(self) -> str:
26         return "Test"
27
28
29 class TestFailStep(BaseStep):
30
31     @BaseStep.store_state
32     def execute(self):
33         super().execute()
34         raise OnapTestException
35
36     @BaseStep.store_state(cleanup=True)
37     def cleanup(self) -> None:
38         raise OnapTestException
39
40     @property
41     def description(self):
42         return "Test fail step"
43
44     @property
45     def component(self) -> str:
46         return "Test"
47
48
49 class TestOneSecStep(BaseStep):
50
51     @BaseStep.store_state
52     def execute(self):
53         super().execute()
54         sleep(1)
55
56     @property
57     def description(self):
58         return "One second test step"
59
60     @property
61     def component(self) -> str:
62         return "Test"
63
64
65 class TestStepNoSuperExecute(BaseStep):
66
67     @BaseStep.store_state
68     def execute(self):
69         sleep(1)
70
71     @property
72     def description(self):
73         return "One second test step - no super execute call"
74
75     @property
76     def component(self) -> str:
77         return "Test"
78
79
80 class TestCleanupStepA(BaseStep):
81
82     @BaseStep.store_state
83     def execute(self):
84         return super().execute()
85
86     @BaseStep.store_state(cleanup=True)
87     def cleanup(self):
88         return super().cleanup()
89
90     @property
91     def description(self):
92         return "Test cleanup step A"
93
94     @property
95     def component(self) -> str:
96         return "Test"
97
98
99 class TestCleanupStepB(TestCleanupStepA):
100
101     @property
102     def description(self):
103         return "Test cleanup step B"
104
105
106 class TestCleanupStepC(TestCleanupStepA):
107
108     @property
109     def description(self):
110         return "Test cleanup step C"
111
112
113 class TestCleanupStepD(TestCleanupStepA):
114
115     @property
116     def description(self):
117         return "Test cleanup step D"
118
119
120 def test_store_state():
121     ts = TestStep()
122     ts.execute()
123     assert len(ts.reports_collection.report) == 1
124     rep = ts.reports_collection.report[0]
125     assert rep.step_description == "[Test] TestStep: Test pass step"
126     assert rep.step_execution_status.value == "PASS"
127     assert rep.step_execution_duration != 0
128
129     fs = TestFailStep()
130     fs.add_step(TestStep())
131     with pytest.raises(Exception):
132         fs.execute()
133     rep_f, rep_s = fs.reports_collection.report
134     assert rep_f.step_description == "[Test] TestFailStep: Test fail step"
135     assert rep_f.step_execution_status.value == "FAIL"
136     assert rep_f.step_execution_duration != 0
137
138     assert rep_s.step_description == "[Test] TestStep: Test pass step"
139     assert rep_s.step_execution_status.value == "PASS"
140     assert rep_s.step_execution_duration != 0
141
142     ts = TestStep(cleanup=True)
143     ts.add_step(TestFailStep(cleanup=True))
144     with pytest.raises(Exception):
145         ts.execute()
146     with pytest.raises(Exception):
147         ts.cleanup()
148     assert len(ts.reports_collection.report) == 4
149     cln_rep_f, cln_rep_s, rep_s, rep_f = ts.reports_collection.report
150     assert rep_f.step_description == "[Test] TestFailStep: Test fail step"
151     assert rep_f.step_execution_status.value == "FAIL"
152     assert rep_f.step_execution_duration != 0
153
154     assert rep_s.step_description == "[Test] TestStep: Test pass step"
155     assert rep_s.step_execution_status.value == "NOT EXECUTED"
156     assert rep_s.step_execution_duration != 0
157
158     assert cln_rep_s.step_description == "[Test] TestStep cleanup: Test pass step"
159     assert cln_rep_s.step_execution_status.value == "PASS"
160     assert cln_rep_s.step_execution_duration != 0
161
162     assert cln_rep_f.step_description == "[Test] TestFailStep cleanup: Test fail step"
163     assert cln_rep_f.step_execution_status.value == "FAIL"
164     assert cln_rep_f.step_execution_duration != 0
165
166     ts = TestStep(cleanup=True)
167     tsf = TestFailStep(cleanup=True)
168     tsf.add_step(TestStep(cleanup=True))
169     ts.add_step(tsf)
170     ts.add_step(TestStep(cleanup=True))
171     with pytest.raises(Exception):
172         ts.execute()
173     with pytest.raises(Exception):
174         ts.cleanup()
175
176     assert len(ts.reports_collection.report) == 5
177     cln_2, cln_1, exec_3, exec_2, exec_1 = ts.reports_collection.report
178
179     assert exec_1.step_description == "[Test] TestStep: Test pass step"
180     assert exec_1.step_execution_status.value == "PASS"
181     assert exec_1.step_execution_duration != 0
182
183     assert exec_2.step_description == "[Test] TestFailStep: Test fail step"
184     assert exec_2.step_execution_status.value == "FAIL"
185     assert exec_2.step_execution_duration != 0
186
187     assert exec_3.step_description == "[Test] TestStep: Test pass step"
188     assert exec_3.step_execution_status.value == "NOT EXECUTED"
189     assert exec_3.step_execution_duration != 0
190
191     assert cln_1.step_description == "[Test] TestStep cleanup: Test pass step"
192     assert cln_1.step_execution_status.value == "PASS"
193     assert cln_1.step_execution_duration != 0
194
195     assert cln_2.step_description == "[Test] TestFailStep cleanup: Test fail step"
196     assert cln_2.step_execution_status.value == "FAIL"
197     assert cln_2.step_execution_duration != 0
198
199
200 def test_store_state_time_measurement():
201
202     ts = TestOneSecStep()
203     ts.execute()
204     assert len(ts.reports_collection.report) == 1
205     rep = ts.reports_collection.report[0]
206     assert rep.step_execution_duration > 1
207
208     ts = TestOneSecStep()
209     ts.add_step(TestOneSecStep())
210     ts.execute()
211     assert len(ts.reports_collection.report) == 2
212     rep_one, rep_two = ts.reports_collection.report
213     assert rep_one.step_execution_duration > 1 and rep_one.step_execution_duration < 2
214     assert rep_two.step_execution_duration > 1 and rep_two.step_execution_duration < 2
215
216     ts = TestStepNoSuperExecute()
217     ts.execute()
218     assert len(ts.reports_collection.report) == 1
219     rep = ts.reports_collection.report[0]
220     assert rep.step_execution_duration < 1
221
222
223 def test_store_state_with_cleanup():
224
225     ts = TestCleanupStepA(cleanup=True)
226     ts_b = TestCleanupStepB(cleanup=True)
227     ts_b.add_step(TestCleanupStepC(cleanup=True))
228     ts.add_step(ts_b)
229     ts.add_step(TestCleanupStepD(cleanup=True))
230     ts.execute()
231     ts.cleanup()
232     assert len(ts.reports_collection.report) == 8
233     (rep_cleanup_step_4, rep_cleanup_step_3, rep_cleanup_step_2, rep_cleanup_step_1,
234         rep_exec_step_4, rep_exec_step_3, rep_exec_step_2, rep_exec_step_1) = ts.reports_collection.report
235     assert rep_exec_step_1.step_description == "[Test] TestCleanupStepC: Test cleanup step C"
236     assert rep_exec_step_2.step_description == "[Test] TestCleanupStepB: Test cleanup step B"
237     assert rep_exec_step_3.step_description == "[Test] TestCleanupStepD: Test cleanup step D"
238     assert rep_exec_step_4.step_description == "[Test] TestCleanupStepA: Test cleanup step A"
239     assert rep_cleanup_step_1.step_description == "[Test] TestCleanupStepA cleanup: Test cleanup step A"
240     assert rep_cleanup_step_2.step_description == "[Test] TestCleanupStepB cleanup: Test cleanup step B"
241     assert rep_cleanup_step_3.step_description == "[Test] TestCleanupStepC cleanup: Test cleanup step C"
242     assert rep_cleanup_step_4.step_description == "[Test] TestCleanupStepD cleanup: Test cleanup step D"