Skip to content

Commit

Permalink
test(monitors): added basic acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rambatino committed Apr 2, 2024
1 parent fc30881 commit 0045f94
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 51 deletions.
136 changes: 136 additions & 0 deletions axiom/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package axiom

import (
"context"
"fmt"
"net/http"
"os"
"testing"

ax "github.com/axiomhq/axiom-go/axiom"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/stretchr/testify/assert"
)

func TestAccAxiomResources_basic(t *testing.T) {
client, err := ax.NewClient()
assert.NoError(t, err)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"axiom": providerserver.NewProtocol6WithError(NewAxiomProvider()),
},
CheckDestroy: testAccCheckAxiomResourcesDestroyed(client),
Steps: []resource.TestStep{
{
Config: testAccAxiomDatasetConfig_basic(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAxiomResourcesExist(client, "axiom_dataset.test"),
resource.TestCheckResourceAttr("axiom_dataset.test", "name", "test-dataset"),
resource.TestCheckResourceAttr("axiom_dataset.test", "description", "A test dataset"),
testAccCheckAxiomResourcesExist(client, "axiom_monitor.test_monitor"),
resource.TestCheckResourceAttr("axiom_monitor.test_monitor", "name", "test monitor"),
testAccCheckAxiomResourcesExist(client, "axiom_notifier.slack_test"),
resource.TestCheckResourceAttr("axiom_notifier.slack_test", "name", "slack_test"),
),
},
},
})
}

func testAccPreCheck(t *testing.T) {
if os.Getenv("AXIOM_TOKEN") == "" || os.Getenv("AXIOM_ID") == "" {
t.Fatalf("AXIOM_TOKEN and AXIOM_ID must be set for acceptance tests")
}
}

func testAccCheckAxiomResourcesDestroyed(client *ax.Client) func(s *terraform.State) error {
return func(s *terraform.State) error {
for id, resource := range s.Modules[0].Resources {
var err error
switch resource.Type {
case "axiom_notifier":
_, err = client.Notifiers.Get(context.Background(), resource.Primary.ID)
case "axiom_dataset":
_, err = client.Datasets.Get(context.Background(), resource.Primary.ID)
case "axiom_monitor":
_, err = client.Monitors.Get(context.Background(), resource.Primary.ID)
}
datasetErr, ok := err.(ax.HTTPError)
if !ok {
return fmt.Errorf("could not assert error for %s as ax.HTTPError: %T", id, err)
}
if datasetErr.Status != http.StatusNotFound {
return fmt.Errorf("http error incorrect for %s GET: %v", id, datasetErr.Status)
}
}

return nil
}
}

func testAccCheckAxiomResourcesExist(client *ax.Client, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, resource := range s.Modules[0].Resources {
var err error
switch resource.Type {
case "axiom_notifier":
_, err = client.Notifiers.Get(context.Background(), resource.Primary.ID)
case "axiom_dataset":
_, err = client.Datasets.Get(context.Background(), resource.Primary.ID)
case "axiom_monitor":
_, err = client.Monitors.Get(context.Background(), resource.Primary.ID)
}
return err
}
return nil
}
}

