Migrate unit.states.test_ansiblegate to PyTest

This commit is contained in:
Pedro Algarvio 2021-05-19 15:13:07 +01:00 committed by Gareth J. Greenaway
parent 2d2c1c2005
commit cedc24249c
3 changed files with 71 additions and 109 deletions

View file

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

View file

@ -0,0 +1,70 @@
import json
import pathlib
import pytest
import salt.states.ansiblegate as ansiblegate
from tests.support.mock import MagicMock, patch
from tests.support.runtests import RUNTIME_VARS
@pytest.fixture
def configure_loader_modules():
return {ansiblegate: {}}
@pytest.fixture
def playbooks_examples_dir():
return pathlib.Path(RUNTIME_VARS.TESTS_DIR) / "unit" / "files" / "playbooks"
def test_ansible_playbooks_states_success(playbooks_examples_dir):
"""
Test ansible.playbooks states executions success.
"""
success_output = json.loads(
playbooks_examples_dir.joinpath("success_example.json").read_text()
)
with patch.dict(
ansiblegate.__salt__,
{"ansible.playbooks": MagicMock(return_value=success_output)},
), patch("salt.utils.path.which", MagicMock(return_value=True)):
with patch.dict(ansiblegate.__opts__, {"test": False}):
ret = ansiblegate.playbooks("foobar")
assert ret["result"] is True
assert ret["comment"] == "Changes were made by playbook foobar"
assert ret["changes"] == {
"py2hosts": {
"Ansible copy file to remote server": {"centos7-host1.tf.local": {}}
}
}
def test_ansible_playbooks_states_failed(playbooks_examples_dir):
"""
Test ansible.playbooks failed states executions.
:return:
"""
failed_output = json.loads(
playbooks_examples_dir.joinpath("failed_example.json").read_text()
)
with patch.dict(
ansiblegate.__salt__,
{"ansible.playbooks": MagicMock(return_value=failed_output)},
), patch("salt.utils.path.which", MagicMock(return_value=True)):
with patch.dict(ansiblegate.__opts__, {"test": False}):
ret = ansiblegate.playbooks("foobar")
assert ret["result"] is False
assert (
ret["comment"] == "There were some issues running the playbook foobar"
)
assert ret["changes"] == {
"py2hosts": {
"yum": {
"centos7-host1.tf.local": [
"No package matching 'rsyndc' found available, installed or updated"
]
}
}
}

View file

@ -1,108 +0,0 @@
#
# Copyright 2020 SUSE LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Import Salt Testing Libs
import json
import os
import salt.states.ansiblegate as ansible
import salt.utils.files
import salt.utils.platform
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import TestCase, skipIf
try:
import pytest
except ImportError:
pytest = None
@skipIf(pytest is None, "PyTest is missing")
@skipIf(salt.utils.platform.is_windows(), "Not supported on Windows")
class AnsiblegateTestCase(TestCase, LoaderModuleMockMixin):
@classmethod
def setUpClass(cls):
cls.playbooks_examples_dir = os.path.join(
RUNTIME_VARS.TESTS_DIR, "unit/files/playbooks/"
)
def setup_loader_modules(self):
return {ansible: {}}
def test_ansible_playbooks_states_success(self):
"""
Test ansible.playbooks states executions success.
:return:
"""
with salt.utils.files.fopen(
os.path.join(self.playbooks_examples_dir, "success_example.json")
) as f:
success_output = json.loads(f.read())
with patch.dict(
ansible.__salt__,
{"ansible.playbooks": MagicMock(return_value=success_output)},
), patch("salt.utils.path.which", MagicMock(return_value=True)):
with patch.dict(ansible.__opts__, {"test": False}):
ret = ansible.playbooks("foobar")
self.assertTrue(ret["result"])
self.assertEqual(ret["comment"], "Changes were made by playbook foobar")
self.assertDictEqual(
ret["changes"],
{
"py2hosts": {
"Ansible copy file to remote server": {
"centos7-host1.tf.local": {}
}
}
},
)
def test_ansible_playbooks_states_failed(self):
"""
Test ansible.playbooks failed states executions.
:return:
"""
with salt.utils.files.fopen(
os.path.join(self.playbooks_examples_dir, "failed_example.json")
) as f:
failed_output = json.loads(f.read())
with patch.dict(
ansible.__salt__,
{"ansible.playbooks": MagicMock(return_value=failed_output)},
), patch("salt.utils.path.which", MagicMock(return_value=True)):
with patch.dict(ansible.__opts__, {"test": False}):
ret = ansible.playbooks("foobar")
self.assertFalse(ret["result"])
self.assertEqual(
ret["comment"], "There were some issues running the playbook foobar"
)
self.assertDictEqual(
ret["changes"],
{
"py2hosts": {
"yum": {
"centos7-host1.tf.local": [
"No package matching 'rsyndc' found available, installed or updated"
]
}
}
},
)