move roster unit tests to pytest

This commit is contained in:
MKLeb 2022-10-05 17:19:34 -04:00 committed by Megan Wilhite
parent 8112e8a445
commit 6d8ca439ce
12 changed files with 351 additions and 348 deletions

View file

@ -0,0 +1,49 @@
"""
unit tests for clustershell roster
"""
import pytest
from tests.support.mock import MagicMock, patch
try:
from ClusterShell.NodeSet import NodeSet # pylint: disable=unused-import
HAS_CLUSTERSHELL = True
except (ImportError, OSError) as e:
HAS_CLUSTERSHELL = False
pytestmark = [
pytest.mark.skipif(
HAS_CLUSTERSHELL is False,
reason="Install Python Clustershell bindings before running these tests.",
)
]
def test_targets():
mock_socket = MagicMock()
mock_nodeset = MagicMock()
mock_nodeset.NodeSet.return_value = ["foo"]
with patch.dict(
"sys.modules", **{"socket": mock_socket, "ClusterShell.NodeSet": mock_nodeset}
):
import salt.roster.clustershell
salt.roster.clustershell.__opts__ = {}
with patch.dict(
salt.roster.clustershell.__opts__,
{"ssh_scan_ports": [1, 2, 3], "ssh_scan_timeout": 30},
):
# Reimports are necessary to re-init the namespace.
# pylint: disable=unused-import
import socket
from ClusterShell.NodeSet import NodeSet
# pylint: enable=unused-import
ret = salt.roster.clustershell.targets("foo")
mock_socket.gethostbyname.assert_any_call("foo")
assert "foo" in ret
assert ret["foo"]["port"] == 3

View file

@ -0,0 +1,94 @@
"""
Test the scan roster.
"""
import socket
import pytest
import salt.roster.scan as scan_
from tests.support.mock import MagicMock, patch
@pytest.fixture
def configure_loader_modules():
return {scan_: {"__opts__": {"ssh_scan_ports": "22", "ssh_scan_timeout": 0.01}}}
def test_single_ip():
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets("127.0.0.1")
assert ret == {"127.0.0.1": {"host": "127.0.0.1", "port": 22}}
def test_single_network():
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets("127.0.0.0/30")
assert ret == {
"127.0.0.1": {"host": "127.0.0.1", "port": 22},
"127.0.0.2": {"host": "127.0.0.2", "port": 22},
}
def test_multiple_ips():
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets(["127.0.0.1", "127.0.0.2"], tgt_type="list")
assert ret == {
"127.0.0.1": {"host": "127.0.0.1", "port": 22},
"127.0.0.2": {"host": "127.0.0.2", "port": 22},
}
def test_multiple_networks():
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets(
["127.0.0.0/30", "127.0.2.1", "127.0.1.0/30"], tgt_type="list"
)
assert ret == {
"127.0.0.1": {"host": "127.0.0.1", "port": 22},
"127.0.0.2": {"host": "127.0.0.2", "port": 22},
"127.0.2.1": {"host": "127.0.2.1", "port": 22},
"127.0.1.1": {"host": "127.0.1.1", "port": 22},
"127.0.1.2": {"host": "127.0.1.2", "port": 22},
}
def test_malformed_ip():
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets("127001")
assert ret == {}
def test_multiple_with_malformed():
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets(["127.0.0.1", "127002", "127.0.1.0/30"], tgt_type="list")
assert ret == {
"127.0.0.1": {"host": "127.0.0.1", "port": 22},
"127.0.1.1": {"host": "127.0.1.1", "port": 22},
"127.0.1.2": {"host": "127.0.1.2", "port": 22},
}
def test_multiple_no_connection():
"""Test that minion files in the directory roster match and render."""
socket_mock = MagicMock()
socket_mock.connect = MagicMock(
side_effect=[None, socket.error(), None, socket.error(), None]
)
with patch("salt.utils.network.get_socket", return_value=socket_mock):
ret = scan_.targets(
["127.0.0.0/30", "127.0.2.1", "127.0.1.0/30"], tgt_type="list"
)
assert ret == {
"127.0.0.1": {"host": "127.0.0.1", "port": 22},
"127.0.0.2": {},
"127.0.2.1": {"host": "127.0.2.1", "port": 22},
"127.0.1.1": {},
"127.0.1.2": {"host": "127.0.1.2", "port": 22},
}

View file

