[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:
Felix Fontein 2025-03-07 07:31:42 +01:00 committed by GitHub
parent 9a6bd80613
commit 013fb9c006
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
80 changed files with 3745 additions and 3977 deletions

View file

@ -71,9 +71,9 @@ def response_delete():
def test_scaleway_private_network_without_arguments(capfd):
set_module_args({})
with pytest.raises(SystemExit) as results:
scaleway_private_network.main()
with set_module_args({}):
with pytest.raises(SystemExit) as results:
scaleway_private_network.main()
out, err = capfd.readouterr()
assert not err
@ -81,20 +81,21 @@ def test_scaleway_private_network_without_arguments(capfd):
def test_scaleway_create_pn(capfd):
set_module_args({"state": "present",
"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
"region": "par2",
"name": "new_network_name",
"tags": ["tag1"]
})
with set_module_args({
"state": "present",
"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
"region": "par2",
"name": "new_network_name",
"tags": ["tag1"]
}):
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 patch.object(Scaleway, 'post') as mock_scw_post:
mock_scw_post.return_value = response_create_new()
with pytest.raises(SystemExit) as results:
scaleway_private_network.main()
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 patch.object(Scaleway, 'post') as mock_scw_post:
mock_scw_post.return_value = response_create_new()
with pytest.raises(SystemExit) as results:
scaleway_private_network.main()
mock_scw_post.assert_any_call(path='private-networks/', data={'name': 'new_network_name',
'project_id': 'a123b4cd-ef5g-678h-90i1-jk2345678l90',
'tags': ['tag1']})
@ -105,18 +106,19 @@ def test_scaleway_create_pn(capfd):
def test_scaleway_existing_pn(capfd):
set_module_args({"state": "present",
"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
"region": "par2",
"name": "new_network_name",
"tags": ["tag1"]
})
with set_module_args({
"state": "present",
"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
"region": "par2",
"name": "new_network_name",
"tags": ["tag1"]
}):
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:
scaleway_private_network.main()
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:
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})
out, err = capfd.readouterr()
@ -127,20 +129,21 @@ def test_scaleway_existing_pn(capfd):
def test_scaleway_add_tag_pn(capfd):
set_module_args({"state": "present",
"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
"region": "par2",
"name": "new_network_name",
"tags": ["newtag"]
})
with set_module_args({
"state": "present",
"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
"region": "par2",
"name": "new_network_name",
"tags": ["newtag"]
}):
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 patch.object(Scaleway, 'patch') as mock_scw_patch:
mock_scw_patch.return_value = response_create_new_newtag()
with pytest.raises(SystemExit) as results:
scaleway_private_network.main()
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 patch.object(Scaleway, 'patch') as mock_scw_patch:
mock_scw_patch.return_value = response_create_new_newtag()
with pytest.raises(SystemExit) as results:
scaleway_private_network.main()
mock_scw_patch.assert_any_call(path='private-networks/c123b4cd-ef5g-678h-90i1-jk2345678l90', data={'name': 'new_network_name', 'tags': ['newtag']})
mock_scw_get.assert_any_call('private-networks', params={'name': 'new_network_name', 'order_by': 'name_asc', 'page': 1, 'page_size': 10})
@ -152,20 +155,21 @@ def test_scaleway_add_tag_pn(capfd):
def test_scaleway_remove_pn(capfd):
set_module_args({"state": "absent",
"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
"region": "par2",
"name": "new_network_name",
"tags": ["newtag"]
})
with set_module_args({
"state": "absent",
"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
"region": "par2",
"name": "new_network_name",
"tags": ["newtag"]
}):
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 patch.object(Scaleway, 'delete') as mock_scw_delete:
mock_scw_delete.return_value = response_delete()
with pytest.raises(SystemExit) as results:
scaleway_private_network.main()
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 patch.object(Scaleway, 'delete') as mock_scw_delete:
mock_scw_delete.return_value = response_delete()
with pytest.raises(SystemExit) as results:
scaleway_private_network.main()
mock_scw_delete.assert_any_call('private-networks/c123b4cd-ef5g-678h-90i1-jk2345678l90')
mock_scw_get.assert_any_call('private-networks', params={'name': 'new_network_name', 'order_by': 'name_asc', 'page': 1, 'page_size': 10})
@ -177,18 +181,19 @@ def test_scaleway_remove_pn(capfd):
def test_scaleway_absent_pn_not_exists(capfd):
set_module_args({"state": "absent",
"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
"region": "par2",
"name": "new_network_name",
"tags": ["newtag"]
})
with set_module_args({
"state": "absent",
"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
"region": "par2",
"name": "new_network_name",
"tags": ["newtag"]
}):
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:
scaleway_private_network.main()
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:
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})
out, err = capfd.readouterr()