from pathlib import Path from unittest.mock import ANY, patch from typer.testing import CliRunner from datadog_tools.cli.software_catalog import app runner = CliRunner() @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_create_service(software_catalog, datadog): """Test the create-service command.""" result = runner.invoke( app, [ "create-service", "--repository-url", "https://github.com/theorchard/test-service", "--repository-path", "/path/to/repo", ], ) assert result.exit_code == 0 software_catalog.return_value.upsert_service.assert_called_once_with( repository_url="https://github.com/theorchard/test-service", repository_path="/path/to/repo", service_definition_file_path=None, service_path="", pipeline_url=None, dry_run=False, ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_create_service_with_optional_args(software_catalog, datadog): """Test the create-service command.""" result = runner.invoke( app, [ "create-service", "--repository-url", "https://github.com/theorchard/test-service", "--repository-path", "/path/to/repo", "--service-definition-file-path", "service/path/other-software-catalog-file.yaml", "--service-path", "service/path", "--pipeline-url", "https://pipeline.theorchard.io/test-service", "--no-fetch-sentry-metadata", "--no-fetch-sonar-metadata", "--no-fetch-swagger-metadata", "--no-fetch-aws-metadata", ], ) assert result.exit_code == 0 software_catalog.return_value.upsert_service.assert_called_once_with( repository_url="https://github.com/theorchard/test-service", repository_path="/path/to/repo", service_definition_file_path="service/path/other-software-catalog-file.yaml", service_path="service/path", pipeline_url="https://pipeline.theorchard.io/test-service", dry_run=False, ) software_catalog.assert_called_once_with(datadog=ANY, sentry=None, sonar=None, swagger=None, aws=None) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_validate_service(software_catalog, datadog): """Test the validate-service command.""" result = runner.invoke( app, [ "validate-service", "--repository-path", "/path/to/repo", "--service-path", "service/path", ], ) assert result.exit_code == 0 software_catalog.return_value.validate_service.assert_called_once_with( repository_path="/path/to/repo", service_definition_file_path=None, service_path="service/path" ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_validate_service_with_optional_args(software_catalog, datadog): """Test the validate-service command.""" result = runner.invoke( app, [ "validate-service", "--repository-path", "/path/to/repo", "--service-definition-file-path", "service/path/other-software-catalog-file.yaml", "--service-path", "service/path", ], ) assert result.exit_code == 0 software_catalog.return_value.validate_service.assert_called_once_with( repository_path="/path/to/repo", service_definition_file_path="service/path/other-software-catalog-file.yaml", service_path="service/path", ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_create_library(software_catalog, datadog): """Test the create-library command.""" result = runner.invoke( app, [ "create-library", "--repository-url", "https://github.com/theorchrd/test-library", "--repository-path", "/path/to/repo", ], ) assert result.exit_code == 0 software_catalog.return_value.upsert_library.assert_called_once_with( repository_url="https://github.com/theorchrd/test-library", repository_path="/path/to/repo", library_definition_file_path=None, library_path="", pipeline_url=None, dry_run=False, ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_create_library_with_optional_args(software_catalog, datadog): """Test the create-library command.""" result = runner.invoke( app, [ "create-library", "--repository-url", "https://github.com/theorchrd/test-library", "--repository-path", "/path/to/repo", "--library-definition-file-path", "library/path/other-software-catalog-file.yaml", "--library-path", "library/path", "--pipeline-url", "https://pipeline.theorchard.io/test-library", "--dry-run", ], ) assert result.exit_code == 0 software_catalog.return_value.upsert_library.assert_called_once_with( repository_url="https://github.com/theorchrd/test-library", repository_path="/path/to/repo", library_definition_file_path="library/path/other-software-catalog-file.yaml", library_path="library/path", pipeline_url="https://pipeline.theorchard.io/test-library", dry_run=True, ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_validate_library(software_catalog, datadog): """Test the validate-library command.""" result = runner.invoke( app, [ "validate-library", "--repository-path", "/path/to/repo", "--library-path", "library/path", ], ) assert result.exit_code == 0 software_catalog.return_value.validate_library.assert_called_once_with( repository_path="/path/to/repo", library_definition_file_path=None, library_path="library/path" ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_validate_library_with_optional_args(software_catalog, datadog): """Test the validate-library command.""" result = runner.invoke( app, [ "validate-library", "--repository-path", "/path/to/repo", "--library-definition-file-path", "library/path/other-software-catalog-file.yaml", "--library-path", "library/path", ], ) assert result.exit_code == 0 software_catalog.return_value.validate_library.assert_called_once_with( repository_path="/path/to/repo", library_definition_file_path="library/path/other-software-catalog-file.yaml", library_path="library/path", ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_create_frontend(software_catalog, datadog): """Test the create-frontend command.""" result = runner.invoke( app, [ "create-frontend", "--repository-url", "https://github.com/theorchard/test-frontend", "--repository-path", "/path/to/repo", ], ) assert result.exit_code == 0 software_catalog.return_value.upsert_frontend.assert_called_once_with( repository_url="https://github.com/theorchard/test-frontend", repository_path="/path/to/repo", frontend_definition_file_path=None, frontend_path="", pipeline_url=None, dry_run=False, ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_create_frontend_with_optional_args(software_catalog, datadog): """Test the create-frontend command with optional arguments.""" result = runner.invoke( app, [ "create-frontend", "--repository-url", "https://github.com/theorchard/test-frontend", "--repository-path", "/path/to/repo", "--frontend-definition-file-path", "frontend/path/other-software-catalog-file.yaml", "--frontend-path", "frontend/path", "--pipeline-url", "https://pipeline.theorchard.io/test-frontend", "--dry-run", ], ) assert result.exit_code == 0 software_catalog.return_value.upsert_frontend.assert_called_once_with( repository_url="https://github.com/theorchard/test-frontend", repository_path="/path/to/repo", frontend_definition_file_path="frontend/path/other-software-catalog-file.yaml", frontend_path="frontend/path", pipeline_url="https://pipeline.theorchard.io/test-frontend", dry_run=True, ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_validate_frontend(software_catalog, datadog): """Test the validate-frontend command.""" result = runner.invoke( app, [ "validate-frontend", "--repository-path", "/path/to/repo", "--frontend-path", "frontend/path", ], ) assert result.exit_code == 0 software_catalog.return_value.validate_frontend.assert_called_once_with( repository_path="/path/to/repo", frontend_definition_file_path=None, frontend_path="frontend/path" ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_validate_frontend_with_optional_args(software_catalog, datadog): """Test the validate-frontend command with optional arguments.""" result = runner.invoke( app, [ "validate-frontend", "--repository-path", "/path/to/repo", "--frontend-definition-file-path", "frontend/path/other-software-catalog-file.yaml", "--frontend-path", "frontend/path", ], ) assert result.exit_code == 0 software_catalog.return_value.validate_frontend.assert_called_once_with( repository_path="/path/to/repo", frontend_definition_file_path="frontend/path/other-software-catalog-file.yaml", frontend_path="frontend/path", ) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_list_services(software_catalog, datadog): """Test the list-services command.""" software_catalog.return_value.list_entities.return_value = [{"name": "test-service"}] result = runner.invoke( app, [ "list-services", ], ) assert result.exit_code == 0 software_catalog.return_value.list_entities.assert_called_once_with(kind="service", raw=False) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_list_services_raw(software_catalog, datadog): """Test the list-services --raw command.""" software_catalog.return_value.list_entities.return_value = [{"name": "test-service"}] result = runner.invoke( app, ["list-services", "--raw"], ) assert result.exit_code == 0 software_catalog.return_value.list_entities.assert_called_once_with(kind="service", raw=True) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_list_libraries(software_catalog, datadog): """Test the list-libraries command.""" software_catalog.return_value.list_entities.return_value = [{"name": "test-library"}] result = runner.invoke( app, [ "list-libraries", ], ) assert result.exit_code == 0 software_catalog.return_value.list_entities.assert_called_once_with(kind="library", raw=False) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_list_libraries_raw(software_catalog, datadog): """Test the list-libraries --raw command.""" software_catalog.return_value.list_entities.return_value = [{"name": "test-library"}] result = runner.invoke( app, ["list-libraries", "--raw"], ) assert result.exit_code == 0 software_catalog.return_value.list_entities.assert_called_once_with(kind="library", raw=True) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_list_frontends(software_catalog, datadog): """Test the list-frontends command.""" software_catalog.return_value.list_entities.return_value = [{"name": "test-frontend"}] result = runner.invoke( app, [ "list-frontends", ], ) assert result.exit_code == 0 software_catalog.return_value.list_entities.assert_called_once_with(kind="frontend", raw=False) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_list_frontends_raw(software_catalog, datadog): """Test the list-frontends --raw command.""" software_catalog.return_value.list_entities.return_value = [{"name": "test-frontend"}] result = runner.invoke( app, ["list-frontends", "--raw"], ) assert result.exit_code == 0 software_catalog.return_value.list_entities.assert_called_once_with(kind="frontend", raw=True) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_list_systems(software_catalog, datadog): """Test the list-systems command.""" software_catalog.return_value.list_entities.return_value = [{"name": "test-library"}] result = runner.invoke( app, ["list-libraries", "--raw"], ) assert result.exit_code == 0 software_catalog.return_value.list_entities.assert_called_once_with(kind="library", raw=True) @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_get_service(software_catalog, datadog): """Test the get-service command.""" software_catalog.return_value.get_entity_schema.return_value = {"name": "test-service"} result = runner.invoke( app, ["get-service", "--name", "test-service"], ) assert result.exit_code == 0 software_catalog.return_value.get_entity_schema.assert_called_once_with(name="test-service", kind="service") @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_get_library(software_catalog, datadog): """Test the get-library command.""" software_catalog.return_value.get_entity_schema.return_value = {"name": "test-library"} result = runner.invoke( app, ["get-library", "--name", "test-library"], ) assert result.exit_code == 0 software_catalog.return_value.get_entity_schema.assert_called_once_with(name="test-library", kind="library") @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_get_frontend(software_catalog, datadog): """Test the get-frontend command.""" software_catalog.return_value.get_entity_schema.return_value = {"name": "test-frontend"} result = runner.invoke( app, ["get-frontend", "--name", "test-frontend"], ) assert result.exit_code == 0 software_catalog.return_value.get_entity_schema.assert_called_once_with(name="test-frontend", kind="frontend") @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_get_system(software_catalog, datadog): """Test the get-system command.""" software_catalog.return_value.get_entity_schema.return_value = {"name": "test-system"} result = runner.invoke( app, ["get-system", "--name", "test-system"], ) assert result.exit_code == 0 software_catalog.return_value.get_entity_schema.assert_called_once_with(name="test-system", kind="system") @patch("datadog_tools.cli.software_catalog.get_entity_kind_from_entity_definition_file") @patch("datadog_tools.cli.software_catalog.get_full_entity_definition_file_path") @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_create_service_entity( software_catalog, datadog, get_full_entity_definition_file_path, get_entity_kind_from_entity_definition_file ): """Test the create-entity command.""" get_entity_kind_from_entity_definition_file.return_value = "service" get_full_entity_definition_file_path.return_value = Path( "/path/to/repo/service/path/other-software-catalog-file.yaml" ) result = runner.invoke( app, [ "create-entity", "--repository-url", "https://github.com/theorchard/test-service", "--repository-path", "/path/to/repo", ], ) assert result.exit_code == 0 software_catalog.return_value.upsert_service.assert_called_once_with( repository_url="https://github.com/theorchard/test-service", repository_path="/path/to/repo", service_definition_file_path=None, service_path="", pipeline_url=None, dry_run=False, ) @patch("datadog_tools.cli.software_catalog.get_entity_kind_from_entity_definition_file") @patch("datadog_tools.cli.software_catalog.get_full_entity_definition_file_path") @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_create_library_entity( software_catalog, datadog, get_full_entity_definition_file_path, get_entity_kind_from_entity_definition_file ): """Test the create-entity command.""" get_entity_kind_from_entity_definition_file.return_value = "library" get_full_entity_definition_file_path.return_value = Path( "/path/to/repo/library/path/other-software-catalog-file.yaml" ) result = runner.invoke( app, [ "create-entity", "--repository-url", "https://github.com/theorchrd/test-library", "--repository-path", "/path/to/repo", ], ) assert result.exit_code == 0 software_catalog.return_value.upsert_library.assert_called_once_with( repository_url="https://github.com/theorchrd/test-library", repository_path="/path/to/repo", library_definition_file_path=None, library_path="", pipeline_url=None, dry_run=False, ) @patch("datadog_tools.cli.software_catalog.get_entity_kind_from_entity_definition_file") @patch("datadog_tools.cli.software_catalog.get_full_entity_definition_file_path") @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_validate_service_entity( software_catalog, datadog, get_full_entity_definition_file_path, get_entity_kind_from_entity_definition_file ): """Test the validate-entity command.""" get_entity_kind_from_entity_definition_file.return_value = "service" get_full_entity_definition_file_path.return_value = Path( "/path/to/repo/service/path/other-software-catalog-file.yaml" ) result = runner.invoke( app, [ "validate-entity", "--repository-path", "/path/to/repo", ], ) assert result.exit_code == 0 software_catalog.return_value.validate_service.assert_called_once_with( repository_path="/path/to/repo", service_definition_file_path=None, service_path="" ) @patch("datadog_tools.cli.software_catalog.get_entity_kind_from_entity_definition_file") @patch("datadog_tools.cli.software_catalog.get_full_entity_definition_file_path") @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_validate_library_entity( software_catalog, datadog, get_full_entity_definition_file_path, get_entity_kind_from_entity_definition_file ): """Test the validate-entity command.""" get_entity_kind_from_entity_definition_file.return_value = "library" get_full_entity_definition_file_path.return_value = Path( "/path/to/repo/library/path/other-software-catalog-file.yaml" ) result = runner.invoke( app, [ "validate-entity", "--repository-path", "/path/to/repo", ], ) assert result.exit_code == 0 software_catalog.return_value.validate_library.assert_called_once_with( repository_path="/path/to/repo", library_definition_file_path=None, library_path="" ) @patch("datadog_tools.cli.software_catalog.get_entity_kind_from_entity_definition_file") @patch("datadog_tools.cli.software_catalog.get_full_entity_definition_file_path") @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_create_frontend_entity( software_catalog, datadog, get_full_entity_definition_file_path, get_entity_kind_from_entity_definition_file ): """Test the create-entity command for frontend.""" get_entity_kind_from_entity_definition_file.return_value = "frontend" get_full_entity_definition_file_path.return_value = Path( "/path/to/repo/frontend/path/other-software-catalog-file.yaml" ) result = runner.invoke( app, [ "create-entity", "--repository-url", "https://github.com/theorchard/test-frontend", "--repository-path", "/path/to/repo", ], ) assert result.exit_code == 0 software_catalog.return_value.upsert_frontend.assert_called_once_with( repository_url="https://github.com/theorchard/test-frontend", repository_path="/path/to/repo", frontend_definition_file_path=None, frontend_path="", pipeline_url=None, dry_run=False, ) @patch("datadog_tools.cli.software_catalog.get_entity_kind_from_entity_definition_file") @patch("datadog_tools.cli.software_catalog.get_full_entity_definition_file_path") @patch("datadog_tools.cli.software_catalog.Datadog") @patch("datadog_tools.cli.software_catalog.SoftwareCatalog") def test_validate_frontend_entity( software_catalog, datadog, get_full_entity_definition_file_path, get_entity_kind_from_entity_definition_file ): """Test the validate-entity command for frontend.""" get_entity_kind_from_entity_definition_file.return_value = "frontend" get_full_entity_definition_file_path.return_value = Path( "/path/to/repo/frontend/path/other-software-catalog-file.yaml" ) result = runner.invoke( app, [ "validate-entity", "--repository-path", "/path/to/repo", ], ) assert result.exit_code == 0 software_catalog.return_value.validate_frontend.assert_called_once_with( repository_path="/path/to/repo", frontend_definition_file_path=None, frontend_path="" )