func testAccAxiomDatasetConfig_basic() string {
return `
provider "axiom" {
api_token = "` + os.Getenv("AXIOM_TOKEN") + `"
org_id = "` + os.Getenv("AXIOM_ID") + `"
base_url = "` + os.Getenv("AXIOM_URL") + `"
}
resource "axiom_dataset" "test" {
name = "test-dataset"
description = "A test dataset"
}
resource "axiom_notifier" "slack_test" {
name = "slack_test"
properties = {
slack = {
slack_url = "https://hooks.slack.com/services/EXAMPLE/EXAMPLE/EXAMPLE"
}
}
}
resource "axiom_monitor" "test_monitor" {
depends_on = [axiom_dataset.test, axiom_notifier.slack_test]
name = "test monitor"
description = "test_monitor updated"
apl_query = <<EOT
['test-dataset']
| summarize count() by bin_auto(_time)
EOT
interval_minutes = 5
operator = "Above"
range_minutes = 5
threshold = 1
notifier_ids = [
axiom_notifier.slack_test.id
]
alert_on_no_data = false
notify_by_group = false
}
`
}
7 changes: 7 additions & 0 deletions axiom/resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type MonitorResourceModel struct {
Description types.String `tfsdk:"description"`
ID types.String `tfsdk:"id"`
AlertOnNoData types.Bool `tfsdk:"alert_on_no_data"`
NotifyByGroup types.Bool `tfsdk:"notify_by_group"`
APLQuery types.String `tfsdk:"apl_query"`
DisabledUntil types.String `tfsdk:"disabled_until"`
IntervalMinutes types.Int64 `tfsdk:"interval_minutes"`
Expand Down Expand Up @@ -76,6 +77,10 @@ func (r *MonitorResource) Schema(_ context.Context, _ resource.SchemaRequest, re
MarkdownDescription: "If the monitor should trigger an alert if there is no data",
Required: true,
},
"notify_by_group": schema.BoolAttribute{
MarkdownDescription: "If the monitor should track non-time groups separately",
Required: true,
},
"apl_query": schema.StringAttribute{
MarkdownDescription: "The query used inside the monitor",
Required: true,
Expand Down Expand Up @@ -256,6 +261,7 @@ func extractMonitorResourceModel(ctx context.Context, plan MonitorResourceModel)
return &axiom.Monitor{
Name: plan.Name.ValueString(),
AlertOnNoData: plan.AlertOnNoData.ValueBool(),
NotifyByGroup: plan.NotifyByGroup.ValueBool(),
APLQuery: plan.APLQuery.ValueString(),
Description: plan.Description.ValueString(),
DisabledUntil: disabledUntil,
Expand All @@ -277,6 +283,7 @@ func flattenMonitor(monitor *axiom.Monitor) MonitorResourceModel {
Name: types.StringValue(monitor.Name),
Description: types.StringValue(monitor.Description),
AlertOnNoData: types.BoolValue(monitor.AlertOnNoData),
NotifyByGroup: types.BoolValue(monitor.NotifyByGroup),
APLQuery: types.StringValue(monitor.APLQuery),
DisabledUntil: disabledUntil,
IntervalMinutes: types.Int64Value(int64(monitor.Interval.Minutes())),
Expand Down
1 change: 1 addition & 0 deletions example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ EOT
axiom_notifier.slack_test.id
]
alert_on_no_data = false
notify_by_group = false
}

resource "axiom_user" "test_user" {
Expand Down
23 changes: 12 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ module terraform-provider-axiom-provider
go 1.21

require (
github.com/axiomhq/axiom-go v0.17.3-0.20240315112126-78f3ed4bc184
github.com/axiomhq/axiom-go v0.17.3-0.20240402152701-0fc6c8a162e8
github.com/hashicorp/terraform-plugin-docs v0.18.0
github.com/hashicorp/terraform-plugin-framework v1.5.0
github.com/hashicorp/terraform-plugin-framework v1.7.0
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.20.0
github.com/hashicorp/terraform-plugin-go v0.22.1
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.6.0
github.com/hashicorp/terraform-plugin-testing v1.7.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/Kunde21/markdownfmt/v3 v3.1.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/ProtonMail/go-crypto v1.1.0-alpha.0 // indirect
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
Expand All @@ -43,12 +44,12 @@ require (
github.com/hashicorp/go-plugin v1.6.0 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hc-install v0.6.2 // indirect
github.com/hashicorp/hcl/v2 v2.19.1 // indirect
github.com/hashicorp/hc-install v0.6.3 // indirect
github.com/hashicorp/hcl/v2 v2.20.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.20.0 // indirect
github.com/hashicorp/terraform-json v0.21.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
Expand All @@ -70,13 +71,12 @@ require (
github.com/russross/blackfriday v1.6.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yuin/goldmark v1.6.0 // indirect
github.com/yuin/goldmark-meta v1.1.0 // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
github.com/zclconf/go-cty v1.14.3 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.48.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.48.0 // indirect
go.opentelemetry.io/otel v1.23.1 // indirect
Expand All @@ -91,8 +91,9 @@ require (
golang.org/x/tools v0.19.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 0045f94

Please sign in to comment.