TFVars wrote vmid_base and hostpci, which only the Proxmox environment understands. A second backend would have had to declare and ignore them. The variables the runner writes are now listed in provision.Contract and say what the cluster is, not how Proxmox builds it: devices replaces hostpci, and the Proxmox environment derives vmid_base from cluster_id itself (still overridable, which `hpcod provision -vmid-base` now does). A test holds every terraform/envs/*/variables.tf to the contract and TFVars to nothing outside it. Checked by planning the Proxmox environment offline for the same cluster before and after: the planned resources and outputs are byte-identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
172 lines
6.6 KiB
Go
172 lines
6.6 KiB
Go
package provision
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/ilkermanap/hpcod/internal/store"
|
|
)
|
|
|
|
func TestRenderTFVarsCarriesThePlacement(t *testing.T) {
|
|
c := store.Cluster{SerialID: 7, ClusterID: 3, TenantID: 2, Site: "hv01", AccountingName: "c003-7"}
|
|
vms := []store.VM{
|
|
{Role: "head", NodeIndex: 0, HostNode: "hv01", Cores: 2, MemoryGB: 4},
|
|
{Role: "compute", NodeIndex: 1, HostNode: "pve2", Cores: 4, MemoryGB: 10,
|
|
Devices: []store.Device{{NativeID: "0000:01:00.0", Capability: "gpu"}}},
|
|
{Role: "compute", NodeIndex: 2, HostNode: "hv03", Cores: 4, MemoryGB: 10},
|
|
}
|
|
|
|
got, err := TFVars(c, vms, nil, "acme", "10.0.0.63/24")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, want := range []string{
|
|
`cluster_id = 3`,
|
|
`tenant_name = "acme"`,
|
|
`head_host = "hv01"`,
|
|
`compute_hosts = ["pve2", "hv03"]`,
|
|
`"1" = ["0000:01:00.0"]`,
|
|
`compute = { count = 2, cores = 4, memory_gb = 10 }`,
|
|
} {
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("missing %q in:\n%s", want, got)
|
|
}
|
|
}
|
|
// The second compute node has no card and must not acquire one.
|
|
if strings.Contains(got, `"2" = [`) {
|
|
t.Errorf("a device was invented for compute2:\n%s", got)
|
|
}
|
|
}
|
|
|
|
func TestRenderTFVarsOrdersComputeNodesByIndex(t *testing.T) {
|
|
c := store.Cluster{SerialID: 1, ClusterID: 1, TenantID: 1}
|
|
vms := []store.VM{
|
|
{Role: "compute", NodeIndex: 2, HostNode: "pveB", Cores: 1, MemoryGB: 1},
|
|
{Role: "head", NodeIndex: 0, HostNode: "hv01", Cores: 1, MemoryGB: 1},
|
|
{Role: "compute", NodeIndex: 1, HostNode: "pveA", Cores: 1, MemoryGB: 1},
|
|
}
|
|
got, err := TFVars(c, vms, nil, "t", "10.0.0.1/24")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The module indexes positionally, so the order here is which node gets
|
|
// which hypervisor - and which card.
|
|
if !strings.Contains(got, `compute_hosts = ["pveA", "pveB"]`) {
|
|
t.Errorf("compute hosts came out in the wrong order:\n%s", got)
|
|
}
|
|
}
|
|
|
|
// A gap in the indexes would shift every later node by one, quietly moving
|
|
// a GPU to a node that was not given one.
|
|
func TestRenderTFVarsRefusesGappyIndexes(t *testing.T) {
|
|
c := store.Cluster{SerialID: 1, ClusterID: 1, TenantID: 1}
|
|
vms := []store.VM{
|
|
{Role: "head", NodeIndex: 0, HostNode: "hv01", Cores: 1, MemoryGB: 1},
|
|
{Role: "compute", NodeIndex: 1, HostNode: "hv01", Cores: 1, MemoryGB: 1},
|
|
{Role: "compute", NodeIndex: 3, HostNode: "hv01", Cores: 1, MemoryGB: 1},
|
|
}
|
|
if _, err := TFVars(c, vms, nil, "t", "10.0.0.1/24"); err == nil {
|
|
t.Error("accepted compute nodes indexed 1 and 3")
|
|
}
|
|
}
|
|
|
|
func TestRenderTFVarsNeedsBothRoles(t *testing.T) {
|
|
c := store.Cluster{SerialID: 1, ClusterID: 1, TenantID: 1}
|
|
head := store.VM{Role: "head", NodeIndex: 0, HostNode: "hv01", Cores: 1, MemoryGB: 1}
|
|
comp := store.VM{Role: "compute", NodeIndex: 1, HostNode: "hv01", Cores: 1, MemoryGB: 1}
|
|
|
|
if _, err := TFVars(c, []store.VM{head}, nil, "t", "10.0.0.1/24"); err == nil {
|
|
t.Error("accepted a cluster with no compute nodes")
|
|
}
|
|
if _, err := TFVars(c, []store.VM{comp}, nil, "t", "10.0.0.1/24"); err == nil {
|
|
t.Error("accepted a cluster with no head")
|
|
}
|
|
}
|
|
|
|
func TestQuoteEscapes(t *testing.T) {
|
|
if got := quote(`a"b`); got != `"a\"b"` {
|
|
t.Errorf("quote = %s", got)
|
|
}
|
|
}
|
|
|
|
// Site settings must not appear in the generated file. Defined in two
|
|
// places, which one wins becomes a question of filename ordering.
|
|
func TestRenderTFVarsLeavesSiteSettingsAlone(t *testing.T) {
|
|
c := store.Cluster{SerialID: 1, ClusterID: 1, TenantID: 1}
|
|
vms := []store.VM{
|
|
{Role: "head", NodeIndex: 0, HostNode: "hv01", Cores: 1, MemoryGB: 1},
|
|
{Role: "compute", NodeIndex: 1, HostNode: "hv01", Cores: 1, MemoryGB: 1},
|
|
}
|
|
got, err := TFVars(c, vms, nil, "t", "10.0.0.1/24")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, unwanted := range []string{"node_name", "mgmt_gateway", "datastore_id", "pve_token", "ssh_public_keys"} {
|
|
if strings.Contains(got, unwanted) {
|
|
t.Errorf("%q is a site setting and must not be generated:\n%s", unwanted, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The regression this whole change exists for. Every layer below this one -
|
|
// the Terraform variable, the vendor-data template, the apps Ansible role -
|
|
// was already correct and already unreachable, because nothing ever wrote
|
|
// the variable. A cluster was built with the module's own empty default and
|
|
// reported ready, and the only sign was a user finding /opt/apps empty.
|
|
func TestRenderTFVarsCarriesTheApplications(t *testing.T) {
|
|
c := store.Cluster{SerialID: 7, ClusterID: 3, TenantID: 2, Site: "hv01"}
|
|
vms := []store.VM{
|
|
{Role: "head", NodeIndex: 0, HostNode: "hv01", Cores: 2, MemoryGB: 4},
|
|
{Role: "compute", NodeIndex: 1, HostNode: "hv01", Cores: 4, MemoryGB: 8},
|
|
}
|
|
apps := []store.ClusterApp{
|
|
{Name: "gromacs", Image: "oras://gitea.mynodes.xyz/hpcapps/gromacs:2024.4",
|
|
Digest: "sha256:aaa", SIFSHA256: "bbb",
|
|
Version: "2024.4", Module: "gromacs", Binaries: []string{"gmx_mpi", "gmx"}},
|
|
// Published before the hashes were recorded. It must still be
|
|
// emitted, with empty strings, so the head can tell "not recorded"
|
|
// from "not passed".
|
|
{Name: "busybox", Image: "docker://docker.io/library/busybox:latest"},
|
|
}
|
|
|
|
got, err := TFVars(c, vms, apps, "acme", "10.0.0.63/24")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, want := range []string{
|
|
`apps = [`,
|
|
`{ name = "gromacs", version = "2024.4", image = "oras://gitea.mynodes.xyz/hpcapps/gromacs:2024.4", digest = "sha256:aaa", sif_sha256 = "bbb", module = "gromacs", binaries = ["gmx_mpi", "gmx"] },`,
|
|
`{ name = "busybox", version = "", image = "docker://docker.io/library/busybox:latest", digest = "", sif_sha256 = "", module = "", binaries = [] },`,
|
|
} {
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("missing %q in:\n%s", want, got)
|
|
}
|
|
}
|
|
// Order is the catalogue's, not the map iteration's: a rebuild that
|
|
// reordered these would rewrite the cloud-init document and look like
|
|
// a change to Terraform every time.
|
|
if strings.Index(got, `"gromacs"`) > strings.Index(got, `"busybox"`) {
|
|
t.Errorf("applications came out reordered:\n%s", got)
|
|
}
|
|
}
|
|
|
|
// An empty list is written rather than omitted. Omitting it is what the code
|
|
// did before, and it is indistinguishable at the Terraform end from a
|
|
// cluster that asked for nothing - which is why every cluster looked like
|
|
// one that had asked for nothing.
|
|
func TestRenderTFVarsWritesAppsEvenWhenEmpty(t *testing.T) {
|
|
c := store.Cluster{SerialID: 1, ClusterID: 1, TenantID: 1}
|
|
vms := []store.VM{
|
|
{Role: "head", NodeIndex: 0, HostNode: "hv01", Cores: 1, MemoryGB: 1},
|
|
{Role: "compute", NodeIndex: 1, HostNode: "hv01", Cores: 1, MemoryGB: 1},
|
|
}
|
|
got, err := TFVars(c, vms, nil, "t", "10.0.0.1/24")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(got, "apps = []") {
|
|
t.Errorf("an empty application list was left out rather than stated:\n%s", got)
|
|
}
|
|
}
|