def test_terminates_target_instances(self): ec2 = mock.Mock() ec2.terminate_instances.return_value = {} chaos.terminate_targets(ec2, [("a", "i-11111111"), ("b", "i-22222222")]) ec2.terminate_instances.assert_called_once_with( InstanceIds=["i-11111111", "i-22222222"])
def test_parseable_log_line_for_each_termination_result(self): ec2 = mock.Mock() # We're cheating here and returning results that are unrelated to the # list passed to terminate_targets ec2.terminate_instances.return_value = { "TerminatingInstances": [{ "InstanceId": "i-00000000", "CurrentState": { "Name": "s1" } }, { "InstanceId": "i-11111111", "CurrentState": { "Name": "s2" } }, { "InstanceId": "i-22222222", "CurrentState": { "Name": "s3" } }] } chaos.terminate_targets(ec2, [("a", "i-11111111")]) logged = self.get_log_lines("result") self.assertEqual( set((part[1], part[3]) for part in logged), set([("i-00000000", "s1"), ("i-11111111", "s2"), ("i-22222222", "s3")]))
def test_terminates_target_instances(self): ec2 = mock.Mock() ec2.terminate_instances.return_value = {} chaos.terminate_targets(ec2, [ ("a", "i-11111111"), ("b", "i-22222222") ]) ec2.terminate_instances.assert_called_once_with( InstanceIds=["i-11111111", "i-22222222"] )
def test_parseable_log_line_for_each_targeted_instance(self): ec2 = mock.Mock() ec2.terminate_instances.return_value = {} chaos.terminate_targets(ec2, [("asg-name-one", "i-00000000"), ("second-asg", "i-11111111"), ("the-third-asg", "i-22222222")]) logged = self.get_log_lines("targeting") self.assertEqual( set((part[1], part[3]) for part in logged), set([("i-00000000", "asg-name-one"), ("i-11111111", "second-asg"), ("i-22222222", "the-third-asg")]))
def test_handles_sns_exception(self): self.os.environ.get.return_value = "MyTestTopic" ec2 = mock.Mock() sns = mock.Mock() ec2.terminate_instances.return_value = {} sns.publish.side_effect = Exception("boom") chaos.terminate_targets(ec2, sns, [("a", "i-11111111"), ("b", "i-22222222")]) ec2.terminate_instances.assert_called_once_with( InstanceIds=["i-11111111", "i-22222222"])
def test_parseable_log_line_for_each_targeted_instance(self): ec2 = mock.Mock() ec2.terminate_instances.return_value = {} chaos.terminate_targets(ec2, [ ("asg-name-one", "i-00000000"), ("second-asg", "i-11111111"), ("the-third-asg", "i-22222222") ]) logged = self.get_log_lines("targeting") self.assertEqual(set((part[1], part[3]) for part in logged), set([ ("i-00000000", "asg-name-one"), ("i-11111111", "second-asg"), ("i-22222222", "the-third-asg") ]))
def test_returns_termination_results(self): ec2 = mock.Mock() # We're cheating here and returning results that are unrelated to the # list passed to terminate_targets ec2.terminate_instances.return_value = { "TerminatingInstances": [{ "InstanceId": "i-00000000", "CurrentState": { "Name": "s1" } }, { "InstanceId": "i-11111111", "CurrentState": { "Name": "s2" } }, { "InstanceId": "i-22222222", "CurrentState": { "Name": "s3" } }] } results = chaos.terminate_targets(ec2, []) self.assertEqual( set(results), set([("i-00000000", "s1"), ("i-11111111", "s2"), ("i-22222222", "s3")]))
def test_parseable_log_line_for_each_termination_result(self): ec2 = mock.Mock() # We're cheating here and returning results that are unrelated to the # list passed to terminate_targets ec2.terminate_instances.return_value = { "TerminatingInstances": [ {"InstanceId": "i-00000000", "CurrentState": {"Name": "s1"}}, {"InstanceId": "i-11111111", "CurrentState": {"Name": "s2"}}, {"InstanceId": "i-22222222", "CurrentState": {"Name": "s3"}} ] } chaos.terminate_targets(ec2, [("a", "i-11111111")]) logged = self.get_log_lines("result") self.assertEqual(set((part[1], part[3]) for part in logged), set([ ("i-00000000", "s1"), ("i-11111111", "s2"), ("i-22222222", "s3") ]))
def test_returns_termination_results(self): ec2 = mock.Mock() # We're cheating here and returning results that are unrelated to the # list passed to terminate_targets ec2.terminate_instances.return_value = { "TerminatingInstances": [ {"InstanceId": "i-00000000", "CurrentState": {"Name": "s1"}}, {"InstanceId": "i-11111111", "CurrentState": {"Name": "s2"}}, {"InstanceId": "i-22222222", "CurrentState": {"Name": "s3"}} ] } results = chaos.terminate_targets(ec2, []) self.assertEqual(set(results), set([ ("i-00000000", "s1"), ("i-11111111", "s2"), ("i-22222222", "s3") ]))
def test_sends_notification_per_instance(self): self.os.environ.get.return_value = "MyTestTopic" ec2 = mock.Mock() sns = mock.Mock() ec2.terminate_instances.return_value = {"TerminatingInstances": []} results = chaos.terminate_targets(ec2, sns, [("a1", "i1"), ("a2", "i2")]) sns.publish.assert_any_call(TopicArn="MyTestTopic", Message=MatchJson({ "event_name": "chaos_lambda.terminating", "asg_name": "a1", "instance_id": "i1" })) sns.publish.assert_any_call(TopicArn="MyTestTopic", Message=MatchJson({ "event_name": "chaos_lambda.terminating", "asg_name": "a2", "instance_id": "i2" })) self.assertEquals(2, sns.publish.call_count)