Skip to content

Commit 501126c

Browse files
toppercodesampagent
andcommitted
Add acceptance tests for dashboard lifecycle
Amp-Thread-ID: https://ampcode.com/threads/T-019c9bc7-a165-774f-b025-2e92d6e0b9d5 Co-authored-by: Amp <amp@ampcode.com>
1 parent c1a2feb commit 501126c

2 files changed

Lines changed: 225 additions & 0 deletions

File tree

axiom/provider_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,14 @@ func testAccCheckAxiomResourcesDestroyed(client *ax.Client) func(s *terraform.St
746746
_, err = client.Tokens.Get(context.Background(), resource.Primary.ID)
747747
case "axiom_virtual_field":
748748
_, err = client.VirtualFields.Get(context.Background(), resource.Primary.ID)
749+
case "axiom_dashboard":
750+
uid := resource.Primary.Attributes["uid"]
751+
if uid == "" {
752+
uid = resource.Primary.ID
753+
}
754+
_, err = client.Dashboards.GetRaw(context.Background(), uid)
755+
default:
756+
continue
749757
}
750758
datasetErr, ok := err.(ax.HTTPError)
751759
if !ok {
@@ -779,6 +787,14 @@ func testAccCheckAxiomResourcesExist(client *ax.Client, resourceName string) res
779787
_, err = client.Tokens.Get(context.Background(), rs.Primary.ID)
780788
case "axiom_virtual_field":
781789
_, err = client.VirtualFields.Get(context.Background(), rs.Primary.ID)
790+
case "axiom_dashboard":
791+
uid := rs.Primary.Attributes["uid"]
792+
if uid == "" {
793+
uid = rs.Primary.ID
794+
}
795+
_, err = client.Dashboards.GetRaw(context.Background(), uid)
796+
default:
797+
return fmt.Errorf("unsupported resource type in existence check: %s", rs.Type)
782798
}
783799

