def fxt_ami_custom(ec2: botostubs.EC2, public_subnet): instance = ec2.run_instances(**DEFAULT_INSTANCE_KWARGS, ImageId=IMAGE_NAME) instance_id = instance["Instances"][0]["InstanceId"] image = ec2.create_image(Name="mock_custom_application", InstanceId=instance_id) yield image
def fxt_ami_centos(ec2: botostubs.EC2, public_subnet): instance = ec2.run_instances(**DEFAULT_INSTANCE_KWARGS, ImageId=IMAGE_NAME) instance_id = instance["Instances"][0]["InstanceId"] image = ec2.create_image( Description="Mock CentOS Linux Image", Name="mock_centos_linux", InstanceId=instance_id, ) yield image
def test_no_description_username_fallback(ec2: botostubs.EC2, ami_custom, public_subnet): """Tests username resolution expecting the default fallback to ec2-user.""" new_instance = ec2.run_instances( **DEFAULT_INSTANCE_KWARGS, ImageId=ami_custom["ImageId"], SubnetId=public_subnet["SubnetId"], ) instance_id = new_instance["Instances"][0]["InstanceId"] instances = ec2.describe_instances(InstanceIds=[instance_id]) instance = Instance(instances["Reservations"][0]["Instances"][0]) assert instance.default_username() == "ec2-user"
def test_centos_username_retreival(ec2: botostubs.EC2, ami_centos, public_subnet): """Tests username resolution expecting centos.""" new_centos_instance = ec2.run_instances( **DEFAULT_INSTANCE_KWARGS, ImageId=ami_centos["ImageId"], SubnetId=public_subnet["SubnetId"], ) instance_id = new_centos_instance["Instances"][0]["InstanceId"] instances = ec2.describe_instances(InstanceIds=[instance_id]) instance = Instance(instances["Reservations"][0]["Instances"][0]) assert instance.default_username() == "centos"
def fxt_terminated_aws_instance(ec2: botostubs.EC2, public_subnet, ami_amzn): new_instance = ec2.run_instances( **DEFAULT_INSTANCE_KWARGS, ImageId=ami_amzn["ImageId"], SubnetId=public_subnet["SubnetId"], TagSpecifications=[{ "ResourceType": "instance", "Tags": [{ "Key": "Name", "Value": "Terminated Instance" }], }], ) instance_id = new_instance["Instances"][0]["InstanceId"] ec2.terminate_instances(InstanceIds=[instance_id])
def fxt_public_aws_instance(ec2: botostubs.EC2, public_subnet, ami_amzn): new_instance = ec2.run_instances( **DEFAULT_INSTANCE_KWARGS, ImageId=ami_amzn["ImageId"], SubnetId=public_subnet["SubnetId"], TagSpecifications=[{ "ResourceType": "instance", "Tags": [{ "Key": "Name", "Value": PUBLIC_INSTANCE_NAME }], }], ) instance_id = new_instance["Instances"][0]["InstanceId"] instances = ec2.describe_instances(InstanceIds=[instance_id]) yield instances["Reservations"][0]["Instances"][0]
def test_custom_username_matcher(ec2: botostubs.EC2, ami_ubuntu, public_subnet): """Test custom username matchers.""" new_instance = ec2.run_instances( **DEFAULT_INSTANCE_KWARGS, ImageId=ami_ubuntu["ImageId"], SubnetId=public_subnet["SubnetId"], ) instance_id = new_instance["Instances"][0]["InstanceId"] instances = ec2.describe_instances(InstanceIds=[instance_id]) instance = Instance(instances["Reservations"][0]["Instances"][0]) image_name_ubuntu_test = {"username": "******", "image-name": "ubuntu"} assert instance.default_username([image_name_ubuntu_test]) == "test" image_name_ubuntu_regex = { "username": "******", "image-name": "^.*ubuntu.*$" } assert instance.default_username([image_name_ubuntu_regex]) == "test" description_ubuntu_test2 = {"username": "******", "description": "ubuntu"} assert instance.default_username([description_ubuntu_test2]) == "test2"
def waitInstanceTerminated(instanceId, client: botostubs.EC2): waiter: botostubs.EC2.InstanceTerminatedWaiter = client.get_waiter( 'instance_terminated') waiter.wait(InstanceIds=[instanceId])
def waitInstanceStopped(instanceId, client: botostubs.EC2): waiter: botostubs.EC2.InstanceStoppedWaiter = client.get_waiter( 'instance_stopped') waiter.wait(InstanceIds=[instanceId])
def waitInstanceStatusOK(instanceId, client: botostubs.EC2): waiter: botostubs.EC2.InstanceStatusOkWaiter = client.get_waiter( 'instance_status_ok') waiter.wait(InstanceIds=[instanceId])
def fxt_private_subnet(ec2: botostubs.EC2, vpc): subnet = ec2.create_subnet(VpcId=vpc["VpcId"], CidrBlock="10.0.1.0/24") yield subnet["Subnet"]
def fxt_public_subnet(ec2: botostubs.EC2, vpc): subnet = ec2.create_subnet(VpcId=vpc["VpcId"], CidrBlock="10.0.0.0/24") ec2.modify_subnet_attribute(SubnetId=subnet["Subnet"]["SubnetId"], MapPublicIpOnLaunch={"Value": True}) yield subnet["Subnet"]
def fxt_vpc(ec2: botostubs.EC2): vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16", DryRun=False) yield vpc["Vpc"]