Files
llmgw/terraform-provider-llmgw/internal/provider/tenancy_test.go
T
Ilker ManapandClaude Opus 5.5 15a209e652 Terraform: organizations, teams, projects, keys and memberships
llmgw_org, llmgw_team and llmgw_project rename and move in place;
llmgw_key holds the key in a sensitive secret and is made again when
revoked or expired outside Terraform; llmgw_membership puts a person in
the tree. Tried with OpenTofu against the fake admin API, drift and
import included.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-09 06:42:07 +02:00

168 lines
5.5 KiB
Go

package provider
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"gitea.mynodes.xyz/ilker/llmgw/terraform-provider-llmgw/internal/client"
)
func tenancyConfig(project, team, role string) string {
roleLine := ""
if role != "" {
roleLine = fmt.Sprintf(" role = %q\n", role)
}
return fmt.Sprintf(`
resource "llmgw_org" "globex" {
name = "Globex"
}
resource "llmgw_team" "ops" {
name = "ops"
org = %s
}
resource "llmgw_project" "web" {
name = %q
team = llmgw_team.ops.id
}
resource "llmgw_membership" "ayse" {
user = "ayse@example.com"
target = "project:${llmgw_project.web.id}"
%s}
# Keys in the project that is not destroyed: one that ever had a key stays.
resource "llmgw_key" "ci" {
name = "ci"
project = "Acme/ml/training"
expires_in = "720h"
}
`, team, project, roleLine)
}
func TestAccTheTreeKeysAndMembershipsAreManaged(t *testing.T) {
api, cfg := fake(t)
var team, project string
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: factories,
CheckDestroy: func(*terraform.State) error {
if api.Has(client.LevelOrg, "org-1") || api.Has(client.LevelTeam, team) || api.Has(client.LevelProject, project) {
return fmt.Errorf("a part of the tree is left")
}
for _, k := range api.Keys() {
if k.State != "revoked" {
return fmt.Errorf("key %s is %s", k.ID, k.State)
}
}
if m := api.Memberships("u1"); len(m) != 0 {
return fmt.Errorf("memberships left: %+v", m)
}
return nil
},
Steps: []resource.TestStep{
{
Config: cfg + tenancyConfig("web", "llmgw_org.globex.name", ""),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("llmgw_project.web", "path", "Globex/ops/web"),
resource.TestCheckResourceAttrPair("llmgw_team.ops", "org_id", "llmgw_org.globex", "id"),
func(s *terraform.State) error {
got := s.RootModule().Resources["llmgw_membership.ayse"].Primary.Attributes["scope"]
if want := "project:" + s.RootModule().Resources["llmgw_project.web"].Primary.ID; got != want {
return fmt.Errorf("scope %q, not %q", got, want)
}
return nil
},
resource.TestCheckResourceAttr("llmgw_membership.ayse", "user_id", "u1"),
resource.TestCheckResourceAttr("llmgw_key.ci", "project_id", "p1"),
resource.TestCheckResourceAttrWith("llmgw_key.ci", "secret", func(v string) error {
if !strings.HasPrefix(v, "sk-llmgw-") {
return fmt.Errorf("secret %q", v)
}
return nil
}),
resource.TestCheckResourceAttrWith("llmgw_key.ci", "expires_at", func(v string) error {
if v == "" {
return fmt.Errorf("no expiry")
}
return nil
}),
func(s *terraform.State) error {
team = s.RootModule().Resources["llmgw_team.ops"].Primary.ID
project = s.RootModule().Resources["llmgw_project.web"].Primary.ID
return nil
},
),
},
{
// Renamed and the role changed, in place.
Config: cfg + tenancyConfig("site", "llmgw_org.globex.name", "manager"),
ConfigPlanChecks: resource.ConfigPlanChecks{PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("llmgw_project.web", plancheck.ResourceActionUpdate),
plancheck.ExpectResourceAction("llmgw_membership.ayse", plancheck.ResourceActionUpdate),
plancheck.ExpectResourceAction("llmgw_key.ci", plancheck.ResourceActionNoop),
}},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("llmgw_project.web", "path", "Globex/ops/site"),
func(*terraform.State) error {
if m := api.Memberships("u1"); len(m) != 1 || m[0].Role != "manager" {
return fmt.Errorf("memberships %+v", m)
}
return nil
},
),
},
{
// Moved to another organization with its project.
Config: cfg + tenancyConfig("site", `"Acme"`, "manager"),
ConfigPlanChecks: resource.ConfigPlanChecks{PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("llmgw_team.ops", plancheck.ResourceActionUpdate),
}},
// The project's path follows at the next refresh: the
// project itself did not change.
Check: resource.TestCheckResourceAttr("llmgw_team.ops", "org_id", "o1"),
},
{
// Renamed behind Terraform, and the key revoked: the name is
// put back, the key made again.
PreConfig: func() {
api.RenameBehindTerraform(client.LevelProject, project, "renamed")
for id, k := range api.Keys() {
if k.Name == "ci" && k.State == "active" {
api.RevokeBehindTerraform(id)
}
}
},
Config: cfg + tenancyConfig("site", `"Acme"`, "manager"),
ConfigPlanChecks: resource.ConfigPlanChecks{PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("llmgw_project.web", plancheck.ResourceActionUpdate),
plancheck.ExpectResourceAction("llmgw_key.ci", plancheck.ResourceActionCreate),
}},
Check: resource.TestCheckResourceAttr("llmgw_project.web", "path", "Acme/ops/site"),
},
{
ResourceName: "llmgw_org.globex",
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "llmgw_membership.ayse",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"user", "target"},
},
{
ResourceName: "llmgw_key.ci",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"secret", "expires_in", "project"},
},
},
})
}