diff --git a/tests/unittests/config/test_cc_chef.py b/tests/unittests/config/test_cc_chef.py index 8ba4aa0e76a..c4e669518b4 100644 --- a/tests/unittests/config/test_cc_chef.py +++ b/tests/unittests/config/test_cc_chef.py @@ -18,7 +18,6 @@ from tests.unittests.helpers import ( SCHEMA_EMPTY_ERROR, mock, - skipIf, skipUnlessJsonSchema, ) from tests.unittests.util import MockDistro, get_cloud @@ -128,7 +127,10 @@ def test_no_config(self): for d in cc_chef.CHEF_DIRS: assert not os.path.isdir(d) - @skipIf(not CLIENT_TEMPL, "templates/chef_client.rb.tmpl is not available") + @pytest.mark.skipif( + not CLIENT_TEMPL, + reason="templates/chef_client.rb.tmpl is not available", + ) def test_basic_config(self): """ test basic config looks correct @@ -265,7 +267,10 @@ def test_migrate_chef_config_dirs( for expected_msg, count in expected_msg_count.items(): assert caplog.text.count(expected_msg) == count - @skipIf(not CLIENT_TEMPL, "templates/chef_client.rb.tmpl is not available") + @pytest.mark.skipif( + not CLIENT_TEMPL, + reason="templates/chef_client.rb.tmpl is not available", + ) def test_template_deletes(self): util.write_file( @@ -284,7 +289,10 @@ def test_template_deletes(self): assert "json_attribs" not in c assert "Formatter.show_time" not in c - @skipIf(not CLIENT_TEMPL, "templates/chef_client.rb.tmpl is not available") + @pytest.mark.skipif( + not CLIENT_TEMPL, + reason="templates/chef_client.rb.tmpl is not available", + ) def test_validation_cert_and_validation_key(self): # test validation_cert content is written to validation_key path util.write_file( @@ -306,7 +314,10 @@ def test_validation_cert_and_validation_key(self): util.load_text_file(v_path) assert v_cert == util.load_text_file(v_path) - @skipIf(not CLIENT_TEMPL, "templates/chef_client.rb.tmpl is not available") + @pytest.mark.skipif( + not CLIENT_TEMPL, + reason="templates/chef_client.rb.tmpl is not available", + ) def test_validation_cert_with_system(self): # test validation_cert content is not written over system file diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py index a52860b49f7..7453d06d12e 100644 --- a/tests/unittests/helpers.py +++ b/tests/unittests/helpers.py @@ -13,6 +13,7 @@ from unittest.util import strclass from urllib.parse import urlsplit, urlunsplit +import pytest import responses from cloudinit import distros, helpers, settings, util @@ -21,10 +22,6 @@ from tests.helpers import cloud_init_project_dir from tests.hypothesis_jsonschema import HAS_HYPOTHESIS_JSONSCHEMA -# Used for skipping tests -skipIf = unittest.skipIf - - try: import apt_pkg # type: ignore # noqa: F401 @@ -304,9 +301,8 @@ def readResource(name, mode="r"): def skipIfAptPkg(): - return skipIf( - HAS_APT_PKG, - "No python-apt dependency present.", + return pytest.mark.skipif( + HAS_APT_PKG, reason="No python-apt dependency present." ) @@ -329,7 +325,7 @@ def skipIfAptPkg(): def skipUnlessJsonSchemaVersionGreaterThan(version=(0, 0, 0)): - return skipIf( + return pytest.mark.skipif( _jsonschema_version <= version, reason=( f"python3-jsonschema {_jsonschema_version} not greater than" @@ -339,33 +335,38 @@ def skipUnlessJsonSchemaVersionGreaterThan(version=(0, 0, 0)): def skipUnlessJsonSchema(): - return skipIf( - _missing_jsonschema_dep, "No python-jsonschema dependency present." + return pytest.mark.skipif( + _missing_jsonschema_dep, + reason="No python-jsonschema dependency present.", ) def skipUnlessJinja(): - return skipIf(not JINJA_AVAILABLE, "No jinja dependency present.") + return pytest.mark.skipif( + not JINJA_AVAILABLE, reason="No jinja dependency present." + ) @skipUnlessJinja() def skipUnlessJinjaVersionGreaterThan(version=(0, 0, 0)): import jinja2 - return skipIf( - condition=tuple(map(int, jinja2.__version__.split("."))) < version, + return pytest.mark.skipif( + tuple(map(int, jinja2.__version__.split("."))) < version, reason=f"jinj2 version is less than {version}", ) def skipIfJinja(): - return skipIf(JINJA_AVAILABLE, "Jinja dependency present.") + return pytest.mark.skipif( + JINJA_AVAILABLE, reason="Jinja dependency present." + ) def skipUnlessHypothesisJsonSchema(): - return skipIf( + return pytest.mark.skipif( not HAS_HYPOTHESIS_JSONSCHEMA, - "No python-hypothesis-jsonschema dependency present.", + reason="No python-hypothesis-jsonschema dependency present.", ) diff --git a/tests/unittests/sources/test_azure_helper.py b/tests/unittests/sources/test_azure_helper.py index 4fc252b9ca7..d28d7e20a72 100644 --- a/tests/unittests/sources/test_azure_helper.py +++ b/tests/unittests/sources/test_azure_helper.py @@ -3,7 +3,6 @@ import os import re -import unittest from textwrap import dedent from unittest import mock from xml.etree import ElementTree as ET @@ -517,7 +516,7 @@ def _data_file(self, name): path = "tests/data/azure" return os.path.join(path, name) - @unittest.skip("todo move to cloud_test") + @pytest.mark.skip(reason="todo move to cloud_test") def test_pubkey_extract(self): cert = load_text_file(self._data_file("pubkey_extract_cert")) good_key = load_text_file(self._data_file("pubkey_extract_ssh_key")) @@ -529,7 +528,7 @@ def test_pubkey_extract(self): fingerprint = sslmgr._get_fingerprint_from_cert(cert) assert good_fingerprint == fingerprint - @unittest.skip("todo move to cloud_test") + @pytest.mark.skip(reason="todo move to cloud_test") @mock.patch.object(azure_helper.OpenSSLManager, "_decrypt_certs_from_xml") def test_parse_certificates(self, mock_decrypt_certs): """Azure control plane puts private keys as well as certificates diff --git a/tests/unittests/sources/test_smartos.py b/tests/unittests/sources/test_smartos.py index 3e76a639e0d..e25e7818189 100644 --- a/tests/unittests/sources/test_smartos.py +++ b/tests/unittests/sources/test_smartos.py @@ -44,7 +44,7 @@ ) from cloudinit.subp import ProcessExecutionError, subp, which from cloudinit.util import write_file -from tests.unittests.helpers import mock, skipIf +from tests.unittests.helpers import mock DSMOS = "cloudinit.sources.DataSourceSmartOS" SDC_NICS = json.loads( @@ -726,7 +726,9 @@ class TestIdentifyFile: """Test the 'identify_file' utility.""" @pytest.mark.allow_subp_for("file") - @skipIf(not which("file"), "command 'file' not available.") + @pytest.mark.skipif( + not which("file"), reason="command 'file' not available." + ) def test_file_happy_path(self, tmp_path): """Test file is available and functional on plain text.""" fname = str(tmp_path / "myfile") diff --git a/tests/unittests/test_templating.py b/tests/unittests/test_templating.py index 3b14e8a3f25..83c1ba4fc69 100644 --- a/tests/unittests/test_templating.py +++ b/tests/unittests/test_templating.py @@ -142,7 +142,7 @@ def test_jinja_warns_on_missing_dep_and_uses_basic_renderer( self, caplog, tmp_path ): """Test jinja render_from_file will fallback to basic renderer.""" - tmpl_fn = tmp_path("j-render-from-file.template") + tmpl_fn = str(tmp_path / "j-render-from-file.template") write_file( tmpl_fn, omode="wb", diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index 46a4a17f80e..f2ff3fa8672 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -35,7 +35,7 @@ from cloudinit.sources import DataSourceHostname from cloudinit.subp import SubpResult from tests.unittests import helpers -from tests.unittests.helpers import random_string, skipIf, skipUnlessJinja +from tests.unittests.helpers import random_string, skipUnlessJinja LOG = logging.getLogger(__name__) M_PATH = "cloudinit.util." @@ -2819,8 +2819,9 @@ def test_comments_handled_correctly(self): ) -@skipIf( - not util.is_Linux(), "These tests don't make sense on non-Linux systems." +@pytest.mark.skipif( + not util.is_Linux(), + reason="These tests don't make sense on non-Linux systems.", ) class TestGetProcEnv: """test get_proc_env.""" @@ -2871,14 +2872,20 @@ def test_non_existing_file_returns_empty_dict(self, m_load_file): class TestGetProcPpid: """test get_proc_ppid""" - @skipIf(not util.is_Linux(), "/proc/$pid/stat is not useful on not-Linux") + @pytest.mark.skipif( + not util.is_Linux(), + reason="/proc/$pid/stat is not useful on not-Linux", + ) def test_get_proc_ppid_linux(self): """get_proc_ppid returns correct parent pid value.""" my_pid = os.getpid() my_ppid = os.getppid() assert my_ppid == Distro.get_proc_ppid(my_pid) - @skipIf(not util.is_Linux(), "/proc/$pid/stat is not useful on not-Linux") + @pytest.mark.skipif( + not util.is_Linux(), + reason="/proc/$pid/stat is not useful on not-Linux", + ) def test_get_proc_pgrp_linux(self): """get_proc_ppid returns correct parent pid value.""" assert os.getpgid(0) == Distro.get_proc_pgid(os.getpid())