From dc07caab953401b7ec1cea6de90f09d1e0075716 Mon Sep 17 00:00:00 2001 From: Megan Wilhite Date: Wed, 14 Jun 2023 11:38:52 -0600 Subject: [PATCH] ensure we unset ONEDIR env for unit/functional tests --- tests/pytests/functional/conftest.py | 18 +++++++++++++- .../pytests/integration/grains/test_grains.py | 24 +++++++++++++++++++ tests/pytests/unit/conftest.py | 17 +++++++++++++ tests/pytests/unit/grains/test_package.py | 2 +- tests/pytests/unit/test_version.py | 4 +++- tests/pytests/unit/utils/test_package.py | 4 ++-- 6 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 tests/pytests/integration/grains/test_grains.py diff --git a/tests/pytests/functional/conftest.py b/tests/pytests/functional/conftest.py index 835464eed2a..b2e6607dd19 100644 --- a/tests/pytests/functional/conftest.py +++ b/tests/pytests/functional/conftest.py @@ -1,4 +1,5 @@ import logging +import os import shutil import pytest @@ -7,6 +8,21 @@ from saltfactories.utils.functional import Loaders log = logging.getLogger(__name__) +@pytest.fixture(scope="package", autouse=True) +def onedir_env(): + """ + Functional tests cannot currently test the + onedir artifact. This will need to be removed + when we do add onedir support for functional tests. + """ + if os.environ.get("ONEDIR_TESTRUN", "0") == "1": + try: + os.environ["ONEDIR_TESTRUN"] = "0" + yield + finally: + os.environ["ONEDIR_TESTRUN"] = "1" + + @pytest.fixture(scope="package") def minion_id(): return "func-tests-minion-opts" @@ -127,7 +143,7 @@ def master_opts( @pytest.fixture(scope="module") def loaders(minion_opts): - return Loaders(minion_opts, loaded_base_name="{}.loaded".format(__name__)) + return Loaders(minion_opts, loaded_base_name=f"{__name__}.loaded") @pytest.fixture(autouse=True) diff --git a/tests/pytests/integration/grains/test_grains.py b/tests/pytests/integration/grains/test_grains.py new file mode 100644 index 00000000000..f83db6a97b8 --- /dev/null +++ b/tests/pytests/integration/grains/test_grains.py @@ -0,0 +1,24 @@ +""" +Grains include tests +""" +import os + + +def test_grains_package(salt_call_cli): + """ + An integration test for the package grain + is a bit overkill, but it is necessary currently + because the onedir package is only tested with + integration tests, so I want to ensure its working + correctly. Once the onedir package is tested with unit + and functional tests this test can be removed since + there is plenty of test coverage in both unit and functional + for this new grain. + """ + ret = salt_call_cli.run("grains.get", "package") + assert ret.returncode == 0 + assert ret.data + if os.environ.get("ONEDIR_TESTRUN", "0") == "0": + assert ret.data == "pip" + else: + assert ret.data == "onedir" diff --git a/tests/pytests/unit/conftest.py b/tests/pytests/unit/conftest.py index 43deeaa618e..24d6e7ea1a5 100644 --- a/tests/pytests/unit/conftest.py +++ b/tests/pytests/unit/conftest.py @@ -1,8 +1,25 @@ +import os + import pytest import salt.config +@pytest.fixture(scope="package", autouse=True) +def onedir_env(): + """ + Unit tests cannot currently test the + onedir artifact. This will need to be removed + when we do add onedir support for functional tests. + """ + if os.environ.get("ONEDIR_TESTRUN", "0") == "1": + try: + os.environ["ONEDIR_TESTRUN"] = "0" + yield + finally: + os.environ["ONEDIR_TESTRUN"] = "1" + + @pytest.fixture def minion_opts(tmp_path): """ diff --git a/tests/pytests/unit/grains/test_package.py b/tests/pytests/unit/grains/test_package.py index 383eb0f8ea6..2333a6af9ec 100644 --- a/tests/pytests/unit/grains/test_package.py +++ b/tests/pytests/unit/grains/test_package.py @@ -3,7 +3,7 @@ import os import salt.grains.package -def test_grain_package_type(tmp_path): +def test_grain_package_type(): """ Test grains.package_type for both package types """ diff --git a/tests/pytests/unit/test_version.py b/tests/pytests/unit/test_version.py index 1cb94c619ca..ec1abed9f77 100644 --- a/tests/pytests/unit/test_version.py +++ b/tests/pytests/unit/test_version.py @@ -158,7 +158,9 @@ def test_version_report_lines(): line_lengths = { len(line.split(":")[0]) for line in versions_report_ret[start_looking_index:] - if line != " " and line not in ("System Versions:", "Salt Extensions:") + if line != " " + and line + not in ("System Versions:", "Salt Extensions:", "Salt Package Information:") } # Check that they are all the same size (only one element in the set) assert len(line_lengths) == 1 diff --git a/tests/pytests/unit/utils/test_package.py b/tests/pytests/unit/utils/test_package.py index df1ffd39317..81192a13e5d 100644 --- a/tests/pytests/unit/utils/test_package.py +++ b/tests/pytests/unit/utils/test_package.py @@ -6,6 +6,6 @@ import salt.utils.package def test_pkg_type(): ret = salt.utils.package.pkg_type() if os.environ.get("ONEDIR_TESTRUN", "0") == "0": - assert ret == "onedir" + assert ret == "pip" else: - assert ret == "system" + assert ret == "onedir"