Skip to content

Commit

Permalink
fix(ec2): ensure ec2 minimal image search string works
Browse files Browse the repository at this point in the history
Ensured 100% unit test coverage for ec2._get_name_for_image_type().
  • Loading branch information
a-dubs authored and blackboxsw committed Sep 25, 2024
1 parent 3381503 commit 0ffd035
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
18 changes: 11 additions & 7 deletions pycloudlib/ec2/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,24 @@ def _get_name_for_image_type(
):
disk_type = "hvm-ssd" if release in NO_GP3_RELEASES else "hvm-ssd-gp3"
if image_type in (ImageType.GENERIC, ImageType.MINIMAL):
base_location = "ubuntu/{images_path}/{disk_type}".format(
images_path="images-testing" if daily else "images",
disk_type=disk_type,
base_location = "ubuntu{}/{}/{}".format(
"-minimal" if image_type == ImageType.MINIMAL else "",
"images-testing" if daily else "images",
disk_type,
)
if release in LTS_RELEASES:
return "{}/ubuntu-{}{}-*-server{}-*".format(
return "{}/ubuntu-{}{}-*-{}-*".format(
base_location,
release,
"-daily" if daily else "",
"-minimal" if image_type == ImageType.MINIMAL else "",
"minimal" if image_type == ImageType.MINIMAL else "server",
)

return "{}/ubuntu-{}{}-*".format(
base_location, release, "-daily" if daily else ""
return "{}/ubuntu-{}{}-*-{}-*".format(
base_location,
release,
"-daily" if daily else "",
"minimal" if image_type == ImageType.MINIMAL else "server",
)

release_ver = UBUNTU_RELEASE_VERSION_MAP.get(release)
Expand Down
56 changes: 50 additions & 6 deletions tests/unit_tests/ec2/test_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TestEC2:
@pytest.mark.parametrize(
["release", "image_type", "daily", "expected_name_filter"],
[
# Test GENERIC with LTS release and daily = True
pytest.param(
"focal",
ImageType.GENERIC,
Expand All @@ -45,32 +46,64 @@ class TestEC2:
"jammy",
ImageType.MINIMAL,
True,
"ubuntu/images-testing/hvm-ssd/ubuntu-jammy-daily-*-server-minimal-*",
"ubuntu-minimal/images-testing/hvm-ssd/ubuntu-jammy-daily-*-minimal-*",
id="minimal-lts-daily",
),
# Test MINIMAL with LTS release and daily = False
pytest.param(
"noble",
ImageType.MINIMAL,
False,
"ubuntu/images/hvm-ssd-gp3/ubuntu-noble-*-server-minimal-*",
"ubuntu-minimal/images/hvm-ssd-gp3/ubuntu-noble-*-minimal-*",
id="minimal-lts-non-daily",
),
# Test PRO with non-LTS release
# Test PRO with LTS release
pytest.param(
"jammy",
ImageType.PRO,
False,
"ubuntu-pro-server/images/hvm-ssd/ubuntu-jammy-22.04-*",
id="pro-non-lts",
id="pro-lts",
),
# Test PRO_FIPS with non-LTS release
# Test PRO_FIPS with LTS release
pytest.param(
"noble",
ImageType.PRO_FIPS,
False,
"ubuntu-pro-fips*/images/hvm-ssd-gp3/ubuntu-noble-24.04-*",
id="pro-fips-non-lts",
id="pro-fips-lts",
),
# Test GENERIC with non-LTS release and daily = False
pytest.param(
"oracular",
ImageType.GENERIC,
False,
"ubuntu/images/hvm-ssd-gp3/ubuntu-oracular-*-server-*",
id="generic-non-lts-non-daily",
),
# Test MINIMAL with non-LTS release and daily = False
pytest.param(
"oracular",
ImageType.MINIMAL,
False,
"ubuntu-minimal/images/hvm-ssd-gp3/ubuntu-oracular-*-minimal-*",
id="minimal-non-lts-non-daily",
),
# Test GENERIC with non-LTS release and daily = True
pytest.param(
"oracular",
ImageType.GENERIC,
True,
"ubuntu/images-testing/hvm-ssd-gp3/ubuntu-oracular-daily-*-server-*",
id="generic-non-lts-daily",
),
# Test MINIMAL with non-LTS release and daily = True
pytest.param(
"oracular",
ImageType.MINIMAL,
True,
"ubuntu-minimal/images-testing/hvm-ssd-gp3/ubuntu-oracular-daily-*-minimal-*",
id="minimal-non-lts-daily",
),
],
)
Expand All @@ -91,6 +124,17 @@ def test_get_name_for_image_type(
)
assert result == expected_name_filter

def test_get_name_for_image_type_invalid_image_type(self):
"""
Test the _get_name_for_image_type() method with an invalid ImageType
"""
ec2 = FakeEC2()
with pytest.raises(ValueError) as exc_info:
ec2._get_name_for_image_type(
release="focal", image_type=None, daily=True
)
assert "Invalid image_type" in str(exc_info.value)

def test_get_owner_for_all_image_types(self):
"""
Test the _get_project() method against all possible ImageType enum values
Expand Down

0 comments on commit 0ffd035

Please sign in to comment.