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 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_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_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_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_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_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_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())