2021-10-19 13:04:30 +00:00
|
|
|
import ipaddress
|
2023-11-30 15:55:51 +00:00
|
|
|
from copy import deepcopy
|
2021-10-19 13:04:30 +00:00
|
|
|
from unittest.mock import patch
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2022-02-17 13:38:31 +00:00
|
|
|
from pytest import raises
|
2021-10-19 13:04:30 +00:00
|
|
|
|
2015-09-15 23:55:26 +00:00
|
|
|
from moto.ec2 import utils
|
|
|
|
|
2023-10-12 18:13:41 +00:00
|
|
|
from .helpers import check_private_key
|
2019-05-25 10:17:52 +00:00
|
|
|
|
2015-09-15 23:55:26 +00:00
|
|
|
|
|
|
|
def test_random_key_pair():
|
2023-10-12 18:13:41 +00:00
|
|
|
key_pair = utils.random_rsa_key_pair()
|
|
|
|
check_private_key(key_pair["material"], "rsa")
|
2019-05-25 10:17:52 +00:00
|
|
|
|
|
|
|
# AWS uses MD5 fingerprints, which are 47 characters long, *not* SHA1
|
|
|
|
# fingerprints with 59 characters.
|
|
|
|
assert len(key_pair["fingerprint"]) == 47
|
2021-10-19 13:04:30 +00:00
|
|
|
|
2023-10-12 18:13:41 +00:00
|
|
|
key_pair = utils.random_ed25519_key_pair()
|
|
|
|
check_private_key(key_pair["material"], "ed25519")
|
|
|
|
|
2021-10-19 13:04:30 +00:00
|
|
|
|
|
|
|
def test_random_ipv6_cidr():
|
|
|
|
def mocked_random_resource_id(chars: int):
|
|
|
|
return "a" * chars
|
|
|
|
|
|
|
|
with patch("moto.ec2.utils.random_resource_id", mocked_random_resource_id):
|
|
|
|
cidr_address = utils.random_ipv6_cidr()
|
|
|
|
# this will throw value error if host bits are set
|
|
|
|
ipaddress.ip_network(cidr_address)
|
2022-02-17 13:38:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_gen_moto_amis():
|
|
|
|
image_with_all_reqd_keys = {
|
|
|
|
"ImageId": "ami-03cf127a",
|
|
|
|
"State": "available",
|
|
|
|
"Public": True,
|
|
|
|
"OwnerId": "801119661308",
|
|
|
|
"RootDeviceType": "ebs",
|
|
|
|
"RootDeviceName": "/dev/sda1",
|
|
|
|
"Description": "Microsoft Windows Server 2016 Nano Locale English AMI provided by Amazon",
|
|
|
|
"ImageType": "machine",
|
|
|
|
"Architecture": "x86_64",
|
|
|
|
"Name": "Windows_Server-2016-English-Nano-Base-2017.10.13",
|
|
|
|
"VirtualizationType": "hvm",
|
|
|
|
"Hypervisor": "xen",
|
|
|
|
}
|
|
|
|
|
|
|
|
images = []
|
|
|
|
images.append(deepcopy(image_with_all_reqd_keys))
|
|
|
|
images.append(deepcopy(image_with_all_reqd_keys))
|
|
|
|
|
|
|
|
# make one of the copies of the image miss a key
|
|
|
|
images[1].pop("Public")
|
|
|
|
|
|
|
|
# with drop=True, it shouldn't throw but will give us only one AMI in the result
|
2023-07-17 09:31:05 +00:00
|
|
|
assert len(images) == 2
|
2022-02-17 13:38:31 +00:00
|
|
|
amis = utils.gen_moto_amis(images, drop_images_missing_keys=True)
|
2023-07-17 09:31:05 +00:00
|
|
|
assert len(amis) == 1
|
2022-02-17 13:38:31 +00:00
|
|
|
|
|
|
|
# with drop=False, it should raise KeyError because of the missing key
|
|
|
|
with raises(KeyError, match="'Public'"):
|
|
|
|
utils.gen_moto_amis(images, drop_images_missing_keys=False)
|