Migrate grains tests to pytest

This commit is contained in:
Daniel A. Wozniak 2024-08-08 15:26:44 -07:00
parent c24ccef828
commit 9c353984d7
4 changed files with 49 additions and 88 deletions

View file

@ -202,8 +202,8 @@ salt/config/*:
- pytests.unit.config.test__validate_opts
salt/loader/*:
- integration.loader.test_ext_grains
- integration.loader.test_ext_modules
- pytests.integration.loader.test_ext_grains
- pytests.functional.loader.test_loader
- pytests.functional.loader.test_loaded_base_name

View file

@ -1,87 +0,0 @@
"""
integration.loader.ext_grains
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Test Salt's loader regarding external grains
"""
import os
import time
import pytest
import salt.config
import salt.loader
from tests.support.case import ModuleCase
from tests.support.runtests import RUNTIME_VARS
@pytest.mark.skip_on_photonos(
reason="Consistant failures on photon, test needs refactoring"
)
@pytest.mark.windows_whitelisted
class LoaderGrainsTest(ModuleCase):
"""
Test the loader standard behavior with external grains
"""
# def setUp(self):
# self.opts = minion_config(None)
# self.opts['disable_modules'] = ['pillar']
# self.opts['grains'] = grains(self.opts)
@pytest.mark.slow_test
@pytest.mark.timeout(240)
def test_grains_overwrite(self):
# Force a grains sync
self.run_function("saltutil.sync_grains")
# To avoid a race condition on Windows, we need to make sure the
# `test_custom_grain2.py` file is present in the _grains directory
# before trying to get the grains. This test may execute before the
# minion has finished syncing down the files it needs.
module = os.path.join(
RUNTIME_VARS.RUNTIME_CONFIGS["minion"]["cachedir"],
"files",
"base",
"_grains",
"custom_grain2.py",
)
tries = 0
while not os.path.exists(module):
tries += 1
if tries > 60:
self.fail(
"Failed to found custom grains module in cache path {}".format(
module
)
)
break
time.sleep(1)
grains = self.run_function("grains.items")
# Check that custom grains are overwritten
self.assertEqual({"k2": "v2"}, grains["a_custom"])
@pytest.mark.skip(reason="needs a way to reload minion after config change")
@pytest.mark.windows_whitelisted
class LoaderGrainsMergeTest(ModuleCase):
"""
Test the loader deep merge behavior with external grains
"""
def setUp(self):
# XXX: This seems like it should become a unit test instead
self.opts = salt.config.minion_config(None)
self.opts["grains_deep_merge"] = True
self.assertTrue(self.opts["grains_deep_merge"])
self.opts["disable_modules"] = ["pillar"]
__grains__ = salt.loader.grains(self.opts)
def test_grains_merge(self):
__grain__ = self.run_function("grains.item", ["a_custom"])
# Check that the grain is present
self.assertIn("a_custom", __grain__)
# Check that the grains are merged
self.assertEqual({"k1": "v1", "k2": "v2"}, __grain__["a_custom"])

View file

@ -0,0 +1,48 @@
import pytest
def test_grains_overwrite(salt_cli, salt_master, salt_minion):
assert not salt_minion.config.get("grains_deep_merge", False)
# Force a grains sync
salt_cli.run("saltutil.sync_grains", minion_tgt=salt_minion.id)
# XXX: This should no longer be neede because of using salt_cli.run.
# To avoid a race condition on Windows, we need to make sure the
# `test_custom_grain2.py` file is present in the _grains directory
# before trying to get the grains. This test may execute before the
# minion has finished syncing down the files it needs.
# module = os.path.join(
# salt_minion.config["cachedir"],
# "files",
# "base",
# "_grains",
# "custom_grain2.py",
# )
# assert os.path.exists(module)
# Check that custom grains are overwritten
ret = salt_cli.run("grains.items", minion_tgt=salt_minion.id)
assert ret.data["a_custom"] == {"k2": "v2"}
def test_grains_merge(salt_cli, salt_master):
minion = salt_master.salt_minion_daemon(
"test_grains_merge",
overrides={
"grains_deep_merge": True,
# Grains in the minon config won't get merged.
# "grains": {"a_custom": {"k1": "v1"}},
},
)
minion.after_terminate(
pytest.helpers.remove_stale_minion_key, salt_master, minion.id
)
content = """
def grain():
return {"a_custom": {"k1": "v1"}}
"""
with salt_master.state_tree.base.temp_file("_grains/tempcustom.py", content):
with minion.started():
salt_cli.run("saltutil.sync_grains", minion_tgt=minion.id)
ret = salt_cli.run("grains.item", "a_custom", minion_tgt=minion.id)
assert ret.data["a_custom"] == {"k1": "v1", "k2": "v2"}