Skip to content

Commit

Permalink
test: Add instance id integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
holmanb committed Jul 30, 2024
1 parent 22cf638 commit 291485f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/integration_tests/integration_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# <image_id>[::<os>::<release>::<version>]. If given, os and release should
# describe the image specified by image_id. (Ubuntu releases are converted
# to this format internally; in this case, to "None::ubuntu::focal::20.04".)
OS_IMAGE = "focal"
OS_IMAGE = "jammy"

# Populate if you want to use a pre-launched instance instead of
# creating a new one. The exact contents will be platform dependent
Expand Down Expand Up @@ -69,7 +69,7 @@
# Install from a PPA. It MUST start with 'ppa:'
# <file path>
# A path to a valid package to be uploaded and installed
CLOUD_INIT_SOURCE = "NONE"
CLOUD_INIT_SOURCE = "IN_PLACE"

# Before an instance is torn down, we run `cloud-init collect-logs`
# and transfer them locally. These settings specify when to collect these
Expand Down
97 changes: 97 additions & 0 deletions tests/integration_tests/test_instance_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from typing import cast

import pytest
from pycloudlib.lxd.instance import LXDInstance

from cloudinit import subp
from tests.integration_tests.instances import IntegrationInstance
from tests.integration_tests.integration_settings import PLATFORM

_INSTANCE_ID = 0


def setup_meta_data(instance: LXDInstance):
"""Increment the instance id and apply it to the instance."""
global _INSTANCE_ID
_INSTANCE_ID += 1
command = [
"lxc",
"config",
"set",
instance.name,
f"user.meta-data=instance-id: test_{_INSTANCE_ID}",
]
subp.subp(command)


# class TestInstanceID:
@pytest.mark.skipif(
PLATFORM not in ["lxd_container", "lxd_vm"],
reason="Uses lxd-specific behavior.",
)
@pytest.mark.lxd_setup.with_args(setup_meta_data)
@pytest.mark.lxd_use_exec
def test_instance_id_changes(client: IntegrationInstance):
"""Verify instance id change behavior
If the id from the datasource changes, cloud-init should update the
instance id link.
"""
client.execute("cloud-init status --wait")
# check that instance id is the one we set
assert (
"test_1"
== client.execute("cloud-init query instance-id").stdout.rstrip()
)
assert (
"/var/lib/cloud/instances/test_1"
== client.execute(
"readlink -f /var/lib/cloud/instance"
).stdout.rstrip()
)

instance = cast(LXDInstance, client.instance)
setup_meta_data(instance)
client.restart()
client.execute("cloud-init status --wait")
# check that instance id is the one we reset
assert (
"test_2"
== client.execute("cloud-init query instance-id").stdout.rstrip()
)
assert (
"/var/lib/cloud/instances/test_2"
== client.execute(
"readlink -f /var/lib/cloud/instance"
).stdout.rstrip()
)


@pytest.mark.lxd_use_exec
def test_instance_id_no_changes(client: IntegrationInstance):
"""Verify instance id no change behavior
If the id from the datasource does not change, cloud-init should not
update the instance id link.
"""
instance_id = client.execute(
"cloud-init query instance-id"
).stdout.rstrip()
assert (
f"/var/lib/cloud/instances/{instance_id}"
== client.execute(
"readlink -f /var/lib/cloud/instance"
).stdout.rstrip()
)
client.restart()
client.execute("cloud-init status --wait")
assert (
instance_id
== client.execute("cloud-init query instance-id").stdout.rstrip()
)
assert (
f"/var/lib/cloud/instances/{instance_id}"
== client.execute(
"readlink -f /var/lib/cloud/instance"
).stdout.rstrip()
)

0 comments on commit 291485f

Please sign in to comment.