Best Python code snippet using localstack_python
test_spot_fleet.py
Source:test_spot_fleet.py
...103 launch_spec["SpotPrice"].should.equal("0.13")104 launch_spec["SubnetId"].should.equal(subnet_id)105 launch_spec["UserData"].should.equal("some user data")106 launch_spec["WeightedCapacity"].should.equal(2.0)107 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)108 instances = instance_res["ActiveInstances"]109 len(instances).should.equal(3)110@mock_ec2111def test_create_diversified_spot_fleet():112 conn = boto3.client("ec2", region_name="us-west-2")113 subnet_id = get_subnet_id(conn)114 diversified_config = spot_config(subnet_id, allocation_strategy="diversified")115 spot_fleet_res = conn.request_spot_fleet(SpotFleetRequestConfig=diversified_config)116 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]117 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)118 instances = instance_res["ActiveInstances"]119 len(instances).should.equal(2)120 instance_types = set([instance["InstanceType"] for instance in instances])121 instance_types.should.equal(set(["t2.small", "t2.large"]))122 instances[0]["InstanceId"].should.contain("i-")123@mock_ec2124def test_create_spot_fleet_request_with_tag_spec():125 conn = boto3.client("ec2", region_name="us-west-2")126 subnet_id = get_subnet_id(conn)127 tag_spec = [128 {129 "ResourceType": "instance",130 "Tags": [131 {"Key": "tag-1", "Value": "foo"},132 {"Key": "tag-2", "Value": "bar"},133 ],134 }135 ]136 config = spot_config(subnet_id)137 config["LaunchSpecifications"][0]["TagSpecifications"] = tag_spec138 spot_fleet_res = conn.request_spot_fleet(SpotFleetRequestConfig=config)139 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]140 spot_fleet_requests = conn.describe_spot_fleet_requests(141 SpotFleetRequestIds=[spot_fleet_id]142 )["SpotFleetRequestConfigs"]143 spot_fleet_config = spot_fleet_requests[0]["SpotFleetRequestConfig"]144 spot_fleet_config["LaunchSpecifications"][0]["TagSpecifications"][0][145 "ResourceType"146 ].should.equal("instance")147 for tag in tag_spec[0]["Tags"]:148 spot_fleet_config["LaunchSpecifications"][0]["TagSpecifications"][0][149 "Tags"150 ].should.contain(tag)151 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)152 instances = conn.describe_instances(153 InstanceIds=[i["InstanceId"] for i in instance_res["ActiveInstances"]]154 )155 for instance in instances["Reservations"][0]["Instances"]:156 for tag in tag_spec[0]["Tags"]:157 instance["Tags"].should.contain(tag)158@mock_ec2159def test_cancel_spot_fleet_request():160 conn = boto3.client("ec2", region_name="us-west-2")161 subnet_id = get_subnet_id(conn)162 spot_fleet_res = conn.request_spot_fleet(163 SpotFleetRequestConfig=spot_config(subnet_id)164 )165 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]166 conn.cancel_spot_fleet_requests(167 SpotFleetRequestIds=[spot_fleet_id], TerminateInstances=True168 )169 spot_fleet_requests = conn.describe_spot_fleet_requests(170 SpotFleetRequestIds=[spot_fleet_id]171 )["SpotFleetRequestConfigs"]172 len(spot_fleet_requests).should.equal(0)173@mock_ec2174def test_modify_spot_fleet_request_up():175 conn = boto3.client("ec2", region_name="us-west-2")176 subnet_id = get_subnet_id(conn)177 spot_fleet_res = conn.request_spot_fleet(178 SpotFleetRequestConfig=spot_config(subnet_id)179 )180 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]181 conn.modify_spot_fleet_request(SpotFleetRequestId=spot_fleet_id, TargetCapacity=20)182 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)183 instances = instance_res["ActiveInstances"]184 len(instances).should.equal(10)185 spot_fleet_config = conn.describe_spot_fleet_requests(186 SpotFleetRequestIds=[spot_fleet_id]187 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]188 spot_fleet_config["TargetCapacity"].should.equal(20)189 spot_fleet_config["FulfilledCapacity"].should.equal(20.0)190@mock_ec2191def test_modify_spot_fleet_request_up_diversified():192 conn = boto3.client("ec2", region_name="us-west-2")193 subnet_id = get_subnet_id(conn)194 spot_fleet_res = conn.request_spot_fleet(195 SpotFleetRequestConfig=spot_config(subnet_id, allocation_strategy="diversified")196 )197 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]198 conn.modify_spot_fleet_request(SpotFleetRequestId=spot_fleet_id, TargetCapacity=19)199 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)200 instances = instance_res["ActiveInstances"]201 len(instances).should.equal(7)202 spot_fleet_config = conn.describe_spot_fleet_requests(203 SpotFleetRequestIds=[spot_fleet_id]204 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]205 spot_fleet_config["TargetCapacity"].should.equal(19)206 spot_fleet_config["FulfilledCapacity"].should.equal(20.0)207@mock_ec2208def test_modify_spot_fleet_request_down_no_terminate():209 conn = boto3.client("ec2", region_name="us-west-2")210 subnet_id = get_subnet_id(conn)211 spot_fleet_res = conn.request_spot_fleet(212 SpotFleetRequestConfig=spot_config(subnet_id)213 )214 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]215 conn.modify_spot_fleet_request(216 SpotFleetRequestId=spot_fleet_id,217 TargetCapacity=1,218 ExcessCapacityTerminationPolicy="noTermination",219 )220 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)221 instances = instance_res["ActiveInstances"]222 len(instances).should.equal(3)223 spot_fleet_config = conn.describe_spot_fleet_requests(224 SpotFleetRequestIds=[spot_fleet_id]225 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]226 spot_fleet_config["TargetCapacity"].should.equal(1)227 spot_fleet_config["FulfilledCapacity"].should.equal(6.0)228@mock_ec2229def test_modify_spot_fleet_request_down_odd():230 conn = boto3.client("ec2", region_name="us-west-2")231 subnet_id = get_subnet_id(conn)232 spot_fleet_res = conn.request_spot_fleet(233 SpotFleetRequestConfig=spot_config(subnet_id)234 )235 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]236 conn.modify_spot_fleet_request(SpotFleetRequestId=spot_fleet_id, TargetCapacity=7)237 conn.modify_spot_fleet_request(SpotFleetRequestId=spot_fleet_id, TargetCapacity=5)238 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)239 instances = instance_res["ActiveInstances"]240 len(instances).should.equal(3)241 spot_fleet_config = conn.describe_spot_fleet_requests(242 SpotFleetRequestIds=[spot_fleet_id]243 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]244 spot_fleet_config["TargetCapacity"].should.equal(5)245 spot_fleet_config["FulfilledCapacity"].should.equal(6.0)246@mock_ec2247def test_modify_spot_fleet_request_down():248 conn = boto3.client("ec2", region_name="us-west-2")249 subnet_id = get_subnet_id(conn)250 spot_fleet_res = conn.request_spot_fleet(251 SpotFleetRequestConfig=spot_config(subnet_id)252 )253 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]254 conn.modify_spot_fleet_request(SpotFleetRequestId=spot_fleet_id, TargetCapacity=1)255 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)256 instances = instance_res["ActiveInstances"]257 len(instances).should.equal(1)258 spot_fleet_config = conn.describe_spot_fleet_requests(259 SpotFleetRequestIds=[spot_fleet_id]260 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]261 spot_fleet_config["TargetCapacity"].should.equal(1)262 spot_fleet_config["FulfilledCapacity"].should.equal(2.0)263@mock_ec2264def test_modify_spot_fleet_request_down_no_terminate_after_custom_terminate():265 conn = boto3.client("ec2", region_name="us-west-2")266 subnet_id = get_subnet_id(conn)267 spot_fleet_res = conn.request_spot_fleet(268 SpotFleetRequestConfig=spot_config(subnet_id)269 )270 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]271 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)272 instances = instance_res["ActiveInstances"]273 conn.terminate_instances(InstanceIds=[i["InstanceId"] for i in instances[1:]])274 conn.modify_spot_fleet_request(275 SpotFleetRequestId=spot_fleet_id,276 TargetCapacity=1,277 ExcessCapacityTerminationPolicy="noTermination",278 )279 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)280 instances = instance_res["ActiveInstances"]281 len(instances).should.equal(1)282 spot_fleet_config = conn.describe_spot_fleet_requests(283 SpotFleetRequestIds=[spot_fleet_id]284 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]285 spot_fleet_config["TargetCapacity"].should.equal(1)286 spot_fleet_config["FulfilledCapacity"].should.equal(2.0)287@mock_ec2288def test_create_spot_fleet_without_spot_price():289 conn = boto3.client("ec2", region_name="us-west-2")290 subnet_id = get_subnet_id(conn)291 # remove prices to force a fallback to ondemand price292 spot_config_without_price = spot_config(subnet_id)293 del spot_config_without_price["SpotPrice"]...
index.py
Source:index.py
...17 physical_id = 'describeInstances-{}'.format(spotfleet_request_id)18 print("create new resource with props %s" % props)19 data = {}20 while True:21 result = ec2.describe_spot_fleet_instances(22 SpotFleetRequestId=spotfleet_request_id)23 if 'ActiveInstances' in result and len(result['ActiveInstances']) > 0:24 data = {25 'InstanceId': result['ActiveInstances'][0]['InstanceId'],26 'InstanceType': result['ActiveInstances'][0]['InstanceType'],27 'SpotInstanceRequestId': result['ActiveInstances'][0]['SpotInstanceRequestId']28 }29 break30 else:31 time.sleep(3)32 continue33 return {'PhysicalResourceId': physical_id, 'Data': data}34def on_update(event):35 return on_create(event)36def on_delete(event):37 return38def is_complete(event, context):39 # physical_id = event["PhysicalResourceId"]40 request_type = event["RequestType"]41 props = event["ResourceProperties"]42 # already returns true on delete43 if request_type == 'Delete':44 return {'IsComplete': True}45 spot_fleet_request_id = props['SpotFleetRequestId']46 result = ec2.describe_spot_fleet_instances(47 SpotFleetRequestId=spot_fleet_request_id)48 is_ready = 'ActiveInstances' in result and len(49 result['ActiveInstances']) > 0...
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!!