How to use run_and_assert_calculate_alarm_state method in localstack

Best Python code snippet using localstack_python

test_cloudwatch.py

Source:test_cloudwatch.py Github

copy

Full Screen

...99 if metric_data == self.TEST_4_DATA:100 return [3.0, 1.5, 1.0, None, 1.5]101 if metric_data == self.TEST_5_DATA:102 return [None, None, 1.0, None, None]103 run_and_assert_calculate_alarm_state(104 mock_metric_alarm_details, mock_collect_metric_data, expected_calls, expected_state105 )106 @pytest.mark.parametrize(107 "initial_state,expected_state,expected_calls,treat_missing,metric_data",108 [109 # 0 - X - X110 ("OK", "ALARM", 1, "missing", TEST_1_M_OF_N),111 ("OK", "ALARM", 1, "ignore", TEST_1_M_OF_N),112 ("OK", "ALARM", 1, "breaching", TEST_1_M_OF_N),113 ("OK", "ALARM", 1, "notBreaching", TEST_1_M_OF_N),114 # 0 0 X 0 X115 ("OK", "ALARM", 1, "missing", TEST_2_M_OF_N),116 ("OK", "ALARM", 1, "ignore", TEST_2_M_OF_N),117 ("OK", "ALARM", 1, "breaching", TEST_2_M_OF_N),118 ("OK", "ALARM", 1, "notBreaching", TEST_2_M_OF_N),119 # 0 - X - -120 ("INSUFFICIENT_DATA", "OK", 1, "missing", TEST_3_M_OF_N),121 ("INSUFFICIENT_DATA", "OK", 1, "ignore", TEST_3_M_OF_N),122 ("INSUFFICIENT_DATA", "ALARM", 1, "breaching", TEST_3_M_OF_N),123 ("INSUFFICIENT_DATA", "OK", 1, "notBreaching", TEST_3_M_OF_N),124 # - - - - 0125 ("INSUFFICIENT_DATA", "OK", 1, "missing", TEST_4_M_OF_N),126 ("INSUFFICIENT_DATA", "OK", 1, "ignore", TEST_4_M_OF_N),127 ("INSUFFICIENT_DATA", "ALARM", 1, "breaching", TEST_4_M_OF_N),128 ("INSUFFICIENT_DATA", "OK", 1, "notBreaching", TEST_4_M_OF_N),129 # - - - X -130 ("INSUFFICIENT_DATA", "ALARM", 1, "missing", TEST_5_M_OF_N),131 ("INSUFFICIENT_DATA", "INSUFFICIENT_DATA", 0, "ignore", TEST_5_M_OF_N),132 ("INSUFFICIENT_DATA", "ALARM", 1, "breaching", TEST_5_M_OF_N),133 ("INSUFFICIENT_DATA", "OK", 1, "notBreaching", TEST_5_M_OF_N),134 ],135 )136 def test_calculate_alarm_state_2_out_of_3(137 self, initial_state, expected_state, expected_calls, treat_missing, metric_data138 ):139 """140 Tests Table 2 depicted in the docs: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html#alarm-evaluation141 Datapoints to Alarm = 2 and Evaluation Periods = 3142 Cloudwatch uses 5 latest datapoints for evaluation:143 0 = ok144 X = breaking threshold145 - = no data available146 |Test | Data points | MISSING | IGNORE | BREACHING | NOT BREACHING | # missing datapoints |147 |--------------|--------------|------------------|-------------|-----------|---------------| ---------------------|148 |TEST_1_M_OF_N | 0 - X - X | ALARM | ALARM | ALARM | ALARM | 0 |149 |TEST_2_M_OF_N | 0 0 X 0 X | ALARM | ALARM | ALARM | ALARM | 0 |150 |TEST_3_M_OF_N | 0 - X - - | OK | OK | ALARM | OK | 1 |151 |TEST_4_M_OF_N | - - - - 0 | OK | OK | ALARM | OK | 2 |152 |TEST_5_M_OF_N | - - - X - | ALARM | Retain state| ALARM | OK | 2 -> Premature alarm |153 """154 def mock_metric_alarm_details(alarm_arn):155 details = {156 "AlarmName": "test-alarm",157 "StateValue": initial_state,158 "TreatMissingData": treat_missing,159 "DatapointsToAlarm": 2,160 "EvaluationPeriods": 3,161 "Period": 5,162 "ComparisonOperator": "LessThanThreshold",163 "Threshold": 2,164 }165 return details166 def mock_collect_metric_data(alarm_details, client):167 if metric_data == self.TEST_1_M_OF_N:168 return [2.5, None, 1, None, 1.7]169 if metric_data == self.TEST_2_M_OF_N:170 return [3.0, 4.0, 1.0, 3.5, 1.5]171 if metric_data == self.TEST_3_M_OF_N:172 return [4.0, None, 1.2, None, None]173 if metric_data == self.TEST_4_M_OF_N:174 return [None, None, None, None, 8]175 if metric_data == self.TEST_5_M_OF_N:176 return [None, None, None, 1.0, None]177 run_and_assert_calculate_alarm_state(178 mock_metric_alarm_details, mock_collect_metric_data, expected_calls, expected_state179 )180 def test_calculate_alarm_state_with_datapoints_value_zero(self):181 def mock_metric_alarm_details(alarm_arn):182 details = {183 "AlarmName": "test-alarm",184 "StateValue": "OK",185 "TreatMissingData": "notBreaching",186 "EvaluationPeriods": 1,187 "Period": 5,188 "ComparisonOperator": "LessThanThreshold",189 "Threshold": 1,190 }191 return details192 def mock_collect_metric_data(alarm_details, client):193 return [0.0, None, 0.0]194 run_and_assert_calculate_alarm_state(195 mock_metric_alarm_details, mock_collect_metric_data, 1, "ALARM"196 )197def run_and_assert_calculate_alarm_state(198 mock_metric_alarm_details, mock_collect_metric_data, expected_calls, expected_state199):200 mock_client = Mock()201 def mock_cloudwatch_client(alarm_arn):202 return mock_client203 patches = Patches(204 [205 Patch.function(206 alarm_scheduler.get_metric_alarm_details_for_alarm_arn,207 mock_metric_alarm_details,208 pass_target=False,209 ),210 Patch.function(211 alarm_scheduler.get_cloudwatch_client_for_region_of_alarm,...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful