Skip to content

Commit

Permalink
add network_data and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealFalcon committed Oct 21, 2023
1 parent 7a79c58 commit 8f35492
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
10 changes: 10 additions & 0 deletions pycloudlib/qemu/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def _create_seed_iso(
user_data: Optional[str],
meta_data: Optional[str],
vendor_data: Optional[str] = None,
network_data: Optional[str] = None,
) -> Optional[Path]:
"""Create seed iso for passing cloud-init user data.
Expand All @@ -278,6 +279,7 @@ def _create_seed_iso(
user_data: cloud-init user data
meta_data: cloud-init meta data
vendor_data: cloud-init vendor data
network_data: cloud-init network data
Returns:
Path, path to seed iso
Expand Down Expand Up @@ -322,6 +324,11 @@ def _create_seed_iso(
vendor_data_path.write_text(vendor_data, encoding="utf-8")
args.append(str(vendor_data_path))

if network_data:
network_data_path = instance_dir / "network-config"
network_data_path.write_text(network_data, encoding="utf-8")
args.append(str(network_data_path))

subprocess.run(
args,
check=True,
Expand Down Expand Up @@ -420,6 +427,7 @@ def launch(
user_data=None,
meta_data=None,
vendor_data=None,
network_data=None,
kernel_cmdline="",
kernel_path=None,
no_seed_iso=False,
Expand All @@ -444,6 +452,7 @@ def launch(
user_data: used by cloud-init to run custom scripts/configuration
meta_data: used by cloud-init for custom metadata
vendor_data: used by cloud-init for custom vendor data
network_data: used by cloud-init for custom network data
kernel_cmdline: kernel command line arguments
kernel_path: path to kernel to use
no_seed_iso: if True, do not create a seed iso
Expand Down Expand Up @@ -519,6 +528,7 @@ def launch(
),
meta_data=meta_data,
vendor_data=vendor_data,
network_data=network_data,
)

if seed_path:
Expand Down
58 changes: 51 additions & 7 deletions tests/integration_tests/test_qemu.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""Integration tests for Qemu class.""" ""
import pytest
import yaml

from pycloudlib import Qemu

SNAPSHOT_CONFIG = """\
Expand All @@ -10,7 +14,7 @@
def test_snapshots():
"""Test that snapshots work as expected."""
# Since we're managing everything with directories, and qcow images
# are copy-on-wrtie, ensure multiple snapshots don't step on each other
# are copy-on-write, ensure multiple snapshots don't step on each other
with Qemu(tag="test", timestamp_suffix=True) as cloud:
image = cloud.released_image("jammy")
instance = cloud.launch(
Expand Down Expand Up @@ -88,11 +92,51 @@ def test_kernel_cli():
)


def test_console_log():
with Qemu(tag="test", timestamp_suffix=True) as cloud:
image = cloud.released_image("jammy")
instance = cloud.launch(image_id=image)
instance.wait()
V2_CONFIG = """\
network:
version: 2
ethernets:
pycloudlib_test_qemu:
dhcp4: true
match:
name: e*
"""


class TestQemu:
"""Tests that can use a common launch to save time."""

@pytest.fixture(scope="class")
def instance(self):
"""Fixture for Qemu class."""
with Qemu(tag="test", timestamp_suffix=True) as cloud:
image = cloud.released_image("jammy")
instance = cloud.launch(
image_id=image,
user_data=SNAPSHOT_CONFIG % "1",
meta_data="instance-id: iid-local01\nlocal-hostname: cloudimg",
vendor_data=(
"#cloud-config\nruncmd:\n - echo '2' > /var/tmp/two"
),
network_data=V2_CONFIG,
)
instance.wait()
yield instance

def test_nocloud_data(self, instance):
"""Test that nocloud data is passed through."""
assert instance.execute("cat /var/tmp/message") == "1"
assert instance.execute("cat /var/tmp/two") == "2"
assert instance.execute("hostname") == "cloudimg"
remote_netplan = instance.execute(
"cat /etc/netplan/50-cloud-init.yaml"
)
remote_yaml = yaml.load(remote_netplan, Loader=yaml.SafeLoader)
local_yaml = yaml.load(V2_CONFIG, Loader=yaml.SafeLoader)
assert local_yaml == remote_yaml

def test_console_log(self, instance):
"""Test that console log is captured."""
console_log = instance.console_log()
assert "Booting from Hard Disk..." in console_log
assert "ubuntu login: " in console_log
assert "cloudimg login: " in console_log
7 changes: 7 additions & 0 deletions tests/unit_tests/qemu/test_qemu_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
"""


@pytest.fixture(autouse=True)
def common_mocks():
"""Mock out common functions."""
with mock.patch("shutil.which", return_value=True):
yield


@pytest.fixture
def qemu(tmp_path: Path):
"""Fixture to create a Qemu instance."""
Expand Down

0 comments on commit 8f35492

Please sign in to comment.