How to use request_spot_instances method in localstack

Best Python code snippet using localstack_python

test_spot_instances.py

Source:test_spot_instances.py Github

copy

Full Screen

...10from moto import mock_ec2, mock_ec2_deprecated, settings11from moto.ec2.models import ec2_backends12from moto.core.utils import iso_8601_datetime_with_milliseconds13@mock_ec214def test_request_spot_instances():15 conn = boto3.client("ec2", "us-east-1")16 vpc = conn.create_vpc(CidrBlock="10.0.0.0/16")["Vpc"]17 subnet = conn.create_subnet(18 VpcId=vpc["VpcId"], CidrBlock="10.0.0.0/16", AvailabilityZone="us-east-1a"19 )["Subnet"]20 subnet_id = subnet["SubnetId"]21 conn.create_security_group(GroupName="group1", Description="description")22 conn.create_security_group(GroupName="group2", Description="description")23 start_dt = datetime.datetime(2013, 1, 1).replace(tzinfo=pytz.utc)24 end_dt = datetime.datetime(2013, 1, 2).replace(tzinfo=pytz.utc)25 start = iso_8601_datetime_with_milliseconds(start_dt)26 end = iso_8601_datetime_with_milliseconds(end_dt)27 with assert_raises(ClientError) as ex:28 request = conn.request_spot_instances(29 SpotPrice="0.5",30 InstanceCount=1,31 Type="one-time",32 ValidFrom=start,33 ValidUntil=end,34 LaunchGroup="the-group",35 AvailabilityZoneGroup="my-group",36 LaunchSpecification={37 "ImageId": "ami-abcd1234",38 "KeyName": "test",39 "SecurityGroups": ["group1", "group2"],40 "UserData": "some test data",41 "InstanceType": "m1.small",42 "Placement": {"AvailabilityZone": "us-east-1c"},43 "KernelId": "test-kernel",44 "RamdiskId": "test-ramdisk",45 "Monitoring": {"Enabled": True},46 "SubnetId": subnet_id,47 },48 DryRun=True,49 )50 ex.exception.response["Error"]["Code"].should.equal("DryRunOperation")51 ex.exception.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)52 ex.exception.response["Error"]["Message"].should.equal(53 "An error occurred (DryRunOperation) when calling the RequestSpotInstance operation: Request would have succeeded, but DryRun flag is set"54 )55 request = conn.request_spot_instances(56 SpotPrice="0.5",57 InstanceCount=1,58 Type="one-time",59 ValidFrom=start,60 ValidUntil=end,61 LaunchGroup="the-group",62 AvailabilityZoneGroup="my-group",63 LaunchSpecification={64 "ImageId": "ami-abcd1234",65 "KeyName": "test",66 "SecurityGroups": ["group1", "group2"],67 "UserData": "some test data",68 "InstanceType": "m1.small",69 "Placement": {"AvailabilityZone": "us-east-1c"},70 "KernelId": "test-kernel",71 "RamdiskId": "test-ramdisk",72 "Monitoring": {"Enabled": True},73 "SubnetId": subnet_id,74 },75 )76 requests = conn.describe_spot_instance_requests()["SpotInstanceRequests"]77 requests.should.have.length_of(1)78 request = requests[0]79 request["State"].should.equal("open")80 request["SpotPrice"].should.equal("0.5")81 request["Type"].should.equal("one-time")82 request["ValidFrom"].should.equal(start_dt)83 request["ValidUntil"].should.equal(end_dt)84 request["LaunchGroup"].should.equal("the-group")85 request["AvailabilityZoneGroup"].should.equal("my-group")86 launch_spec = request["LaunchSpecification"]87 security_group_names = [88 group["GroupName"] for group in launch_spec["SecurityGroups"]89 ]90 set(security_group_names).should.equal(set(["group1", "group2"]))91 launch_spec["ImageId"].should.equal("ami-abcd1234")92 launch_spec["KeyName"].should.equal("test")93 launch_spec["InstanceType"].should.equal("m1.small")94 launch_spec["KernelId"].should.equal("test-kernel")95 launch_spec["RamdiskId"].should.equal("test-ramdisk")96 launch_spec["SubnetId"].should.equal(subnet_id)97@mock_ec298def test_request_spot_instances_default_arguments():99 """100 Test that moto set the correct default arguments101 """102 conn = boto3.client("ec2", "us-east-1")103 request = conn.request_spot_instances(104 SpotPrice="0.5", LaunchSpecification={"ImageId": "ami-abcd1234"}105 )106 requests = conn.describe_spot_instance_requests()["SpotInstanceRequests"]107 requests.should.have.length_of(1)108 request = requests[0]109 request["State"].should.equal("open")110 request["SpotPrice"].should.equal("0.5")111 request["Type"].should.equal("one-time")112 request.shouldnt.contain("ValidFrom")113 request.shouldnt.contain("ValidUntil")114 request.shouldnt.contain("LaunchGroup")115 request.shouldnt.contain("AvailabilityZoneGroup")116 launch_spec = request["LaunchSpecification"]117 security_group_names = [118 group["GroupName"] for group in launch_spec["SecurityGroups"]119 ]120 security_group_names.should.equal(["default"])121 launch_spec["ImageId"].should.equal("ami-abcd1234")122 request.shouldnt.contain("KeyName")123 launch_spec["InstanceType"].should.equal("m1.small")124 request.shouldnt.contain("KernelId")125 request.shouldnt.contain("RamdiskId")126 request.shouldnt.contain("SubnetId")127@mock_ec2_deprecated128def test_cancel_spot_instance_request():129 conn = boto.connect_ec2()130 conn.request_spot_instances(price=0.5, image_id="ami-abcd1234")131 requests = conn.get_all_spot_instance_requests()132 requests.should.have.length_of(1)133 with assert_raises(EC2ResponseError) as ex:134 conn.cancel_spot_instance_requests([requests[0].id], dry_run=True)135 ex.exception.error_code.should.equal("DryRunOperation")136 ex.exception.status.should.equal(400)137 ex.exception.message.should.equal(138 "An error occurred (DryRunOperation) when calling the CancelSpotInstance operation: Request would have succeeded, but DryRun flag is set"139 )140 conn.cancel_spot_instance_requests([requests[0].id])141 requests = conn.get_all_spot_instance_requests()142 requests.should.have.length_of(0)143@mock_ec2_deprecated144def test_request_spot_instances_fulfilled():145 """146 Test that moto correctly fullfills a spot instance request147 """148 conn = boto.ec2.connect_to_region("us-east-1")149 request = conn.request_spot_instances(price=0.5, image_id="ami-abcd1234")150 requests = conn.get_all_spot_instance_requests()151 requests.should.have.length_of(1)152 request = requests[0]153 request.state.should.equal("open")154 if not settings.TEST_SERVER_MODE:155 ec2_backends["us-east-1"].spot_instance_requests[request.id].state = "active"156 requests = conn.get_all_spot_instance_requests()157 requests.should.have.length_of(1)158 request = requests[0]159 request.state.should.equal("active")160@mock_ec2_deprecated161def test_tag_spot_instance_request():162 """163 Test that moto correctly tags a spot instance request164 """165 conn = boto.connect_ec2()166 request = conn.request_spot_instances(price=0.5, image_id="ami-abcd1234")167 request[0].add_tag("tag1", "value1")168 request[0].add_tag("tag2", "value2")169 requests = conn.get_all_spot_instance_requests()170 requests.should.have.length_of(1)171 request = requests[0]172 tag_dict = dict(request.tags)173 tag_dict.should.equal({"tag1": "value1", "tag2": "value2"})174@mock_ec2_deprecated175def test_get_all_spot_instance_requests_filtering():176 """177 Test that moto correctly filters spot instance requests178 """179 conn = boto.connect_ec2()180 request1 = conn.request_spot_instances(price=0.5, image_id="ami-abcd1234")181 request2 = conn.request_spot_instances(price=0.5, image_id="ami-abcd1234")182 conn.request_spot_instances(price=0.5, image_id="ami-abcd1234")183 request1[0].add_tag("tag1", "value1")184 request1[0].add_tag("tag2", "value2")185 request2[0].add_tag("tag1", "value1")186 request2[0].add_tag("tag2", "wrong")187 requests = conn.get_all_spot_instance_requests(filters={"state": "active"})188 requests.should.have.length_of(0)189 requests = conn.get_all_spot_instance_requests(filters={"state": "open"})190 requests.should.have.length_of(3)191 requests = conn.get_all_spot_instance_requests(filters={"tag:tag1": "value1"})192 requests.should.have.length_of(2)193 requests = conn.get_all_spot_instance_requests(194 filters={"tag:tag1": "value1", "tag:tag2": "value2"}195 )196 requests.should.have.length_of(1)197@mock_ec2_deprecated198def test_request_spot_instances_setting_instance_id():199 conn = boto.ec2.connect_to_region("us-east-1")200 request = conn.request_spot_instances(price=0.5, image_id="ami-abcd1234")201 if not settings.TEST_SERVER_MODE:202 req = ec2_backends["us-east-1"].spot_instance_requests[request[0].id]203 req.state = "active"204 req.instance_id = "i-12345678"205 request = conn.get_all_spot_instance_requests()[0]206 assert request.state == "active"...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful