Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minimal images on gce and ec2 #416

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1!9.1.0
1!9.1.1
16 changes: 10 additions & 6 deletions pycloudlib/ec2/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,17 @@ def _get_name_for_image_type(
self, release: str, image_type: ImageType, daily: bool
):
disk_type = "hvm-ssd" if release in NO_GP3_RELEASES else "hvm-ssd-gp3"
if image_type == ImageType.GENERIC:
base_location = "ubuntu/{image_type}/{disk_type}".format(
image_type="images-testing" if daily else "images",
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,
)
if release in LTS_RELEASES:
return "{}/ubuntu-{}{}-*-server-*".format(
base_location, release, "-daily" if daily else ""
return "{}/ubuntu-{}{}-*-server{}-*".format(
base_location,
release,
"-daily" if daily else "",
"-minimal" if image_type == ImageType.MINIMAL else "",
)

return "{}/ubuntu-{}{}-*".format(
Expand All @@ -170,7 +173,8 @@ def _get_name_for_image_type(
def _get_owner(self, image_type: ImageType):
return (
"099720109477"
if image_type in (ImageType.GENERIC, ImageType.PRO)
if image_type
in (ImageType.GENERIC, ImageType.MINIMAL, ImageType.PRO)
else "aws-marketplace"
)

Expand Down
7 changes: 6 additions & 1 deletion pycloudlib/gce/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def released_image(
def _get_project(self, image_type: ImageType):
return (
"ubuntu-os-cloud-devel"
if image_type == ImageType.GENERIC
if image_type in (ImageType.GENERIC, ImageType.MINIMAL)
else "ubuntu-os-pro-cloud"
)

Expand All @@ -149,6 +149,11 @@ def _get_name_filter(self, release: str, image_type: ImageType):
UBUNTU_RELEASE_VERSION_MAP[release].replace(".", ""), release
)

if image_type == ImageType.MINIMAL:
return "daily-ubuntu-minimal-{}-{}-*".format(
UBUNTU_RELEASE_VERSION_MAP[release].replace(".", ""), release
)

if image_type == ImageType.PRO:
return "ubuntu-pro-{}-{}-*".format(
UBUNTU_RELEASE_VERSION_MAP[release].replace(".", ""), release
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/test_public_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_public_api(cloud: BaseCloud):
],
indirect=True,
)
def test_public_api_mininal_images(cloud: BaseCloud):
def test_public_api_minimal_images(cloud: BaseCloud):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I much prefer mininal.... But, if we prefer to be correct and all +1.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bahahahah

latest_lts = LTS_RELEASES[-1]
print(
f"Checking latest {cloud.__class__.__name__} daily minimal image: "
Expand Down
Empty file.
114 changes: 114 additions & 0 deletions tests/unit_tests/ec2/test_cloud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""Tests related to pycloudlib.gce.cloud module."""

import mock
import pytest

from pycloudlib.cloud import ImageType
from pycloudlib.ec2.cloud import EC2

# mock module path
MPATH = "pycloudlib.ec2.cloud."


class FakeEC2(EC2):
"""EC2 Class that doesn't load config or make requests during __init__."""

# pylint: disable=super-init-not-called
def __init__(self, *_, **__):
"""Fake __init__ that sets mocks for needed variables."""


# pylint: disable=protected-access,missing-function-docstring
class TestEC2:
"""General EC2 testing."""

@pytest.mark.parametrize(
["release", "image_type", "daily", "expected_name_filter"],
[
pytest.param(
"focal",
ImageType.GENERIC,
True,
"ubuntu/images-testing/hvm-ssd/ubuntu-focal-daily-*-server-*",
id="generic-lts-daily",
),
# Test GENERIC with LTS release and daily = False
pytest.param(
"noble",
ImageType.GENERIC,
False,
"ubuntu/images/hvm-ssd-gp3/ubuntu-noble-*-server-*",
id="generic-lts-non-daily",
),
# Test MINIMAL with LTS release and daily = True
pytest.param(
"jammy",
ImageType.MINIMAL,
True,
"ubuntu/images-testing/hvm-ssd/ubuntu-jammy-daily-*-server-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-*",
id="minimal-lts-non-daily",
),
# Test PRO with non-LTS release
pytest.param(
"jammy",
ImageType.PRO,
False,
"ubuntu-pro-server/images/hvm-ssd/ubuntu-jammy-22.04-*",
id="pro-non-lts",
),
# Test PRO_FIPS with non-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",
),
],
)
def test_get_name_for_image_type(
self,
release: str,
image_type: ImageType,
daily: str,
expected_name_filter: str,
):
"""
Test the _get_name_for_image_type() method against various
combinations of release, image_type, and daily
"""
ec2 = FakeEC2()
result = ec2._get_name_for_image_type(
release=release, image_type=image_type, daily=daily
)
assert result == expected_name_filter

def test_get_owner_for_all_image_types(self):
"""
Test the _get_project() method against all possible ImageType enum values
"""
expected_project_per_image_type = {
ImageType.GENERIC: "099720109477",
ImageType.MINIMAL: "099720109477",
ImageType.PRO: "099720109477",
ImageType.PRO_FIPS: "aws-marketplace",
}

ec2 = FakeEC2()

# for each value of ImageType, check if it is in the expected_project_per_image_type dict
# if not, then the test will fail because a new ImageType was added
for image_type in ImageType:
assert image_type in expected_project_per_image_type
assert (
ec2._get_owner(image_type)
== expected_project_per_image_type[image_type]
)
35 changes: 33 additions & 2 deletions tests/unit_tests/gce/test_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_query_image_list( # noqa: D102
expected_filter_calls,
expected_image_list,
):
gce = FakeGCE(tag="tag")
gce = FakeGCE()
with mock.patch.object(gce, "compute") as m_compute:
m_execute = mock.MagicMock(
name="m_execute", side_effect=api_side_effects
Expand Down Expand Up @@ -142,7 +142,7 @@ def test_daily_image_returns_latest_from_query( # noqa: D102
m_get_name_filter,
m_query_image_list,
):
gce = FakeGCE(tag="tag")
gce = FakeGCE()
image = gce.daily_image(
"jammy", arch="x86_64", image_type=ImageType.GENERIC
)
Expand All @@ -156,3 +156,34 @@ def test_daily_image_returns_latest_from_query( # noqa: D102
mock.call("jammy", "project-name", "name-filter", "x86_64")
]
assert image == "projects/project-name/global/images/4"

@pytest.mark.parametrize(
["release", "image_type", "expected_name_filter"],
[
pytest.param(
"jammy",
ImageType.GENERIC,
"daily-ubuntu-2204-jammy-*",
),
pytest.param(
"noble",
ImageType.MINIMAL,
"daily-ubuntu-minimal-2404-noble-*",
),
pytest.param(
"focal",
ImageType.PRO,
"ubuntu-pro-2004-focal-*",
),
pytest.param(
"focal",
ImageType.PRO_FIPS,
"ubuntu-pro-fips-2004-focal-*",
),
],
)
def test_get_name_filter(self, release, image_type, expected_name_filter):
gce = FakeGCE()
assert (
gce._get_name_filter(release, image_type) == expected_name_filter
)
Loading