terraform-provider-llmgw/ is a module of its own, built on terraform-plugin-framework, so the gateway does not carry its dependencies. llmgw_model is a model added at runtime: scaling, class, owner and adapters change in place, anything else replaces it; the definition is kept as written rather than as the catalog filled it in. llmgw_quota and llmgw_guard_attachment keep the scope as written beside the ID it resolved to, and import by that ID. Data sources llmgw_models and llmgw_nodes. The client lists every endpoint it calls and refuses any other; a test checks each, and every field it sends and reads, against docs/admin-api.openapi.json, so a change to the admin API that breaks the provider fails here. Acceptance tests run Terraform against an admin API in memory when TF_ACC is set. CI vets and tests the module. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
308 lines
9.1 KiB
Go
308 lines
9.1 KiB
Go
package provider
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/providerserver"
|
|
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
|
|
"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"
|
|
"gitea.mynodes.xyz/ilker/llmgw/terraform-provider-llmgw/internal/fakeapi"
|
|
)
|
|
|
|
// These run Terraform itself against the provider and an admin API in
|
|
// memory, and so need TF_ACC=1 and a terraform binary (on PATH, or
|
|
// TF_ACC_TERRAFORM_PATH); without TF_ACC they are skipped. Each step's apply
|
|
// is followed by a plan that must be empty, which is what shows the state
|
|
// read back does not differ from the configuration on every run.
|
|
|
|
var factories = map[string]func() (tfprotov6.ProviderServer, error){
|
|
"llmgw": providerserver.NewProtocol6WithError(New("test")()),
|
|
}
|
|
|
|
func fake(t *testing.T) (*fakeapi.API, string) {
|
|
t.Helper()
|
|
api := fakeapi.New("secret",
|
|
client.NodeView{ID: "gpu01", State: "ready", GPUTotal: 8, GPUFree: 8, Classes: []string{"h100-80g"}},
|
|
client.NodeView{ID: "cpu01", State: "cordoned", CPUCount: 32, MemoryGB: 128})
|
|
srv := httptest.NewServer(api)
|
|
t.Cleanup(srv.Close)
|
|
return api, fmt.Sprintf(`
|
|
provider "llmgw" {
|
|
endpoint = %q
|
|
token = "secret"
|
|
}
|
|
`, srv.URL)
|
|
}
|
|
|
|
const modelV1 = `
|
|
resource "llmgw_model" "qwen" {
|
|
name = "qwen-small"
|
|
source = "/srv/models/qwen.gguf"
|
|
aliases = ["qwen"]
|
|
profiles = [{
|
|
name = "cpu"
|
|
engine = "llamacpp"
|
|
min_memory_gb = 4
|
|
cpu_cores = 4
|
|
}]
|
|
scaling = {
|
|
min = 1
|
|
idle_timeout = "30m"
|
|
}
|
|
}
|
|
`
|
|
|
|
// In place: scaling, class and owner.
|
|
const modelV2 = `
|
|
resource "llmgw_model" "qwen" {
|
|
name = "qwen-small"
|
|
source = "/srv/models/qwen.gguf"
|
|
aliases = ["qwen"]
|
|
profiles = [{
|
|
name = "cpu"
|
|
engine = "llamacpp"
|
|
min_memory_gb = 4
|
|
cpu_cores = 4
|
|
}]
|
|
scaling = {
|
|
min = 0
|
|
max = 2
|
|
idle_timeout = "30m"
|
|
}
|
|
class = "opportunistic"
|
|
owner = "team:Acme/ml"
|
|
}
|
|
`
|
|
|
|
// A profile changed: the model is replaced.
|
|
const modelV3 = `
|
|
resource "llmgw_model" "qwen" {
|
|
name = "qwen-small"
|
|
source = "/srv/models/qwen.gguf"
|
|
aliases = ["qwen"]
|
|
profiles = [{
|
|
name = "cpu"
|
|
engine = "llamacpp"
|
|
min_memory_gb = 6
|
|
cpu_cores = 4
|
|
}]
|
|
scaling = {
|
|
min = 0
|
|
max = 2
|
|
idle_timeout = "30m"
|
|
}
|
|
class = "opportunistic"
|
|
owner = "team:Acme/ml"
|
|
}
|
|
`
|
|
|
|
func TestAccAModelIsAddedChangedInPlaceReplacedAndRemoved(t *testing.T) {
|
|
api, cfg := fake(t)
|
|
inCluster := func(check func(client.Model) error) resource.TestCheckFunc {
|
|
return func(*terraform.State) error {
|
|
m, ok := api.Model("qwen-small")
|
|
if !ok {
|
|
return fmt.Errorf("qwen-small is not in the catalog")
|
|
}
|
|
return check(m)
|
|
}
|
|
}
|
|
resource.Test(t, resource.TestCase{
|
|
ProtoV6ProviderFactories: factories,
|
|
CheckDestroy: func(*terraform.State) error {
|
|
if _, ok := api.Model("qwen-small"); ok {
|
|
return fmt.Errorf("qwen-small is still in the catalog")
|
|
}
|
|
return nil
|
|
},
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: cfg + modelV1,
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
resource.TestCheckResourceAttr("llmgw_model.qwen", "id", "qwen-small"),
|
|
// Filled in by the catalog: a max not written follows min.
|
|
resource.TestCheckResourceAttr("llmgw_model.qwen", "scaling.max", "1"),
|
|
// As written, not as the catalog defaulted it.
|
|
resource.TestCheckNoResourceAttr("llmgw_model.qwen", "profiles.0.tp"),
|
|
inCluster(func(m client.Model) error {
|
|
if m.Scaling.IdleTimeout != 30*time.Minute || m.Profiles[0].MinMemoryGB != 4 {
|
|
return fmt.Errorf("added as %+v", m)
|
|
}
|
|
return nil
|
|
}),
|
|
),
|
|
},
|
|
{
|
|
Config: cfg + modelV2,
|
|
ConfigPlanChecks: resource.ConfigPlanChecks{PreApply: []plancheck.PlanCheck{
|
|
plancheck.ExpectResourceAction("llmgw_model.qwen", plancheck.ResourceActionUpdate),
|
|
}},
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
resource.TestCheckResourceAttr("llmgw_model.qwen", "owner", "team:Acme/ml"),
|
|
resource.TestCheckResourceAttr("llmgw_model.qwen", "owner_id", "team:t1"),
|
|
inCluster(func(m client.Model) error {
|
|
if m.Scaling.Min != 0 || m.Scaling.Max != 2 || m.Class != "opportunistic" || m.Owner != "team:t1" {
|
|
return fmt.Errorf("changed to %+v", m)
|
|
}
|
|
return nil
|
|
}),
|
|
),
|
|
},
|
|
{
|
|
// Scaled from a terminal: the next plan puts it back, in place.
|
|
PreConfig: func() { api.ScaleBehindTerraform("qwen-small", 3, 3) },
|
|
Config: cfg + modelV2,
|
|
ConfigPlanChecks: resource.ConfigPlanChecks{PreApply: []plancheck.PlanCheck{
|
|
plancheck.ExpectResourceAction("llmgw_model.qwen", plancheck.ResourceActionUpdate),
|
|
}},
|
|
Check: inCluster(func(m client.Model) error {
|
|
if m.Scaling.Min != 0 || m.Scaling.Max != 2 {
|
|
return fmt.Errorf("not put back: %+v", m.Scaling)
|
|
}
|
|
return nil
|
|
}),
|
|
},
|
|
{
|
|
Config: cfg + modelV3,
|
|
ConfigPlanChecks: resource.ConfigPlanChecks{PreApply: []plancheck.PlanCheck{
|
|
plancheck.ExpectResourceAction("llmgw_model.qwen", plancheck.ResourceActionDestroyBeforeCreate),
|
|
}},
|
|
Check: inCluster(func(m client.Model) error {
|
|
if m.Profiles[0].MinMemoryGB != 6 || m.Owner != "team:t1" {
|
|
return fmt.Errorf("replaced as %+v", m)
|
|
}
|
|
return nil
|
|
}),
|
|
},
|
|
{
|
|
// Removed from a terminal: planned again.
|
|
PreConfig: func() { api.RemoveBehindTerraform("qwen-small") },
|
|
Config: cfg + modelV3,
|
|
ConfigPlanChecks: resource.ConfigPlanChecks{PreApply: []plancheck.PlanCheck{
|
|
plancheck.ExpectResourceAction("llmgw_model.qwen", plancheck.ResourceActionCreate),
|
|
}},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccAQuotaIsSetChangedImportedAndCleared(t *testing.T) {
|
|
api, cfg := fake(t)
|
|
resource.Test(t, resource.TestCase{
|
|
ProtoV6ProviderFactories: factories,
|
|
CheckDestroy: func(*terraform.State) error {
|
|
if _, ok := api.Quota("team:t1"); ok {
|
|
return fmt.Errorf("the quota is still set")
|
|
}
|
|
return nil
|
|
},
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: cfg + `
|
|
resource "llmgw_quota" "ml" {
|
|
scope = "team:Acme/ml"
|
|
limit = { gpus = 8 }
|
|
}
|
|
`,
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
resource.TestCheckResourceAttr("llmgw_quota.ml", "id", "team:t1"),
|
|
resource.TestCheckResourceAttr("llmgw_quota.ml", "name", "Acme / ml"),
|
|
resource.TestCheckResourceAttr("llmgw_quota.ml", "limit.gpus", "8"),
|
|
),
|
|
},
|
|
{
|
|
Config: cfg + `
|
|
resource "llmgw_quota" "ml" {
|
|
scope = "team:Acme/ml"
|
|
guarantee = { gpus = 2 }
|
|
limit = { gpus = 8, memory_gb = 256 }
|
|
}
|
|
`,
|
|
ConfigPlanChecks: resource.ConfigPlanChecks{PreApply: []plancheck.PlanCheck{
|
|
plancheck.ExpectResourceAction("llmgw_quota.ml", plancheck.ResourceActionUpdate),
|
|
}},
|
|
Check: func(*terraform.State) error {
|
|
q, ok := api.Quota("team:t1")
|
|
if !ok || q.Guarantee.GPUs == nil || *q.Guarantee.GPUs != 2 || q.Limit.MemoryGB == nil || *q.Limit.MemoryGB != 256 {
|
|
return fmt.Errorf("quota %+v", q)
|
|
}
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
ResourceName: "llmgw_quota.ml",
|
|
ImportState: true,
|
|
ImportStateId: "team:t1",
|
|
ImportStateVerify: true,
|
|
// Imported by its ID, which is then the only name it has.
|
|
ImportStateVerifyIgnore: []string{"scope"},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccAPolicyIsAttachedImportedAndDetached(t *testing.T) {
|
|
api, cfg := fake(t)
|
|
resource.Test(t, resource.TestCase{
|
|
ProtoV6ProviderFactories: factories,
|
|
CheckDestroy: func(*terraform.State) error {
|
|
if n := len(api.Attachments()); n != 0 {
|
|
return fmt.Errorf("%d attachments left", n)
|
|
}
|
|
return nil
|
|
},
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: cfg + `
|
|
resource "llmgw_guard_attachment" "ml" {
|
|
target = "team:Acme/ml"
|
|
policy = "finance"
|
|
}
|
|
`,
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
resource.TestCheckResourceAttr("llmgw_guard_attachment.ml", "id", "team:t1:finance"),
|
|
resource.TestCheckResourceAttr("llmgw_guard_attachment.ml", "scope_id", "t1"),
|
|
),
|
|
},
|
|
{
|
|
ResourceName: "llmgw_guard_attachment.ml",
|
|
ImportState: true,
|
|
ImportStateVerify: true,
|
|
ImportStateVerifyIgnore: []string{"target"},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccTheModelsAndNodesAreRead(t *testing.T) {
|
|
_, cfg := fake(t)
|
|
resource.Test(t, resource.TestCase{
|
|
ProtoV6ProviderFactories: factories,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: cfg + modelV1 + `
|
|
data "llmgw_nodes" "all" {}
|
|
data "llmgw_models" "all" {
|
|
depends_on = [llmgw_model.qwen]
|
|
}
|
|
`,
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
resource.TestCheckResourceAttr("data.llmgw_nodes.all", "nodes.#", "2"),
|
|
resource.TestCheckResourceAttr("data.llmgw_nodes.all", "nodes.0.gpu_classes.0", "h100-80g"),
|
|
resource.TestCheckResourceAttr("data.llmgw_nodes.all", "nodes.1.state", "cordoned"),
|
|
resource.TestCheckResourceAttr("data.llmgw_models.all", "models.#", "1"),
|
|
resource.TestCheckResourceAttr("data.llmgw_models.all", "models.0.name", "qwen-small"),
|
|
resource.TestCheckResourceAttr("data.llmgw_models.all", "models.0.profiles.0", "cpu"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|