Best Python code snippet using localstack_python
deploy.py
Source:deploy.py
...10 '''11 n_calls = 312 client = cfn.Cloudformation()13 describe_stack_events = cfn.make_describe_stack_events(client, n_calls)14 describe_stacks = cfn.make_describe_stacks(client, n_calls, 'CREATE_COMPLETE')15 client.mock('describe_stack_events', describe_stack_events)16 client.mock('describe_stacks', describe_stacks)17 _wait_for_stack(client, 'foo')18 self.assertEqual(client.called['describe_stack_events'], n_calls)19 self.assertEqual(client.called['describe_stacks'], n_calls)20 # we're only testing that this runs to completion21 self.assertEqual(True, True)22 def test_stack_exists(self):23 '''verify a stack exists24 '''25 client = cfn.Cloudformation()26 describe_stacks = cfn.make_describe_stacks(client, 3, 'CREATE_COMPLETE')27 client.mock('describe_stacks', describe_stacks)28 exists = _stack_exists(client, 'foo')29 self.assertEqual(client.called['describe_stacks'], 1)30 self.assertEqual(exists, True)31 def test_stack_not_exists(self):32 '''verify a stack exists33 '''34 client = cfn.Cloudformation()35 describe_stacks = cfn.make_describe_stacks(client, 3, 'CREATE_COMPLETE', 'bar')36 client.mock('describe_stacks', describe_stacks)37 exists = _stack_exists(client, 'foo')38 self.assertEqual(client.called['describe_stacks'], 1)39 self.assertEqual(exists, False)40 def test_make_create_change_set(self):41 '''create a changeset42 '''43 stack_name = 'foo'44 client = cfn.Cloudformation()45 describe_stacks = cfn.make_describe_stacks(client, 3, 'CREATE_COMPLETE', 'bar')46 client.mock('describe_stacks', describe_stacks)47 def create_change_set(StackName, TemplateURL, UsePreviousTemplate, Parameters, Capabilities, ChangeSetName, ChangeSetType):48 self.assertEqual(ChangeSetType, 'CREATE')49 return {50 'Id': 'string',51 'StackId': 'string'52 }53 client.mock('create_change_set', create_change_set)54 _make_change_set(client, stack_name, '', {})55 self.assertEqual(client.called['create_change_set'], 1)56 def test_make_update_change_set(self):57 '''update a stack with changeset58 '''59 stack_name = 'foo'60 client = cfn.Cloudformation()61 describe_stacks = cfn.make_describe_stacks(client, 3, 'CREATE_COMPLETE')62 client.mock('describe_stacks', describe_stacks)63 def create_change_set(StackName, TemplateURL, UsePreviousTemplate, Parameters, Capabilities, ChangeSetName, ChangeSetType):64 self.assertEqual(ChangeSetType, 'UPDATE')65 return {66 'Id': 'string',67 'StackId': 'string'68 }69 client.mock('create_change_set', create_change_set)70 _make_change_set(client, stack_name, '', {})71 self.assertEqual(client.called['create_change_set'], 1)72 def test_wait_for_changeset(self):73 '''pause execution until the changeset creation is complete74 '''75 n_calls = 376 client = cfn.Cloudformation()77 describe_change_set = cfn.make_describe_change_set(client, n_calls, 'CREATE_COMPLETE')78 client.mock('describe_change_set', describe_change_set)79 finished = _wait_for_changeset(client, 'foo', 'bar')80 self.assertEqual(client.called['describe_change_set'], n_calls)81 # we're only testing that this runs to completion82 self.assertEqual(finished, True)83 def test_stack_complete(self):84 '''Check if the stack status is in a complete state85 '''86 client = cfn.Cloudformation()87 describe_stacks = cfn.make_describe_stacks(client, 1, 'CREATE_COMPLETE', 'foo')88 client.mock('describe_stacks', describe_stacks)89 stack_status = _stack_complete(client, 'foo')90 self.assertEqual(client.called['describe_stacks'], 1)91 self.assertEqual(stack_status['complete'], True)92 client = cfn.Cloudformation()93 describe_stacks = cfn.make_describe_stacks(client, 1, 'UPDATE_ROLLBACK_FAILED')94 client.mock('describe_stacks', describe_stacks)95 stack_status = _stack_complete(client, 'foo')96 self.assertEqual(client.called['describe_stacks'], 1)97 self.assertEqual(stack_status['complete'], True)98 def test_execute_changeset(self):99 '''Check that execute changeset gets called100 '''101 client = cfn.Cloudformation()102 def execute_change_set(ChangeSetName, StackName):103 return {}104 client.mock('execute_change_set', execute_change_set)105 _execute_changeset(client, 'foo', 'bar')106 self.assertEqual(client.called['execute_change_set'], 1)107 def test_get_parameters(self):...
cloudformation.py
Source:cloudformation.py
...44def get_template(stack_name):45 result = _make_api_call('get_template',46 StackName=stack_name)47 return result48def describe_stacks(stack_name=None):49 kwargs = dict()50 if stack_name:51 kwargs['StackName'] = stack_name52 response = _make_api_call('describe_stacks', **kwargs)53 next_token = response.get('NextToken')54 all_stacks = response['Stacks']55 while next_token:56 utils.sleep(sleep_time=0.5)57 response = _make_api_call('describe_stacks')58 all_stacks.extend(response['Stacks'])59 next_token = response.get('NextToken')60 return all_stacks61def stack_names():62 all_stacks = describe_stacks()63 return [stack['StackName'] for stack in all_stacks]64def wait_until_stack_exists(stack_name, timeout=120):65 """66 Given a template name, wait until stack is successfully created67 or 'timeout' seconds have elapsed.68 :param stack_name: name of the CloudFormation stack whose creation is pending69 :param timeout: number of seconds after which to stop polling 'DescribeStacks'70 :return:71 """72 LOG.debug('Inside describe_stacks api wrapper')73 stack_exists = False74 start_time = datetime.now()75 while not stack_exists:76 if (datetime.now() - start_time) > timedelta(timeout):77 raise CFNTemplateNotFound(78 "Could not find CFN stack, '{stack_name}'.".format(stack_name=stack_name)79 )80 all_stacks = describe_stacks()81 if [stack for stack in all_stacks if stack['StackName'] == stack_name]:82 stack_exists = True83 else:84 utils.sleep()85class CFNTemplateNotFound(EBCLIException):86 """87 Exception class to raise when a CFN stack is not found...
test_server_manager.py
Source:test_server_manager.py
1import warnings2from unittest.mock import patch3with warnings.catch_warnings():4 warnings.filterwarnings("ignore", category=SyntaxWarning)5 import boto36from botocore.stub import Stubber7from pytest import fixture8from discord_notifier.lib.server_manager import ServerManager9from discord_notifier.tstlib.test_mock_objects import newDescribeStacks10@fixture(params=["Running", "Stopped"])11def state(request):12 return request.param13@fixture14def cfn_client():15 cfn = boto3.client("cloudformation", region_name="us-west-2")16 stubber = Stubber(cfn)17 stubber.activate()18 with patch("boto3.client") as mock_boto3:19 mock_boto3.return_value = cfn20 yield stubber21 stubber.assert_no_pending_responses()22def set_describe_stacks_value(cfn, describe_stacks):23 expected_params = {"StackName": "StackName"}24 stack_state = describe_stacks.output25 cfn.add_response("describe_stacks", stack_state, expected_params)26def test_stop_server_started(cfn_client):27 describe_stacks = newDescribeStacks(server_state="Running")28 set_describe_stacks_value(cfn_client, describe_stacks)29 expected_params = {30 "StackName": describe_stacks.stack_name,31 "UsePreviousTemplate": True,32 "Capabilities": ["CAPABILITY_IAM"],33 "Parameters": newDescribeStacks(server_state="Stopped").parameters,34 }35 cfn_client.add_response("update_stack", {"StackId": "minecraft"}, expected_params)36 ServerManager("StackName").stop_server()37def test_stop_server_stopped(cfn_client):38 describe_stacks = newDescribeStacks(server_state="Stopped")39 set_describe_stacks_value(cfn_client, describe_stacks)40 expected_params = {41 "StackName": describe_stacks.stack_name,42 "UsePreviousTemplate": True,43 "Capabilities": ["CAPABILITY_IAM"],44 "Parameters": newDescribeStacks(server_state="Stopped").parameters,45 }46 cfn_client.add_client_error(47 "update_stack",48 service_error_code="ValidationError",49 service_message="No updates are to be performed.",50 expected_params=expected_params,51 )...
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!!