Skip to content

Commit

Permalink
DEVOPS-9151: properly retrieve aws asg resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhang committed May 17, 2017
1 parent 9cd9126 commit adecf1d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 61 deletions.
9 changes: 5 additions & 4 deletions License2Deploy/rolling_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self,
self.stack_name = stack_name
self.force_redeploy = force_redeploy
self.stack_resources = False
self.autoscaling_groups = False
self.autoscaling_group = False
self.cloudwatch_alarms = False
self.environments = AWSConn.load_config(self.regions_conf).get(self.env)
self.region = AWSConn.determine_region(self.environments)
Expand Down Expand Up @@ -97,9 +97,10 @@ def get_autoscale_group_name(self):
return next((instance.name for instance in filter(lambda n: n.name, self.get_group_info()) if self.project in instance.name and self.env in instance.name), None)

def get_autoscaling_group_name_from_cloudformation(self):
if not self.autoscaling_groups:
self.autoscaling_groups = self.get_resources_from_stack_of_type('AWS::AutoScaling::AutoScalingGroup')
return self.get_resources_physical_ids_by_project(self.autoscaling_groups)[0]
if not self.autoscaling_group:
asg_logical_name = '{0}ASG{1}'.format(self.project, self.env)
self.autoscaling_group = self.cloudformation_client.describe_stack_resource(StackName=self.stack_name, LogicalResourceId=asg_logical_name)['StackResourceDetail']
return self.autoscaling_group['PhysicalResourceId']

def get_resources_from_stack_of_type(self, resource_type):
return [resource for resource in self.get_stack_resources() if resource['ResourceType'] == resource_type]
Expand Down
10 changes: 5 additions & 5 deletions tests/cloudformation_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def setUp(self):
self.rolling_deploy = RollingDeploy('stg', 'server-gms-extender', '0', 'ami-abcd1234', None, './regions.yml', stack_name='test-stack-name', session=session)

def test_get_autoscaling_group_name_via_cloudformation(self):
self.assertEquals(self.rolling_deploy.stack_resources, False)
self.assertEquals(self.rolling_deploy.autoscaling_groups, False)
self.assertEquals(self.rolling_deploy.autoscaling_group, False)
asg_name = self.rolling_deploy.get_autoscale_group_name()
self.assertTrue(self.rolling_deploy.stack_resources)
self.assertTrue(self.rolling_deploy.autoscaling_groups)
self.assertEquals(asg_name, 'dnbi-backend-qa-servergmsextenderASGqa-QO8UAEHUFJD')
self.assertTrue(self.rolling_deploy.autoscaling_group)
self.assertEquals(asg_name, 'dnbi-backend-qa-dnbigmsextenderASGqa-1NP5ZBSVZRD0N')

def test_retrieve_project_cloudwatch_alarms(self):
self.assertEquals(self.rolling_deploy.stack_resources, False)
self.assertEquals(self.rolling_deploy.cloudwatch_alarms, False)
cloudwatch_alarms = self.rolling_deploy.retrieve_project_cloudwatch_alarms()
self.assertTrue(self.rolling_deploy.stack_resources)
self.assertEquals(cloudwatch_alarms, ['dnbi-servergmsextender-SCALEDOWNALARMqa-123123', 'dnbi-servergmsextender-SCALEUPALARMqa-4asdhjks'])

104 changes: 52 additions & 52 deletions tests/rolling_deploy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from boto.ec2.autoscale.launchconfig import LaunchConfiguration
from boto.ec2.autoscale.group import AutoScalingGroup
from boto.ec2.cloudwatch.alarm import MetricAlarm
from moto import mock_autoscaling
from moto import mock_ec2
from moto import mock_elb
from moto.cloudwatch import mock_cloudwatch
from moto import mock_autoscaling_deprecated
from moto import mock_ec2_deprecated
from moto import mock_elb_deprecated
from moto.cloudwatch import mock_cloudwatch_deprecated
from nose.tools import raises