784800
if err != nil {
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package axiom
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"testing"
8+
9+
"github.com/google/uuid"
10+
"github.com/hashicorp/terraform-plugin-framework/providerserver"
11+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
12+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
13+
"github.com/hashicorp/terraform-plugin-testing/terraform"
14+
"github.com/stretchr/testify/assert"
15+
16+
ax "github.com/axiomhq/axiom-go/axiom"
17+
)
18+
19+
func TestAccAxiomDashboardResource_WithProvidedUID(t *testing.T) {
20+
if os.Getenv("TF_ACC") == "" {
21+
t.Skip("acceptance tests skipped unless TF_ACC is set")
22+
}
23+
testAccPreCheck(t)
24+
25+
client, err := ax.NewClient()
26+
assert.NoError(t, err)
27+
28+
uid := "tf-dashboard-" + strings.ReplaceAll(uuid.NewString(), "_", "-")
29+
name := "dash-provided-" + uuid.NewString()
30+
updatedName := name + "-updated"
31+
32+
resourceName := "axiom_dashboard.test"
33+
34+
resource.Test(t, resource.TestCase{
35+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
36+
"axiom": providerserver.NewProtocol6WithError(NewAxiomProvider()),
37+
},
38+
CheckDestroy: testAccCheckAxiomResourcesDestroyed(client),
39+
Steps: []resource.TestStep{
40+
{
41+
Config: testAccAxiomDashboardConfig(uid, name, false),
42+
Check: resource.ComposeTestCheckFunc(
43+
testAccCheckAxiomResourcesExist(client, resourceName),
44+
resource.TestCheckResourceAttr(resourceName, "uid", uid),
45+
resource.TestCheckResourceAttrSet(resourceName, "id"),
46+
resource.TestCheckResourceAttrSet(resourceName, "version"),
47+
),
48+
},
49+
{
50+
Config: testAccAxiomDashboardConfig(uid, updatedName, false),
51+
Check: resource.ComposeTestCheckFunc(
52+
testAccCheckAxiomResourcesExist(client, resourceName),
53+
resource.TestCheckResourceAttr(resourceName, "uid", uid),
54+
resource.TestCheckResourceAttrWith(resourceName, "dashboard", func(v string) error {
55+
if !strings.Contains(v, updatedName) {
56+
return fmt.Errorf("expected updated dashboard name in dashboard JSON, got %s", v)
57+
}
58+
return nil
59+
}),
60+
),
61+
},
62+
{
63+
ResourceName: resourceName,
64+
ImportState: true,
65+
ImportStateVerify: true,
66+
ImportStateId: uid,
67+
ImportStateVerifyIgnore: []string{
68+
"message",
69+
},
70+
},
71+
},
72+
})
73+
}
74+
75+
func TestAccAxiomDashboardResource_ServerGeneratedUID(t *testing.T) {
76+
if os.Getenv("TF_ACC") == "" {
77+
t.Skip("acceptance tests skipped unless TF_ACC is set")
78+
}
79+
testAccPreCheck(t)
80+
81+
client, err := ax.NewClient()
82+
assert.NoError(t, err)
83+
84+
name := "dash-generated-" + uuid.NewString()
85+
updatedName := name + "-updated"
86+
resourceName := "axiom_dashboard.test"
87+
88+
var generatedUID string
89+
90+
resource.Test(t, resource.TestCase{
91+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
92+
"axiom": providerserver.NewProtocol6WithError(NewAxiomProvider()),
93+
},
94+
CheckDestroy: testAccCheckAxiomResourcesDestroyed(client),
95+
Steps: []resource.TestStep{
96+
{
97+
Config: testAccAxiomDashboardConfigWithoutUID(name, false),
98+
Check: resource.ComposeTestCheckFunc(
99+
testAccCheckAxiomResourcesExist(client, resourceName),
100+
resource.TestCheckResourceAttrSet(resourceName, "uid"),
101+
resource.TestCheckResourceAttrSet(resourceName, "id"),
102+
resource.TestCheckResourceAttrSet(resourceName, "version"),
103+
testAccCaptureDashboardUID(resourceName, &generatedUID),
104+
),
105+
},
106+
{
107+
Config: testAccAxiomDashboardConfigWithoutUID(updatedName, true),
108+
Check: resource.ComposeTestCheckFunc(
109+
testAccCheckAxiomResourcesExist(client, resourceName),
110+
resource.TestCheckResourceAttrWith(resourceName, "uid", func(v string) error {
111+
if generatedUID == "" {
112+
return fmt.Errorf("expected captured uid to be non-empty")
113+
}
114+
if v != generatedUID {
115+
return fmt.Errorf("expected server-generated uid to stay stable, want %s got %s", generatedUID, v)
116+
}
117+
return nil
118+
}),
119+
resource.TestCheckResourceAttrWith(resourceName, "dashboard", func(v string) error {
120+
if !strings.Contains(v, updatedName) {
121+
return fmt.Errorf("expected updated dashboard name in dashboard JSON, got %s", v)
122+
}
123+
return nil
124+
}),
125+
),
126+
},
127+
{
128+
ResourceName: resourceName,
129+
ImportState: true,
130+
ImportStateIdFunc: func(*terraform.State) (string, error) {
131+
if generatedUID == "" {
132+
return "", fmt.Errorf("generated uid was not captured")
133+
}
134+
return generatedUID, nil
135+
},
136+
ImportStateVerify: true,
137+
ImportStateVerifyIgnore: []string{
138+
"message",
139+
},
140+
},
141+
},
142+
})
143+
}
144+
145+
func testAccAxiomDashboardConfig(uid, name string, overwrite bool) string {
146+
return fmt.Sprintf(`
147+
provider "axiom" {
148+
api_token = %q
149+
base_url = %q
150+
}
151+
152+
resource "axiom_dashboard" "test" {
153+
uid = %q
154+
overwrite = %t
155+
message = "terraform apply"
156+
dashboard = jsonencode({
157+
name = %q
158+
description = "terraform acceptance dashboard"
159+
refreshTime = 60
160+
schemaVersion = 2
161+
timeWindowStart = "qr-now-1h"
162+
timeWindowEnd = "qr-now"
163+
charts = []
164+
layout = []
165+
})
166+
}
167+
`, os.Getenv("AXIOM_TOKEN"), os.Getenv("AXIOM_URL"), uid, overwrite, name)
168+
}
169+
170+
func testAccAxiomDashboardConfigWithoutUID(name string, overwrite bool) string {
171+
return fmt.Sprintf(`
172+
provider "axiom" {
173+
api_token = %q
174+
base_url = %q
175+
}
176+
177+
resource "axiom_dashboard" "test" {
178+
overwrite = %t
179+
message = "terraform apply"
180+
dashboard = jsonencode({
181+
name = %q
182+
description = "terraform acceptance dashboard"
183+
refreshTime = 60
184+
schemaVersion = 2
185+
timeWindowStart = "qr-now-1h"
186+
timeWindowEnd = "qr-now"
187+
charts = []
188+
layout = []
189+
})
190+
}
191+
`, os.Getenv("AXIOM_TOKEN"), os.Getenv("AXIOM_URL"), overwrite, name)
192+
}
193+
194+
func testAccCaptureDashboardUID(resourceName string, out *string) resource.TestCheckFunc {
195+
return func(s *terraform.State) error {
196+
rs, ok := s.RootModule().Resources[resourceName]
197+
if !ok {
198+
return fmt.Errorf("resource not found: %s", resourceName)
199+
}
200+
201+
uid := rs.Primary.Attributes["uid"]
202+
if uid == "" {
203+
return fmt.Errorf("resource %s has empty uid", resourceName)
204+
}
205+
206+
*out = uid
207+
return nil
208+
}
209+
}

0 commit comments

Comments
 (0)