Best Python code snippet using lisa_python
test_terminate.py
Source:test_terminate.py
...17 @patch.object(BaseSession, "client", new_callable=PropertyMock)18 @patch.object(EC2, "wait")19 @patch.object(EC2, "print_instance_details")20 @patch.object(EC2, "set_ec2_instance")21 def test_terminate_instance(22 self,23 mocked_set_instance,24 mocked_detail,25 mocked_wait,26 mocked_client,27 mocked_confirmation,28 ):29 # mock the boto client30 ec2 = boto3.client("ec2")31 stubber = Stubber(ec2)32 response = {"TerminatingInstances": []}33 stubber.add_response("terminate_instances", response)34 stubber.activate()35 mocked_client.return_value = ec236 mocked_confirmation.return_value = True37 terminate_instance(False, False, False)38 mocked_set_instance.assert_called_once()39 mocked_detail.assert_called_once()40 self.assertRegex(41 self.capturedOutput.getvalue(), r".*Instance termination initiated.*"42 )43 ec2 = boto3.client("ec2")44 stubber = Stubber(ec2)45 response = {"TerminatingInstances": []}46 stubber.add_response("terminate_instances", response)47 stubber.activate()48 mocked_client.return_value = ec249 mocked_confirmation.return_value = True50 terminate_instance(False, False, True)51 mocked_wait.assert_called_once_with(52 "instance_terminated", "Wating for instance to be terminated ..."...
lambda_function.py
Source:lambda_function.py
1# coding=utf-82import time3import json4import boto35import logging6from botocore.errorfactory import ClientError7logger = logging.getLogger()8logger.setLevel(logging.INFO)9def lambda_handler(event, context):10 instance_id = event.get('instance_id')11 terminate_instance = event.get('terminate_instance', "false")12 image_name = 'beam-automation-'+time.strftime("%Y-%m-%d-%H%M%S", time.gmtime())13 ami_id = create_ami(image_name, instance_id, terminate_instance)14 return ami_id + "|" + image_name15def create_ami(image_name, instance_id, terminate_instance):16 ec2 = boto3.client('ec2',region_name='us-east-2')17 logger.info('creating ami of instance' + instance_id)18 res = ec2.create_image(InstanceId=instance_id,19 Name=image_name, NoReboot=True)20 logger.info('ami creation started for ' + res['ImageId'])21 if terminate_instance == "true":22 logger.info('shutting down instance' + instance_id)23 ec2.terminate_instances(InstanceIds=[instance_id])24 logger.info('shutting down complete for instance' + instance_id)...
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!!