from License2Deploy.rolling_deploy import RollingDeploy
Expand All @@ -26,9 +26,9 @@ class RollingDeployTest(unittest.TestCase):
GMS_AUTOSCALING_GROUP_STG = 'server-backend-stg-servergmsextenderASGstg-3ELOD1FOTESTING'
GMS_AUTOSCALING_GROUP_PRD = 'server-backend-prd-servergmsextenderASGprd-3ELOD1FOTESTING'

@mock_autoscaling
@mock_elb
@mock_ec2
@mock_autoscaling_deprecated
@mock_elb_deprecated
@mock_ec2_deprecated
def setUp(self):
self.setUpELB()
self.rolling_deploy = RollingDeploy('stg', 'server-gms-extender', '0', 'ami-abcd1234', None, './regions.yml', force_redeploy=True)
Expand All @@ -39,7 +39,7 @@ def get_autoscaling_configurations(self, launch_configuration_name, autoscaling_
self.launch_configuration_name: launch_configuration_name
}

@mock_autoscaling
@mock_autoscaling_deprecated
def setUpAutoScaleGroup(self, configurations, env="stg"):
conn = boto.connect_autoscale()
for configuration in configurations:
Expand All @@ -66,7 +66,7 @@ def setUpAutoScaleGroup(self, configurations, env="stg"):
conn.create_launch_configuration(config)
conn.create_auto_scaling_group(group)

@mock_elb
@mock_elb_deprecated
def setUpELB(self, env='stg'):
conn_elb = boto.connect_elb()
zones = ['us-east-1a']
Expand All @@ -76,8 +76,8 @@ def setUpELB(self, env='stg'):
balancers = conn_elb.get_all_load_balancers(load_balancer_names=[load_balancer_name])
self.assertEqual(balancers[0].name, load_balancer_name)

@mock_ec2
@mock_elb
@mock_ec2_deprecated
@mock_elb_deprecated
def setUpEC2(self, tag=True):
self.setUpELB()
conn_elb = boto.connect_elb()
Expand All @@ -96,7 +96,7 @@ def setUpEC2(self, tag=True):

return [conn, instance_id_list]

