Skip to content

Commit

Permalink
add dev examples and tests to verify that their schema loads and plan…
Browse files Browse the repository at this point in the history
…s work
  • Loading branch information
aschepis committed Apr 23, 2024
1 parent c115769 commit 305abc2
Show file tree
Hide file tree
Showing 125 changed files with 1,197 additions and 20 deletions.
55 changes: 35 additions & 20 deletions chronosphere/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,41 @@ import (
"github.com/stretchr/testify/require"
)

// Sanity test that the provider schema can be loaded by TF.
func TestProviderLoadSchema(t *testing.T) {
p := NewProject(t)
func TestExamples(t *testing.T) {
skips := map[string]string{
"data-bucket": "datasources are not supported without a real tenant",
"data-collection": "datasources are not supported without a real tenant",
"service-datasource": "datasources are not supported without a real tenant",
"entity-namespaces-internal": "aliased provider configuration not supported by test",
"entity-namespaces-managed": "aliased provider configuration not supported by test",
"entity-namespaces-mixed": "aliased provider configuration not supported by test",
}

p.Write(t, "main.tf", `
resource "chronosphere_collection" "c" {
name = "C"
exampleRoot := "../dev/examples"
examplesList, err := os.ReadDir(exampleRoot)
require.NoError(t, err)
for _, entry := range examplesList {
if !entry.IsDir() {
continue
}
if reason, ok := skips[entry.Name()]; ok {
t.Logf("skipping %s: %s", entry.Name(), reason)
continue
}
t.Run(entry.Name(), func(t *testing.T) {
p := NewProject(t)
exampleFiles, err := os.ReadDir(filepath.Join(exampleRoot, entry.Name()))
require.NoError(t, err)
for _, fileEntry := range exampleFiles {
path := filepath.Join(exampleRoot, entry.Name(), fileEntry.Name())
contents, err := os.ReadFile(path)
require.NoError(t, err)
p.Write(t, filepath.Base(fileEntry.Name()), string(contents))
}
p.Init(t)
p.Plan(t)
})
}
`)
p.Init(t)
p.Plan(t)
}

// Project represents a Terraform project.
Expand All @@ -52,23 +76,14 @@ func NewProject(t testing.TB) *Project {
dir, err := os.MkdirTemp(t.TempDir(), "tf-work")
require.NoError(t, err)

require.NoError(t, os.WriteFile(filepath.Join(dir, "provider.tf"), []byte(`
require.NoError(t, os.WriteFile(filepath.Join(dir, "provider_config.tf"), []byte(`
`), 0o666))

p := &Project{
dir: dir,
providerCfg: startProvider(t),
}
p.Write(t, "provider.tf", `
terraform {
required_providers {
chronosphere = {
version = "0.0.1-dev"
source = "local/chronosphereio/chronosphere"
}
}
}
p.Write(t, "provider_config.tf", `
provider "chronosphere" {
org = "test"
api_token = "test"
Expand Down
1 change: 1 addition & 0 deletions dev/examples/bucket-inline-policy/email.tf
11 changes: 11 additions & 0 deletions dev/examples/bucket-inline-policy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "chronosphere_bucket" "b" {
name = "${var.prefix} bucket"
notification_policy_data = chronosphere_notification_policy.np.notification_policy_data
}

resource "chronosphere_notification_policy" "np" {
route {
severity = "warn"
notifiers = [chronosphere_email_alert_notifier.email.id]
}
}
1 change: 1 addition & 0 deletions dev/examples/bucket-inline-policy/prefix.tf
1 change: 1 addition & 0 deletions dev/examples/bucket-inline-policy/version.tf
5 changes: 5 additions & 0 deletions dev/examples/bucket/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "chronosphere_bucket" "b" {
name = "${var.prefix} Bucket"
description = "${var.prefix} bucket created by terraform dev/examples"
labels = { "foo" : "bar" }
}
1 change: 1 addition & 0 deletions dev/examples/bucket/prefix.tf
1 change: 1 addition & 0 deletions dev/examples/bucket/version.tf
9 changes: 9 additions & 0 deletions dev/examples/classic-dashboard/collection.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "chronosphere_team" "t" {
name = "${var.prefix} team"
}

resource "chronosphere_collection" "c" {
name = "${var.prefix} Bucket"
team_id = chronosphere_team.t.id
description = "${var.prefix} collection created by terraform dev/examples"
}
22 changes: 22 additions & 0 deletions dev/examples/classic-dashboard/dashboard.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resource "chronosphere_classic_dashboard" "my_dashboard" {
collection_id = chronosphere_collection.c.id
dashboard_json = jsonencode({
title : "${var.prefix} Dashboard",
panels : [{
"gridPos" : {
"h" : 12,
"w" : 24,
"x" : 0,
"y" : 0
},
id : 2,
targets : [
{
expr : "1",
}
],
title : "Panel Title",
type : "graph",
}],
})
}
1 change: 1 addition & 0 deletions dev/examples/classic-dashboard/prefix.tf
1 change: 1 addition & 0 deletions dev/examples/classic-dashboard/version.tf
4 changes: 4 additions & 0 deletions dev/examples/collection-no-team/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "chronosphere_collection" "infra" {
name = "${var.prefix} Infrastructure Collection"
description = "Collection of resources related to infrastructure services."
}
1 change: 1 addition & 0 deletions dev/examples/collection-no-team/prefix.tf
1 change: 1 addition & 0 deletions dev/examples/collection-no-team/version.tf
1 change: 1 addition & 0 deletions dev/examples/collection-notification-policy/email.tf
17 changes: 17 additions & 0 deletions dev/examples/collection-notification-policy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "chronosphere_notification_policy" "np" {
team_id = chronosphere_team.t.id
name = "${var.prefix} team NP"

route {
severity = "warn"
notifiers = [chronosphere_email_alert_notifier.email.id]
}
}

resource "chronosphere_collection" "infra" {
name = "${var.prefix} Infrastructure Collection"
team_id = chronosphere_team.t.id
description = "Collection of resources related to infrastructure services."

notification_policy_id = chronosphere_notification_policy.np.id
}
1 change: 1 addition & 0 deletions dev/examples/collection-notification-policy/prefix.tf
1 change: 1 addition & 0 deletions dev/examples/collection-notification-policy/team.tf
1 change: 1 addition & 0 deletions dev/examples/collection-notification-policy/version.tf
5 changes: 5 additions & 0 deletions dev/examples/collection/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "chronosphere_collection" "infra" {
name = "${var.prefix} Infrastructure Collection"
description = "Collection of resources related to infrastructure services."
team_id = chronosphere_team.t.id
}
1 change: 1 addition & 0 deletions dev/examples/collection/prefix.tf
1 change: 1 addition & 0 deletions dev/examples/collection/teams.tf
1 change: 1 addition & 0 deletions dev/examples/collection/version.tf
5 changes: 5 additions & 0 deletions dev/examples/dashboard/collections.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "chronosphere_collection" "c" {
name = "${var.prefix} Collection"
description = "collection created by terraform dev/examples."
team_id = chronosphere_team.t.id
}
12 changes: 12 additions & 0 deletions dev/examples/dashboard/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "chronosphere_dashboard" "my_dashboard" {
slug = "slug"
collection_id = chronosphere_collection.c.id
dashboard_json = jsonencode({
kind : "Dashboard",
metadata : {
name : "Test Chrono Managed: Native Dashboard"
}
spec : {
}
})
}
5 changes: 5 additions & 0 deletions dev/examples/dashboard/prefix.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "prefix" {
type = string
default = "TF"
}

4 changes: 4 additions & 0 deletions dev/examples/dashboard/teams.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "chronosphere_team" "t" {
name = "${var.prefix} Team"
description = "Optional ${var.prefix} Team Description"
}
13 changes: 13 additions & 0 deletions dev/examples/dashboard/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
chronosphere = {
# Version used by "make install" to simplify development.
version = "0.0.1-dev"
source = "local/chronosphereio/chronosphere"

# To use the registry, update the version above and uncomment
# the source line below:
# source = "tf-registry.chronosphere.io/chronosphere/chronosphere"
}
}
}
9 changes: 9 additions & 0 deletions dev/examples/data-bucket/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "chronosphere_bucket" "default1" {
name = "Default"
}


data "chronosphere_bucket" "default2" {
slug = "default"
}

1 change: 1 addition & 0 deletions dev/examples/data-bucket/prefix.tf
1 change: 1 addition & 0 deletions dev/examples/data-bucket/version.tf
3 changes: 3 additions & 0 deletions dev/examples/data-collection/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "chronosphere_collection" "default" {
slug = "default"
}
1 change: 1 addition & 0 deletions dev/examples/data-collection/version.tf
87 changes: 87 additions & 0 deletions dev/examples/datasets/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
resource "chronosphere_dataset" "example_prod_payfe" {
name = "${var.prefix} Production paymentfe Traces"
description = "Traces passing through the paymentfe service in production"
configuration {
type = "TRACES"

trace_dataset {
match_criteria {
span {
duration {
max_secs = 99
min_secs = 1
}

error {
value = true
}

match_type = "INCLUDE"

operation {
value = "importantop"
match = "EXACT"
}

parent_operation {
value = "payments/.*"
match = "REGEX"
}

parent_service {
value = "frontdoor-[east|west]"
match = "REGEX"
}

service {
value = "importantsvc"
match = "EXACT"
}

span_count {
max = 2
min = 1
}

tag {
key = "cool_tag"

value {
value = "coolvalue"
match = "EXACT"
}
}

tag {
key = "env_tag"

value {
value = "prod.*"
match = "REGEX"
}
}

tag {
key = "http.status_code"

numeric_value {
comparison = "GREATER_THAN"
value = 299
}
}
}

trace {
duration {
max_secs = 10
min_secs = 5
}

error {
value = true
}
}
}
}
}
}
1 change: 1 addition & 0 deletions dev/examples/datasets/prefix.tf
1 change: 1 addition & 0 deletions dev/examples/datasets/version.tf
Loading

0 comments on commit 305abc2

Please sign in to comment.