oncall-engine/tools/migrators/lib/tests/grafana/test_transform.py

111 lines
3.7 KiB
Python
Raw Normal View History

Service to service model migration (#5485) # What this PR does Adds Service and Business Service migration to the Pager Duty Migrator. To test, in addition to the OnCall configs, you need to crate a Grafana Service Account with `Admin` permission and generate a token. You will set `GRAFANA_SERVICE_ACCOUNT_URL`, per the README, to `https://<namespace>:<token>@<server>` The namespace is the stack id, in the format of `stacks-<stack id>` Service migration is configurable, filterable, and idempotent. ## Which issue(s) this PR closes Related to [issue link here] <!-- *Note*: If you want the issue to be auto-closed once the PR is merged, change "Related to" to "Closes" in the line above. If you have more than one GitHub issue that this PR closes, be sure to preface each issue link with a [closing keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue). This ensures that the issue(s) are auto-closed once the PR has been merged. --> ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [ ] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes. --------- Co-authored-by: Joey Orlando <joey.orlando@grafana.com> Co-authored-by: GitHub Actions <actions@github.com> Co-authored-by: grafana-irm-app[bot] <165293418+grafana-irm-app[bot]@users.noreply.github.com> Co-authored-by: Joey Orlando <joseph.t.orlando@gmail.com>
2025-03-15 19:07:59 -06:00
"""
Unit tests for the Grafana Service Model transformation logic.
"""
from unittest.mock import Mock
import pytest
from lib.grafana.transform import transform_service, validate_component
from lib.pagerduty.resources.business_service import BusinessService
from lib.pagerduty.resources.services import TechnicalService
@pytest.fixture
def technical_service():
"""Create a mock technical service for testing."""
service = Mock(spec=TechnicalService)
service.name = "Test Service"
service.description = "A test service"
service.id = "P123456"
service.status = "active"
service.html_url = "https://pagerduty.com/services/P123456"
service.self_url = "https://api.pagerduty.com/services/P123456"
return service
@pytest.fixture
def business_service():
"""Create a mock business service for testing."""
service = Mock(spec=BusinessService)
service.name = "Test Business Service"
service.description = "A test business service"
service.id = "P789012"
service.html_url = "https://pagerduty.com/services/P789012"
service.self_url = "https://api.pagerduty.com/services/P789012"
return service
def test_transform_technical_service(technical_service):
"""Test transforming a technical service."""
component = transform_service(technical_service)
# Verify the component structure
assert component["apiVersion"] == "servicemodel.ext.grafana.com/v1alpha1"
assert component["kind"] == "Component"
assert component["metadata"]["name"] == "test-service"
assert component["spec"]["type"] == "service"
assert component["spec"]["description"] == "A test service"
# Verify annotations
annotations = component["metadata"]["annotations"]
assert annotations["pagerduty.com/service-id"] == "P123456"
assert annotations["pagerduty.com/status"] == "active"
assert (
annotations["pagerduty.com/html-url"]
== "https://pagerduty.com/services/P123456"
)
assert (
annotations["pagerduty.com/api-url"]
== "https://api.pagerduty.com/services/P123456"
)
def test_transform_business_service(business_service):
"""Test transforming a business service."""
component = transform_service(business_service)
# Verify the component structure
assert component["apiVersion"] == "servicemodel.ext.grafana.com/v1alpha1"
assert component["kind"] == "Component"
assert component["metadata"]["name"] == "test-business-service"
assert component["spec"]["type"] == "business_service"
assert component["spec"]["description"] == "A test business service"
# Verify annotations
annotations = component["metadata"]["annotations"]
assert annotations["pagerduty.com/service-id"] == "P789012"
assert (
annotations["pagerduty.com/html-url"]
== "https://pagerduty.com/services/P789012"
)
assert (
annotations["pagerduty.com/api-url"]
== "https://api.pagerduty.com/services/P789012"
)
def test_validate_component():
"""Test component validation."""
# Test valid component
valid_component = {
"apiVersion": "servicemodel.ext.grafana.com/v1alpha1",
"kind": "Component",
"metadata": {
"name": "test-service",
"annotations": {
"pagerduty.com/service-id": "P123456",
"pagerduty.com/status": "active",
},
},
"spec": {"type": "service", "description": "A test service"},
}
errors = validate_component(valid_component)
assert errors == []
# Test missing required field
invalid_component = valid_component.copy()
del invalid_component["spec"]
errors = validate_component(invalid_component)
assert "Missing required field: spec" in errors