Skip to content

Commit

Permalink
Updated tests for saltutil for code-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
David Murphy committed Oct 4, 2023
1 parent a932628 commit 341b9f9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
30 changes: 30 additions & 0 deletions tests/pytests/integration/runners/test_saltutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ def world():
assert "{}.hello".format(module_type) in ret.stdout


def test_sync_refresh_false(
module_type, module_sync_functions, salt_run_cli, salt_minion, salt_master
):
"""
Ensure modules are synced when various sync functions are called
"""
module_name = "hello_sync_{}".format(module_type)
module_contents = """
def __virtual__():
return "hello"
def world():
return "world"
"""

test_moduledir = salt_master.state_tree.base.write_path / "_{}".format(module_type)
test_moduledir.mkdir(parents=True, exist_ok=True)
module_tempfile = salt_master.state_tree.base.temp_file(
"_{}/{}.py".format(module_type, module_name), module_contents
)

with module_tempfile:
salt_cmd = "saltutil.sync_{}".format(module_sync_functions[module_type])
ret = salt_run_cli.run(salt_cmd, saltenv=None, refresh=False)
assert ret.returncode == 0
assert (
"saltutil.sync_{}".format(module_sync_functions[module_type]) in ret.stdout
)


def _write_module_dir_and_file(module_type, salt_minion, salt_master):
"""
Write out dummy module to appropriate module location
Expand Down
69 changes: 68 additions & 1 deletion tests/pytests/unit/modules/test_saltutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@

@pytest.fixture
def configure_loader_modules():
return {saltutil: {"__opts__": {"file_client": "local"}}}
return {
saltutil: {
"__opts__": {
"file_client": "local",
"cachedir": "/tmp",
"pki_dir": "/tmp/pki_dir",
"id": "minion",
"master_uri": "tcp://127.0.0.1:4505",
"__role": "minion",
"keysize": 2048,
}
}
}


def test_exec_kwargs():
Expand Down Expand Up @@ -90,12 +102,24 @@ def test_refresh_grains_default_clean_pillar_cache():
refresh_pillar.assert_called_with(clean_cache=False)


def test_refresh_grains_default_clean_pillar_cache_with_refresh_false():
with patch("salt.modules.saltutil.refresh_modules") as refresh_modules:
saltutil.refresh_grains(refresh_pillar=False)
refresh_modules.assert_called()


def test_refresh_grains_clean_pillar_cache():
with patch("salt.modules.saltutil.refresh_pillar") as refresh_pillar:
saltutil.refresh_grains(clean_pillar_cache=True)
refresh_pillar.assert_called_with(clean_cache=True)


def test_refresh_grains_clean_pillar_cache_with_refresh_false():
with patch("salt.modules.saltutil.refresh_modules") as refresh_modules:
saltutil.refresh_grains(clean_pillar_cache=True, refresh_pillar=False)
refresh_modules.assert_called()


def test_sync_grains_default_clean_pillar_cache():
with patch("salt.modules.saltutil._sync"):
with patch("salt.modules.saltutil.refresh_pillar") as refresh_pillar:
Expand Down Expand Up @@ -136,3 +160,46 @@ def test_sync_all_clean_pillar_cache():
with patch("salt.modules.saltutil.refresh_pillar") as refresh_pillar:
saltutil.sync_all(clean_pillar_cache=True)
refresh_pillar.assert_called_with(clean_cache=True)


@pytest.mark.skip_on_windows(reason="making use of /tmp directory")
def test_list_extmods(salt_call_cli):
ret = salt_call_cli.run("--local", "cmd.run", "mkdir -p /tmp/extmods/dummydir")
assert ret.returncode == 0

ret = saltutil.list_extmods()
assert "dummydir" in ret
assert ret["dummydir"] == []


def test_refresh_beacons():
ret = saltutil.refresh_beacons()
assert ret is False


def test_refresh_matchers():
ret = saltutil.refresh_matchers()
assert ret is False


def test_refresh_modules_async_false():
## ret = saltutil.refresh_modules( kwargs({"async": False}) )
kwargs = {"async": False}
ret = saltutil.refresh_modules(**kwargs)
assert ret is False


def test_clear_job_cache(salt_call_cli):
ret = salt_call_cli.run("--local", "cmd.run", "mkdir -p /tmp/minion_jobs/dummydir")
assert ret.returncode == 0

ret = saltutil.clear_job_cache(hours=1)
assert ret is True


@pytest.mark.destructive_test
def test_regen_keys(salt_call_cli):
ret = salt_call_cli.run("--local", "cmd.run", "mkdir -p /tmp/pki_dir/dummydir")
assert ret.returncode == 0

saltutil.regen_keys()

0 comments on commit 341b9f9

Please sign in to comment.