Migrate the ansiblegate state integration tests to pytest

This commit is contained in:
Pedro Algarvio 2021-05-19 07:39:43 +01:00 committed by Gareth J. Greenaway
parent 5d62aebadd
commit 2d2c1c2005
3 changed files with 87 additions and 82 deletions

View file

@ -30,7 +30,7 @@ salt/modules/(debian_service|freebsdservice|gentoo_service|launchctl_service|mac
salt/modules/ansiblegate.py:
- unit.states.test_ansiblegate
- integration.states.test_ansiblegate
- pytests.integration.states.test_ansiblegate
- pytests.functional.modules.test_ansiblegate
- pytests.unit.modules.test_ansiblegate

View file

@ -1,81 +0,0 @@
"""
Test AnsibleGate State Module
"""
import os
import shutil
import tempfile
import pytest
import salt.utils.files
import salt.utils.path
import yaml
from tests.support.case import ModuleCase
from tests.support.helpers import requires_system_grains
from tests.support.mixins import SaltReturnAssertsMixin
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import SkipTest
@pytest.mark.destructive_test
@pytest.mark.requires_sshd_server
@pytest.mark.skip_if_binaries_missing(
"ansible-playbook", reason="ansible-playbook is not installed"
)
class AnsiblePlaybooksTestCase(ModuleCase, SaltReturnAssertsMixin):
"""
Test ansible.playbooks states
"""
@classmethod
@requires_system_grains
def setUpClass(cls, grains=None): # pylint: disable=arguments-differ
if grains.get("os_family") == "RedHat" and grains.get("osmajorrelease") == 6:
raise SkipTest(
"This test hangs the test suite on RedHat 6. Skipping for now."
)
def setUp(self):
priv_file = os.path.join(RUNTIME_VARS.TMP_SSH_CONF_DIR, "client_key")
data = {
"all": {
"hosts": {
"localhost": {
"ansible_host": "127.0.0.1",
"ansible_port": 2827,
"ansible_user": RUNTIME_VARS.RUNNING_TESTS_USER,
"ansible_ssh_private_key_file": priv_file,
"ansible_ssh_extra_args": (
"-o StrictHostKeyChecking=false "
"-o UserKnownHostsFile=/dev/null "
),
},
},
},
}
self.tempdir = tempfile.mkdtemp()
self.inventory = self.tempdir + "inventory"
with salt.utils.files.fopen(self.inventory, "w") as yaml_file:
yaml.dump(data, yaml_file, default_flow_style=False)
def tearDown(self):
shutil.rmtree(self.tempdir)
delattr(self, "tempdir")
delattr(self, "inventory")
@pytest.mark.flaky(max_runs=4)
def test_ansible_playbook(self):
ret = self.run_state(
"ansible.playbooks",
name="remove.yml",
git_repo="git://github.com/gtmanfred/playbooks.git",
ansible_kwargs={"inventory": self.inventory},
)
self.assertSaltTrueReturn(ret)
ret = self.run_state(
"ansible.playbooks",
name="install.yml",
git_repo="git://github.com/gtmanfred/playbooks.git",
ansible_kwargs={"inventory": self.inventory},
)
self.assertSaltTrueReturn(ret)

View file

@ -0,0 +1,86 @@
"""
Test AnsibleGate State Module
"""
import shutil
import pytest
import salt.utils.files
import salt.utils.path
import yaml
from tests.support.pytest.helpers import StateReturnAsserts
from tests.support.runtests import RUNTIME_VARS
pytestmark = [
# root access is necessary because the playbooks install/uninstall software
pytest.mark.skip_if_not_root,
# Because of the above, these are also destructive tests
pytest.mark.destructive_test,
pytest.mark.skip_if_binaries_missing(
"ansible-playbook", message="ansible-playbook is not installed"
),
]
@pytest.fixture(scope="module")
def ansible_inventory_directory(tmp_path_factory, grains):
import pprint
pprint.pprint(grains)
if grains["os_family"] != "RedHat":
pytest.skip("Currently, the test targets the RedHat OS familly only.")
tmp_dir = tmp_path_factory.mktemp("ansible")
try:
yield tmp_dir
finally:
shutil.rmtree(str(tmp_dir))
@pytest.fixture(scope="module", autouse=True)
def ansible_inventory(ansible_inventory_directory, sshd_server):
inventory = ansible_inventory_directory / "inventory"
client_key = str(sshd_server.config_dir / "client_key")
data = {
"all": {
"hosts": {
"localhost": {
"ansible_host": "127.0.0.1",
"ansible_port": sshd_server.listen_port,
"ansible_user": RUNTIME_VARS.RUNNING_TESTS_USER,
"ansible_ssh_private_key_file": client_key,
"ansible_ssh_extra_args": (
"-o StrictHostKeyChecking=false "
"-o UserKnownHostsFile=/dev/null "
),
},
},
},
}
with salt.utils.files.fopen(str(inventory), "w") as yaml_file:
yaml.dump(data, yaml_file, default_flow_style=False)
return str(inventory)
@pytest.mark.requires_sshd_server
def test_ansible_playbook(salt_call_cli, ansible_inventory):
ret = salt_call_cli.run(
"state.single",
"ansible.playbooks",
name="remove.yml",
git_repo="https://github.com/saltstack/salt-test-suite-ansible-playbooks.git",
ansible_kwargs={"inventory": ansible_inventory},
)
assert ret.exitcode == 0
state_asserts = StateReturnAsserts(ret.json)
state_asserts.assert_state_true_return()
ret = salt_call_cli.run(
"state.single",
"ansible.playbooks",
name="install.yml",
git_repo="https://github.com/saltstack/salt-test-suite-ansible-playbooks.git",
ansible_kwargs={"inventory": ansible_inventory},
)
assert ret.exitcode == 0
state_asserts = StateReturnAsserts(ret.json)
state_asserts.assert_state_true_return()