Skip to content

Commit bfef988

Browse files
committed
Test profile data source against Kernel
1 parent 1d4b930 commit bfef988

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

.github/workflows/acceptance.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ jobs:
2727
- name: Project data source
2828
package: ./internal/datasources/project
2929
project_id_required: true
30+
- name: Profile data source
31+
package: ./internal/datasources/profile
32+
project_id_required: true
3033
# Keep the job timeout above the go test timeout so Go can report the test
3134
# timeout before the runner stops the job. Either hard timeout can bypass
3235
# t.Cleanup, so release operators still inspect for leaked test resources.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package profile_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"testing"
8+
"time"
9+
10+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+
kernel "github.com/kernel/kernel-go-sdk"
12+
"github.com/kernel/terraform-provider-kernel/internal/acctest"
13+
"github.com/kernel/terraform-provider-kernel/internal/projectscope"
14+
)
15+
16+
func TestAccProfileDataSourceByIDAndName(t *testing.T) {
17+
if os.Getenv("TF_ACC") != "1" {
18+
t.Skip("set TF_ACC=1 to run Terraform acceptance tests")
19+
}
20+
acctest.PreCheck(t)
21+
22+
projectID := os.Getenv(acctest.EnvProjectID)
23+
if projectID == "" {
24+
t.Fatalf("%s must be set for the profile data source acceptance test", acctest.EnvProjectID)
25+
}
26+
27+
name := acctest.UniqueName(t, "profile-data")
28+
id := testAccCreateProfileFixture(t, projectID, name)
29+
config := testAccProfileDataSourceConfig(id, name, projectID)
30+
31+
resource.Test(t, resource.TestCase{
32+
PreCheck: func() { acctest.PreCheck(t) },
33+
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories(),
34+
Steps: []resource.TestStep{
35+
{
36+
Config: config,
37+
Check: resource.ComposeAggregateTestCheckFunc(
38+
resource.TestCheckResourceAttr("data.kernel_profile.by_id", "id", id),
39+
resource.TestCheckResourceAttr("data.kernel_profile.by_id", "name", name),
40+
resource.TestCheckResourceAttr("data.kernel_profile.by_id", "project_id", projectID),
41+
resource.TestCheckResourceAttrSet("data.kernel_profile.by_id", "created_at"),
42+
resource.TestCheckResourceAttr("data.kernel_profile.by_name", "id", id),
43+
resource.TestCheckResourceAttr("data.kernel_profile.by_name", "name", name),
44+
resource.TestCheckNoResourceAttr("data.kernel_profile.by_name", "project_id"),
45+
resource.TestCheckResourceAttrSet("data.kernel_profile.by_name", "created_at"),
46+
),
47+
},
48+
{
49+
Config: config,
50+
PlanOnly: true,
51+
},
52+
},
53+
})
54+
}
55+
56+
func testAccCreateProfileFixture(t *testing.T, projectID, name string) string {
57+
t.Helper()
58+
59+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
60+
defer cancel()
61+
62+
profile, err := acctest.ClientFromEnv().CreateProfile(ctx, projectID, kernel.ProfileNewParams{Name: kernel.String(name)})
63+
if err != nil {
64+
t.Fatalf("create Kernel profile fixture: %v", err)
65+
}
66+
if profile == nil || profile.ID == "" {
67+
t.Fatal("Kernel returned an empty profile fixture response")
68+
}
69+
70+
// Cleanups run last-in-first-out, so deletion must register after verification.
71+
testAccRequireProfileDeleted(t, projectID, profile.ID)
72+
acctest.CleanupProfile(t, projectID, profile.ID)
73+
74+
if profile.Name != name {
75+
t.Fatalf("created Kernel profile name = %q, want %q", profile.Name, name)
76+
}
77+
return profile.ID
78+
}
79+
80+
func testAccRequireProfileDeleted(t *testing.T, projectID, id string) {
81+
t.Helper()
82+
83+
t.Cleanup(func() {
84+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
85+
defer cancel()
86+
87+
_, err := acctest.ClientFromEnv().GetProfile(ctx, projectID, id)
88+
if projectscope.IsNotFound(err) {
89+
return
90+
}
91+
if err != nil {
92+
t.Errorf("read Kernel profile %s after cleanup: %v", id, err)
93+
return
94+
}
95+
t.Errorf("Kernel profile %s still exists after cleanup", id)
96+
})
97+
}
98+
99+
func testAccProfileDataSourceConfig(id, name, projectID string) string {
100+
return acctest.ProviderConfig() + fmt.Sprintf(`
101+
data "kernel_profile" "by_id" {
102+
id = %q
103+
project_id = %q
104+
}
105+
106+
data "kernel_profile" "by_name" {
107+
name = %q
108+
}
109+
`, id, projectID, name)
110+
}

0 commit comments

Comments
 (0)