示例#1
0
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"
示例#2
0
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"
示例#3
0
def test_serialisation(public_aws_instance, ami_amzn):
    """Test serialisation of Instance class to dictionary."""
    instance = Instance(public_aws_instance)

    assert instance.to_dict() == {
        "id": public_aws_instance["InstanceId"],
        "private_ip": public_aws_instance["PrivateIpAddress"],
        "public_ip": public_aws_instance["PublicIpAddress"],
        "state": public_aws_instance["State"]["Name"],
        "type": INSTANCE_TYPE,
        "image": ami_amzn["ImageId"],
        "keyname": KEY_NAME,
        "tags": {
            "Name": PUBLIC_INSTANCE_NAME
        },
    }
示例#4
0
def test_data_extraction(public_aws_instance, ami_amzn):
    """Test extraction of data from AWS format."""
    instance = Instance(public_aws_instance)

    assert instance.id == public_aws_instance["InstanceId"]
    assert instance.private_ip == public_aws_instance["PrivateIpAddress"]
    assert instance.public_ip == public_aws_instance["PublicIpAddress"]
    assert instance.state == public_aws_instance["State"]["Name"]
    assert instance.type == INSTANCE_TYPE
    assert instance.image == ami_amzn["ImageId"]
    assert instance.keyname == KEY_NAME
示例#5
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"
示例#6
0
def test_deserialisation():
    """Test deserialisation of Instance class from dictionary."""
    instance_dict = {
        "id": "i-123abc",
        "private_ip": "10.0.0.1",
        "public_ip": "1.2.3.4",
        "state": "running",
        "type": INSTANCE_TYPE,
        "image": IMAGE_NAME,
        "keyname": KEY_NAME,
        "tags": {
            "Name": PUBLIC_INSTANCE_NAME
        },
    }

    instance = Instance.from_dict(instance_dict)
    assert instance.to_dict() == instance_dict
示例#7
0
def get_instances(cache_dir):
    if not cache_dir.exists():
        os.makedirs(cache_dir)

    region_suffix = "default"
    cache_path = cache_dir / f"instances-{region_suffix}.json"
    cached_response = {}
    instances = []

    if cache_path.exists():
        with open(cache_path) as cache_file:
            cached_response = json.load(cache_file)

    cache_updated = datetime.datetime.fromtimestamp(cached_response.get(
        "fetched_at", 0),
                                                    tz=datetime.timezone.utc)
    now = datetime.datetime.now(datetime.timezone.utc)

    if cache_updated < (now - datetime.timedelta(minutes=1)):
        instances = _get_fresh_instances()

        with open(cache_path, "w+") as cache_file:
            json.dump(
                {
                    "fetched_at": now.timestamp(),
                    "instances":
                    [instance.to_dict() for instance in instances],
                },
                cache_file,
            )
    else:
        instances = [
            Instance.from_dict(instance)
            for instance in cached_response["instances"]
        ]

    return instances
示例#8
0
def test_ubuntu_username_retrieval(ec2: botostubs.EC2, ami_ubuntu,
                                   private_aws_instance):
    """Tests username resolution expecting ubuntu."""
    instance = Instance(private_aws_instance)

    assert instance.default_username() == "ubuntu"
示例#9
0
def test_amazon_linux_username_retrieval(ec2: botostubs.EC2, ami_amzn,
                                         public_aws_instance):
    """Tests username resolution expecting ec2-user."""
    instance = Instance(public_aws_instance)

    assert instance.default_username() == "ec2-user"
示例#10
0
def test_parse_private_instance(private_aws_instance):
    """Test parsing of a private instance (without a public IP)."""
    instance = Instance(private_aws_instance)

    assert instance.public_ip == None
示例#11
0
def test_name_extraction(public_aws_instance):
    """Test extraction of Name from tags of an instance."""
    instance = Instance(public_aws_instance)

    assert instance.name == PUBLIC_INSTANCE_NAME