@ -0,0 +1,103 @@
import collections
import textwrap
import pytest
import salt.roster.sshconfig as sshconfig
from tests.support.mock import mock_open, patch
@pytest.fixture
def target_abc():
return collections.OrderedDict(
[
("user", "user.mcuserface"),
("priv", "~/.ssh/id_rsa_abc"),
("host", "abc.asdfgfdhgjkl.com"),
]
)
@pytest.fixture
def target_abc123():
return collections.OrderedDict(
[
("user", "user.mcuserface"),
("priv", "~/.ssh/id_rsa_abc"),
("host", "abc123.asdfgfdhgjkl.com"),
]
)
@pytest.fixture
def target_def():
return collections.OrderedDict(
[
("user", "user.mcuserface"),
("priv", "~/.ssh/id_rsa_def"),
("host", "def.asdfgfdhgjkl.com"),
]
)
@pytest.fixture
def all_(target_abc, target_abc123, target_def):
return {
"abc.asdfgfdhgjkl.com": target_abc,
"abc123.asdfgfdhgjkl.com": target_abc123,
"def.asdfgfdhgjkl.com": target_def,
}
@pytest.fixture
def abc_glob(target_abc, target_abc123):
return {
"abc.asdfgfdhgjkl.com": target_abc,
"abc123.asdfgfdhgjkl.com": target_abc123,
}
@pytest.fixture
def mock_fp():
sample_ssh_config = textwrap.dedent(
"""
Host *
User user.mcuserface
Host abc*
IdentityFile ~/.ssh/id_rsa_abc
Host def*
IdentityFile ~/.ssh/id_rsa_def
Host abc.asdfgfdhgjkl.com
HostName 123.123.123.123
Host abc123.asdfgfdhgjkl.com
HostName 123.123.123.124
Host def.asdfgfdhgjkl.com
HostName 234.234.234.234
"""
)
return mock_open(read_data=sample_ssh_config)
@pytest.fixture
def configure_loader_modules():
return {sshconfig: {}}
def test_all(mock_fp, all_):
with patch("salt.utils.files.fopen", mock_fp):
with patch("salt.roster.sshconfig._get_ssh_config_file"):
targets = sshconfig.targets("*")
assert targets == all_
def test_abc_glob(mock_fp, abc_glob):
with patch("salt.utils.files.fopen", mock_fp):
with patch("salt.roster.sshconfig._get_ssh_config_file"):
targets = sshconfig.targets("abc*")
assert targets == abc_glob

View file

@ -0,0 +1,105 @@
"""
unittests for terraform roster
"""
import pathlib
import pytest
from salt.roster import terraform
from salt.utils import roster_matcher
@pytest.fixture
def roster_file():
return pathlib.Path(__file__).parent / "terraform.data" / "terraform.tfstate"
@pytest.fixture
def pki_dir():
return pathlib.Path(__file__).parent / "terraform.data"
@pytest.fixture
def configure_loader_modules(roster_file, pki_dir):
# opts = salt.config.master_config(
# os.path.join(RUNTIME_VARS.TMP_CONF_DIR, "master")
# )
# utils = salt.loader.utils(opts, whitelist=["roster_matcher"])
return {
terraform: {
"__utils__": {
"roster_matcher.targets": roster_matcher.targets,
},
"__opts__": {
"roster_file": str(roster_file),
"pki_dir": str(pki_dir),
},
},
roster_matcher: {},
}
def test_default_output(pki_dir):
"""
Test the output of a fixture tfstate file which contains libvirt
resources.
"""
expected_result = {
"db0": {
"host": "192.168.122.174",
"user": "root",
"passwd": "dbpw",
"tty": True,
"priv": str(pki_dir / "ssh" / "salt-ssh.rsa"),
},
"db1": {
"host": "192.168.122.190",
"user": "root",
"passwd": "dbpw",
"tty": True,
"priv": str(pki_dir / "ssh" / "salt-ssh.rsa"),
},
"web0": {
"host": "192.168.122.106",
"user": "root",
"passwd": "linux",
"timeout": 22,
"priv": str(pki_dir / "ssh" / "salt-ssh.rsa"),
},
"web1": {
"host": "192.168.122.235",
"user": "root",
"passwd": "linux",
"timeout": 22,
"priv": str(pki_dir / "ssh" / "salt-ssh.rsa"),
},
}
ret = terraform.targets("*")
assert expected_result == ret
def test_default_matching(pki_dir):
"""
Test the output of a fixture tfstate file which contains libvirt
resources using matching
"""
expected_result = {
"web0": {
"host": "192.168.122.106",
"user": "root",
"passwd": "linux",
"timeout": 22,
"priv": str(pki_dir / "ssh" / "salt-ssh.rsa"),
},
"web1": {
"host": "192.168.122.235",
"user": "root",
"passwd": "linux",
"timeout": 22,
"priv": str(pki_dir / "ssh" / "salt-ssh.rsa"),
},
}
ret = terraform.targets("*web*")
assert expected_result == ret

View file

