Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions datadog/resource_datadog_service_definition_yaml_resource_test.go
Original file line number Diff line number Diff line change
@@ -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: [email protected]
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: [email protected]`

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)
}
}
93 changes: 93 additions & 0 deletions datadog/service_definition_validation_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
67 changes: 66 additions & 1 deletion datadog/tests/resource_datadog_service_definition_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -170,7 +189,7 @@ links:
tags:
- my:tag
- service:tag
team: my-team
team: my-team
EOF
}`, uniq)
}
Expand Down Expand Up @@ -217,6 +236,52 @@ EOF
}`, uniq)
}

func testAccCheckDatadogServiceDefinitionV3(uniq string) string {
return fmt.Sprintf(`
resource "datadog_service_definition_yaml" "service_definition" {
service_definition =<<EOF
schema-version: v3
dd-service: %s
contacts:
- contact: [email protected]
name: Team Email
type: email
extensions:
myorgextension: extensionvalue
integrations:
opsgenie:
region: US
service-url: https://my-org.opsgenie.com/service/123e4567-e89b-12d3-a456-426614174000
pagerduty:
service-url: https://my-org.pagerduty.com/service-directory/PMyService
links:
- name: Architecture
type: doc
provider: Google Drive
url: https://my-runbook
- name: Runbook
type: runbook
url: https://my-runbook
- name: Source Code
type: repo
provider: GitHub
url: https://github.com/DataDog/schema
tags:
- my:tag
- service:tag
team: my-team
languages:
- go
- python
type: web
lifecycle: production
tier: high
application: e-commerce
description: A test service using schema v3
EOF
}`, uniq)
}

func testAccCheckDatadogServiceDefinitionBackstage(uniq string) string {
return fmt.Sprintf(`
resource "datadog_service_definition_yaml" "service_definition_backstage" {
Expand Down
52 changes: 52 additions & 0 deletions examples/resources/datadog_service_definition_yaml/resource.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
// Service Definition with v3 Schema Definition
resource "datadog_service_definition_yaml" "service_definition_v3" {
service_definition = <<EOF
schema-version: v3
dd-service: shopping-cart
team: e-commerce-team
contacts:
- name: Support Email
type: email
contact: [email protected]
- name: Support Slack
type: slack
contact: https://www.slack.com/archives/shopping-cart
description: shopping cart service responsible for managing shopping carts
tier: high
lifecycle: production
application: e-commerce
languages:
- go
- python
type: web
ci-pipeline-fingerprints:
- fp1
- fp2
links:
- name: shopping-cart runbook
type: runbook
url: https://runbook/shopping-cart
- name: shopping-cart architecture
type: doc
provider: gdoc
url: https://google.drive/shopping-cart-architecture
- name: shopping-cart service Wiki
type: doc
provider: wiki
url: https://wiki/shopping-cart
- name: shopping-cart source code
type: repo
provider: github
url: http://github/shopping-cart
tags:
- business-unit:retail
- cost-center:engineering
integrations:
pagerduty:
service-url: https://www.pagerduty.com/service-directory/Pshopping-cart
extensions:
mycompany.com/shopping-cart:
customField: customValue
EOF
}

// Service Definition with v2.2 Schema Definition
resource "datadog_service_definition_yaml" "service_definition_v2_2" {
service_definition = <<EOF
Expand Down