diff --git a/datadog/resource_datadog_service_definition_yaml_resource_test.go b/datadog/resource_datadog_service_definition_yaml_resource_test.go new file mode 100644 index 0000000000..62d34ab52c --- /dev/null +++ b/datadog/resource_datadog_service_definition_yaml_resource_test.go @@ -0,0 +1,107 @@ +package datadog + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func TestServiceDefinitionYAMLResourceV3Schema(t *testing.T) { + resource := resourceDatadogServiceDefinitionYAML() + + // Test that the resource is properly configured + if resource.CreateContext == nil { + t.Fatal("CreateContext should not be nil") + } + if resource.ReadContext == nil { + t.Fatal("ReadContext should not be nil") + } + if resource.UpdateContext == nil { + t.Fatal("UpdateContext should not be nil") + } + if resource.DeleteContext == nil { + t.Fatal("DeleteContext should not be nil") + } + + // Test schema + schemaMap := resource.SchemaFunc() + serviceDefSchema, ok := schemaMap["service_definition"] + if !ok { + t.Fatal("service_definition should be in schema") + } + + if serviceDefSchema.Type != schema.TypeString { + t.Error("service_definition should be of type string") + } + + if !serviceDefSchema.Required { + t.Error("service_definition should be required") + } + + // Test v3 schema validation through the ValidateFunc + validV3YAML := `schema-version: v3 +dd-service: test-service-v3 +team: test-team +tier: high +lifecycle: production +contacts: + - name: Support Email + type: email + contact: team@example.com +links: + - name: Runbook + type: runbook + url: https://runbook/test-service +tags: + - env:prod + - team:platform +integrations: + pagerduty: + service-url: https://my-org.pagerduty.com/service-directory/Ptest-service` + + warnings, errors := serviceDefSchema.ValidateFunc(validV3YAML, "service_definition") + + if len(errors) > 0 { + t.Errorf("Valid v3 YAML should not produce errors: %v", errors) + } + + if len(warnings) > 0 { + t.Logf("Warnings for v3 YAML: %v", warnings) + } +} + +func TestServiceDefinitionYAMLStateFunc(t *testing.T) { + resource := resourceDatadogServiceDefinitionYAML() + schemaMap := resource.SchemaFunc() + serviceDefSchema := schemaMap["service_definition"] + + // Test that StateFunc normalizes v3 YAML properly + inputV3YAML := `schema-version: v3 +dd-service: test-service-v3 +team: test-team +tier: high +lifecycle: production +tags: + - env:prod + - team:platform +contacts: + - name: Support Email + type: email + contact: team@example.com` + + normalizedYAML := serviceDefSchema.StateFunc(inputV3YAML) + + // The StateFunc should return a normalized version + if normalizedYAML == "" { + t.Error("StateFunc should not return empty string for valid v3 YAML") + } + + // Verify the normalized YAML is still valid + warnings, errors := serviceDefSchema.ValidateFunc(normalizedYAML, "service_definition") + if len(errors) > 0 { + t.Errorf("Normalized v3 YAML should be valid: %v", errors) + } + if len(warnings) > 0 { + t.Logf("Warnings for normalized v3 YAML: %v", warnings) + } +} diff --git a/datadog/service_definition_validation_test.go b/datadog/service_definition_validation_test.go new file mode 100644 index 0000000000..3828f8f764 --- /dev/null +++ b/datadog/service_definition_validation_test.go @@ -0,0 +1,93 @@ +package datadog + +import ( + "testing" +) + +func TestServiceDefinitionValidation(t *testing.T) { + tests := []struct { + name string + yaml string + expectError bool + }{ + { + name: "valid v2 schema", + yaml: `schema-version: v2 +dd-service: test-service +team: test-team`, + expectError: false, + }, + { + name: "valid v2.1 schema", + yaml: `schema-version: v2.1 +dd-service: test-service +team: test-team`, + expectError: false, + }, + { + name: "valid v2.2 schema", + yaml: `schema-version: v2.2 +dd-service: test-service +team: test-team`, + expectError: false, + }, + { + name: "valid v3 schema", + yaml: `schema-version: v3 +dd-service: test-service +team: test-team +tier: high +lifecycle: production`, + expectError: false, + }, + { + name: "valid v3.1 schema", + yaml: `schema-version: v3.1 +dd-service: test-service +team: test-team +tier: high +lifecycle: production`, + expectError: false, + }, + { + name: "invalid v1 schema", + yaml: `schema-version: v1 +dd-service: test-service +team: test-team`, + expectError: true, + }, + { + name: "missing schema-version", + yaml: `dd-service: test-service +team: test-team`, + expectError: true, + }, + { + name: "missing dd-service", + yaml: `schema-version: v3 +team: test-team`, + expectError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + warnings, errors := isValidServiceDefinition(tt.yaml, "service_definition") + + if tt.expectError { + if len(errors) == 0 { + t.Errorf("Expected validation errors but got none") + } + } else { + if len(errors) > 0 { + t.Errorf("Expected no validation errors but got: %v", errors) + } + } + + // warnings are not fatal, so we just log them + if len(warnings) > 0 { + t.Logf("Warnings: %v", warnings) + } + }) + } +} diff --git a/datadog/tests/resource_datadog_service_definition_yaml_test.go b/datadog/tests/resource_datadog_service_definition_yaml_test.go index b22c483183..4ce75af1ef 100644 --- a/datadog/tests/resource_datadog_service_definition_yaml_test.go +++ b/datadog/tests/resource_datadog_service_definition_yaml_test.go @@ -77,6 +77,25 @@ func TestAccDatadogServiceDefinition_BasicV2_2(t *testing.T) { }) } +func TestAccDatadogServiceDefinition_BasicV3(t *testing.T) { + t.Parallel() + ctx, accProviders := testAccProviders(context.Background(), t) + uniq := strings.ToLower(uniqueEntityName(ctx, t)) + accProvider := testAccProvider(t, accProviders) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: accProviders, + CheckDestroy: testAccCheckDatadogServiceDefinitionDestroy(accProvider), + Steps: []resource.TestStep{ + { + Config: testAccCheckDatadogServiceDefinitionV3(uniq), + Check: checkServiceDefinitionExists(accProvider), + }, + }, + }) +} + func TestAccDatadogServiceDefinition_BasicBackstage(t *testing.T) { t.Parallel() ctx, accProviders := testAccProviders(context.Background(), t) @@ -170,7 +189,7 @@ links: tags: - my:tag - service:tag -team: my-team +team: my-team EOF }`, uniq) } @@ -217,6 +236,52 @@ EOF }`, uniq) } +func testAccCheckDatadogServiceDefinitionV3(uniq string) string { + return fmt.Sprintf(` +resource "datadog_service_definition_yaml" "service_definition" { + service_definition =<