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.
This commit is contained in:
Felix Fontein 2025-03-07 07:21:03 +01:00 committed by GitHub
parent 402f725424
commit a1781d09dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
84 changed files with 4043 additions and 4302 deletions

View file

@ -82,169 +82,160 @@ class TestAlertaCustomerModule(ModuleTestCase):
def test_without_parameters(self):
"""Failure if no parameters set"""
with self.assertRaises(AnsibleFailJson):
set_module_args({})
self.module.main()
with set_module_args({}):
self.module.main()
def test_without_content(self):
"""Failure if customer and match are missing"""
set_module_args({
with set_module_args({
'alerta_url': "http://localhost:8080",
'api_username': "admin@example.com",
'api_password': "password"
})
with self.assertRaises(AnsibleFailJson):
self.module.main()
}):
with self.assertRaises(AnsibleFailJson):
self.module.main()
def test_successful_existing_customer_creation(self):
"""Test the customer creation (already exists)."""
set_module_args({
with set_module_args({
'alerta_url': "http://localhost:8080",
'api_username': "admin@example.com",
'api_password': "password",
'customer': 'Developer',
'match': 'dev@example.com'
})
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page1()
with self.assertRaises(AnsibleExitJson):
self.module.main()
self.assertTrue(fetch_url_mock.call_count, 1)
}):
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page1()
with self.assertRaises(AnsibleExitJson):
self.module.main()
self.assertTrue(fetch_url_mock.call_count, 1)
def test_successful_customer_creation(self):
"""Test the customer creation."""
set_module_args({
with set_module_args({
'alerta_url': "http://localhost:8080",
'api_username': "admin@example.com",
'api_password': "password",
'customer': 'Developer',
'match': 'dev2@example.com'
})
}):
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page1()
with self.assertRaises(AnsibleExitJson):
self.module.main()
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page1()
with self.assertRaises(AnsibleExitJson):
self.module.main()
self.assertTrue(fetch_url_mock.call_count, 1)
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
assert call_data['match'] == "dev2@example.com"
assert call_data['customer'] == "Developer"
self.assertTrue(fetch_url_mock.call_count, 1)
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
assert call_data['match'] == "dev2@example.com"
assert call_data['customer'] == "Developer"
def test_successful_customer_creation_key(self):
"""Test the customer creation using api_key."""
set_module_args({
with set_module_args({
'alerta_url': "http://localhost:8080",
'api_key': "demo-key",
'customer': 'Developer',
'match': 'dev2@example.com'
})
}):
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page1()
with self.assertRaises(AnsibleExitJson):
self.module.main()
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page1()
with self.assertRaises(AnsibleExitJson):
self.module.main()
self.assertTrue(fetch_url_mock.call_count, 1)
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
assert call_data['match'] == "dev2@example.com"
assert call_data['customer'] == "Developer"
self.assertTrue(fetch_url_mock.call_count, 1)
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
assert call_data['match'] == "dev2@example.com"
assert call_data['customer'] == "Developer"
def test_failed_not_found(self):
"""Test failure with wrong URL."""
set_module_args({
with set_module_args({
'alerta_url': "http://localhost:8080/s",
'api_username': "admin@example.com",
'api_password': "password",
'customer': 'Developer',
'match': 'dev@example.com'
})
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 404, 'msg': 'Not found for request GET on http://localhost:8080/a/api/customers'})
with self.assertRaises(AnsibleFailJson):
self.module.main()
}):
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 404, 'msg': 'Not found for request GET on http://localhost:8080/a/api/customers'})
with self.assertRaises(AnsibleFailJson):
self.module.main()
def test_failed_forbidden(self):
"""Test failure with wrong user."""
set_module_args({
with set_module_args({
'alerta_url': "http://localhost:8080",
'api_username': "dev@example.com",
'api_password': "password",
'customer': 'Developer',
'match': 'dev@example.com'
})
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 403, 'msg': 'Permission Denied for GET on http://localhost:8080/api/customers'})
with self.assertRaises(AnsibleFailJson):
self.module.main()
}):
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 403, 'msg': 'Permission Denied for GET on http://localhost:8080/api/customers'})
with self.assertRaises(AnsibleFailJson):
self.module.main()
def test_failed_unauthorized(self):
"""Test failure with wrong username or password."""
set_module_args({
with set_module_args({
'alerta_url': "http://localhost:8080",
'api_username': "admin@example.com",
'api_password': "password_wrong",
'customer': 'Developer',
'match': 'dev@example.com'
})
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 401, 'msg': 'Unauthorized to request GET on http://localhost:8080/api/customers'})
with self.assertRaises(AnsibleFailJson):
self.module.main()
}):
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 401, 'msg': 'Unauthorized to request GET on http://localhost:8080/api/customers'})
with self.assertRaises(AnsibleFailJson):
self.module.main()
def test_successful_customer_deletion(self):
"""Test the customer deletion."""
set_module_args({
with set_module_args({
'alerta_url': "http://localhost:8080",
'api_username': "admin@example.com",
'api_password': "password",
'customer': 'Developer',
'match': 'dev@example.com',
'state': 'absent'
})
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page1()
with self.assertRaises(AnsibleExitJson):
self.module.main()
}):
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page1()
with self.assertRaises(AnsibleExitJson):
self.module.main()
def test_successful_customer_deletion_page2(self):
"""Test the customer deletion on the second page."""
set_module_args({
with set_module_args({
'alerta_url': "http://localhost:8080",
'api_username': "admin@example.com",
'api_password': "password",
'customer': 'Developer',
'match': 'dev@example.com',
'state': 'absent'
})
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page2()
with self.assertRaises(AnsibleExitJson):
self.module.main()
}):
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page2()
with self.assertRaises(AnsibleExitJson):
self.module.main()
def test_successful_nonexisting_customer_deletion(self):
"""Test the customer deletion (non existing)."""
set_module_args({
with set_module_args({
'alerta_url': "http://localhost:8080",
'api_username': "admin@example.com",
'api_password': "password",
'customer': 'Billing',
'match': 'dev@example.com',
'state': 'absent'
})
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page1()
with self.assertRaises(AnsibleExitJson):
self.module.main()
}):
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = customer_response_page1()
with self.assertRaises(AnsibleExitJson):
self.module.main()