diff options
author | Rick Elrod <rick@elrod.me> | 2020-01-26 03:16:16 -0600 |
---|---|---|
committer | Matt Clay <matt@mystile.com> | 2020-01-29 13:29:40 -0800 |
commit | e2a57414f4008fe3092c231b8f39b1adefc1c16f (patch) | |
tree | 474e07870facffe55b2a96346e5357d85460d860 /test | |
parent | 1156962cded3cfed8b40a23b841415811fb24cba (diff) | |
download | ansible-e2a57414f4008fe3092c231b8f39b1adefc1c16f.tar.gz |
Remove `with` statement for pytest-mock unit tests
As per:
https://github.com/pytest-dev/pytest-mock#note-about-usage-as-context-manager
pytest-mock is not meant to be used within a `with` context or as a
decorator. Instead, pytest-mock will automatically unpatch the mocked
methods when each test is complete.
In newer pytest-mock, this use actually throws an exception and causes
the tests to fail.
This hasn't been hit in Ansible's CI yet, because the docker image
that the tests run in uses an older version of pytest-mock. However,
there is no constraint on the upper bound of pytest-mock in
test/lib/ansible_test/_data/requirements/constraints.txt which means
that when running the tests locally, outside of that docker image, the
tests never pass.
This patch removes the `with` context in each such case.
Signed-off-by: Rick Elrod <rick@elrod.me>
Diffstat (limited to 'test')
-rw-r--r-- | test/units/module_utils/facts/hardware/test_sunos_get_uptime_facts.py | 8 | ||||
-rw-r--r-- | test/units/plugins/lookup/test_aws_secret.py | 8 | ||||
-rw-r--r-- | test/units/plugins/lookup/test_aws_ssm.py | 24 |
3 files changed, 20 insertions, 20 deletions
diff --git a/test/units/module_utils/facts/hardware/test_sunos_get_uptime_facts.py b/test/units/module_utils/facts/hardware/test_sunos_get_uptime_facts.py index 1c852babaa..43ae726761 100644 --- a/test/units/module_utils/facts/hardware/test_sunos_get_uptime_facts.py +++ b/test/units/module_utils/facts/hardware/test_sunos_get_uptime_facts.py @@ -11,7 +11,7 @@ def test_sunos_get_uptime_facts(mocker): inst = sunos.SunOSHardware(module) - with mocker.patch('time.time', return_value=1567052602.5089788): - expected = int(time.time()) - 1548249689 - result = inst.get_uptime_facts() - assert expected == result['uptime_seconds'] + mocker.patch('time.time', return_value=1567052602.5089788) + expected = int(time.time()) - 1548249689 + result = inst.get_uptime_facts() + assert expected == result['uptime_seconds'] diff --git a/test/units/plugins/lookup/test_aws_secret.py b/test/units/plugins/lookup/test_aws_secret.py index 5e1949ed4b..ae7734501c 100644 --- a/test/units/plugins/lookup/test_aws_secret.py +++ b/test/units/plugins/lookup/test_aws_secret.py @@ -70,8 +70,8 @@ def test_lookup_variable(mocker, dummy_credentials): boto3_double.Session.return_value.client.return_value.get_secret_value.return_value = simple_variable_success_response boto3_client_double = boto3_double.Session.return_value.client - with mocker.patch.object(boto3, 'session', boto3_double): - retval = lookup.run(["simple_variable"], None, **dummy_credentials) + mocker.patch.object(boto3, 'session', boto3_double) + retval = lookup.run(["simple_variable"], None, **dummy_credentials) assert(retval[0] == '{"secret":"simplesecret"}') boto3_client_double.assert_called_with('secretsmanager', 'eu-west-1', aws_access_key_id='notakey', aws_secret_access_key="notasecret", aws_session_token=None) @@ -86,5 +86,5 @@ def test_warn_denied_variable(mocker, dummy_credentials): boto3_double.Session.return_value.client.return_value.get_secret_value.side_effect = ClientError(error_response, operation_name) with pytest.raises(AnsibleError): - with mocker.patch.object(boto3, 'session', boto3_double): - lookup_loader.get('aws_secret').run(["denied_variable"], None, **dummy_credentials) + mocker.patch.object(boto3, 'session', boto3_double) + lookup_loader.get('aws_secret').run(["denied_variable"], None, **dummy_credentials) diff --git a/test/units/plugins/lookup/test_aws_ssm.py b/test/units/plugins/lookup/test_aws_ssm.py index 570a63f0ea..811ccfb489 100644 --- a/test/units/plugins/lookup/test_aws_ssm.py +++ b/test/units/plugins/lookup/test_aws_ssm.py @@ -90,8 +90,8 @@ def test_lookup_variable(mocker): boto3_double.Session.return_value.client.return_value.get_parameters.return_value = simple_variable_success_response boto3_client_double = boto3_double.Session.return_value.client - with mocker.patch.object(boto3, 'session', boto3_double): - retval = lookup.run(["simple_variable"], {}, **dummy_credentials) + mocker.patch.object(boto3, 'session', boto3_double) + retval = lookup.run(["simple_variable"], {}, **dummy_credentials) assert(retval[0] == "simplevalue") boto3_client_double.assert_called_with('ssm', 'eu-west-1', aws_access_key_id='notakey', aws_secret_access_key="notasecret", aws_session_token=None) @@ -106,10 +106,10 @@ def test_path_lookup_variable(mocker): get_path_fn.return_value = path_success_response boto3_client_double = boto3_double.Session.return_value.client - with mocker.patch.object(boto3, 'session', boto3_double): - args = copy(dummy_credentials) - args["bypath"] = 'true' - retval = lookup.run(["/testpath"], {}, **args) + mocker.patch.object(boto3, 'session', boto3_double) + args = copy(dummy_credentials) + args["bypath"] = 'true' + retval = lookup.run(["/testpath"], {}, **args) assert(retval[0]["/testpath/won"] == "simple_value_won") assert(retval[0]["/testpath/too"] == "simple_value_too") boto3_client_double.assert_called_with('ssm', 'eu-west-1', aws_access_key_id='notakey', @@ -129,8 +129,8 @@ def test_return_none_for_missing_variable(mocker): boto3_double = mocker.MagicMock() boto3_double.Session.return_value.client.return_value.get_parameters.return_value = missing_variable_response - with mocker.patch.object(boto3, 'session', boto3_double): - retval = lookup.run(["missing_variable"], {}, **dummy_credentials) + mocker.patch.object(boto3, 'session', boto3_double) + retval = lookup.run(["missing_variable"], {}, **dummy_credentials) assert(retval[0] is None) @@ -145,8 +145,8 @@ def test_match_retvals_to_call_params_even_with_some_missing_variables(mocker): boto3_double = mocker.MagicMock() boto3_double.Session.return_value.client.return_value.get_parameters.return_value = some_missing_variable_response - with mocker.patch.object(boto3, 'session', boto3_double): - retval = lookup.run(["simple", "missing_variable", "/testpath/won", "simple"], {}, **dummy_credentials) + mocker.patch.object(boto3, 'session', boto3_double) + retval = lookup.run(["simple", "missing_variable", "/testpath/won", "simple"], {}, **dummy_credentials) assert(retval == ["simple_value", None, "simple_value_won", "simple_value"]) @@ -162,5 +162,5 @@ def test_warn_denied_variable(mocker): boto3_double.Session.return_value.client.return_value.get_parameters.side_effect = ClientError(error_response, operation_name) with pytest.raises(AnsibleError): - with mocker.patch.object(boto3, 'session', boto3_double): - lookup.run(["denied_variable"], {}, **dummy_credentials) + mocker.patch.object(boto3, 'session', boto3_double) + lookup.run(["denied_variable"], {}, **dummy_credentials) |