Пример #1
0
 def _setup_test_asg_to_be_deleted(self):
     """
     Setup a test ASG that is tagged to be deleted.
     """
     # pylint: disable=attribute-defined-outside-init
     self.test_asg_name = "test-asg-random-tags"
     self.test_autoscale = boto.connect_autoscale()
     launch_config = boto.ec2.autoscale.LaunchConfiguration(
         name='my-launch_config',
         image_id='my-ami',
         key_name='my_key_name',
         security_groups=['my_security_groups'])
     self.test_autoscale.create_launch_configuration(launch_config)
     asg = boto.ec2.autoscale.AutoScalingGroup(
         group_name=self.test_asg_name,
         load_balancers=['my-lb'],
         availability_zones=['us-east-1a', 'us-east-1b'],
         launch_config=launch_config,
         min_size=4,
         max_size=8,
         connection=self.test_autoscale)
     create_elb('my-lb')
     self.test_autoscale.create_auto_scaling_group(asg)
     ec2.tag_asg_for_deletion(self.test_asg_name, 0)
     self.test_asg = self.test_autoscale.get_all_groups(
         [self.test_asg_name])[0]
Пример #2
0
    def test_wait_for_healthy_elbs(self):
        first_elb_name = "healthy-lb-1"
        second_elb_name = "healthy-lb-2"
        first_elb = create_elb(first_elb_name)
        second_elb = create_elb(second_elb_name)
        mock_function = "boto.ec2.elb.loadbalancer.LoadBalancer.get_instance_health"

        # Setup a side effect to simulate how a instances may come online in the load balancer.
        # 2 load balancers * 2 instances per * 3 iterations (They way these instances come online in to the load
        # balancer will ensure that the ELB will be removed from the list on the second iteration, then the second ELB
        # is removed on the 3rd iteation.
        first_elb_instances = first_elb.get_instance_health()
        second_elb_instances = second_elb.get_instance_health()

        return_vals = [
            clone_elb_instances_with_state(first_elb_instances,
                                           "OutOfService"),
            clone_elb_instances_with_state(second_elb_instances,
                                           "OutOfService")
        ]
        return_vals += [
            clone_elb_instances_with_state(first_elb_instances, "InService"),
            clone_elb_instances_with_state(second_elb_instances,
                                           "OutOfService")
        ]
        return_vals += [
            clone_elb_instances_with_state(second_elb_instances, "InService")
        ]

        with mock.patch(mock_function, side_effect=return_vals):
            with mock.patch('tubular.ec2.WAIT_SLEEP_TIME', 1):
                self.assertEqual(
                    None,
                    ec2.wait_for_healthy_elbs(
                        [first_elb_name, second_elb_name], 3))
Пример #3
0
 def _setup_test_asg_to_be_deleted(self):
     """
     Setup a test ASG that is tagged to be deleted.
     """
     # pylint: disable=attribute-defined-outside-init
     self.test_asg_name = "test-asg-random-tags"
     self.test_autoscale = boto.connect_autoscale()
     launch_config = boto.ec2.autoscale.LaunchConfiguration(
         name='my-launch_config',
         image_id='my-ami',
         key_name='my_key_name',
         security_groups=['my_security_groups']
     )
     self.test_autoscale.create_launch_configuration(launch_config)
     asg = boto.ec2.autoscale.AutoScalingGroup(
         group_name=self.test_asg_name,
         load_balancers=['my-lb'],
         availability_zones=['us-east-1a', 'us-east-1b'],
         launch_config=launch_config,
         min_size=4,
         max_size=8,
         connection=self.test_autoscale
     )
     create_elb('my-lb')
     self.test_autoscale.create_auto_scaling_group(asg)
     ec2.tag_asg_for_deletion(self.test_asg_name, 0)
     self.test_asg = self.test_autoscale.get_all_groups([self.test_asg_name])[0]
Пример #4
0
    def test_wait_for_healthy_elbs(self):
        first_elb_name = "healthy-lb-1"
        second_elb_name = "healthy-lb-2"
        first_elb = create_elb(first_elb_name)
        second_elb = create_elb(second_elb_name)
        mock_function = "boto.ec2.elb.loadbalancer.LoadBalancer.get_instance_health"

        # Setup a side effect to simulate how a instances may come online in the load balancer.
        # 2 load balancers * 2 instances per * 3 iterations (They way these instances come online in to the load
        # balancer will ensure that the ELB will be removed from the list on the second iteration, then the second ELB
        # is removed on the 3rd iteation.
        first_elb_instances = first_elb.get_instance_health()
        second_elb_instances = second_elb.get_instance_health()

        return_vals = [
            clone_elb_instances_with_state(first_elb_instances, "OutOfService"),
            clone_elb_instances_with_state(second_elb_instances, "OutOfService")
        ]
        return_vals += [
            clone_elb_instances_with_state(first_elb_instances, "InService"),
            clone_elb_instances_with_state(second_elb_instances, "OutOfService")
        ]
        return_vals += [clone_elb_instances_with_state(second_elb_instances, "InService")]

        with mock.patch(mock_function, side_effect=return_vals):
            with mock.patch('tubular.ec2.WAIT_SLEEP_TIME', 1):
                self.assertEqual(None, ec2.wait_for_healthy_elbs([first_elb_name, second_elb_name], 3))
