Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/acceptance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- name: Project data source
package: ./internal/datasources/project
project_id_required: true
- name: Profile data source
package: ./internal/datasources/profile
project_id_required: true
# Keep the job timeout above the go test timeout so Go can report the test
# timeout before the runner stops the job. Either hard timeout can bypass
# t.Cleanup, so release operators still inspect for leaked test resources.
Expand Down
110 changes: 110 additions & 0 deletions internal/datasources/profile/datasource_acc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package profile_test

import (
"context"
"fmt"
"os"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
kernel "github.com/kernel/kernel-go-sdk"
"github.com/kernel/terraform-provider-kernel/internal/acctest"
"github.com/kernel/terraform-provider-kernel/internal/projectscope"
)

func TestAccProfileDataSourceByIDAndName(t *testing.T) {
if os.Getenv("TF_ACC") != "1" {
t.Skip("set TF_ACC=1 to run Terraform acceptance tests")
}
acctest.PreCheck(t)

projectID := os.Getenv(acctest.EnvProjectID)
if projectID == "" {
t.Fatalf("%s must be set for the profile data source acceptance test", acctest.EnvProjectID)
}

name := acctest.UniqueName(t, "profile-data")
id := testAccCreateProfileFixture(t, projectID, name)
config := testAccProfileDataSourceConfig(id, name, projectID)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.kernel_profile.by_id", "id", id),
resource.TestCheckResourceAttr("data.kernel_profile.by_id", "name", name),
resource.TestCheckResourceAttr("data.kernel_profile.by_id", "project_id", projectID),
resource.TestCheckResourceAttrSet("data.kernel_profile.by_id", "created_at"),
resource.TestCheckResourceAttr("data.kernel_profile.by_name", "id", id),
resource.TestCheckResourceAttr("data.kernel_profile.by_name", "name", name),
resource.TestCheckNoResourceAttr("data.kernel_profile.by_name", "project_id"),
resource.TestCheckResourceAttrSet("data.kernel_profile.by_name", "created_at"),
),
},
{
Config: config,
PlanOnly: true,
},
},
})
}

func testAccCreateProfileFixture(t *testing.T, projectID, name string) string {
t.Helper()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

profile, err := acctest.ClientFromEnv().CreateProfile(ctx, projectID, kernel.ProfileNewParams{Name: kernel.String(name)})
if err != nil {
t.Fatalf("create Kernel profile fixture: %v", err)
}
if profile == nil || profile.ID == "" {
t.Fatal("Kernel returned an empty profile fixture response")
}

// Cleanups run last-in-first-out, so deletion must register after verification.
testAccRequireProfileDeleted(t, projectID, profile.ID)
acctest.CleanupProfile(t, projectID, profile.ID)

if profile.Name != name {
t.Fatalf("created Kernel profile name = %q, want %q", profile.Name, name)
}
return profile.ID
}

func testAccRequireProfileDeleted(t *testing.T, projectID, id string) {
t.Helper()

t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

_, err := acctest.ClientFromEnv().GetProfile(ctx, projectID, id)
if projectscope.IsNotFound(err) {
return
}
if err != nil {
t.Errorf("read Kernel profile %s after cleanup: %v", id, err)
return
}
t.Errorf("Kernel profile %s still exists after cleanup", id)
})
}

func testAccProfileDataSourceConfig(id, name, projectID string) string {
return acctest.ProviderConfig() + fmt.Sprintf(`
data "kernel_profile" "by_id" {
id = %q
project_id = %q
}

data "kernel_profile" "by_name" {
name = %q
}
`, id, projectID, name)
}