|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch |
| 3 | +from fastapi.testclient import TestClient |
| 4 | +from app.main import app |
| 5 | +from app.models import HelmTemplateGeneration, Pod, Persistance, Ingress, Environment, Output |
| 6 | + |
| 7 | +client = TestClient(app) |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def sample_helm_template_input(): |
| 11 | + return HelmTemplateGeneration( |
| 12 | + api_version=3, |
| 13 | + pods=[ |
| 14 | + Pod( |
| 15 | + name="nginx", |
| 16 | + image="nginx:latest", |
| 17 | + target_port=80, |
| 18 | + replicas=2, |
| 19 | + persistance=Persistance(enabled=False), |
| 20 | + environment=[Environment(name="DEBUG", value="true")], |
| 21 | + stateless=True, |
| 22 | + ingress=Ingress(enabled=True) |
| 23 | + ), |
| 24 | + Pod( |
| 25 | + name="redis", |
| 26 | + image="redis:latest", |
| 27 | + target_port=6379, |
| 28 | + replicas=1, |
| 29 | + persistance=Persistance(enabled=True), |
| 30 | + environment=[], |
| 31 | + stateless=False, |
| 32 | + ingress=Ingress(enabled=False) |
| 33 | + ) |
| 34 | + ] |
| 35 | + ) |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def invalid_input(): |
| 39 | + return { |
| 40 | + "api_version": 0, # Invalid api_version |
| 41 | + "pods": [ |
| 42 | + { |
| 43 | + "name": "", # Invalid: empty name |
| 44 | + "image": "nginx:latest", |
| 45 | + "target_port": 70000, # Invalid: exceeds 65535 |
| 46 | + "replicas": 0, # Invalid: less than 1 |
| 47 | + "persistance": {"size": "invalid_size", "accessModes": "InvalidMode"}, # Invalid size and accessModes |
| 48 | + "environment": [{"name": "", "value": ""}], # Invalid: empty name and value |
| 49 | + "stateless": True, |
| 50 | + "ingress": {"enabled": True, "host": ""} # Invalid: empty host |
| 51 | + } |
| 52 | + ] |
| 53 | + } |
| 54 | + |
| 55 | +@patch("app.main.execute_pythonfile") |
| 56 | +@patch("app.main.edit_directory_generator") |
| 57 | +@patch("app.main.gpt_service") |
| 58 | +def test_helm_template_generation( |
| 59 | + mock_gpt_service, |
| 60 | + mock_edit_directory_generator, |
| 61 | + mock_execute_pythonfile, |
| 62 | + sample_helm_template_input |
| 63 | +): |
| 64 | + mock_gpt_service.return_value = "Generated Python Code" |
| 65 | + |
| 66 | + response = client.post("/Helm-template/", json=sample_helm_template_input.model_dump()) |
| 67 | + |
| 68 | + assert response.status_code == 200 |
| 69 | + assert response.json()["output"] == "output" |
| 70 | + |
| 71 | + mock_gpt_service.assert_called_once() |
| 72 | + mock_edit_directory_generator.assert_called_once_with("helm_generator", "Generated Python Code") |
| 73 | + mock_execute_pythonfile.assert_called_once_with("MyHelm", "helm_generator") |
| 74 | + |
| 75 | +@patch("app.main.execute_pythonfile") |
| 76 | +@patch("app.main.edit_directory_generator") |
| 77 | +@patch("app.main.gpt_service") |
| 78 | +def test_helm_invalid_api( |
| 79 | + mock_gpt_service, |
| 80 | + mock_edit_directory_generator, |
| 81 | + mock_execute_pythonfile, |
| 82 | + invalid_input |
| 83 | +): |
| 84 | + invalid_input = invalid_input.copy() |
| 85 | + invalid_input["api_version"] = 0 |
| 86 | + response = client.post("/Helm-template/", json=invalid_input) |
| 87 | + assert response.status_code == 422 |
| 88 | + assert "detail" in response.json() |
| 89 | + errors = response.json()["detail"] |
| 90 | + assert any( |
| 91 | + error["loc"] == ["body", "api_version"] and "API version must be a positive integer." in error["msg"] |
| 92 | + for error in errors |
| 93 | + ) |
| 94 | + mock_gpt_service.assert_not_called() |
| 95 | + mock_edit_directory_generator.assert_not_called() |
| 96 | + mock_execute_pythonfile.assert_not_called() |
| 97 | + |
| 98 | +@patch("app.main.execute_pythonfile") |
| 99 | +@patch("app.main.edit_directory_generator") |
| 100 | +@patch("app.main.gpt_service") |
| 101 | +def test_helm_invalid_port( |
| 102 | + mock_gpt_service, |
| 103 | + mock_edit_directory_generator, |
| 104 | + mock_execute_pythonfile, |
| 105 | + invalid_input): |
| 106 | + |
| 107 | + invalid_input = invalid_input.copy() |
| 108 | + invalid_input["pods"][0]["target_port"] = 70000 # Invalid target_port |
| 109 | + response = client.post("/Helm-template/", json=invalid_input) |
| 110 | + assert response.status_code == 422 |
| 111 | + assert "detail" in response.json() |
| 112 | + errors = response.json()["detail"] |
| 113 | + assert any( |
| 114 | + error["loc"] == ["body", "pods", 0, "target_port"] and "Target port must be between 1 and 65535." in error["msg"] |
| 115 | + for error in errors |
| 116 | + ) |
| 117 | + mock_gpt_service.assert_not_called() |
| 118 | + mock_edit_directory_generator.assert_not_called() |
| 119 | + mock_execute_pythonfile.assert_not_called() |
0 commit comments