Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions tests/unittests/config/test_cc_chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from tests.unittests.helpers import (
SCHEMA_EMPTY_ERROR,
mock,
skipIf,
skipUnlessJsonSchema,
)
from tests.unittests.util import MockDistro, get_cloud
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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

Expand Down
33 changes: 17 additions & 16 deletions tests/unittests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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."
)


Expand All @@ -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"
Expand All @@ -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.",
)


Expand Down
5 changes: 2 additions & 3 deletions tests/unittests/sources/test_azure_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/unittests/sources/test_smartos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/test_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 12 additions & 5 deletions tests/unittests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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())
Expand Down