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

testing: Add NoCloud setup for NoCloud test (SC-983) #1425

Merged
merged 4 commits into from
May 5, 2022

Conversation

TheRealFalcon
Copy link
Member

Proposed Commit Message

testing: Add NoCloud setup for NoCloud test

tests.integration_tests.bugs.test_gh570.test_nocloud_seedfrom_vendordata
is currently failing on Jammy because Jammy no longer uses NoCloud
and instead uses the LXD DataSource. This commit adds a NoCloud
setup to the failing test so we can force the NoCloud datasource.

Also, I moved the test into a NoCloud specific file, so any other
tests that need to be NoCloud specific can use the setup.

Additional Context

Review commits in order to see the actual changes

Failing test:
https://jenkins.canonical.com/server-team/view/cloud-init/job/cloud-init-integration-jammy-lxd_container/20/testReport/tests.integration_tests.bugs/test_gh570/test_nocloud_seedfrom_vendordata/

@TheRealFalcon
Copy link
Member Author

I'll fix the mypy issue, but should otherwise be reviewable

@TheRealFalcon TheRealFalcon changed the title testing: Add NoCloud setup for NoCloud test testing: Add NoCloud setup for NoCloud test (SC-983) May 4, 2022
Copy link
Collaborator

@blackboxsw blackboxsw left a comment

Choose a reason for hiding this comment

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

Thanks for this cleanup.

str(userdata_file),
str(metadata_file),
f"{instance.name}/var/lib/cloud/seed/nocloud-net/",
"--create-dirs",
Copy link
Collaborator

Choose a reason for hiding this comment

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

TIL about --create-dirs. Thanks.

def setup_nocloud(instance: LXDInstance):
# On Jammy and above, LXD no longer uses NoCloud, so we need to set
# it up manually
if ImageSpecification.from_os_image().release in [
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we probably can avoid coupling this implementation to specific series. Given that the underlying cause of the symptom is that LXD on jammy and later will no longer ship metadata templates for nocloud-net files, we can check that metadata and honor any image that doesn't contain these templates.

Here's an alternative suggestion which works on focal and jammy lxd_containers. I don't know if you find lxd config cmdline manipulation more busy than tempfile creation and lxc file push .

diff --git a/tests/integration_tests/datasources/test_nocloud.py b/tests/integration_tests/datasources/test_nocloud.py
index b496d5887..d9410410b 100644
--- a/tests/integration_tests/datasources/test_nocloud.py
+++ b/tests/integration_tests/datasources/test_nocloud.py
@@ -1,12 +1,8 @@
 """NoCloud datasource integration tests."""
-import tempfile
-from pathlib import Path
-
 import pytest
 from pycloudlib.lxd.instance import LXDInstance
 
 from cloudinit.subp import subp
-from tests.integration_tests.clouds import ImageSpecification
 from tests.integration_tests.instances import IntegrationInstance
 
 VENDOR_DATA = """\
@@ -16,32 +12,49 @@ runcmd:
 """
 
 
+LXD_METADATA_NOCLOUD_SEED = """\
+  /var/lib/cloud/seed/nocloud-net/meta-data:
+    when:
+    - create
+    - copy
+    create_only: false
+    template: emptycfg.tpl
+    properties:
+      default: |
+        #cloud-config
+        {}
+  /var/lib/cloud/seed/nocloud-net/user-data:
+    when:
+    - create
+    - copy
+    create_only: false
+    template: emptycfg.tpl
+    properties:
+      default: |
+        #cloud-config
+        {}
+"""
+
+
 def setup_nocloud(instance: LXDInstance):
     # On Jammy and above, LXD no longer uses NoCloud, so we need to set
     # it up manually
-    if ImageSpecification.from_os_image().release in [
-        "bionic",
-        "focal",
-        "impish",
-    ]:
+    lxd_image_metadata = subp(
+        ["lxc", "config", "metadata", "show", instance.name]
+    )
+    if "/var/lib/cloud/seed/nocloud-net" in lxd_image_metadata.stdout:
         return
-    with tempfile.TemporaryDirectory() as tmpdir_str:
-        tmpdir = Path(tmpdir_str)
-        userdata_file = tmpdir / "user-data"
-        metadata_file = tmpdir / "meta-data"
-        userdata_file.touch()
-        metadata_file.touch()
-        subp(
-            [
-                "lxc",
-                "file",
-                "push",
-                str(userdata_file),
-                str(metadata_file),
-                f"{instance.name}/var/lib/cloud/seed/nocloud-net/",
-                "--create-dirs",
-            ]
-        )
+    subp(
+        ["lxc", "config", "template", "create", instance.name, "emptycfg.tpl"],
+    )
+    subp(
+        ["lxc", "config", "template", "edit", instance.name, "emptycfg.tpl"],
+        data="#cloud-config\n{}\n",
+    )
+    subp(
+        ["lxc", "config", "metadata", "edit", instance.name],
+        data=f"{lxd_image_metadata.stdout}{LXD_METADATA_NOCLOUD_SEED}",
+    )
 
 
 # Only running on LXD container because we need NoCloud with custom setup
@@ -57,7 +70,6 @@ def test_nocloud_seedfrom_vendordata(client: IntegrationInstance):
     seed_dir = "/var/tmp/test_seed_dir"
     result = client.execute(
         "mkdir {seed_dir} && "
-        "mkdir -p /var/lib/cloud/seed/nocloud-net && "
         "touch {seed_dir}/user-data && "
         "touch {seed_dir}/meta-data && "
         "echo 'seedfrom: {seed_dir}/' > "

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I like your solution better. Pushed.

Copy link
Collaborator

@blackboxsw blackboxsw left a comment

Choose a reason for hiding this comment

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

Well I have to agree with this PR now. Thanks @TheRealFalcon

@blackboxsw blackboxsw merged commit b1f7d89 into canonical:main May 5, 2022
@TheRealFalcon TheRealFalcon deleted the nocloud-test branch May 5, 2022 17:41
aciba90 pushed a commit to aciba90/cloud-init that referenced this pull request May 10, 2022
tests.integration_tests.bugs.test_gh570.test_nocloud_seedfrom_vendordata
is currently failing on Jammy because Jammy no longer uses NoCloud
and instead uses the LXD DataSource. This commit adds a NoCloud
setup to the failing test so we can force the NoCloud datasource.

Also, moved the test into a NoCloud specific file, so any other
tests that need to be NoCloud specific can use the setup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants