Skip to content

Commit

Permalink
test: Move unit test helpers to global test helpers (#4682)
Browse files Browse the repository at this point in the history
`get_top_level_dir` and `cloud_init_project_dir` have uses in
integration tests. Move them up to a top-level test helper file
accordingly.
  • Loading branch information
TheRealFalcon committed Jan 17, 2024
1 parent 1c5726b commit f1ca01c
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 32 deletions.
21 changes: 21 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pathlib import Path

import cloudinit


def get_top_level_dir() -> Path:
"""Return the absolute path to the top cloudinit project directory
@return Path('<top-cloudinit-dir>')
"""
return Path(cloudinit.__file__).parent.parent.resolve()


def cloud_init_project_dir(sub_path: str) -> str:
"""Get a path within the cloudinit project directory
@return str of the combined path
Example: cloud_init_project_dir("my/path") -> "/path/to/cloud-init/my/path"
"""
return str(get_top_level_dir() / sub_path)
2 changes: 1 addition & 1 deletion tests/unittests/config/test_cc_chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
get_schema,
validate_cloudconfig_schema,
)
from tests.helpers import cloud_init_project_dir
from tests.unittests.helpers import (
FilesystemMockingTestCase,
ResponsesTestCase,
cloud_init_project_dir,
mock,
skipIf,
skipUnlessJsonSchema,
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/config/test_cc_resolv_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
get_schema,
validate_cloudconfig_schema,
)
from tests.helpers import cloud_init_project_dir
from tests.unittests.helpers import (
FilesystemMockingTestCase,
cloud_init_project_dir,
skipUnlessJsonSchema,
)
from tests.unittests.util import MockDistro
Expand Down
3 changes: 2 additions & 1 deletion tests/unittests/config/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from cloudinit.distros import ALL_DISTROS
from cloudinit.settings import FREQUENCIES
from cloudinit.stages import Init
from tests.unittests.helpers import cloud_init_project_dir, mock
from tests.helpers import cloud_init_project_dir
from tests.unittests.helpers import mock

M_PATH = "cloudinit.config.modules."

Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/config/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
from cloudinit.sources import DataSourceNotFoundException
from cloudinit.templater import JinjaSyntaxParsingException
from cloudinit.util import load_file, write_file
from tests.helpers import cloud_init_project_dir
from tests.hypothesis import given
from tests.hypothesis_jsonschema import from_schema
from tests.unittests.helpers import (
CiTestCase,
cloud_init_project_dir,
does_not_raise,
mock,
skipUnlessHypothesisJsonSchema,
Expand Down
21 changes: 1 addition & 20 deletions tests/unittests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
import time
import unittest
from contextlib import ExitStack, contextmanager
from pathlib import Path
from typing import ClassVar, List, Union
from unittest import mock
from unittest.util import strclass
from urllib.parse import urlsplit, urlunsplit

import responses

import cloudinit
from cloudinit import atomic_helper, cloud, distros
from cloudinit import helpers as ch
from cloudinit import subp, util
Expand All @@ -30,6 +28,7 @@
)
from cloudinit.sources import DataSourceNone
from cloudinit.templater import JINJA_AVAILABLE
from tests.helpers import cloud_init_project_dir
from tests.hypothesis_jsonschema import HAS_HYPOTHESIS_JSONSCHEMA

_real_subp = subp.subp
Expand Down Expand Up @@ -596,24 +595,6 @@ def __mock_assert_not_called(mmock):
mock.Mock.assert_not_called = __mock_assert_not_called # type: ignore


def get_top_level_dir() -> Path:
"""Return the absolute path to the top cloudinit project directory
@return Path('<top-cloudinit-dir>')
"""
return Path(cloudinit.__file__).parent.parent.resolve()


def cloud_init_project_dir(sub_path: str) -> str:
"""Get a path within the cloudinit project directory
@return str of the combined path
Example: cloud_init_project_dir("my/path") -> "/path/to/cloud-init/my/path"
"""
return str(get_top_level_dir() / sub_path)


@contextmanager
def does_not_raise():
"""Context manager to parametrize tests raising and not raising exceptions
Expand Down
3 changes: 2 additions & 1 deletion tests/unittests/sources/vmware/test_vmware_config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
get_network_data_from_vmware_cust_cfg,
get_non_network_data_from_vmware_cust_cfg,
)
from tests.unittests.helpers import CiTestCase, cloud_init_project_dir
from tests.helpers import cloud_init_project_dir
from tests.unittests.helpers import CiTestCase

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/test_ds_identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from cloudinit.sources import DataSourceIBMCloud as ds_ibm
from cloudinit.sources import DataSourceOracle as ds_oracle
from cloudinit.sources import DataSourceSmartOS as ds_smartos
from tests.helpers import cloud_init_project_dir
from tests.unittests.helpers import (
CiTestCase,
cloud_init_project_dir,
dir2dict,
populate_dir,
populate_dir_with_ts,
Expand Down
9 changes: 5 additions & 4 deletions tests/unittests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from pathlib import Path

from cloudinit import sources
from tests.unittests import helpers as test_helpers
from tests.helpers import cloud_init_project_dir, get_top_level_dir
from tests.unittests.helpers import ResourceUsingTestCase


class MyDataSource(sources.DataSource):
Expand All @@ -16,7 +17,7 @@ def get_instance_id(self):
return self._instance_id


class TestPaths(test_helpers.ResourceUsingTestCase):
class TestPaths(ResourceUsingTestCase):
def test_get_ipath_and_instance_id_with_slashes(self):
myds = MyDataSource(sys_cfg={}, distro=None, paths={})
myds._instance_id = "/foo/bar"
Expand All @@ -37,7 +38,7 @@ def test_get_ipath_and_empty_instance_id_returns_none(self):


class Testcloud_init_project_dir:
top_dir = test_helpers.get_top_level_dir()
top_dir = get_top_level_dir()

@staticmethod
def _get_top_level_dir_alt_implementation():
Expand All @@ -61,6 +62,6 @@ def test_cloud_init_project_dir(self):
"""
assert (
str(Path(self.top_dir, "test"))
== test_helpers.cloud_init_project_dir("test")
== cloud_init_project_dir("test")
== str(Path(self._get_top_level_dir_alt_implementation(), "test"))
)
2 changes: 1 addition & 1 deletion tests/unittests/test_render_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from cloudinit import subp, templater, util
from tests.unittests.helpers import cloud_init_project_dir
from tests.helpers import cloud_init_project_dir

# TODO(Look to align with tools.render-template or cloudinit.distos.OSFAMILIES)
DISTRO_VARIANTS = [
Expand Down
3 changes: 2 additions & 1 deletion tests/unittests/test_subp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from unittest import mock

from cloudinit import subp, util
from tests.unittests.helpers import CiTestCase, get_top_level_dir
from tests.helpers import get_top_level_dir
from tests.unittests.helpers import CiTestCase

BASH = subp.which("bash")
BOGUS_COMMAND = "this-is-not-expected-to-be-a-program-name"
Expand Down

0 comments on commit f1ca01c

Please sign in to comment.