Best Python code snippet using localstack_python
test_spots.py
Source:test_spots.py
1from unittest import TestCase2from SpotPicker.spots import SpotRequests3import boto34from botocore.stub import Stubber5DEFAULT_RESPOSES = {6 "describe_spot_instance_requests": {7 "SpotInstanceRequests": [8 {9 "CreateTime": "2020-01-01T00:00:00.000Z",10 "InstanceId": "i-xxxxxxxxxxxxxx",11 "LaunchSpecification": {12 "SecurityGroups": [13 {14 "GroupName": "example-sg",15 "GroupId": "sg-xxxxxxxxxxxxxx"16 }17 ],18 "BlockDeviceMappings": [19 {20 "DeviceName": "/dev/xvda",21 "Ebs": {22 "DeleteOnTermination": True,23 "VolumeSize": 64,24 "VolumeType": "gp2"25 }26 }27 ],28 "IamInstanceProfile": {29 "Arn": "arn:aws:iam::999999999999:instance-profile/example-profile",30 "Name": "example-profile"31 },32 "ImageId": "ami-xxxxxxxx",33 "InstanceType": "t2.xlarge",34 "KeyName": "example-key",35 "NetworkInterfaces": [36 {37 "DeleteOnTermination": True,38 "DeviceIndex": 0,39 "SubnetId": "subnet-284ef060"40 }41 ],42 "Placement": {43 "AvailabilityZone": "eu-west-1a",44 "Tenancy": "default"45 },46 "Monitoring": {47 "Enabled": False48 }49 },50 "LaunchedAvailabilityZone": "eu-west-1a",51 "ProductDescription": "Linux/UNIX",52 "SpotInstanceRequestId": "sir-xxxxxxx",53 "SpotPrice": "0.107000",54 "State": "active",55 "Status": {56 "Code": "fulfilled",57 "Message": "Your spot request is fulfilled.",58 "UpdateTime": "2020-11-08T10:20:53.000Z"59 },60 "Tags": [],61 "Type": "one-time",62 "InstanceInterruptionBehavior": "terminate"63 }64 ]65 }66}67class TestSpotRequests(TestCase):68 def test_get_spot_instance_types(self):69 client = boto3.client('ec2')70 stubber = Stubber(client)71 stubber.add_response('describe_spot_instance_requests', DEFAULT_RESPOSES['describe_spot_instance_requests'])72 stubber.activate()73 returned_instance_types = SpotRequests(client).get_spot_instance_types()...
example.py
Source:example.py
...19# This pattern can be repeated for any Describe call that supports pagination.20#####################21import boto322ec2 = boto3.client('ec2')23def describe_spot_instance_requests(request_ids):24 described_spot_instance_requests = []25 # Create Paginator and Page Iterator for DescribeInstances26 paginator = ec2.get_paginator('describe_spot_instance_requests')27 page_iterator = paginator.paginate(SpotInstanceRequestIds=request_ids)28 # Iterate Pages and Add Instances To List29 for page in page_iterator:30 for request in page['SpotInstanceRequests']:31 described_spot_instance_requests.append(request)32 return described_spot_instance_requests33def get_spot_instance_requsts_from_fleet(fleet_id):34 35 described_spot_instance_requests = []36 # Create Paginator and Page Iterator for DescribeInstances37 paginator = ec2.get_paginator('describe_spot_fleet_instances')38 page_iterator = paginator.paginate(SpotFleetRequestId=fleet_id)39 # Iterate Pages and Add Instances To List40 for page in page_iterator:41 for instance in page['ActiveInstances']:42 described_spot_instance_requests.append(instance['SpotInstanceRequestId'])43 return described_spot_instance_requests44if __name__ == "__main__":45 spot_fleet_id = "[REQUEST_ID_GOES_HERE]" # Replace With Your Spot Fleet Request ID46 # First, Get all Spot Fleet Request IDs in a Fleet Request47 spot_fleet_request_ids = get_spot_instance_requsts_from_fleet(spot_fleet_id)48 # Next, Describe Each Spot Fleet Request49 described_spot_fleet_requests = describe_spot_instance_requests(spot_fleet_request_ids)50 # Use Results as Needed...
example-reusable-paginator.py
Source:example-reusable-paginator.py
...25 paginator = client.get_paginator(method.__name__)26 for page in paginator.paginate(**kwargs).result_key_iters():27 for item in page:28 yield item29def describe_spot_instance_requests(request_ids):30 described_spot_instance_requests = []31 response = paginate(ec2.describe_spot_instance_requests, SpotInstanceRequestIds=request_ids)32 for item in response:33 described_spot_instance_requests.append(item)34 return described_spot_instance_requests35def get_spot_instance_requsts_from_fleet(fleet_id):36 37 described_spot_instance_requests = []38 response = paginate(ec2.describe_spot_fleet_instances, SpotFleetRequestId=fleet_id, MaxResults=1000)39 for item in response:40 described_spot_instance_requests.append(item['SpotInstanceRequestId'])41 return described_spot_instance_requests42if __name__ == "__main__":43 spot_fleet_id = "[REQUEST_ID_GOES_HERE]" # Replace With Your Spot Fleet Request ID44 # First, Get all Spot Fleet Request IDs in a Fleet Request45 spot_fleet_request_ids = get_spot_instance_requsts_from_fleet(spot_fleet_id)46 # Next, Describe Each Spot Fleet Request47 described_spot_fleet_requests = describe_spot_instance_requests(spot_fleet_request_ids)48 # Use Results as Needed...
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!!