@mock_cloudwatch
@mock_cloudwatch_deprecated
def setUpCloudWatch(self, instance_ids, env="stg"):
alarm = MetricAlarm(
name = "servergmsextender_CloudWatchAlarm" + env,
Expand All @@ -114,7 +114,7 @@ def setUpCloudWatch(self, instance_ids, env="stg"):
watch_conn = boto.connect_cloudwatch()
watch_conn.put_metric_alarm(alarm)

@mock_cloudwatch
@mock_cloudwatch_deprecated
def setUpCloudWatchWithWrongConfig(self, instance_ids, env="stg"):
alarm = MetricAlarm(
name = "servergmsextender_CloudWatchAlarm" + env,
Expand All @@ -131,42 +131,42 @@ def setUpCloudWatchWithWrongConfig(self, instance_ids, env="stg"):
)
watch_conn = boto.connect_cloudwatch()
watch_conn.put_metric_alarm(alarm)
@mock_cloudwatch

@mock_cloudwatch_deprecated
def test_retrieve_project_cloudwatch_alarms(self):
instance_ids = self.setUpEC2()
self.setUpCloudWatch(instance_ids)
cloud_watch_alarms = self.rolling_deploy.retrieve_project_cloudwatch_alarms()
print cloud_watch_alarms
self.assertEqual(1, len(cloud_watch_alarms))

@mock_cloudwatch
@mock_cloudwatch_deprecated
def test_retrieve_project_cloudwatch_alarms_with_no_valid_alarms(self):
instance_ids = self.setUpEC2()
self.setUpCloudWatch(instance_ids)
self.rolling_deploy.env = "wrong_env_prd" # set a wrong environment
self.rolling_deploy.env = "wrong_env_prd" # set a wrong environment
cloud_watch_alarms = self.rolling_deploy.retrieve_project_cloudwatch_alarms()
self.assertEqual(0, len(cloud_watch_alarms))

@mock_cloudwatch
@mock_cloudwatch_deprecated
def test_retrieve_project_cloudwatch_alarms_with_wrong_config(self):
instance_ids = self.setUpEC2()
self.setUpCloudWatchWithWrongConfig(instance_ids)
self.assertRaises(SystemExit, lambda: self.rolling_deploy.retrieve_project_cloudwatch_alarms())

@mock_cloudwatch
@mock_cloudwatch_deprecated
def test_enable_project_cloudwatch_alarms_Error(self):
instance_ids = self.setUpEC2()
self.setUpCloudWatch(instance_ids)
self.assertRaises(SystemExit, lambda: self.rolling_deploy.enable_project_cloudwatch_alarms())

@mock_cloudwatch
@mock_cloudwatch_deprecated
def test_disable_project_cloudwatch_alarms_Error(self):
instance_ids = self.setUpEC2()
self.setUpCloudWatch(instance_ids)
self.assertRaises(SystemExit, lambda: self.rolling_deploy.disable_project_cloudwatch_alarms())

@mock_ec2
@mock_ec2_deprecated
def test_tag_ami(self):
conn = self.setUpEC2()[0]
reservation = conn.run_instances('ami-1234xyz1', min_count=1)
Expand All @@ -180,19 +180,19 @@ def test_tag_ami(self):
self.rolling_deploy.tag_ami(str(_ami_id), 'qa')
self.assertRaises(SystemExit, lambda: self.rolling_deploy.tag_ami('blargness', 'qa'))

@mock_ec2
@mock_ec2_deprecated
def test_load_config(self):
self.assertEqual(AWSConn.load_config('regions.yml').get('qa'), 'us-west-1')
self.assertEqual(AWSConn.load_config('regions.yml').get('stg'), 'us-east-1')
self.assertEqual(AWSConn.load_config('regions.yml').get('prd'), 'us-east-1')
self.assertEqual(AWSConn.load_config('regions.yml').get('default'), 'us-west-1')
self.assertEqual(AWSConn.load_config('regions.yml').get('zero'), None)

@mock_ec2
@mock_ec2_deprecated
def test_load_config(self):
self.assertEqual(AWSConn.determine_region('get-shwifty'), 'us-west-1')

@mock_ec2
@mock_ec2_deprecated
def test_wait_ami_availability(self):
conn = self.setUpEC2()[0]
inst_ids = self.setUpEC2()[1]
Expand All @@ -204,21 +204,21 @@ def test_wait_ami_availability(self):
self.assertRaises(SystemExit, lambda: self.rolling_deploy.wait_ami_availability('bad-id')) #Will raise exception because ami can't be found
self.assertRaises(SystemExit, lambda: self.rolling_deploy.wait_ami_availability(ami_id.id, -100)) #Will raise exception as time limit is over

@mock_ec2
@mock_elb
@mock_ec2_deprecated
@mock_elb_deprecated
def test_confirm_lb_has_only_new_instances(self):
instance_ids = self.setUpEC2()[1]
self.rolling_deploy.load_balancer = self.rolling_deploy.get_lb()
self.assertEqual(len(instance_ids), len(self.rolling_deploy.confirm_lb_has_only_new_instances())) #Return All LB's with the proper build number

@mock_elb
@mock_elb_deprecated
def test_get_lb(self):
self.setUpELB()
self.assertEqual(u'servergmsextenderELBstg', self.rolling_deploy.get_lb()) #Return All LB's with the proper build number

# assertRaises is a context manager since Python 2.7. Only testing in Python 2.7
# https://docs.python.org/2.7/library/unittest.html
@mock_elb
@mock_elb_deprecated
def test_get_lb_failure(self):
if sys.version_info >= (2, 7):
self.setUpELB()
Expand All @@ -227,8 +227,8 @@ def test_get_lb_failure(self):
bad_rolling_deploy.load_balancer = bad_rolling_deploy.get_lb()
self.assertEqual(2, rolling_deploy.exception.code)

@mock_ec2
@mock_elb
@mock_ec2_deprecated
@mock_elb_deprecated
def test_lb_healthcheck(self):
instance_ids = self.setUpEC2()[1]
self.assertTrue(self.rolling_deploy.lb_healthcheck(instance_ids)) #Return InService for all instances in ELB
Expand All @@ -237,18 +237,18 @@ def test_lb_healthcheck(self):
## https://github.com/spulec/moto/blob/master/moto/elb/responses.py#L219 ##
#self.assertRaises(SystemExit, lambda: self.rolling_deploy.lb_healthcheck(instance_ids, 1, 1)) #Return OutOfService for the first instance in the ELB which will raise an exit call

@mock_autoscaling
@mock_autoscaling_deprecated
def test_get_group_info(self):
self.setUpAutoScaleGroup([self.get_autoscaling_configurations(self.GMS_LAUNCH_CONFIGURATION_STG, self.GMS_AUTOSCALING_GROUP_STG)])
group = self.rolling_deploy.get_group_info([self.GMS_AUTOSCALING_GROUP_STG])[0]
self.assertEqual(group.name, self.GMS_AUTOSCALING_GROUP_STG)

@mock_autoscaling
@mock_autoscaling_deprecated
def test_failure_get_group_info(self):
self.setUpAutoScaleGroup([self.get_autoscaling_configurations(self.GMS_LAUNCH_CONFIGURATION_STG, self.GMS_AUTOSCALING_GROUP_STG)])
self.assertRaises(SystemExit, lambda: self.rolling_deploy.get_group_info('cool'))

@mock_autoscaling
@mock_autoscaling_deprecated
def test_get_autoscale_group_name_stg(self):
autoscaling_configurations = list()
autoscaling_configurations.append(self.get_autoscaling_configurations(self.GMS_LAUNCH_CONFIGURATION_STG, self.GMS_AUTOSCALING_GROUP_STG))
Expand All @@ -258,8 +258,8 @@ def test_get_autoscale_group_name_stg(self):
self.assertEqual(group, self.GMS_AUTOSCALING_GROUP_STG)
self.assertNotEqual(group, self.GMS_AUTOSCALING_GROUP_PRD)

@mock_autoscaling
@mock_elb
@mock_autoscaling_deprecated
@mock_elb_deprecated
def test_get_autoscale_group_name_prd(self):
self.setUpELB(env='prd')
self.rolling_deploy = RollingDeploy('prd', 'server-gms-extender', '0', 'ami-test212', None, './regions.yml')
Expand All @@ -270,33 +270,33 @@ def test_get_autoscale_group_name_prd(self):
self.assertEqual(group, self.GMS_AUTOSCALING_GROUP_PRD)
self.assertNotEqual(group, self.GMS_AUTOSCALING_GROUP_STG)

@mock_autoscaling
@mock_autoscaling_deprecated
def test_calculate_autoscale_desired_instance_count(self):
self.setUpAutoScaleGroup([self.get_autoscaling_configurations(self.GMS_LAUNCH_CONFIGURATION_STG, self.GMS_AUTOSCALING_GROUP_STG)])
increase = self.rolling_deploy.calculate_autoscale_desired_instance_count(self.GMS_AUTOSCALING_GROUP_STG, 'increase')
decrease = self.rolling_deploy.calculate_autoscale_desired_instance_count(self.GMS_AUTOSCALING_GROUP_STG, 'decrease')
self.assertEqual(increase, 4)
self.assertEqual(decrease, 1)

@mock_autoscaling
@mock_autoscaling_deprecated
def test_calculate_autoscale_desired_instance_count_failure(self):
self.setUpAutoScaleGroup([self.get_autoscaling_configurations(self.GMS_LAUNCH_CONFIGURATION_STG, self.GMS_AUTOSCALING_GROUP_STG)])
self.assertRaises(SystemExit, lambda: self.rolling_deploy.calculate_autoscale_desired_instance_count(self.GMS_AUTOSCALING_GROUP_STG, 'nothing'))

@mock_ec2
@mock_ec2_deprecated
def test_get_instance_ip_addrs(self):
self.setUpEC2()
self.rolling_deploy.get_instance_ip_addrs(self.setUpEC2()[1])
self.rolling_deploy.log_instances_ips(self.setUpEC2()[1], 'group')
self.assertRaises(SystemExit, lambda: self.rolling_deploy.get_instance_ip_addrs(['blah', 'blarg']))

@mock_ec2
@mock_ec2_deprecated
def test_is_redeploy(self):
self.setUpEC2()
self.assertTrue(self.rolling_deploy.is_redeploy())

@raises(SystemExit)
@mock_ec2
@mock_ec2_deprecated
def test_is_redeploy_fails(self):
self.setUpEC2(tag=False)
self.assertRaises(self.rolling_deploy.is_redeploy(), Exception)
Expand All @@ -305,20 +305,20 @@ def test_is_redeploy_fails(self):
def test_stop_deploy(self):
self.assertRaises(self.rolling_deploy.stop_deploy('error!'), Exception)

@mock_ec2
@mock_autoscaling
@mock_elb
@mock_ec2_deprecated
@mock_autoscaling_deprecated
@mock_elb_deprecated
def test_get_all_instance_ids(self):
self.setUpELB()
self.setUpAutoScaleGroup([self.get_autoscaling_configurations(self.GMS_LAUNCH_CONFIGURATION_STG, self.GMS_AUTOSCALING_GROUP_STG)])
conn = boto.connect_ec2()
reservation = conn.run_instances('ami-1234abcd', min_count=2, private_ip_address="10.10.10.10")
instance_ids = reservation.instances
rslt = self.rolling_deploy.get_all_instance_ids(self.GMS_AUTOSCALING_GROUP_STG)
self.assertEqual(len(instance_ids), len(rslt))
self.assertEqual(len(instance_ids), len(rslt))

@mock_ec2
@mock_autoscaling
@mock_ec2_deprecated
@mock_autoscaling_deprecated
def test_get_instance_ids_by_requested_build_tag(self):
self.setUpEC2()
self.setUpAutoScaleGroup([self.get_autoscaling_configurations(self.GMS_LAUNCH_CONFIGURATION_STG, self.GMS_AUTOSCALING_GROUP_STG)])
Expand All @@ -340,8 +340,8 @@ def test_get_instance_ids_by_requested_build_tag(self):
self.rolling_deploy.force_redeploy = True
self.assertRaises(Exception, lambda: self.rolling_deploy.get_instance_ids_by_requested_build_tag(new_inst, 0))

@mock_ec2
@mock_autoscaling
@mock_ec2_deprecated
@mock_autoscaling_deprecated
def test_get_instance_ids_by_requested_build_tag_race_condition(self):
self.setUpEC2()
self.setUpAutoScaleGroup([self.get_autoscaling_configurations(self.GMS_LAUNCH_CONFIGURATION_STG, self.GMS_AUTOSCALING_GROUP_STG)])
Expand All @@ -358,22 +358,22 @@ def test_get_instance_ids_by_requested_build_tag_race_condition(self):
self.assertRaises(Exception, lambda: self.rolling_deploy.get_instance_ids_by_requested_build_tag(new_inst, 1))


@mock_ec2
@mock_ec2_deprecated
def test_get_instance_ids_by_requested_build_tag_failure(self):
self.setUpEC2()
self.assertRaises(Exception, lambda: self.rolling_deploy.get_instance_ids_by_requested_build_tag([], 0))

@mock_autoscaling
@mock_autoscaling_deprecated
def test_set_autoscale_instance_desired_count(self):
self.setUpAutoScaleGroup([self.get_autoscaling_configurations(self.GMS_LAUNCH_CONFIGURATION_STG, self.GMS_AUTOSCALING_GROUP_STG)])
self.assertTrue(self.rolling_deploy.set_autoscale_instance_desired_count(4, self.GMS_AUTOSCALING_GROUP_STG))

@mock_ec2
@mock_ec2_deprecated
def test_wait_for_new_instances(self):
instance_ids = self.setUpEC2()[1]
self.assertEqual(self.rolling_deploy.wait_for_new_instances(instance_ids, 9), None)

@mock_ec2
@mock_ec2_deprecated
def test_wait_for_new_instances_failure(self):
conn = self.setUpEC2()[0]
instance_ids = self.setUpEC2()[1]
Expand Down
Loading

0 comments on commit adecf1d

Please sign in to comment.