Skip to content

Commit b9aed91

Browse files
toppercodesampagent
andcommitted
Harden dashboard import assertions
Amp-Thread-ID: https://ampcode.com/threads/T-019cb38a-d07d-75bf-9176-bc810a11c485 Co-authored-by: Amp <amp@ampcode.com>
1 parent a7515d0 commit b9aed91

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

axiom/resource_dashboard_integration_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func TestAccAxiomDashboardResource_WithProvidedUID(t *testing.T) {
6666
ImportStateVerifyIgnore: []string{
6767
"dashboard",
6868
},
69+
ImportStateCheck: testAccCheckImportedDashboardState(uid, updatedName, nil),
6970
},
7071
},
7172
})
@@ -136,6 +137,12 @@ func TestAccAxiomDashboardResource_ServerGeneratedUID(t *testing.T) {
136137
"dashboard",
137138
"overwrite",
138139
},
140+
ImportStateCheck: testAccCheckImportedDashboardState(generatedUID, updatedName, func(v string) error {
141+
if v != "false" {
142+
return fmt.Errorf("expected imported overwrite to default to false, got %q", v)
143+
}
144+
return nil
145+
}),
139146
},
140147
},
141148
})
@@ -274,3 +281,32 @@ func testAccCaptureDashboardUID(resourceName string, out *string) resource.TestC
274281
return nil
275282
}
276283
}
284+
285+
func testAccCheckImportedDashboardState(expectedUID, expectedName string, overwriteCheck func(string) error) resource.ImportStateCheckFunc {
286+
return func(states []*terraform.InstanceState) error {
287+
if len(states) != 1 {
288+
return fmt.Errorf("expected exactly one imported instance state, got %d", len(states))
289+
}
290+
291+
attrs := states[0].Attributes
292+
if attrs["uid"] != expectedUID {
293+
return fmt.Errorf("expected imported uid %q, got %q", expectedUID, attrs["uid"])
294+
}
295+
296+
dashboardJSON := attrs["dashboard"]
297+
if dashboardJSON == "" {
298+
return fmt.Errorf("expected imported dashboard JSON to be present")
299+
}
300+
if !strings.Contains(dashboardJSON, expectedName) {
301+
return fmt.Errorf("expected imported dashboard JSON to include name %q, got %s", expectedName, dashboardJSON)
302+
}
303+
304+
if overwriteCheck != nil {
305+
if err := overwriteCheck(attrs["overwrite"]); err != nil {
306+
return err
307+
}
308+
}
309+
310+
return nil
311+
}
312+
}

0 commit comments

Comments
 (0)