@ -1,52 +0,0 @@
"""
unit tests for clustershell roster
"""
from tests.support.mock import MagicMock, patch
# Import Salt Testing libraries
from tests.support.unit import TestCase, skipIf
try:
from ClusterShell.NodeSet import NodeSet # pylint: disable=unused-import
HAS_CLUSTERSHELL = True
except (ImportError, OSError) as e:
HAS_CLUSTERSHELL = False
@skipIf(
HAS_CLUSTERSHELL is False,
"Install Python Clustershell bindings before running these tests.",
)
class ClusterShellTestCase(TestCase):
"""
Test cases for clustershell roster
"""
def test_targets(self):
mock_socket = MagicMock()
mock_nodeset = MagicMock()
mock_nodeset.NodeSet.return_value = ["foo"]
with patch.dict(
"sys.modules",
**{"socket": mock_socket, "ClusterShell.NodeSet": mock_nodeset}
):
import salt.roster.clustershell
salt.roster.clustershell.__opts__ = {}
with patch.dict(
salt.roster.clustershell.__opts__,
{"ssh_scan_ports": [1, 2, 3], "ssh_scan_timeout": 30},
):
# Reimports are necessary to re-init the namespace.
# pylint: disable=unused-import
import socket
from ClusterShell.NodeSet import NodeSet
# pylint: enable=unused-import
ret = salt.roster.clustershell.targets("foo")
mock_socket.gethostbyname.assert_any_call("foo")
self.assertTrue("foo" in ret)
self.assertTrue(ret["foo"]["port"] == 3)

View file

@ -1,106 +0,0 @@
"""
Test the scan roster.
"""
import socket
import salt.roster.scan as scan_
from tests.support import mixins
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
class ScanRosterTestCase(TestCase, mixins.LoaderModuleMockMixin):
"""Test the directory roster"""
def setup_loader_modules(self):
return {scan_: {"__opts__": {"ssh_scan_ports": "22", "ssh_scan_timeout": 0.01}}}
def test_single_ip(self):
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets("127.0.0.1")
self.assertEqual(ret, {"127.0.0.1": {"host": "127.0.0.1", "port": 22}})
def test_single_network(self):
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets("127.0.0.0/30")
self.assertEqual(
ret,
{
"127.0.0.1": {"host": "127.0.0.1", "port": 22},
"127.0.0.2": {"host": "127.0.0.2", "port": 22},
},
)
def test_multiple_ips(self):
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets(["127.0.0.1", "127.0.0.2"], tgt_type="list")
self.assertEqual(
ret,
{
"127.0.0.1": {"host": "127.0.0.1", "port": 22},
"127.0.0.2": {"host": "127.0.0.2", "port": 22},
},
)
def test_multiple_networks(self):
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets(
["127.0.0.0/30", "127.0.2.1", "127.0.1.0/30"], tgt_type="list"
)
self.assertEqual(
ret,
{
"127.0.0.1": {"host": "127.0.0.1", "port": 22},
"127.0.0.2": {"host": "127.0.0.2", "port": 22},
"127.0.2.1": {"host": "127.0.2.1", "port": 22},
"127.0.1.1": {"host": "127.0.1.1", "port": 22},
"127.0.1.2": {"host": "127.0.1.2", "port": 22},
},
)
def test_malformed_ip(self):
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets("127001")
self.assertEqual(ret, {})
def test_multiple_with_malformed(self):
"""Test that minion files in the directory roster match and render."""
with patch("salt.utils.network.get_socket"):
ret = scan_.targets(
["127.0.0.1", "127002", "127.0.1.0/30"], tgt_type="list"
)
self.assertEqual(
ret,
{
"127.0.0.1": {"host": "127.0.0.1", "port": 22},
"127.0.1.1": {"host": "127.0.1.1", "port": 22},
"127.0.1.2": {"host": "127.0.1.2", "port": 22},
},
)
def test_multiple_no_connection(self):
"""Test that minion files in the directory roster match and render."""
socket_mock = MagicMock()
socket_mock.connect = MagicMock(
side_effect=[None, socket.error(), None, socket.error(), None]
)
with patch("salt.utils.network.get_socket", return_value=socket_mock):
ret = scan_.targets(
["127.0.0.0/30", "127.0.2.1", "127.0.1.0/30"], tgt_type="list"
)
self.assertEqual(
ret,
{
"127.0.0.1": {"host": "127.0.0.1", "port": 22},
"127.0.0.2": {},
"127.0.2.1": {"host": "127.0.2.1", "port": 22},
"127.0.1.1": {},
"127.0.1.2": {"host": "127.0.1.2", "port": 22},
},
)

View file

