Best Python code snippet using localstack_python
test_rollout.py
Source:test_rollout.py
1import itertools2import unittest3from unittest import mock4from cloudformation_seed.cfn_stackset import StackSetRollout5existing_stack_simple = {6 'Summaries': [7 {8 'Account': '111111111111',9 'Region': 'ap-southeast-2',10 'Status': 'CURRENT',11 },12 {13 'Account': '222222222222',14 'Region': 'ap-southeast-2',15 'Status': 'CURRENT',16 },17 {18 'Account': '333333333333',19 'Region': 'ap-southeast-2',20 'Status': 'CURRENT',21 },22 {23 'Account': '444444444444',24 'Region': 'ap-southeast-2',25 'Status': 'CURRENT',26 },27 ]28}29existing_stack_multi_reg = {30 'Summaries': [31 {32 'Account': '111111111111',33 'Region': 'ap-southeast-2',34 'Status': 'CURRENT',35 },36 {37 'Account': '111111111111',38 'Region': 'eu-west-1',39 'Status': 'CURRENT',40 },41 {42 'Account': '222222222222',43 'Region': 'ap-southeast-2',44 'Status': 'CURRENT',45 },46 {47 'Account': '333333333333',48 'Region': 'ap-southeast-2',49 'Status': 'CURRENT',50 },51 {52 'Account': '333333333333',53 'Region': 'us-east-1',54 'Status': 'CURRENT',55 },56 {57 'Account': '333333333333',58 'Region': 'eu-west-1',59 'Status': 'CURRENT',60 },61 {62 'Account': '444444444444',63 'Region': 'ap-southeast-2',64 'Status': 'CURRENT',65 },66 ]67}68existing_stack_instance = {69 'StackInstance': {70 'ParameterOverrides': [71 {72 'ParameterKey': 'testParam1',73 'ParameterValue': 'testValue1'74 }75 ],76 'Status': 'CURRENT',77 }78}79existing_stack_instance2 = {80 'StackInstance': {81 'ParameterOverrides': [82 {83 'ParameterKey': 'testParam2',84 'ParameterValue': 'testValue2'85 }86 ],87 'Status': 'CURRENT',88 }89}90class TestGroupedRollout(unittest.TestCase):91 longMessage = 'Group rollout failure: '92 def check_if_deploying(self, rollout, account, region, overrides=None):93 for xr in rollout:94 for xa in xr['accounts']:95 if account in xa['accounts'] and region in xa['regions']:96 if overrides is None:97 return True98 if sorted(xr['override'], key=lambda x: x['ParameterKey']) == \99 sorted(overrides, key=lambda x: x['ParameterKey']):100 return True101 return False102 @mock.patch('cloudformation_seed.util.session')103 def test_single_region_no_update(self, mock_session):104 mock_session.client.return_value.list_stack_instances.return_value = existing_stack_simple105 mock_session.client.return_value.describe_stack_instance.return_value = existing_stack_instance106 config = [{107 'account': xa['Account'],108 'regions': {'ap-southeast-2'},109 'override': existing_stack_instance['StackInstance']['ParameterOverrides']110 } for xa in existing_stack_simple['Summaries']]111 r = StackSetRollout('x0-test-stack', config)112 d = r.rollout_delete()113 c, u = r.rollout_create_update()114 self.assertEqual(len(d), 0, 'should not be deleting instances')115 self.assertEqual(len(c), 0, 'should not be creating instances')116 self.assertEqual(len(u), 0, 'should not be updating instances')117 @mock.patch('cloudformation_seed.util.session')118 def test_multi_region_no_update(self, mock_session):119 config = list()120 for account, group in itertools.groupby(sorted(existing_stack_multi_reg['Summaries'],121 key=lambda x: x['Account']), lambda x: x['Account']):122 regions = set([xg['Region'] for xg in group])123 config.append({124 'account': account,125 'regions': regions,126 'override': existing_stack_instance['StackInstance']['ParameterOverrides']127 })128 mock_session.client.return_value.list_stack_instances.return_value = existing_stack_multi_reg129 mock_session.client.return_value.describe_stack_instance.return_value = existing_stack_instance130 r = StackSetRollout('x0-test-stack', config)131 d = r.rollout_delete()132 c, u = r.rollout_create_update()133 self.assertEqual(len(d), 0, 'should not be deleting instances')134 self.assertEqual(len(c), 0, 'should not be creating instances')135 self.assertEqual(len(u), 0, 'should not be updating instances')136 @mock.patch('cloudformation_seed.util.session')137 def test_multi_region_create_update(self, mock_session):138 config = list()139 creating = {140 '222222222222': {'eu-west-1', 'us-east-1'},141 '333333333333': {'ap-southeast-1'}142 }143 updating = {144 '111111111111': {'eu-west-1', 'ap-southeast-2'},145 '444444444444': {'ap-southeast-2'}146 }147 for account, group in itertools.groupby(sorted(existing_stack_multi_reg['Summaries'],148 key=lambda x: x['Account']), lambda x: x['Account']):149 regions = set([xg['Region'] for xg in group])150 override = existing_stack_instance['StackInstance']['ParameterOverrides']151 if account in creating:152 regions.update(creating[account])153 if account in updating:154 override = existing_stack_instance2['StackInstance']['ParameterOverrides']155 config.append({156 'account': account,157 'regions': regions,158 'override': override159 })160 mock_session.client.return_value.list_stack_instances.return_value = existing_stack_multi_reg161 mock_session.client.return_value.describe_stack_instance.return_value = existing_stack_instance162 r = StackSetRollout('x0-test-stack', config)163 d = r.rollout_delete()164 c, u = r.rollout_create_update()165 self.assertEqual(len(d), 0, 'should not be deleting instances')166 for xa, xr in creating.items():167 for xr in xr:168 self.assertTrue(self.check_if_deploying(c, xa, xr,169 existing_stack_instance['StackInstance']['ParameterOverrides']))170 for xa, xr in updating.items():171 for xr in xr:172 self.assertTrue(self.check_if_deploying(u, xa, xr,173 existing_stack_instance2['StackInstance']['ParameterOverrides']))174if __name__ == '__main__':...
cloudformation.py
Source:cloudformation.py
...83 },84 RetainStacks=False85 )["OperationId"]86 return operation_id87 def describe_stack_instance(self, stack_set_name, stack_instance_account, stack_instance_region):88 stack_instance_info = self.client.describe_stack_instance(89 StackSetName=stack_set_name,90 StackInstanceAccount=stack_instance_account,91 StackInstanceRegion=stack_instance_region92 )["StackInstance"]93 return stack_instance_info94 def list_stack_instances(self, stack_set_name):95 summaries = self.client.list_stack_instances(96 StackSetName=stack_set_name,97 )["Summaries"]98 return summaries99 def update_stack_instances(self, stack_set_name, accounts, regions, parameter_overrides=None, region_order=None):100 if region_order is None:101 region_order = []102 if parameter_overrides is None:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!