Skip to content

Commit

Permalink
test: test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yanksyoon committed Apr 10, 2024
1 parent e7deb4b commit 69d22bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
16 changes: 8 additions & 8 deletions src-docs/openstack_manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Module for handling interactions with OpenStack.
**Global Variables**
---------------
- **IMAGE_PATH_TMPL**
- **IMAGE_NAME**
- **IMAGE_NAME_TMPL**
- **BUILD_OPENSTACK_IMAGE_SCRIPT_FILENAME**

---

<a href="../src/openstack_cloud/openstack_manager.py#L299"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/openstack_cloud/openstack_manager.py#L307"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `build_image`

Expand Down Expand Up @@ -51,7 +51,7 @@ Build and upload an image to OpenStack.

---

<a href="../src/openstack_cloud/openstack_manager.py#L344"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/openstack_cloud/openstack_manager.py#L354"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `create_instance_config`

Expand Down Expand Up @@ -83,7 +83,7 @@ Create an instance config from charm data.

---

<a href="../src/utilities.py#L420"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/utilities.py#L430"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `create_instance`

Expand Down Expand Up @@ -116,7 +116,7 @@ Create an OpenStack instance.

---

<a href="../src/openstack_cloud/openstack_manager.py#L81"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/openstack_cloud/openstack_manager.py#L82"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>class</kbd> `ProxyStringValues`
Wrapper class to proxy values to string.
Expand All @@ -135,7 +135,7 @@ Wrapper class to proxy values to string.

---

<a href="../src/openstack_cloud/openstack_manager.py#L198"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/openstack_cloud/openstack_manager.py#L199"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>class</kbd> `InstanceConfig`
The configuration values for creating a single runner instance.
Expand Down Expand Up @@ -174,7 +174,7 @@ __init__(

---

<a href="../src/openstack_cloud/openstack_manager.py#L247"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/openstack_cloud/openstack_manager.py#L248"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>class</kbd> `BuildImageConfig`
The configuration values for building openstack image.
Expand Down Expand Up @@ -209,7 +209,7 @@ __init__(

---

<a href="../src/openstack_cloud/openstack_manager.py#L262"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/openstack_cloud/openstack_manager.py#L263"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>class</kbd> `ImageDeleteError`
Represents an error while deleting existing openstack image.
Expand Down
24 changes: 17 additions & 7 deletions src/openstack_cloud/openstack_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@

logger = logging.getLogger(__name__)

IMAGE_PATH_TMPL = "jammy-server-cloudimg-{architecture}-compressed.img"
IMAGE_NAME = "jammy"
IMAGE_PATH_TMPL = "{base_image}-server-cloudimg-{architecture}-compressed.img"
# Update the version when the image are modified.
IMAGE_NAME_TMPL = "github-runner-{base_image}-v1"
BUILD_OPENSTACK_IMAGE_SCRIPT_FILENAME = "scripts/build-openstack-image.sh"


Expand Down Expand Up @@ -263,12 +264,15 @@ class ImageDeleteError(Exception):
"""Represents an error while deleting existing openstack image."""


def _put_image(cloud_config: dict[str, dict], image_arch: SupportedCloudImageArch) -> str:
def _put_image(
cloud_config: dict[str, dict], image_arch: SupportedCloudImageArch, base_image: BaseImage
) -> str:
"""Create or replace the image with existing name.
Args:
cloud_config: The cloud configuration to connect OpenStack with.
image_arch: Ubuntu cloud image architecture.
base_image: The ubuntu base image to use.
Raises:
ImageDeleteError: If there was an error deleting the image.
Expand All @@ -280,14 +284,18 @@ def _put_image(cloud_config: dict[str, dict], image_arch: SupportedCloudImageArc
try:
with _create_connection(cloud_config) as conn:
existing_image: openstack.image.v2.image.Image
for existing_image in conn.search_images(name_or_id=IMAGE_NAME):
for existing_image in conn.search_images(
name_or_id=IMAGE_NAME_TMPL.format(base_image=base_image)
):
# images with same name (different ID) can be created and will error during server
# instantiation.
if not conn.delete_image(name_or_id=existing_image.id, wait=True):
raise ImageDeleteError("Failed to delete duplicate image on Openstack.")
image: openstack.image.v2.image.Image = conn.create_image(
name=IMAGE_NAME,
filename=IMAGE_PATH_TMPL.format(architecture=image_arch),
name=IMAGE_NAME_TMPL.format(base_image=base_image.value),
filename=IMAGE_PATH_TMPL.format(
architecture=image_arch, base_image=base_image.value
),
wait=True,
)
return image.id
Expand Down Expand Up @@ -336,7 +344,9 @@ def build_image(
raise OpenstackImageBuildError(f"Unsupported architecture {runner_arch}") from exc

try:
return _put_image(cloud_config=cloud_config, image_arch=image_arch)
return _put_image(
cloud_config=cloud_config, image_arch=image_arch, base_image=config.base_image
)
except (ImageDeleteError, OpenStackCloudException) as exc:
raise OpenstackImageBuildError(f"Failed to upload image: {str(exc)}") from exc

Expand Down
20 changes: 13 additions & 7 deletions tests/integration/test_openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
from openstack.compute.v2.server import Server

from charm_state import BASE_IMAGE_CONFIG_NAME
from tests.integration.helpers import (
DISPATCH_E2E_TEST_RUN_WORKFLOW_FILENAME,
dispatch_workflow,
ensure_charm_has_runner,
)
from tests.integration.helpers import DISPATCH_E2E_TEST_RUN_WORKFLOW_FILENAME, dispatch_workflow


# 2024/03/19 - The firewall configuration on openstack will be implemented by follow up PR on
Expand Down Expand Up @@ -65,6 +61,7 @@ async def test_openstack_integration(
async def test_noble_base_image(
model: Model,
app_openstack_runner: Application,
openstack_connection: openstack.connection.Connection,
github_repository: Repository,
test_github_branch: Branch,
) -> None:
Expand All @@ -78,7 +75,9 @@ async def test_noble_base_image(
BASE_IMAGE_CONFIG_NAME: "noble",
}
)
await ensure_charm_has_runner(app_openstack_runner, model)
await model.wait_for_idle(apps=[app_openstack_runner.name], status="blocked", timeout=40 * 60)

# 1. when the e2e_test_run workflow is created.
workflow = await dispatch_workflow(
app=app_openstack_runner,
branch=test_github_branch,
Expand All @@ -87,6 +86,13 @@ async def test_noble_base_image(
workflow_id_or_name=DISPATCH_E2E_TEST_RUN_WORKFLOW_FILENAME,
dispatch_input={"runner-tag": app_openstack_runner.name},
)

# 1. the workflow run completes successfully.
workflow_run: WorkflowRun = workflow.get_runs()[0]
assert workflow_run.status == "success"

# 2. when the servers are listed.
servers = openstack_connection.list_servers(detailed=True)
assert len(servers) == 1, f"Unexpected number of servers: {len(servers)}"
server: Server = servers[0]
# 2. a server with image name noble is created.
assert server.image.name == "noble"

0 comments on commit 69d22bf

Please sign in to comment.