from typing import Any import pytest from datadog_api_client.v2.model.entity_attributes import EntityAttributes from datadog_api_client.v2.model.entity_data import EntityData from datadog_tools.models.software_catalog import Frontend, Library, Service @pytest.mark.parametrize( "service_definition, teams, system, valid", [ pytest.param( { "apiVersion": "v3", "kind": "service", "metadata": { "name": "test-service", "description": "Test service", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "web", }, }, ["test-team"], None, True, id="valid service definition", ), pytest.param( { "apiVersion": "v3", "kind": "service", "metadata": {"name": "test-service", "description": "Test service"}, "spec": { "type": "web", }, }, ["test-team"], EntityData( attributes=EntityAttributes( owner="test-team", tags=[ "public:false", ], ) ), True, id="valid service definition with inheritance", ), pytest.param( { "kind": "service", "metadata": { "name": "test-service", "description": "Test service", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "web", }, }, ["test-team"], None, False, id="missing api version", ), pytest.param( { "apiVersion": "v3", "metadata": { "name": "test-service", "description": "Test service", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "web", }, }, ["test-team"], None, False, id="missing kind", ), pytest.param( { "apiVersion": "v3", "kind": "service", "metadata": { "description": "Test service", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "web", }, }, ["test-team"], None, False, id="missing name", ), pytest.param( { "apiVersion": "v3", "kind": "service", "metadata": { "name": "test-service", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "web", }, }, ["test-team"], None, False, id="missing description", ), pytest.param( { "apiVersion": "v3", "kind": "service", "metadata": { "name": "test-service", "description": "Test service", "tags": [ "public:false", ], }, "spec": { "type": "web", }, }, ["test-team"], None, False, id="missing owner", ), pytest.param( { "apiVersion": "v3", "kind": "service", "metadata": { "name": "test-service", "description": "Test service", "owner": "non-existent-team", "tags": [ "public:false", ], }, "spec": { "type": "web", }, }, ["test-team"], None, False, id="invalid owner", ), pytest.param( { "apiVersion": "v3", "kind": "service", "metadata": { "name": "test-service", "description": "Test service", "owner": "test-team", "tags": [ "terraformed:true", ], }, "spec": { "type": "web", }, }, ["test-team"], None, False, id="missing public tag", ), pytest.param( { "apiVersion": "v3", "kind": "service", "metadata": { "name": "test-service", "description": "Test service", "owner": "test-team", "tags": [ "public:false", ], }, "spec": {}, }, ["test-team"], None, False, id="missing type", ), pytest.param( { "apiVersion": "v3", "kind": "service", "metadata": { "name": "test-service", "description": "Test service", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "invalid", }, }, ["test-team"], None, False, id="invalid type", ), ], ) def test_validate_service(service_definition: dict[str, Any], teams: list[str], system: dict[str, Any], valid: bool): context = {"teams": teams, "system": system} if not valid: with pytest.raises(ValueError): Service.model_validate(service_definition, context=context) else: Service.model_validate(service_definition, context=context) @pytest.mark.parametrize( "library_definition, teams, system, valid", [ pytest.param( { "apiVersion": "v3", "kind": "library", "metadata": { "name": "test-library", "description": "Test library", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "sdk", }, }, ["test-team"], None, True, id="valid library definition", ), pytest.param( { "apiVersion": "v3", "kind": "library", "metadata": { "name": "test-library", "description": "Test library", }, "spec": { "type": "sdk", }, }, ["test-team"], EntityData( attributes=EntityAttributes( owner="test-team", tags=[ "public:false", ], ) ), True, id="valid library definition with inheritance", ), pytest.param( { "apiVersion": "v3", "kind": "library", "metadata": { "name": "test-library", "description": "Test library", "tags": [ "public:false", ], }, "spec": { "type": "sdk", }, }, ["test-team"], None, False, id="missing owner", ), pytest.param( { "apiVersion": "v3", "kind": "library", "metadata": { "name": "test-library", "description": "Test library", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "invalid", }, }, ["test-team"], None, False, id="invalid type", ), ], ) def test_validate_library(library_definition: dict[str, Any], teams: list[str], system: dict[str, Any], valid: bool): context = {"teams": teams, "system": system} if not valid: with pytest.raises(ValueError): Library.model_validate(library_definition, context=context) else: Library.model_validate(library_definition, context=context) @pytest.mark.parametrize( "frontend_definition, teams, system, valid", [ pytest.param( { "apiVersion": "v3", "kind": "frontend", "metadata": { "name": "test-frontend", "description": "Test frontend", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "web", }, }, ["test-team"], None, True, id="valid frontend definition", ), pytest.param( { "apiVersion": "v3", "kind": "frontend", "metadata": { "name": "test-frontend", "description": "Test frontend", }, "spec": { "type": "web", }, }, ["test-team"], EntityData( attributes=EntityAttributes( owner="test-team", tags=[ "public:false", ], ) ), True, id="valid frontend definition with inheritance", ), pytest.param( { "apiVersion": "v3", "kind": "frontend", "metadata": { "name": "test-frontend", "description": "Test frontend", "tags": [ "public:false", ], }, "spec": { "type": "web", }, }, ["test-team"], None, False, id="missing owner", ), pytest.param( { "apiVersion": "v3", "kind": "frontend", "metadata": { "name": "test-frontend", "description": "Test frontend", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "invalid", }, }, ["test-team"], None, False, id="invalid type", ), pytest.param( { "apiVersion": "v3", "kind": "frontend", "metadata": { "name": "test-frontend", "description": "Test frontend", "owner": "test-team", "tags": [ "public:false", ], }, "spec": { "type": "mobile", }, }, ["test-team"], None, True, id="valid frontend definition with mobile type", ), ], ) def test_validate_frontend(frontend_definition: dict[str, Any], teams: list[str], system: dict[str, Any], valid: bool): context = {"teams": teams, "system": system} if not valid: with pytest.raises(ValueError): Frontend.model_validate(frontend_definition, context=context) else: Frontend.model_validate(frontend_definition, context=context)