@ -1,81 +0,0 @@
import collections
import salt.roster.sshconfig as sshconfig
from tests.support import mixins
from tests.support.mock import mock_open, patch
from tests.support.unit import TestCase
_SAMPLE_SSH_CONFIG = """
Host *
User user.mcuserface
Host abc*
IdentityFile ~/.ssh/id_rsa_abc
Host def*
IdentityFile ~/.ssh/id_rsa_def
Host abc.asdfgfdhgjkl.com
HostName 123.123.123.123
Host abc123.asdfgfdhgjkl.com
HostName 123.123.123.124
Host def.asdfgfdhgjkl.com
HostName 234.234.234.234
"""
_TARGET_ABC = collections.OrderedDict(
[
("user", "user.mcuserface"),
("priv", "~/.ssh/id_rsa_abc"),
("host", "abc.asdfgfdhgjkl.com"),
]
)
_TARGET_ABC123 = collections.OrderedDict(
[
("user", "user.mcuserface"),
("priv", "~/.ssh/id_rsa_abc"),
("host", "abc123.asdfgfdhgjkl.com"),
]
)
_TARGET_DEF = collections.OrderedDict(
[
("user", "user.mcuserface"),
("priv", "~/.ssh/id_rsa_def"),
("host", "def.asdfgfdhgjkl.com"),
]
)
_ALL = {
"abc.asdfgfdhgjkl.com": _TARGET_ABC,
"abc123.asdfgfdhgjkl.com": _TARGET_ABC123,
"def.asdfgfdhgjkl.com": _TARGET_DEF,
}
_ABC_GLOB = {
"abc.asdfgfdhgjkl.com": _TARGET_ABC,
"abc123.asdfgfdhgjkl.com": _TARGET_ABC123,
}
class SSHConfigRosterTestCase(TestCase, mixins.LoaderModuleMockMixin):
def setUp(self):
self.mock_fp = mock_open(read_data=_SAMPLE_SSH_CONFIG)
def setup_loader_modules(self):
return {sshconfig: {}}
def test_all(self):
with patch("salt.utils.files.fopen", self.mock_fp):
with patch("salt.roster.sshconfig._get_ssh_config_file"):
targets = sshconfig.targets("*")
self.assertEqual(targets, _ALL)
def test_abc_glob(self):
with patch("salt.utils.files.fopen", self.mock_fp):
with patch("salt.roster.sshconfig._get_ssh_config_file"):
targets = sshconfig.targets("abc*")
self.assertEqual(targets, _ABC_GLOB)

View file

@ -1,109 +0,0 @@
"""
unittests for terraform roster
"""
import os.path
import salt.config
import salt.loader
from salt.roster import terraform
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import patch
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import TestCase
class TerraformTestCase(TestCase, LoaderModuleMockMixin):
"""
Test cases for salt.roster.terraform
"""
def setup_loader_modules(self):
opts = salt.config.master_config(
os.path.join(RUNTIME_VARS.TMP_CONF_DIR, "master")
)
utils = salt.loader.utils(opts, whitelist=["roster_matcher"])
return {terraform: {"__utils__": utils, "__opts__": {}}}
def test_default_output(self):
"""
Test the output of a fixture tfstate file which contains libvirt
resources.
"""
tfstate = os.path.join(
os.path.dirname(__file__), "terraform.data", "terraform.tfstate"
)
pki_dir = os.path.abspath(
os.path.join(os.path.dirname(__file__), "terraform.data")
)
with patch.dict(
terraform.__opts__, {"roster_file": tfstate, "pki_dir": pki_dir}
):
expected_result = {
"db0": {
"host": "192.168.122.174",
"user": "root",
"passwd": "dbpw",
"tty": True,
"priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
},
"db1": {
"host": "192.168.122.190",
"user": "root",
"passwd": "dbpw",
"tty": True,
"priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
},
"web0": {
"host": "192.168.122.106",
"user": "root",
"passwd": "linux",
"timeout": 22,
"priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
},
"web1": {
"host": "192.168.122.235",
"user": "root",
"passwd": "linux",
"timeout": 22,
"priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
},
}
ret = terraform.targets("*")
self.assertDictEqual(expected_result, ret)
def test_default_matching(self):
"""
Test the output of a fixture tfstate file which contains libvirt
resources using matching
"""
tfstate = os.path.join(
os.path.dirname(__file__), "terraform.data", "terraform.tfstate"
)
pki_dir = os.path.abspath(
os.path.join(os.path.dirname(__file__), "terraform.data")
)
with patch.dict(
terraform.__opts__, {"roster_file": tfstate, "pki_dir": pki_dir}
):
expected_result = {
"web0": {
"host": "192.168.122.106",
"user": "root",
"passwd": "linux",
"timeout": 22,
"priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
},
"web1": {
"host": "192.168.122.235",
"user": "root",
"passwd": "linux",
"timeout": 22,
"priv": os.path.join(pki_dir, "ssh", "salt-ssh.rsa"),
},
}
ret = terraform.targets("*web*")
self.assertDictEqual(expected_result, ret)