Пример #5
0
    def test_get_all_load_balancers(self, elb_count, expected_result_count, name_filter):
        """
        While I have attempted to test for pagination the moto library does not seem to support this and returns
        all of the ELBs created on the first get request and not 50 per request.
        """
        for i in range(elb_count):
            create_elb("elb_{}".format(i))
        elb = ec2.get_all_load_balancers(name_filter)

        self.assertIsInstance(elb, list)
        self.assertEquals(len(elb), expected_result_count)
        if name_filter:
            self.assertTrue(all(asg.name in name_filter for asg in elb))
Пример #6
0
    def test_get_all_load_balancers(self, elb_count, expected_result_count,
                                    name_filter):
        """
        While I have attempted to test for pagination the moto library does not seem to support this and returns
        all of the ELBs created on the first get request and not 50 per request.
        """
        for i in range(elb_count):
            create_elb("elb_{}".format(i))
        elb = ec2.get_all_load_balancers(name_filter)

        self.assertIsInstance(elb, list)
        self.assertEqual(len(elb), expected_result_count)
        if name_filter:
            self.assertTrue(all(asg.name in name_filter for asg in elb))
Пример #7
0
 def test_wait_for_healthy_elbs_failure(self):
     elb_name = "unhealthy-lb"
     load_balancer = create_elb(elb_name)
     # Make one of the instances un-healthy.
     instances = load_balancer.get_instance_health()
     instances[0].state = "OutOfService"
     mock_function = "boto.ec2.elb.loadbalancer.LoadBalancer.get_instance_health"
     with mock.patch(mock_function, return_value=instances):
         self.assertRaises(TimeoutException, ec2.wait_for_healthy_elbs, [elb_name], 2)
Пример #8
0
 def test_wait_for_healthy_elbs_failure(self):
     elb_name = "unhealthy-lb"
     load_balancer = create_elb(elb_name)
     # Make one of the instances un-healthy.
     instances = load_balancer.get_instance_health()
     instances[0].state = "OutOfService"
     mock_function = "boto.ec2.elb.loadbalancer.LoadBalancer.get_instance_health"
     with mock.patch(mock_function, return_value=instances):
         self.assertRaises(TimeoutException, ec2.wait_for_healthy_elbs,
                           [elb_name], 2)
Пример #9
0
 def test_ami_for_edp_success(self):
     fake_ami_id = self._make_fake_ami()
     fake_elb_name = "healthy-lb-1"
     fake_elb = create_elb(fake_elb_name)
     fake_asg_name = "fully_tagged_asg"
     fake_asg_tags = {
         "environment": "foo",
         "deployment": "bar",
         "play": "baz"
     }
     create_asg_with_tags(fake_asg_name,
                          fake_asg_tags,
                          ami_id=fake_ami_id,
                          elbs=[fake_elb])
     self.assertEqual(ec2.active_ami_for_edp('foo', 'bar', 'baz'),
                      fake_ami_id)
Пример #10
0
 def test_ami_for_edp_multiple_amis(self):
     fake_ami_id1 = self._make_fake_ami()
     fake_ami_id2 = self._make_fake_ami()
     fake_elb_name = "healthy-lb-1"
     fake_elb = create_elb(fake_elb_name)
     fake_asg_name1 = "fully_tagged_asg1"
     fake_asg_name2 = "fully_tagged_asg2"
     fake_asg_tags = {
         "environment": "foo",
         "deployment": "bar",
         "play": "baz"
     }
     create_asg_with_tags(fake_asg_name1,
                          fake_asg_tags,
                          ami_id=fake_ami_id1,
                          elbs=[fake_elb])
     create_asg_with_tags(fake_asg_name2,
                          fake_asg_tags,
                          ami_id=fake_ami_id2,
                          elbs=[fake_elb])
     with self.assertRaises(MultipleImagesFoundException):
         ec2.active_ami_for_edp('foo', 'bar', 'baz')