Skip to content
Snippets Groups Projects
Verified Commit 87a58ef4 authored by Karel van Klink's avatar Karel van Klink :smiley_cat:
Browse files

update unit test cases to expect JSON responses

parent b46fd0d5
No related branches found
No related tags found
No related merge requests found
Pipeline #84800 failed
...@@ -147,7 +147,7 @@ def _run_playbook_proc( ...@@ -147,7 +147,7 @@ def _run_playbook_proc(
} }
request_result = requests.post(callback, json=payload, timeout=DEFAULT_REQUEST_TIMEOUT) request_result = requests.post(callback, json=payload, timeout=DEFAULT_REQUEST_TIMEOUT)
if request_result.status_code != status.HTTP_200_OK: if request_result.status_code != status.HTTP_201_CREATED:
msg = f"Callback failed: {request_result.text}" msg = f"Callback failed: {request_result.text}"
logger.error(msg) logger.error(msg)
......
...@@ -2,15 +2,12 @@ import time ...@@ -2,15 +2,12 @@ import time
from collections.abc import Callable from collections.abc import Callable
from unittest.mock import patch from unittest.mock import patch
import jsonschema
import pytest import pytest
import responses import responses
from faker import Faker from faker import Faker
from fastapi import status from fastapi import status
from starlette.testclient import TestClient from starlette.testclient import TestClient
from lso.playbook import PlaybookLaunchResponse
TEST_CALLBACK_URL = "https://fqdn.abc.xyz/api/resume" TEST_CALLBACK_URL = "https://fqdn.abc.xyz/api/resume"
...@@ -171,7 +168,7 @@ def test_ip_trunk_provisioning( ...@@ -171,7 +168,7 @@ def test_ip_trunk_provisioning(
subscription_object: dict, subscription_object: dict,
mocked_ansible_runner_run: Callable, mocked_ansible_runner_run: Callable,
) -> None: ) -> None:
responses.post(url=TEST_CALLBACK_URL, status=200) responses.post(url=TEST_CALLBACK_URL, status=201)
params = { params = {
"callback": TEST_CALLBACK_URL, "callback": TEST_CALLBACK_URL,
...@@ -185,16 +182,15 @@ def test_ip_trunk_provisioning( ...@@ -185,16 +182,15 @@ def test_ip_trunk_provisioning(
with patch("lso.playbook.ansible_runner.run", new=mocked_ansible_runner_run) as _: with patch("lso.playbook.ansible_runner.run", new=mocked_ansible_runner_run) as _:
rv = client.post("/api/ip_trunk/", json=params) rv = client.post("/api/ip_trunk/", json=params)
assert rv.status_code == status.HTTP_200_OK assert rv.status_code == status.HTTP_201_CREATED
response = rv.json() response = rv.json()
# wait a second for the run thread to finish # wait a second for the run thread to finish
time.sleep(1) time.sleep(1)
jsonschema.validate(response, PlaybookLaunchResponse.model_json_schema()) assert isinstance(response, dict)
assert isinstance(response["job_id"], str)
responses.assert_call_count(TEST_CALLBACK_URL, 1) responses.assert_call_count(TEST_CALLBACK_URL, 1)
assert response["status"] == "ok"
@responses.activate @responses.activate
def test_ip_trunk_modification( def test_ip_trunk_modification(
...@@ -202,7 +198,7 @@ def test_ip_trunk_modification( ...@@ -202,7 +198,7 @@ def test_ip_trunk_modification(
subscription_object: dict, subscription_object: dict,
mocked_ansible_runner_run: Callable, mocked_ansible_runner_run: Callable,
) -> None: ) -> None:
responses.post(url=TEST_CALLBACK_URL, status=200) responses.post(url=TEST_CALLBACK_URL, status=201)
params = { params = {
"callback": TEST_CALLBACK_URL, "callback": TEST_CALLBACK_URL,
...@@ -216,16 +212,15 @@ def test_ip_trunk_modification( ...@@ -216,16 +212,15 @@ def test_ip_trunk_modification(
with patch("lso.playbook.ansible_runner.run", new=mocked_ansible_runner_run) as _: with patch("lso.playbook.ansible_runner.run", new=mocked_ansible_runner_run) as _:
rv = client.put("/api/ip_trunk/", json=params) rv = client.put("/api/ip_trunk/", json=params)
assert rv.status_code == status.HTTP_200_OK assert rv.status_code == status.HTTP_201_CREATED
response = rv.json() response = rv.json()
# wait a second for the run thread to finish # wait a second for the run thread to finish
time.sleep(1) time.sleep(1)
jsonschema.validate(response, PlaybookLaunchResponse.model_json_schema()) assert isinstance(response, dict)
assert isinstance(response["job_id"], str)
responses.assert_call_count(TEST_CALLBACK_URL, 1) responses.assert_call_count(TEST_CALLBACK_URL, 1)
assert response["status"] == "ok"
@responses.activate @responses.activate
def test_ip_trunk_deletion(client: TestClient, subscription_object: dict, mocked_ansible_runner_run: Callable) -> None: def test_ip_trunk_deletion(client: TestClient, subscription_object: dict, mocked_ansible_runner_run: Callable) -> None:
...@@ -242,16 +237,15 @@ def test_ip_trunk_deletion(client: TestClient, subscription_object: dict, mocked ...@@ -242,16 +237,15 @@ def test_ip_trunk_deletion(client: TestClient, subscription_object: dict, mocked
with patch("lso.playbook.ansible_runner.run", new=mocked_ansible_runner_run) as _: with patch("lso.playbook.ansible_runner.run", new=mocked_ansible_runner_run) as _:
rv = client.request(url="/api/ip_trunk/", method=responses.DELETE, json=params) rv = client.request(url="/api/ip_trunk/", method=responses.DELETE, json=params)
assert rv.status_code == status.HTTP_200_OK assert rv.status_code == status.HTTP_201_CREATED
response = rv.json() response = rv.json()
# wait a second for the run thread to finish # wait a second for the run thread to finish
time.sleep(1) time.sleep(1)
jsonschema.validate(response, PlaybookLaunchResponse.model_json_schema()) assert isinstance(response, dict)
assert isinstance(response["job_id"], str)
responses.assert_call_count(TEST_CALLBACK_URL, 1) responses.assert_call_count(TEST_CALLBACK_URL, 1)
assert response["status"] == "ok"
@responses.activate @responses.activate
def test_ip_trunk_migration( def test_ip_trunk_migration(
...@@ -275,12 +269,11 @@ def test_ip_trunk_migration( ...@@ -275,12 +269,11 @@ def test_ip_trunk_migration(
with patch("lso.playbook.ansible_runner.run", new=mocked_ansible_runner_run) as _: with patch("lso.playbook.ansible_runner.run", new=mocked_ansible_runner_run) as _:
rv = client.post(url="/api/ip_trunk/migrate", json=params) rv = client.post(url="/api/ip_trunk/migrate", json=params)
assert rv.status_code == status.HTTP_200_OK assert rv.status_code == status.HTTP_201_CREATED
response = rv.json() response = rv.json()
# Wait a second for the run to finish # Wait a second for the run to finish
time.sleep(1) time.sleep(1)
jsonschema.validate(response, PlaybookLaunchResponse.model_json_schema()) assert isinstance(response, dict)
assert isinstance(response["job_id"], str)
responses.assert_call_count(TEST_CALLBACK_URL, 1) responses.assert_call_count(TEST_CALLBACK_URL, 1)
assert response["status"] == "ok"
...@@ -2,20 +2,17 @@ import time ...@@ -2,20 +2,17 @@ import time
from collections.abc import Callable from collections.abc import Callable
from unittest.mock import patch from unittest.mock import patch
import jsonschema
import responses import responses
from faker import Faker from faker import Faker
from fastapi import status from fastapi import status
from starlette.testclient import TestClient from starlette.testclient import TestClient
from lso.playbook import PlaybookLaunchResponse
TEST_CALLBACK_URL = "https://fqdn.abc.xyz/api/resume" TEST_CALLBACK_URL = "https://fqdn.abc.xyz/api/resume"
@responses.activate @responses.activate
def test_router_provisioning(client: TestClient, faker: Faker, mocked_ansible_runner_run: Callable) -> None: def test_router_provisioning(client: TestClient, faker: Faker, mocked_ansible_runner_run: Callable) -> None:
responses.put(url=TEST_CALLBACK_URL, status=200) responses.put(url=TEST_CALLBACK_URL, status=201)
params = { params = {
"callback": TEST_CALLBACK_URL, "callback": TEST_CALLBACK_URL,
...@@ -48,12 +45,11 @@ def test_router_provisioning(client: TestClient, faker: Faker, mocked_ansible_ru ...@@ -48,12 +45,11 @@ def test_router_provisioning(client: TestClient, faker: Faker, mocked_ansible_ru
with patch("lso.playbook.ansible_runner.run", new=mocked_ansible_runner_run) as _: with patch("lso.playbook.ansible_runner.run", new=mocked_ansible_runner_run) as _:
rv = client.post("/api/router/", json=params) rv = client.post("/api/router/", json=params)
assert rv.status_code == status.HTTP_200_OK assert rv.status_code == status.HTTP_201_CREATED
response = rv.json() response = rv.json()
# wait two seconds for the run thread to finish # wait two seconds for the run thread to finish
time.sleep(2) time.sleep(2)
jsonschema.validate(response, PlaybookLaunchResponse.model_json_schema()) assert isinstance(response, dict)
assert isinstance(response["job_id"], str)
responses.assert_call_count(TEST_CALLBACK_URL, 1) responses.assert_call_count(TEST_CALLBACK_URL, 1)
assert response["status"] == "ok"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment