mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 11:51:26 -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
|
@ -30,8 +30,8 @@ from .linode_conftest import access_token, no_access_token_in_env, default_args,
|
|||
|
||||
def test_mandatory_state_is_validated(capfd):
|
||||
with pytest.raises(SystemExit):
|
||||
set_module_args({'label': 'foo'})
|
||||
linode_v4.initialise_module()
|
||||
with set_module_args({'label': 'foo'}):
|
||||
linode_v4.initialise_module()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
@ -42,8 +42,8 @@ def test_mandatory_state_is_validated(capfd):
|
|||
|
||||
def test_mandatory_label_is_validated(capfd):
|
||||
with pytest.raises(SystemExit):
|
||||
set_module_args({'state': 'present'})
|
||||
linode_v4.initialise_module()
|
||||
with set_module_args({'state': 'present'}):
|
||||
linode_v4.initialise_module()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
@ -56,8 +56,8 @@ def test_mandatory_access_token_is_validated(default_args,
|
|||
no_access_token_in_env,
|
||||
capfd):
|
||||
with pytest.raises(SystemExit):
|
||||
set_module_args(default_args)
|
||||
linode_v4.initialise_module()
|
||||
with set_module_args(default_args):
|
||||
linode_v4.initialise_module()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
@ -72,12 +72,12 @@ def test_mandatory_access_token_is_validated(default_args,
|
|||
|
||||
def test_mandatory_access_token_passed_in_env(default_args,
|
||||
access_token):
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
try:
|
||||
module = linode_v4.initialise_module()
|
||||
except SystemExit:
|
||||
pytest.fail("'access_token' is passed in environment")
|
||||
try:
|
||||
module = linode_v4.initialise_module()
|
||||
except SystemExit:
|
||||
pytest.fail("'access_token' is passed in environment")
|
||||
|
||||
now_set_token = module.params['access_token']
|
||||
assert now_set_token == os.environ['LINODE_ACCESS_TOKEN']
|
||||
|
@ -86,26 +86,26 @@ def test_mandatory_access_token_passed_in_env(default_args,
|
|||
def test_mandatory_access_token_passed_in_as_parameter(default_args,
|
||||
no_access_token_in_env):
|
||||
default_args.update({'access_token': 'foo'})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
try:
|
||||
module = linode_v4.initialise_module()
|
||||
except SystemExit:
|
||||
pytest.fail("'access_token' is passed in as parameter")
|
||||
try:
|
||||
module = linode_v4.initialise_module()
|
||||
except SystemExit:
|
||||
pytest.fail("'access_token' is passed in as parameter")
|
||||
|
||||
assert module.params['access_token'] == 'foo'
|
||||
|
||||
|
||||
def test_instance_by_label_cannot_authenticate(capfd, access_token,
|
||||
default_args):
|
||||
set_module_args(default_args)
|
||||
module = linode_v4.initialise_module()
|
||||
client = LinodeClient(module.params['access_token'])
|
||||
with set_module_args(default_args):
|
||||
module = linode_v4.initialise_module()
|
||||
client = LinodeClient(module.params['access_token'])
|
||||
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, side_effect=LinodeApiError('foo')):
|
||||
with pytest.raises(SystemExit):
|
||||
linode_v4.maybe_instance_from_label(module, client)
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, side_effect=LinodeApiError('foo')):
|
||||
with pytest.raises(SystemExit):
|
||||
linode_v4.maybe_instance_from_label(module, client)
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
@ -116,23 +116,23 @@ def test_instance_by_label_cannot_authenticate(capfd, access_token,
|
|||
|
||||
def test_no_instances_found_with_label_gives_none(default_args,
|
||||
access_token):
|
||||
set_module_args(default_args)
|
||||
module = linode_v4.initialise_module()
|
||||
client = LinodeClient(module.params['access_token'])
|
||||
with set_module_args(default_args):
|
||||
module = linode_v4.initialise_module()
|
||||
client = LinodeClient(module.params['access_token'])
|
||||
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[]):
|
||||
result = linode_v4.maybe_instance_from_label(module, client)
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[]):
|
||||
result = linode_v4.maybe_instance_from_label(module, client)
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
def test_optional_region_is_validated(default_args, capfd, access_token):
|
||||
default_args.update({'type': 'foo', 'image': 'bar'})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
linode_v4.initialise_module()
|
||||
with pytest.raises(SystemExit):
|
||||
linode_v4.initialise_module()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
@ -147,10 +147,10 @@ def test_optional_region_is_validated(default_args, capfd, access_token):
|
|||
|
||||
def test_optional_type_is_validated(default_args, capfd, access_token):
|
||||
default_args.update({'region': 'foo', 'image': 'bar'})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
linode_v4.initialise_module()
|
||||
with pytest.raises(SystemExit):
|
||||
linode_v4.initialise_module()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
@ -165,10 +165,10 @@ def test_optional_type_is_validated(default_args, capfd, access_token):
|
|||
|
||||
def test_optional_image_is_validated(default_args, capfd, access_token):
|
||||
default_args.update({'type': 'foo', 'region': 'bar'})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
linode_v4.initialise_module()
|
||||
with pytest.raises(SystemExit):
|
||||
linode_v4.initialise_module()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
@ -184,9 +184,9 @@ def test_optional_image_is_validated(default_args, capfd, access_token):
|
|||
@pytest.mark.parametrize('value', [True, False])
|
||||
def test_private_ip_valid_values(default_args, access_token, value):
|
||||
default_args.update({'private_ip': value})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
module = linode_v4.initialise_module()
|
||||
module = linode_v4.initialise_module()
|
||||
|
||||
assert module.params['private_ip'] is value
|
||||
|
||||
|
@ -194,10 +194,10 @@ def test_private_ip_valid_values(default_args, access_token, value):
|
|||
@pytest.mark.parametrize('value', ['not-a-bool', 42])
|
||||
def test_private_ip_invalid_values(default_args, capfd, access_token, value):
|
||||
default_args.update({'private_ip': value})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
linode_v4.initialise_module()
|
||||
with pytest.raises(SystemExit):
|
||||
linode_v4.initialise_module()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
@ -208,23 +208,23 @@ def test_private_ip_invalid_values(default_args, capfd, access_token, value):
|
|||
|
||||
def test_private_ip_default_value(default_args, access_token):
|
||||
default_args.pop('private_ip', None)
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
module = linode_v4.initialise_module()
|
||||
module = linode_v4.initialise_module()
|
||||
|
||||
assert module.params['private_ip'] is False
|
||||
|
||||
|
||||
def test_private_ip_is_forwarded_to_linode(default_args, mock_linode, access_token):
|
||||
default_args.update({'private_ip': True})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[]):
|
||||
with pytest.raises(SystemExit):
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instance_create'
|
||||
with mock.patch(target, return_value=(mock_linode, 'passw0rd')) as instance_create_mock:
|
||||
linode_v4.main()
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[]):
|
||||
with pytest.raises(SystemExit):
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instance_create'
|
||||
with mock.patch(target, return_value=(mock_linode, 'passw0rd')) as instance_create_mock:
|
||||
linode_v4.main()
|
||||
|
||||
args, kwargs = instance_create_mock.call_args
|
||||
assert kwargs['private_ip'] is True
|
||||
|
@ -239,12 +239,12 @@ def test_instance_already_created(default_args,
|
|||
'region': 'bar',
|
||||
'image': 'baz'
|
||||
})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[mock_linode]):
|
||||
with pytest.raises(SystemExit) as sys_exit_exc:
|
||||
linode_v4.main()
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[mock_linode]):
|
||||
with pytest.raises(SystemExit) as sys_exit_exc:
|
||||
linode_v4.main()
|
||||
|
||||
assert sys_exit_exc.value.code == 0
|
||||
|
||||
|
@ -268,14 +268,14 @@ def test_instance_to_be_created_without_root_pass(default_args,
|
|||
'region': 'bar',
|
||||
'image': 'baz'
|
||||
})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[]):
|
||||
with pytest.raises(SystemExit) as sys_exit_exc:
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instance_create'
|
||||
with mock.patch(target, return_value=(mock_linode, 'passw0rd')):
|
||||
linode_v4.main()
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[]):
|
||||
with pytest.raises(SystemExit) as sys_exit_exc:
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instance_create'
|
||||
with mock.patch(target, return_value=(mock_linode, 'passw0rd')):
|
||||
linode_v4.main()
|
||||
|
||||
assert sys_exit_exc.value.code == 0
|
||||
|
||||
|
@ -300,14 +300,14 @@ def test_instance_to_be_created_with_root_pass(default_args,
|
|||
'image': 'baz',
|
||||
'root_pass': 'passw0rd',
|
||||
})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[]):
|
||||
with pytest.raises(SystemExit) as sys_exit_exc:
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instance_create'
|
||||
with mock.patch(target, return_value=mock_linode):
|
||||
linode_v4.main()
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[]):
|
||||
with pytest.raises(SystemExit) as sys_exit_exc:
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instance_create'
|
||||
with mock.patch(target, return_value=mock_linode):
|
||||
linode_v4.main()
|
||||
|
||||
assert sys_exit_exc.value.code == 0
|
||||
|
||||
|
@ -327,12 +327,12 @@ def test_instance_to_be_deleted(default_args,
|
|||
capfd,
|
||||
access_token):
|
||||
default_args.update({'state': 'absent'})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[mock_linode]):
|
||||
with pytest.raises(SystemExit) as sys_exit_exc:
|
||||
linode_v4.main()
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[mock_linode]):
|
||||
with pytest.raises(SystemExit) as sys_exit_exc:
|
||||
linode_v4.main()
|
||||
|
||||
assert sys_exit_exc.value.code == 0
|
||||
|
||||
|
@ -351,12 +351,12 @@ def test_instance_already_deleted_no_change(default_args,
|
|||
capfd,
|
||||
access_token):
|
||||
default_args.update({'state': 'absent'})
|
||||
set_module_args(default_args)
|
||||
with set_module_args(default_args):
|
||||
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[]):
|
||||
with pytest.raises(SystemExit) as sys_exit_exc:
|
||||
linode_v4.main()
|
||||
target = 'linode_api4.linode_client.LinodeGroup.instances'
|
||||
with mock.patch(target, return_value=[]):
|
||||
with pytest.raises(SystemExit) as sys_exit_exc:
|
||||
linode_v4.main()
|
||||
|
||||
assert sys_exit_exc.value.code == 0
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue