mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Migrate integration.ssh.test_grains
and integration.cli.test_grains
to PyTest
This commit is contained in:
parent
e8c8054151
commit
a3ddbadb53
5 changed files with 92 additions and 127 deletions
|
@ -107,7 +107,6 @@ salt/cli/key.py:
|
|||
- pytests.integration.cli.test_salt_key
|
||||
|
||||
salt/cli/salt.py:
|
||||
- integration.cli.test_grains
|
||||
- integration.shell.test_enabled
|
||||
- pytests.integration.cli.test_salt
|
||||
- pytests.integration.cli.test_matcher
|
||||
|
@ -160,7 +159,7 @@ salt/cli/daemons.py:
|
|||
salt/(client/ssh/.+|cli/ssh\.py):
|
||||
- integration.cli.test_custom_module
|
||||
- integration.ssh.test_deploy
|
||||
- integration.ssh.test_grains
|
||||
- pytests.integration.ssh.test_grains
|
||||
- integration.ssh.test_jinja_filters
|
||||
- integration.ssh.test_master
|
||||
- integration.ssh.test_mine
|
||||
|
@ -243,8 +242,7 @@ salt/utils/schedule.py:
|
|||
|
||||
salt/utils/vt.py:
|
||||
- integration.cli.test_custom_module
|
||||
- integration.cli.test_grains
|
||||
- integration.ssh.test_grains
|
||||
- pytests.integration.ssh.test_grains
|
||||
- integration.ssh.test_jinja_filters
|
||||
- integration.ssh.test_mine
|
||||
- pytests.integration.ssh.test_pillar
|
||||
|
|
|
@ -1,95 +0,0 @@
|
|||
"""
|
||||
:codeauthor: Daniel Mizyrycki (mzdaniel@glidelink.net)
|
||||
|
||||
|
||||
tests.integration.cli.grains
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Test salt-ssh grains id work for localhost. (gh #16129)
|
||||
|
||||
$ salt-ssh localhost grains.get id
|
||||
localhost:
|
||||
localhost
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
import pytest
|
||||
import salt.utils.files
|
||||
from tests.support.case import ShellCase, SSHCase
|
||||
from tests.support.helpers import flaky, requires_system_grains, slowTest
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@pytest.mark.windows_whitelisted
|
||||
@pytest.mark.usefixtures("salt_sub_minion")
|
||||
class GrainsTargetingTest(ShellCase):
|
||||
"""
|
||||
Integration tests for targeting with grains.
|
||||
"""
|
||||
|
||||
@slowTest
|
||||
@requires_system_grains
|
||||
def test_grains_targeting_os_running(self, grains):
|
||||
"""
|
||||
Tests running "salt -G 'os:<system-os>' test.ping and minions both return True
|
||||
"""
|
||||
test_ret = ["sub_minion:", " True", "minion:", " True"]
|
||||
ret = self.run_salt('-G "os:{}" test.ping'.format(grains["os"]))
|
||||
self.assertEqual(sorted(ret), sorted(test_ret))
|
||||
|
||||
@slowTest
|
||||
def test_grains_targeting_minion_id_running(self):
|
||||
"""
|
||||
Tests return of each running test minion targeting with minion id grain
|
||||
"""
|
||||
minion = self.run_salt('-G "id:minion" test.ping')
|
||||
self.assertEqual(sorted(minion), sorted(["minion:", " True"]))
|
||||
|
||||
sub_minion = self.run_salt('-G "id:sub_minion" test.ping')
|
||||
self.assertEqual(sorted(sub_minion), sorted(["sub_minion:", " True"]))
|
||||
|
||||
@flaky
|
||||
@slowTest
|
||||
def test_grains_targeting_disconnected(self):
|
||||
"""
|
||||
Tests return of minion using grains targeting on a disconnected minion.
|
||||
"""
|
||||
test_ret = "Minion did not return. [No response]"
|
||||
|
||||
# Create a minion key, but do not start the "fake" minion. This mimics a
|
||||
# disconnected minion.
|
||||
key_file = os.path.join(self.master_opts["pki_dir"], "minions", "disconnected")
|
||||
with salt.utils.files.fopen(key_file, "a"):
|
||||
pass
|
||||
|
||||
# ping disconnected minion and ensure it times out and returns with correct message
|
||||
try:
|
||||
ret = ""
|
||||
for item in self.run_salt(
|
||||
'-t 1 -G "id:disconnected" test.ping', timeout=40
|
||||
):
|
||||
if item != "disconnected:":
|
||||
ret = item.strip()
|
||||
break
|
||||
assert ret == test_ret
|
||||
finally:
|
||||
os.unlink(key_file)
|
||||
|
||||
|
||||
@pytest.mark.windows_whitelisted
|
||||
class SSHGrainsTest(SSHCase):
|
||||
"""
|
||||
Test salt-ssh grains functionality
|
||||
Depend on proper environment set by SSHCase class
|
||||
"""
|
||||
|
||||
@slowTest
|
||||
def test_grains_id(self):
|
||||
"""
|
||||
Test salt-ssh grains id work for localhost.
|
||||
"""
|
||||
cmd = self.run_function("grains.get", ["id"])
|
||||
self.assertEqual(cmd, "localhost")
|
|
@ -1,28 +0,0 @@
|
|||
import salt.utils.platform
|
||||
from tests.support.case import SSHCase
|
||||
from tests.support.helpers import slowTest
|
||||
from tests.support.unit import skipIf
|
||||
|
||||
|
||||
@skipIf(salt.utils.platform.is_windows(), "salt-ssh not available on Windows")
|
||||
class SSHGrainsTest(SSHCase):
|
||||
"""
|
||||
testing grains with salt-ssh
|
||||
"""
|
||||
|
||||
@slowTest
|
||||
def test_grains_items(self):
|
||||
"""
|
||||
test grains.items with salt-ssh
|
||||
"""
|
||||
ret = self.run_function("grains.items")
|
||||
if salt.utils.platform.is_darwin():
|
||||
grain = "Darwin"
|
||||
elif salt.utils.platform.is_aix():
|
||||
grain = "AIX"
|
||||
elif salt.utils.platform.is_freebsd():
|
||||
grain = "FreeBSD"
|
||||
else:
|
||||
grain = "Linux"
|
||||
self.assertEqual(ret["kernel"], grain)
|
||||
self.assertTrue(isinstance(ret, dict))
|
|
@ -1,3 +1,5 @@
|
|||
import pathlib
|
||||
|
||||
import pytest
|
||||
import salt.defaults.exitcodes
|
||||
|
||||
|
@ -347,6 +349,59 @@ def test_grain(salt_cli, salt_master, salt_minion, salt_sub_minion):
|
|||
assert salt_sub_minion.id in ret.json
|
||||
|
||||
|
||||
def test_grains_targeting_os_running(grains, salt_cli, salt_minion, salt_sub_minion):
|
||||
"""
|
||||
Tests running "salt -G 'os:<system-os>' test.ping and minions both return True
|
||||
"""
|
||||
ret = salt_cli.run("-G", "test.ping", minion_tgt="os:{}".format(grains["os"]))
|
||||
assert ret.exitcode == 0
|
||||
assert salt_minion.id in ret.json
|
||||
assert ret.json[salt_minion.id] is True
|
||||
assert salt_sub_minion.id in ret.json
|
||||
assert ret.json[salt_sub_minion.id] is True
|
||||
|
||||
|
||||
def test_grains_targeting_minion_id_running(salt_cli, salt_minion, salt_sub_minion):
|
||||
"""
|
||||
Tests return of each running test minion targeting with minion id grain
|
||||
"""
|
||||
ret = salt_cli.run("-G", "test.ping", minion_tgt="id:{}".format(salt_minion.id))
|
||||
assert ret.exitcode == 0
|
||||
assert salt_minion.id in ret.json
|
||||
assert ret.json[salt_minion.id] is True
|
||||
|
||||
ret = salt_cli.run("-G", "test.ping", minion_tgt="id:{}".format(salt_sub_minion.id))
|
||||
assert ret.exitcode == 0
|
||||
assert salt_sub_minion.id in ret.json
|
||||
assert ret.json[salt_sub_minion.id] is True
|
||||
|
||||
|
||||
def test_grains_targeting_minion_id_disconnected(salt_master, salt_minion, salt_cli):
|
||||
"""
|
||||
Tests return of minion using grains targeting on a disconnected minion.
|
||||
"""
|
||||
expected_output = "Minion did not return. [No response]"
|
||||
|
||||
# Create a minion key, but do not start the "fake" minion. This mimics a
|
||||
# disconnected minion.
|
||||
disconnected_minion_id = "disconnected"
|
||||
minions_pki_dir = pathlib.Path(salt_master.config["pki_dir"]) / "minions"
|
||||
with pytest.helpers.temp_file(
|
||||
disconnected_minion_id,
|
||||
minions_pki_dir.joinpath(salt_minion.id).read_text(),
|
||||
minions_pki_dir,
|
||||
):
|
||||
ret = salt_cli.run(
|
||||
"--timeout=1",
|
||||
"-G",
|
||||
"test.ping",
|
||||
minion_tgt="id:{}".format(disconnected_minion_id),
|
||||
)
|
||||
assert ret.exitcode == 1
|
||||
assert disconnected_minion_id in ret.json
|
||||
assert expected_output in ret.json[disconnected_minion_id]
|
||||
|
||||
|
||||
def test_regrain(salt_cli, salt_minion, salt_sub_minion):
|
||||
"""
|
||||
test salt grain matcher
|
||||
|
|
35
tests/pytests/integration/ssh/test_grains.py
Normal file
35
tests/pytests/integration/ssh/test_grains.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
import pytest
|
||||
import salt.utils.platform
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.slow_test,
|
||||
pytest.mark.skip_on_windows(reason="salt-ssh not available on Windows"),
|
||||
]
|
||||
|
||||
|
||||
def test_grains_id(salt_ssh_cli):
|
||||
"""
|
||||
Test salt-ssh grains id work for localhost.
|
||||
"""
|
||||
ret = salt_ssh_cli.run("grains.get", "id")
|
||||
assert ret.exitcode == 0
|
||||
assert ret.json == "localhost"
|
||||
|
||||
|
||||
def test_grains_items(salt_ssh_cli):
|
||||
"""
|
||||
test grains.items with salt-ssh
|
||||
"""
|
||||
ret = salt_ssh_cli.run("grains.items")
|
||||
assert ret.exitcode == 0
|
||||
assert ret.json
|
||||
assert isinstance(ret.json, dict)
|
||||
if salt.utils.platform.is_darwin():
|
||||
grain = "Darwin"
|
||||
elif salt.utils.platform.is_aix():
|
||||
grain = "AIX"
|
||||
elif salt.utils.platform.is_freebsd():
|
||||
grain = "FreeBSD"
|
||||
else:
|
||||
grain = "Linux"
|
||||
assert ret.json["kernel"] == grain
|
Loading…
Add table
Reference in a new issue