Skip to content

Commit

Permalink
azure: define new attribute for pre-22.3 pickles (#1725)
Browse files Browse the repository at this point in the history
A new attribute was added to DataSourceAzure[1].
Since the base class uses CloudInitPickleMixin,
we need to define this new attribute in _unpickle()

Add multiple tests to improve pickle coverage.

[1] #1523
  • Loading branch information
holmanb committed Sep 14, 2022
1 parent 0ef0974 commit 19b08da
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cloudinit/sources/DataSourceAzure.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ def _unpickle(self, ci_pkl_version: int) -> None:
self._ephemeral_dhcp_ctx = None
self._iso_dev = None
self._wireserver_endpoint = DEFAULT_WIRESERVER_ENDPOINT
self._reported_ready_marker_file = os.path.join(
self.paths.cloud_dir, "data", "reported_ready"
)

def __str__(self):
root = sources.DataSource.__str__(self)
Expand Down
Binary file not shown.
9 changes: 8 additions & 1 deletion tests/integration_tests/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ def test_clean_boot_of_upgraded_package(session_cloud: IntegrationCloud):
# have broken across re-constitution of a cached datasource. Some
# platforms invalidate their datasource cache on reboot, so we run
# it here to ensure we get a dirty run.
assert instance.execute("cloud-init init").ok
assert instance.execute(
"cloud-init init --local; "
"cloud-init init; "
"cloud-init modules --mode=config; "
"cloud-init modules --mode=final"
).ok

# Reboot
instance.execute("hostname something-else")
Expand Down Expand Up @@ -185,4 +190,6 @@ def test_subsequent_boot_of_upgraded_package(session_cloud: IntegrationCloud):
source, take_snapshot=False, clean=False
)
instance.restart()
log = instance.read_from_file("/var/log/cloud-init.log")
verify_clean_log(log)
assert instance.execute("cloud-init status --wait --long").ok
6 changes: 6 additions & 0 deletions tests/integration_tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def verify_clean_log(log: str, ignore_deprecations: bool = True):
)
log = "\n".join(log_lines)

error_logs = re.findall("ERROR.*", log)
if error_logs:
raise AssertionError(
"Found unexpected errors: %s" % "\n".join(error_logs)
)

warning_count = log.count("WARN")
expected_warnings = 0
traceback_count = log.count("Traceback")
Expand Down
28 changes: 26 additions & 2 deletions tests/unittests/test_upgrade.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Copyright (C) 2020 Canonical Ltd.
#
# Author: Daniel Watkins <[email protected]>
#
# This file is part of cloud-init. See LICENSE file for license information.

"""Upgrade testing for cloud-init.
Expand All @@ -19,8 +17,15 @@
import pytest

from cloudinit.sources import pkl_load
from cloudinit.sources.DataSourceAzure import DataSourceAzure
from cloudinit.sources.DataSourceNoCloud import DataSourceNoCloud
from tests.unittests.helpers import resourceLocation

DSNAME_TO_CLASS = {
"Azure": DataSourceAzure,
"NoCloud": DataSourceNoCloud,
}


class TestUpgrade:
@pytest.fixture(
Expand All @@ -36,6 +41,25 @@ def previous_obj_pkl(self, request):
"""
return pkl_load(str(request.param))

def test_pkl_load_defines_all_init_side_effect_attributes(
self, previous_obj_pkl
):
"""Any attrs as side-effects of __init__ exist in unpickled obj."""
ds_class = DSNAME_TO_CLASS[previous_obj_pkl.dsname]
sys_cfg = previous_obj_pkl.sys_cfg
distro = previous_obj_pkl.distro
paths = previous_obj_pkl.paths
ds = ds_class(sys_cfg, distro, paths)
if ds.dsname == "NoCloud" and previous_obj_pkl.__dict__:
expected = (
set({"seed_dirs"}), # LP: #1568150 handled with getattr checks
set(),
)
else:
expected = (set(),)
missing_attrs = ds.__dict__.keys() - previous_obj_pkl.__dict__.keys()
assert missing_attrs in expected

def test_networking_set_on_distro(self, previous_obj_pkl):
"""We always expect to have ``.networking`` on ``Distro`` objects."""
assert previous_obj_pkl.distro.networking is not None
Expand Down

0 comments on commit 19b08da

Please sign in to comment.