mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-05 07:49:10 -07:00
[stable-9] Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9840)
Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9838)
Make set_module_args() a context manager, and remove copies of set_module_args().
Prepares for Data Tagging.
(cherry picked from commit a1781d09dd
)
This commit is contained in:
parent
9a6bd80613
commit
013fb9c006
80 changed files with 3745 additions and 3977 deletions
|
@ -8,39 +8,8 @@ __metaclass__ = type
|
|||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import patch
|
||||
from ansible.module_utils import basic
|
||||
from ansible.module_utils.common.text.converters import to_bytes
|
||||
from ansible_collections.community.general.plugins.modules import jenkins_build
|
||||
|
||||
import json
|
||||
|
||||
|
||||
def set_module_args(args):
|
||||
"""prepare arguments so that they will be picked up during module creation"""
|
||||
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
|
||||
basic._ANSIBLE_ARGS = to_bytes(args)
|
||||
|
||||
|
||||
class AnsibleExitJson(Exception):
|
||||
"""Exception class to be raised by module.exit_json and caught by the test case"""
|
||||
pass
|
||||
|
||||
|
||||
class AnsibleFailJson(Exception):
|
||||
"""Exception class to be raised by module.fail_json and caught by the test case"""
|
||||
pass
|
||||
|
||||
|
||||
def exit_json(*args, **kwargs):
|
||||
"""function to patch over exit_json; package return data into an exception"""
|
||||
if 'changed' not in kwargs:
|
||||
kwargs['changed'] = False
|
||||
raise AnsibleExitJson(kwargs)
|
||||
|
||||
|
||||
def fail_json(*args, **kwargs):
|
||||
"""function to patch over fail_json; package return data into an exception"""
|
||||
kwargs['failed'] = True
|
||||
raise AnsibleFailJson(kwargs)
|
||||
from ansible_collections.community.general.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, set_module_args, exit_json, fail_json
|
||||
|
||||
|
||||
class jenkins:
|
||||
|
@ -131,18 +100,18 @@ class TestJenkinsBuild(unittest.TestCase):
|
|||
def test_module_fail_when_required_args_missing(self, test_deps):
|
||||
test_deps.return_value = None
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
set_module_args({})
|
||||
jenkins_build.main()
|
||||
with set_module_args({}):
|
||||
jenkins_build.main()
|
||||
|
||||
@patch('ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies')
|
||||
def test_module_fail_when_missing_build_number(self, test_deps):
|
||||
test_deps.return_value = None
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "required-if",
|
||||
"state": "stopped"
|
||||
})
|
||||
jenkins_build.main()
|
||||
}):
|
||||
jenkins_build.main()
|
||||
|
||||
@patch('ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies')
|
||||
@patch('ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection')
|
||||
|
@ -151,12 +120,12 @@ class TestJenkinsBuild(unittest.TestCase):
|
|||
jenkins_connection.return_value = JenkinsMock()
|
||||
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "host-check",
|
||||
"user": "abc",
|
||||
"token": "xyz"
|
||||
})
|
||||
jenkins_build.main()
|
||||
}):
|
||||
jenkins_build.main()
|
||||
|
||||
@patch('ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies')
|
||||
@patch('ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection')
|
||||
|
@ -165,14 +134,14 @@ class TestJenkinsBuild(unittest.TestCase):
|
|||
jenkins_connection.return_value = JenkinsMock()
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as return_json:
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "host-check",
|
||||
"build_number": "1234",
|
||||
"state": "stopped",
|
||||
"user": "abc",
|
||||
"token": "xyz"
|
||||
})
|
||||
jenkins_build.main()
|
||||
}):
|
||||
jenkins_build.main()
|
||||
|
||||
self.assertTrue(return_json.exception.args[0]['changed'])
|
||||
|
||||
|
@ -183,14 +152,14 @@ class TestJenkinsBuild(unittest.TestCase):
|
|||
jenkins_connection.return_value = JenkinsMockIdempotent()
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as return_json:
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "host-check",
|
||||
"build_number": "1234",
|
||||
"state": "stopped",
|
||||
"user": "abc",
|
||||
"password": "xyz"
|
||||
})
|
||||
jenkins_build.main()
|
||||
}):
|
||||
jenkins_build.main()
|
||||
|
||||
self.assertFalse(return_json.exception.args[0]['changed'])
|
||||
|
||||
|
@ -203,14 +172,14 @@ class TestJenkinsBuild(unittest.TestCase):
|
|||
build_status.return_value = JenkinsBuildMock().get_build_status()
|
||||
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "host-delete",
|
||||
"build_number": "1234",
|
||||
"state": "absent",
|
||||
"user": "abc",
|
||||
"token": "xyz"
|
||||
})
|
||||
jenkins_build.main()
|
||||
}):
|
||||
jenkins_build.main()
|
||||
|
||||
@patch('ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies')
|
||||
@patch('ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection')
|
||||
|
@ -219,14 +188,14 @@ class TestJenkinsBuild(unittest.TestCase):
|
|||
jenkins_connection.return_value = JenkinsMockIdempotent()
|
||||
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "host-delete",
|
||||
"build_number": "1234",
|
||||
"state": "absent",
|
||||
"user": "abc",
|
||||
"token": "xyz"
|
||||
})
|
||||
jenkins_build.main()
|
||||
}):
|
||||
jenkins_build.main()
|
||||
|
||||
@patch('ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies')
|
||||
@patch('ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection')
|
||||
|
@ -237,12 +206,12 @@ class TestJenkinsBuild(unittest.TestCase):
|
|||
build_status.return_value = JenkinsBuildMock().get_build_status()
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as return_json:
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "create-detached",
|
||||
"user": "abc",
|
||||
"token": "xyz"
|
||||
})
|
||||
jenkins_build.main()
|
||||
}):
|
||||
jenkins_build.main()
|
||||
|
||||
self.assertFalse(return_json.exception.args[0]['changed'])
|
||||
|
||||
|
@ -253,12 +222,12 @@ class TestJenkinsBuild(unittest.TestCase):
|
|||
jenkins_connection.return_value = JenkinsMock()
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as return_json:
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "create-detached",
|
||||
"user": "abc",
|
||||
"token": "xyz",
|
||||
"detach": True
|
||||
})
|
||||
jenkins_build.main()
|
||||
}):
|
||||
jenkins_build.main()
|
||||
|
||||
self.assertTrue(return_json.exception.args[0]['changed'])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue