def test_double_termination_failure(self): """Attempting to terminate the same instance instance twice should fail. """ instance_id = self._ec2_mock.instances()[0]['InstanceId'] instance = Ec2(InstanceId=instance_id) instance.terminate() with self.assertRaises(Ec2Exception): instance.terminate() instance = Ec2(InstanceId=instance_id) with self.assertRaises(Ec2Exception): instance.terminate()
def _roll_in(self, ami): """Add a new instance to the target group with the new ami.""" new_instance = Ec2.create_instance(ami) new_instance.wait_ready() self._target_group.add_instance(new_instance) self._target_group.wait_healthy(new_instance)
def healthy_instances(self): """Get a list of Ec2 objects represnting instances that are reporting a target group health check of healthy. """ targets = self._get_target_health() return [Ec2(InstanceId=instance['Target']['Id']) for instance in \ targets if instance['TargetHealth']['State'] == self.HEALTH_HEALTHY ]
def instances(self): """Get a list of Ec2 objects representing instances attached to this target group. """ targets = self._get_target_health() return [ Ec2(InstanceId=instance['Target']['Id']) for instance in targets ]
def test_remove_non_grouped_instance_should_fail(self): """Removing an instance not in the target group should fail.""" ami_id = self._ec2_mock.default_image() instance = Ec2.create_instance(ami_id) target_group = TargetGroup(self._target_group['TargetGroupArn']) with self.assertRaises(ElbException): target_group.remove_instance(instance)
def test_wait_healthy(self): """Health check should return true when target registers as healthy.""" target_group = TargetGroup(self._target_group['TargetGroupArn']) ami_id = self._ec2_mock.default_image() instance = Ec2.create_instance(ami_id) target_group.add_instance(instance) self.assertTrue(target_group.wait_healthy(instance))
def test_load_instance(self): """Instantiating an Ec2 object with an instance id should return an object containing that instance's data. """ instances = self._ec2_mock.instances() instance = Ec2(InstanceId=instances[0]['InstanceId']) self.assertEqual(instance._ec2_data, instances[0]) self.assertEqual(instance.id(), instances[0]['InstanceId'])
def test_add_terminated_instance_failure(self): """Adding a non-ready instance should fail.""" ami_id = self._ec2_mock.default_image() instance = Ec2.create_instance(ami_id) instance.terminate() target_group = TargetGroup(self._target_group['TargetGroupArn']) with self.assertRaises(ElbException): target_group.add_instance(instance)
def test_clean_up_healthy_instance_fails(self): """Attempting to terminate a healthy instance should throw an exception. """ deployer = Deployer(self._target_group) instance = Ec2(self._ec2_mock.instances()[0]['InstanceId']) with self.assertRaises(DeployerException): deployer._clean_up((instance,), wait_interval=0)
def test_is_unhealthy(self): """Test that an instance that is not in the group doesn't report as healthy. """ target_group = TargetGroup(self._target_group['TargetGroupArn']) ami_id = self._ec2_mock.default_image() instance = Ec2.create_instance(ami_id) self.assertFalse(target_group.is_healthy(instance))
def test_terminate_instance(self): """Terminating an instance should work remove it from the live instances. """ instances = self._ec2_mock.instances() instance = Ec2(InstanceId=instances[0]['InstanceId']) instance.terminate() self.assertEqual(len(self._ec2_mock.instances()), self.INSTANCE_COUNT - 1)
def test_state_returns_current_state(self): """Getting the state of an instance should return it's state.""" instance_id = self._ec2_mock.instances()[0]['InstanceId'] instance = Ec2(InstanceId=instance_id) self.assertEqual(instance.state(), Ec2.STATE_RUNNING) instance.terminate() self.assertIn(instance.state(), (Ec2.STATE_SHUTTING_DOWN, Ec2.STATE_TERMINATED))
def test_clean_up(self): """An instance drained from the target group should be terminated.""" deployer = Deployer(self._target_group) instance = Ec2(self._ec2_mock.instances()[0]['InstanceId']) deployer._roll_out(instance) deployer._clean_up((instance,), wait_interval=0) self.assertIn(instance.state(), (Ec2.STATE_SHUTTING_DOWN, Ec2.STATE_TERMINATED,) )
def test_create_instance(self): """Creating an instance should work.""" images = self._ec2_mock.images() # new instance should have an id instance = Ec2.create_instance(images[0]) self.assertEqual(len(instance.id()), self.INSTANCE_ID_LENGTH) # there should only be one new instance self.assertEqual(len(self._ec2_mock.instances()), self.INSTANCE_COUNT + 1)
def test_add_instance(self): """Registering an instance should add it to the target group.""" ami_id = self._ec2_mock.images()[0] instance = Ec2.create_instance(ami_id) target_group = TargetGroup(self._target_group['TargetGroupArn']) target_group.add_instance(instance) self.assertEqual(target_group.count(), self._ec2_mock.INSTANCE_COUNT + 1) self.assertIn(instance.id(), [ec2.id() for ec2 in target_group.instances()] )
def test_roll_out(self): """An instance should be removed from the target group on rollout.""" deployer = Deployer(self._target_group) instance = Ec2(self._ec2_mock.instances()[0]['InstanceId']) deployer._roll_out(instance) self.assertNotIn(instance.id(), [ec2.id() for ec2 in self._target_group.healthy_instances()] ) self.assertEqual(self._target_group.count(), \ self._ec2_mock.INSTANCE_COUNT -1 )
def test_get_old_ami_instances(self): """Only instances with old ami should be fetched from target group.""" deployer = Deployer(self._target_group) ami = self._ec2_mock.default_image() instances = deployer._get_ami_instances(ami) self.assertEqual(set((ami,)), \ set([instance.ami() for instance in instances]) ) new_ec2 = Ec2.create_instance(self._new_ami) self._target_group.add_instance(new_ec2) instances = deployer._get_ami_instances(ami) self.assertEqual(set((ami,)), \ set([instance.ami() for instance in instances]) )
def test_load_instance_bad_id(self): """Instantiating an Ec2 object with a non-existant id should cause an error. """ with self.assertRaises(Ec2Exception): Ec2(InstanceId='badid')
def test_create_instance_bad_ami(self): """Creating an instance with an improper ami id should fail.""" with self.assertRaises(Ec2Exception): instance = Ec2.create_instance('ami-phonyid')
def test_ami_exists(self): """AMI existance tests should return correct boolean values.""" images = self._ec2_mock.images() self.assertTrue(Ec2.ami_exists(images[0])) self.assertFalse(Ec2.ami_exists('ami-phonyid'))
def test_ami_returns_ami_id(self): """Getting the ami of an instance should return it's ami id.""" instance_id = self._ec2_mock.instances()[0]['InstanceId'] instance = Ec2(InstanceId=instance_id) self.assertEqual(instance.ami(), self._ec2_mock.default_image())
def test_wait_ready(self): """Class should poll instance until it times out or becomes ready.""" ec2 = Ec2.create_instance(self._ec2_mock.default_image()) self.assertTrue(ec2.wait_ready())