1# Copyright (C) 2020 Canonical Ltd.
2#
3# Author: Daniel Watkins <oddbloke@ubuntu.com>
4#
5# This file is part of cloud-init. See LICENSE file for license information.
6
7"""Upgrade testing for cloud-init.
8
9This module tests cloud-init's behaviour across upgrades.  Specifically, it
10specifies a set of invariants that the current codebase expects to be true (as
11tests in ``TestUpgrade``) and then checks that these hold true after unpickling
12``obj.pkl``s from previous versions of cloud-init; those pickles are stored in
13``tests/data/old_pickles/``.
14"""
15
16import operator
17import pathlib
18
19import pytest
20
21from cloudinit.stages import _pkl_load
22from cloudinit.tests.helpers import resourceLocation
23
24
25class TestUpgrade:
26    @pytest.fixture(
27        params=pathlib.Path(resourceLocation("old_pickles")).glob("*.pkl"),
28        scope="class",
29        ids=operator.attrgetter("name"),
30    )
31    def previous_obj_pkl(self, request):
32        """Load each pickle to memory once, then run all tests against it.
33
34        Test implementations _must not_ modify the ``previous_obj_pkl`` which
35        they are passed, as that will affect tests that run after them.
36        """
37        return _pkl_load(str(request.param))
38
39    def test_networking_set_on_distro(self, previous_obj_pkl):
40        """We always expect to have ``.networking`` on ``Distro`` objects."""
41        assert previous_obj_pkl.distro.networking is not None
42
43    def test_blacklist_drivers_set_on_networking(self, previous_obj_pkl):
44        """We always expect Networking.blacklist_drivers to be initialised."""
45        assert previous_obj_pkl.distro.networking.blacklist_drivers is None
46
47    def test_paths_has_run_dir_attribute(self, previous_obj_pkl):
48        assert previous_obj_pkl.paths.run_dir is not None
49
50    def test_vendordata_exists(self, previous_obj_pkl):
51        assert previous_obj_pkl.vendordata2 is None
52        assert previous_obj_pkl.vendordata2_raw is None
53