mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-02-04 07:51:50 +00:00
Improve Python code: address unused variables (#11049)
* Address F841 (unused variable). * Reformat. * Add changelog fragment. * More cleanup. * Remove trailing whitespace. * Readd removed code as a comment with TODO.
This commit is contained in:
parent
0d8521c718
commit
396f467bbb
90 changed files with 232 additions and 235 deletions
|
|
@ -226,7 +226,6 @@ class TestLookupModule(unittest.TestCase):
|
|||
with patch("ansible_collections.community.general.plugins.lookup.bitwarden._bitwarden", mock_bitwarden):
|
||||
record = MOCK_RECORDS[0]
|
||||
record_name = record["name"]
|
||||
session = "session"
|
||||
|
||||
self.lookup.run([record_name], field=None)
|
||||
self.assertIsNone(mock_bitwarden.session)
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ class MockLPass(LPass):
|
|||
p = ArgumentParser()
|
||||
sp = p.add_subparsers(help="command", dest="subparser_name")
|
||||
|
||||
logout_p = sp.add_parser("logout", parents=[base_options], help="logout")
|
||||
sp.add_parser("logout", parents=[base_options], help="logout")
|
||||
show_p = sp.add_parser("show", parents=[base_options], help="show entry details")
|
||||
|
||||
field_group = show_p.add_mutually_exclusive_group(required=True)
|
||||
|
|
|
|||
|
|
@ -613,4 +613,4 @@ class TestPritunlApi:
|
|||
api.pritunl_auth_request = get_pritunl_error_mock()
|
||||
|
||||
with pytest.raises(api.PritunlException):
|
||||
response = api.list_pritunl_organizations(**pritunl_settings)
|
||||
api.list_pritunl_organizations(**pritunl_settings)
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ def module():
|
|||
|
||||
|
||||
def test_wrong_name(module):
|
||||
with deps.declare("sys") as sys_dep:
|
||||
with deps.declare("sys"):
|
||||
import sys # noqa: F401, pylint: disable=unused-import
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
|
|
|
|||
|
|
@ -51,5 +51,5 @@ def test_saslprep_conversions(source, target):
|
|||
|
||||
@pytest.mark.parametrize("source,exception", INVALID)
|
||||
def test_saslprep_exceptions(source, exception):
|
||||
with pytest.raises(exception) as ex:
|
||||
with pytest.raises(exception):
|
||||
saslprep(source)
|
||||
|
|
|
|||
|
|
@ -14,9 +14,7 @@ from .common import fake_xenapi_ref
|
|||
|
||||
def test_get_object_ref_xenapi_failure(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests catching of XenAPI failures."""
|
||||
mocked_xenapi = mocker.patch.object(
|
||||
XenAPI.Session, "xenapi_request", side_effect=XenAPI.Failure("Fake XAPI method call error!")
|
||||
)
|
||||
mocker.patch.object(XenAPI.Session, "xenapi_request", side_effect=XenAPI.Failure("Fake XAPI method call error!"))
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.get_object_ref(fake_ansible_module, "name")
|
||||
|
|
@ -37,9 +35,7 @@ def test_get_object_ref_bad_uuid_and_name(mocker, fake_ansible_module, XenAPI, x
|
|||
|
||||
def test_get_object_ref_uuid_not_found(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests when object is not found by uuid."""
|
||||
mocked_xenapi = mocker.patch.object(
|
||||
XenAPI.Session, "xenapi_request", side_effect=XenAPI.Failure("Fake XAPI not found error!")
|
||||
)
|
||||
mocker.patch.object(XenAPI.Session, "xenapi_request", side_effect=XenAPI.Failure("Fake XAPI not found error!"))
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.get_object_ref(fake_ansible_module, "name", uuid="fake-uuid", msg_prefix="Test: ")
|
||||
|
|
@ -52,7 +48,7 @@ def test_get_object_ref_uuid_not_found(mocker, fake_ansible_module, XenAPI, xens
|
|||
|
||||
def test_get_object_ref_name_not_found(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests when object is not found by name."""
|
||||
mocked_xenapi = mocker.patch.object(XenAPI.Session, "xenapi_request", return_value=[])
|
||||
mocker.patch.object(XenAPI.Session, "xenapi_request", return_value=[])
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xenserver.get_object_ref(fake_ansible_module, "name", msg_prefix="Test: ")
|
||||
|
|
@ -63,9 +59,7 @@ def test_get_object_ref_name_not_found(mocker, fake_ansible_module, XenAPI, xens
|
|||
|
||||
def test_get_object_ref_name_multiple_found(mocker, fake_ansible_module, XenAPI, xenserver):
|
||||
"""Tests when multiple objects are found by name."""
|
||||
mocked_xenapi = mocker.patch.object(
|
||||
XenAPI.Session, "xenapi_request", return_value=[fake_xenapi_ref("VM"), fake_xenapi_ref("VM")]
|
||||
)
|
||||
mocker.patch.object(XenAPI.Session, "xenapi_request", return_value=[fake_xenapi_ref("VM"), fake_xenapi_ref("VM")])
|
||||
|
||||
error_msg = "Test: multiple VMs with name 'name' found! Please use UUID."
|
||||
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ def test_xapi_connect_local_session(mocker, fake_ansible_module, XenAPI, xenserv
|
|||
"""Tests that connection to localhost uses XenAPI.xapi_local() function."""
|
||||
mocker.patch("XenAPI.xapi_local")
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
XenAPI.xapi_local.assert_called_once()
|
||||
|
||||
|
|
@ -88,7 +88,7 @@ def test_xapi_connect_local_login(mocker, fake_ansible_module, XenAPI, xenserver
|
|||
"""Tests that connection to localhost uses empty username and password."""
|
||||
mocker.patch.object(XenAPI.Session, "login_with_password", create=True)
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
XenAPI.Session.login_with_password.assert_called_once_with("", "", ANSIBLE_VERSION, "Ansible")
|
||||
|
||||
|
|
@ -100,7 +100,7 @@ def test_xapi_connect_login(mocker, fake_ansible_module, XenAPI, xenserver):
|
|||
"""
|
||||
mocker.patch.object(XenAPI.Session, "login_with_password", create=True)
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
username = fake_ansible_module.params["username"]
|
||||
password = fake_ansible_module.params["password"]
|
||||
|
|
@ -119,7 +119,7 @@ def test_xapi_connect_login_failure(mocker, fake_ansible_module, XenAPI, xenserv
|
|||
username = fake_ansible_module.params["username"]
|
||||
|
||||
with pytest.raises(FailJsonException) as exc_info:
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
assert (
|
||||
exc_info.value.kwargs["msg"]
|
||||
|
|
@ -137,7 +137,7 @@ def test_xapi_connect_remote_scheme(mocker, fake_ansible_module, XenAPI, xenserv
|
|||
"""Tests that explicit scheme in hostname param is preserved."""
|
||||
mocker.patch("XenAPI.Session")
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
hostname = fake_ansible_module.params["hostname"]
|
||||
ignore_ssl = not fake_ansible_module.params["validate_certs"]
|
||||
|
|
@ -155,7 +155,7 @@ def test_xapi_connect_remote_no_scheme(mocker, fake_ansible_module, XenAPI, xens
|
|||
"""Tests that proper scheme is prepended to hostname without scheme."""
|
||||
mocker.patch("XenAPI.Session")
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
hostname = fake_ansible_module.params["hostname"]
|
||||
ignore_ssl = not fake_ansible_module.params["validate_certs"]
|
||||
|
|
@ -168,11 +168,10 @@ def test_xapi_connect_support_ignore_ssl(mocker, fake_ansible_module, XenAPI, xe
|
|||
mocked_session = mocker.patch("XenAPI.Session")
|
||||
mocked_session.side_effect = TypeError()
|
||||
|
||||
with pytest.raises(TypeError) as exc_info:
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module)
|
||||
with pytest.raises(TypeError):
|
||||
xenserver.XAPI.connect(fake_ansible_module)
|
||||
|
||||
hostname = fake_ansible_module.params["hostname"]
|
||||
ignore_ssl = not fake_ansible_module.params["validate_certs"]
|
||||
|
||||
XenAPI.Session.assert_called_with(f"http://{hostname}")
|
||||
|
||||
|
|
@ -181,7 +180,7 @@ def test_xapi_connect_no_disconnect_atexit(mocker, fake_ansible_module, XenAPI,
|
|||
"""Tests skipping registration of atexit disconnect handler."""
|
||||
mocker.patch("atexit.register")
|
||||
|
||||
xapi_session = xenserver.XAPI.connect(fake_ansible_module, disconnect_atexit=False)
|
||||
xenserver.XAPI.connect(fake_ansible_module, disconnect_atexit=False)
|
||||
|
||||
atexit.register.assert_not_called()
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ class OneViewBaseTest:
|
|||
testing_module = getattr(oneview_module, testing_module)
|
||||
try:
|
||||
# Load scenarios from module examples (Also checks if it is a valid yaml)
|
||||
EXAMPLES = yaml.safe_load(testing_module.EXAMPLES)
|
||||
yaml.safe_load(testing_module.EXAMPLES)
|
||||
|
||||
except yaml.scanner.ScannerError:
|
||||
message = f"Something went wrong while parsing yaml from {self.testing_class.__module__}.EXAMPLES"
|
||||
|
|
|
|||
|
|
@ -77,7 +77,6 @@ class TestInterfacesFileModule(unittest.TestCase):
|
|||
string = json.dumps(ifaces, sort_keys=True, indent=4, separators=(",", ": "))
|
||||
if string and not string.endswith("\n"):
|
||||
string += "\n"
|
||||
goldenstring = string
|
||||
goldenData = ifaces
|
||||
if not os.path.isfile(testfilepath):
|
||||
with open(testfilepath, "wb") as f:
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ class JenkinsBuildMock:
|
|||
instance = JenkinsMock()
|
||||
response = JenkinsMock.get_build_info(instance, "host-delete", 1234)
|
||||
return response
|
||||
except jenkins.JenkinsException as e:
|
||||
except jenkins.JenkinsException:
|
||||
response = {}
|
||||
response["result"] = "ABSENT"
|
||||
return response
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ class TestKeycloakRealmRole(ModuleTestCase):
|
|||
with set_module_args(module_args):
|
||||
with mock_good_connection():
|
||||
with patch_keycloak_api(get_realm_info_by_id=return_value) as (mock_get_realm_info_by_id):
|
||||
with self.assertRaises(AnsibleExitJson) as exec_info:
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
self.module.main()
|
||||
|
||||
self.assertEqual(len(mock_get_realm_info_by_id.mock_calls), 1)
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ def _create_wrapper(text_as_string):
|
|||
def _build_mocked_request(get_id_user_count, response_dict):
|
||||
def _mocked_requests(*args, **kwargs):
|
||||
url = args[0]
|
||||
method = kwargs["method"]
|
||||
future_response = response_dict.get(url, None)
|
||||
if callable(future_response):
|
||||
return future_response()
|
||||
|
|
|
|||
|
|
@ -342,7 +342,6 @@ class TestKeycloakUserFederation(ModuleTestCase):
|
|||
}
|
||||
],
|
||||
]
|
||||
return_value_component_delete = [None]
|
||||
return_value_component_create = [
|
||||
{
|
||||
"id": "eb691537-b73c-4cd8-b481-6031c26499d8",
|
||||
|
|
|
|||
|
|
@ -155,7 +155,7 @@ class TestPacman:
|
|||
def test_success(self, mock_empty_inventory):
|
||||
with set_module_args({"update_cache": True}): # Simplest args to let init go through
|
||||
P = pacman.Pacman(pacman.setup_module())
|
||||
with pytest.raises(AnsibleExitJson) as e:
|
||||
with pytest.raises(AnsibleExitJson):
|
||||
P.success()
|
||||
|
||||
def test_fail(self, mock_empty_inventory):
|
||||
|
|
|
|||
|
|
@ -672,7 +672,7 @@ gpg: imported: 1
|
|||
|
||||
@pytest.fixture
|
||||
def patch_get_bin_path(mocker):
|
||||
get_bin_path = mocker.patch.object(
|
||||
mocker.patch.object(
|
||||
AnsibleModule,
|
||||
"get_bin_path",
|
||||
return_value=MOCK_BIN_PATH,
|
||||
|
|
@ -689,7 +689,7 @@ def patch_get_bin_path(mocker):
|
|||
def test_operation(mocker, capfd, patch_get_bin_path, expected):
|
||||
# patch run_command invocations with mock data
|
||||
if "run_command.calls" in expected:
|
||||
mock_run_command = mocker.patch.object(
|
||||
mocker.patch.object(
|
||||
AnsibleModule,
|
||||
"run_command",
|
||||
side_effect=[item[2] for item in expected["run_command.calls"]],
|
||||
|
|
@ -697,7 +697,7 @@ def test_operation(mocker, capfd, patch_get_bin_path, expected):
|
|||
|
||||
# patch save_key invocations with mock data
|
||||
if "save_key_output" in expected:
|
||||
mock_save_key = mocker.patch.object(
|
||||
mocker.patch.object(
|
||||
pacman_key.PacmanKey,
|
||||
"save_key",
|
||||
return_value=expected["save_key_output"],
|
||||
|
|
|
|||
|
|
@ -73,8 +73,8 @@ class TestPritunlOrg(ModuleTestCase):
|
|||
)
|
||||
):
|
||||
# Test creation
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock) as mock_get:
|
||||
with self.patch_add_pritunl_organization(side_effect=PritunlPostOrganizationMock) as mock_add:
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock):
|
||||
with self.patch_add_pritunl_organization(side_effect=PritunlPostOrganizationMock):
|
||||
with self.assertRaises(AnsibleExitJson) as create_result:
|
||||
self.module.main()
|
||||
|
||||
|
|
@ -85,8 +85,8 @@ class TestPritunlOrg(ModuleTestCase):
|
|||
self.assertEqual(create_exc["response"]["user_count"], 0)
|
||||
|
||||
# Test module idempotency
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationAfterPostMock) as mock_get:
|
||||
with self.patch_add_pritunl_organization(side_effect=PritunlPostOrganizationMock) as mock_add:
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationAfterPostMock):
|
||||
with self.patch_add_pritunl_organization(side_effect=PritunlPostOrganizationMock):
|
||||
with self.assertRaises(AnsibleExitJson) as idempotent_result:
|
||||
self.module.main()
|
||||
|
||||
|
|
@ -115,8 +115,8 @@ class TestPritunlOrg(ModuleTestCase):
|
|||
)
|
||||
):
|
||||
# Test deletion
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationAfterPostMock) as mock_get:
|
||||
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock) as mock_delete:
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationAfterPostMock):
|
||||
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock):
|
||||
with self.assertRaises(AnsibleExitJson) as delete_result:
|
||||
self.module.main()
|
||||
|
||||
|
|
@ -126,8 +126,8 @@ class TestPritunlOrg(ModuleTestCase):
|
|||
self.assertEqual(delete_exc["response"], {})
|
||||
|
||||
# Test module idempotency
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock) as mock_get:
|
||||
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock) as mock_add:
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock):
|
||||
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock):
|
||||
with self.assertRaises(AnsibleExitJson) as idempotent_result:
|
||||
self.module.main()
|
||||
|
||||
|
|
@ -149,8 +149,8 @@ class TestPritunlOrg(ModuleTestCase):
|
|||
}
|
||||
with set_module_args(module_args):
|
||||
# Test deletion
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock) as mock_get:
|
||||
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock) as mock_delete:
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock):
|
||||
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock):
|
||||
with self.assertRaises(AnsibleFailJson) as failure_result:
|
||||
self.module.main()
|
||||
|
||||
|
|
@ -160,10 +160,8 @@ class TestPritunlOrg(ModuleTestCase):
|
|||
|
||||
# Switch force=True which should run successfully
|
||||
with set_module_args(dict_merge(module_args, {"force": True})):
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock) as mock_get:
|
||||
with self.patch_delete_pritunl_organization(
|
||||
side_effect=PritunlDeleteOrganizationMock
|
||||
) as mock_delete:
|
||||
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock):
|
||||
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock):
|
||||
with self.assertRaises(AnsibleExitJson) as delete_result:
|
||||
self.module.main()
|
||||
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ class TestPritunlUser(ModuleTestCase):
|
|||
user_params,
|
||||
)
|
||||
):
|
||||
with self.patch_update_pritunl_users(side_effect=PritunlPostUserMock) as post_mock:
|
||||
with self.patch_update_pritunl_users(side_effect=PritunlPostUserMock):
|
||||
with self.assertRaises(AnsibleExitJson) as create_result:
|
||||
self.module.main()
|
||||
|
||||
|
|
@ -132,7 +132,7 @@ class TestPritunlUser(ModuleTestCase):
|
|||
new_user_params,
|
||||
)
|
||||
):
|
||||
with self.patch_update_pritunl_users(side_effect=PritunlPutUserMock) as put_mock:
|
||||
with self.patch_update_pritunl_users(side_effect=PritunlPutUserMock):
|
||||
with self.assertRaises(AnsibleExitJson) as update_result:
|
||||
self.module.main()
|
||||
|
||||
|
|
|
|||
|
|
@ -44,9 +44,7 @@ def test_without_required_parameters_unregistered(mocker, capfd, patch_redhat_su
|
|||
"""
|
||||
Failure must occurs when all parameters are missing
|
||||
"""
|
||||
mock_run_command = mocker.patch.object(
|
||||
basic.AnsibleModule, "run_command", return_value=(1, "This system is not yet registered.", "")
|
||||
)
|
||||
mocker.patch.object(basic.AnsibleModule, "run_command", return_value=(1, "This system is not yet registered.", ""))
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
redhat_subscription.main()
|
||||
|
|
@ -63,7 +61,7 @@ def test_without_required_parameters_registered(mocker, capfd, patch_redhat_subs
|
|||
System already registered, no parameters required (state=present is the
|
||||
default)
|
||||
"""
|
||||
mock_run_command = mocker.patch.object(
|
||||
mocker.patch.object(
|
||||
basic.AnsibleModule,
|
||||
"run_command",
|
||||
return_value=(0, "system identity: b26df632-25ed-4452-8f89-0308bfd167cb", ""),
|
||||
|
|
@ -888,7 +886,7 @@ def test_redhat_subscription(mocker, capfd, patch_redhat_subscription, testcase)
|
|||
|
||||
# Mock function used for running commands first
|
||||
call_results = [item[2] for item in testcase["run_command.calls"]]
|
||||
mock_run_command = mocker.patch.object(basic.AnsibleModule, "run_command", side_effect=call_results)
|
||||
mocker.patch.object(basic.AnsibleModule, "run_command", side_effect=call_results)
|
||||
|
||||
# Try to run test case
|
||||
with pytest.raises(SystemExit):
|
||||
|
|
@ -1266,7 +1264,7 @@ def test_redhat_subscription_syspurpose(
|
|||
|
||||
# Mock function used for running commands first
|
||||
call_results = [item[2] for item in testcase["run_command.calls"]]
|
||||
mock_run_command = mocker.patch.object(basic.AnsibleModule, "run_command", side_effect=call_results)
|
||||
mocker.patch.object(basic.AnsibleModule, "run_command", side_effect=call_results)
|
||||
|
||||
mock_syspurpose_file = tmpdir.mkdir("syspurpose").join("syspurpose.json")
|
||||
# When there there are some existing syspurpose attributes specified, then
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ if tuple(map(int, __version__.split("."))) < (3, 4, 0):
|
|||
|
||||
def test_redis_data_without_arguments(capfd):
|
||||
with set_module_args({}):
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
assert not err
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ if HAS_REDIS_USERNAME_OPTION:
|
|||
|
||||
def test_redis_data_incr_without_arguments(capfd):
|
||||
with set_module_args({}):
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data_incr.main()
|
||||
out, err = capfd.readouterr()
|
||||
assert not err
|
||||
|
|
|
|||
|
|
@ -773,7 +773,7 @@ def test_rhsm_repository(mocker, capfd, patch_rhsm_repository, testcase):
|
|||
|
||||
# Mock function used for running commands first
|
||||
call_results = [item[2] for item in testcase["run_command.calls"]]
|
||||
mock_run_command = mocker.patch.object(basic.AnsibleModule, "run_command", side_effect=call_results)
|
||||
mocker.patch.object(basic.AnsibleModule, "run_command", side_effect=call_results)
|
||||
|
||||
# Try to run test case
|
||||
with pytest.raises(SystemExit):
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ def response_remove_nics():
|
|||
|
||||
def test_scaleway_private_network_without_arguments(capfd):
|
||||
with set_module_args({}):
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_compute_private_network.main()
|
||||
out, err = capfd.readouterr()
|
||||
|
||||
|
|
@ -91,7 +91,7 @@ def test_scaleway_add_nic(capfd):
|
|||
mock_scw_get.return_value = response_without_nics()
|
||||
with patch.object(Scaleway, "post") as mock_scw_post:
|
||||
mock_scw_post.return_value = response_when_add_nics()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_compute_private_network.main()
|
||||
mock_scw_post.assert_any_call(path=url, data={"private_network_id": pnid})
|
||||
mock_scw_get.assert_any_call(url)
|
||||
|
|
@ -119,7 +119,7 @@ def test_scaleway_add_existing_nic(capfd):
|
|||
):
|
||||
with patch.object(Scaleway, "get") as mock_scw_get:
|
||||
mock_scw_get.return_value = response_with_nics()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_compute_private_network.main()
|
||||
mock_scw_get.assert_any_call(url)
|
||||
|
||||
|
|
@ -150,7 +150,7 @@ def test_scaleway_remove_existing_nic(capfd):
|
|||
mock_scw_get.return_value = response_with_nics()
|
||||
with patch.object(Scaleway, "delete") as mock_scw_delete:
|
||||
mock_scw_delete.return_value = response_remove_nics()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_compute_private_network.main()
|
||||
mock_scw_delete.assert_any_call(urlremove)
|
||||
mock_scw_get.assert_any_call(url)
|
||||
|
|
@ -179,7 +179,7 @@ def test_scaleway_remove_absent_nic(capfd):
|
|||
):
|
||||
with patch.object(Scaleway, "get") as mock_scw_get:
|
||||
mock_scw_get.return_value = response_without_nics()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_compute_private_network.main()
|
||||
mock_scw_get.assert_any_call(url)
|
||||
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ def response_delete():
|
|||
|
||||
def test_scaleway_private_network_without_arguments(capfd):
|
||||
with set_module_args({}):
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_private_network.main()
|
||||
out, err = capfd.readouterr()
|
||||
|
||||
|
|
@ -97,7 +97,7 @@ def test_scaleway_create_pn(capfd):
|
|||
mock_scw_get.return_value = response_with_zero_network()
|
||||
with patch.object(Scaleway, "post") as mock_scw_post:
|
||||
mock_scw_post.return_value = response_create_new()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_private_network.main()
|
||||
mock_scw_post.assert_any_call(
|
||||
path="private-networks/",
|
||||
|
|
@ -124,7 +124,7 @@ def test_scaleway_existing_pn(capfd):
|
|||
os.environ["SCW_API_TOKEN"] = "notrealtoken"
|
||||
with patch.object(Scaleway, "get") as mock_scw_get:
|
||||
mock_scw_get.return_value = response_with_new_network()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_private_network.main()
|
||||
mock_scw_get.assert_any_call(
|
||||
"private-networks", params={"name": "new_network_name", "order_by": "name_asc", "page": 1, "page_size": 10}
|
||||
|
|
@ -152,7 +152,7 @@ def test_scaleway_add_tag_pn(capfd):
|
|||
mock_scw_get.return_value = response_with_new_network()
|
||||
with patch.object(Scaleway, "patch") as mock_scw_patch:
|
||||
mock_scw_patch.return_value = response_create_new_newtag()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_private_network.main()
|
||||
mock_scw_patch.assert_any_call(
|
||||
path="private-networks/c123b4cd-ef5g-678h-90i1-jk2345678l90",
|
||||
|
|
@ -184,7 +184,7 @@ def test_scaleway_remove_pn(capfd):
|
|||
mock_scw_get.return_value = response_with_new_network()
|
||||
with patch.object(Scaleway, "delete") as mock_scw_delete:
|
||||
mock_scw_delete.return_value = response_delete()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_private_network.main()
|
||||
mock_scw_delete.assert_any_call("private-networks/c123b4cd-ef5g-678h-90i1-jk2345678l90")
|
||||
mock_scw_get.assert_any_call(
|
||||
|
|
@ -211,7 +211,7 @@ def test_scaleway_absent_pn_not_exists(capfd):
|
|||
os.environ["SCW_API_TOKEN"] = "notrealtoken"
|
||||
with patch.object(Scaleway, "get") as mock_scw_get:
|
||||
mock_scw_get.return_value = response_with_zero_network()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
scaleway_private_network.main()
|
||||
mock_scw_get.assert_any_call(
|
||||
"private-networks", params={"name": "new_network_name", "order_by": "name_asc", "page": 1, "page_size": 10}
|
||||
|
|
|
|||
|
|
@ -47,21 +47,21 @@ class TestStatsDModule(ModuleTestCase):
|
|||
|
||||
def test_udp_without_parameters(self):
|
||||
"""Test udp without parameters"""
|
||||
with self.patch_udp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.patch_udp_statsd_client(side_effect=FakeStatsD):
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
with set_module_args({}):
|
||||
self.module.main()
|
||||
|
||||
def test_tcp_without_parameters(self):
|
||||
"""Test tcp without parameters"""
|
||||
with self.patch_tcp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.patch_tcp_statsd_client(side_effect=FakeStatsD):
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
with set_module_args({}):
|
||||
self.module.main()
|
||||
|
||||
def test_udp_with_parameters(self):
|
||||
"""Test udp with parameters"""
|
||||
with self.patch_udp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
|
||||
with self.patch_udp_statsd_client(side_effect=FakeStatsD):
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with set_module_args(
|
||||
{
|
||||
|
|
@ -73,7 +73,7 @@ class TestStatsDModule(ModuleTestCase):
|
|||
self.module.main()
|
||||
self.assertEqual(result.exception.args[0]["msg"], "Sent counter my_counter -> 1 to StatsD")
|
||||
self.assertEqual(result.exception.args[0]["changed"], True)
|
||||
with self.patch_udp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
|
||||
with self.patch_udp_statsd_client(side_effect=FakeStatsD):
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with set_module_args(
|
||||
{
|
||||
|
|
@ -88,7 +88,7 @@ class TestStatsDModule(ModuleTestCase):
|
|||
|
||||
def test_tcp_with_parameters(self):
|
||||
"""Test tcp with parameters"""
|
||||
with self.patch_tcp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
|
||||
with self.patch_tcp_statsd_client(side_effect=FakeStatsD):
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with set_module_args(
|
||||
{
|
||||
|
|
@ -101,7 +101,7 @@ class TestStatsDModule(ModuleTestCase):
|
|||
self.module.main()
|
||||
self.assertEqual(result.exception.args[0]["msg"], "Sent counter my_counter -> 1 to StatsD")
|
||||
self.assertEqual(result.exception.args[0]["changed"], True)
|
||||
with self.patch_tcp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
|
||||
with self.patch_tcp_statsd_client(side_effect=FakeStatsD):
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with set_module_args(
|
||||
{
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ from ansible_collections.community.internal_test_tools.tests.unit.plugins.module
|
|||
|
||||
def test_terraform_without_argument(capfd):
|
||||
with set_module_args({}):
|
||||
with pytest.raises(SystemExit) as results:
|
||||
with pytest.raises(SystemExit):
|
||||
terraform.main()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "virtual_media_insert") as mock_virtual_media_insert:
|
||||
mock_virtual_media_insert.return_value = {"ret": True, "changed": True, "msg": "success"}
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_VirtualMediaEject_pass(self):
|
||||
|
|
@ -115,7 +115,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "virtual_media_eject") as mock_virtual_media_eject:
|
||||
mock_virtual_media_eject.return_value = {"ret": True, "changed": True, "msg": "success"}
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_VirtualMediaEject_fail_when_required_args_missing(self):
|
||||
|
|
@ -144,7 +144,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request:
|
||||
mock_get_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_GetResource_fail_when_get_return_false(self):
|
||||
|
|
@ -161,7 +161,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request:
|
||||
mock_get_request.return_value = {"ret": False, "msg": "404 error"}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_GetResource_pass(self):
|
||||
|
|
@ -178,7 +178,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request:
|
||||
mock_get_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}}
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_GetCollectionResource_fail_when_required_args_missing(self):
|
||||
|
|
@ -194,7 +194,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request:
|
||||
mock_get_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_GetCollectionResource_fail_when_get_return_false(self):
|
||||
|
|
@ -211,7 +211,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request:
|
||||
mock_get_request.return_value = {"ret": False, "msg": "404 error"}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_GetCollectionResource_fail_when_get_not_colection(self):
|
||||
|
|
@ -228,7 +228,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request:
|
||||
mock_get_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_GetCollectionResource_pass_when_get_empty_collection(self):
|
||||
|
|
@ -245,7 +245,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request:
|
||||
mock_get_request.return_value = {"ret": True, "data": {"Members": [], "Members@odata.count": 0}}
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_GetCollectionResource_pass_when_get_collection(self):
|
||||
|
|
@ -265,7 +265,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
"data": {"Members": [{"@odata.id": "/redfish/v1/testuri/1"}], "Members@odata.count": 1},
|
||||
}
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PatchResource_fail_when_required_args_missing(self):
|
||||
|
|
@ -287,7 +287,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "patch_request") as mock_patch_request:
|
||||
mock_patch_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PatchResource_fail_when_required_args_missing_no_requestbody(self):
|
||||
|
|
@ -310,7 +310,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "patch_request") as mock_patch_request:
|
||||
mock_patch_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PatchResource_fail_when_noexisting_property_in_requestbody(self):
|
||||
|
|
@ -334,7 +334,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "patch_request") as mock_patch_request:
|
||||
mock_patch_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PatchResource_fail_when_get_return_false(self):
|
||||
|
|
@ -358,7 +358,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "patch_request") as mock_patch_request:
|
||||
mock_patch_request.return_value = {"ret": False, "msg": "500 internal error"}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PatchResource_pass(self):
|
||||
|
|
@ -385,7 +385,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
"data": {"teststr": "yyyy", "@odata.etag": "322e0d45d9572723c98"},
|
||||
}
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PostResource_fail_when_required_args_missing(self):
|
||||
|
|
@ -420,7 +420,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request:
|
||||
mock_post_request.return_value = {"ret": True}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PostResource_fail_when_invalid_resourceuri(self):
|
||||
|
|
@ -456,7 +456,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request:
|
||||
mock_post_request.return_value = {"ret": True}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PostResource_fail_when_no_requestbody(self):
|
||||
|
|
@ -492,7 +492,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request:
|
||||
mock_post_request.return_value = {"ret": True}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PostResource_fail_when_no_requestbody_2(self):
|
||||
|
|
@ -528,7 +528,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request:
|
||||
mock_post_request.return_value = {"ret": True}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PostResource_fail_when_requestbody_mismatch_with_data_from_actioninfo_uri(self):
|
||||
|
|
@ -566,7 +566,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request:
|
||||
mock_post_request.return_value = {"ret": True}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PostResource_fail_when_get_return_false(self):
|
||||
|
|
@ -587,7 +587,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request:
|
||||
mock_post_request.return_value = {"ret": True}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PostResource_fail_when_post_return_false(self):
|
||||
|
|
@ -624,7 +624,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request:
|
||||
mock_post_request.return_value = {"ret": False, "msg": "500 internal error"}
|
||||
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
module.main()
|
||||
|
||||
def test_module_command_PostResource_pass(self):
|
||||
|
|
@ -661,5 +661,5 @@ class TestXCCRedfishCommand(unittest.TestCase):
|
|||
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request:
|
||||
mock_post_request.return_value = {"ret": True, "msg": "post success"}
|
||||
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
module.main()
|
||||
|
|
|
|||
|
|
@ -320,7 +320,7 @@ def test_xenserver_guest_powerstate_wait(mocker, patch_ansible_module, capfd, Xe
|
|||
"ansible_collections.community.general.plugins.modules.xenserver_guest_powerstate.gather_vm_facts",
|
||||
return_value=fake_vm_facts,
|
||||
)
|
||||
mocked_set_vm_power_state = mocker.patch(
|
||||
mocker.patch(
|
||||
"ansible_collections.community.general.plugins.modules.xenserver_guest_powerstate.set_vm_power_state",
|
||||
return_value=(True, "somenewstate"),
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue