Files
mesh-controller/internal/inventory/bindings_test.go
T
mesh-tools bfe7fe1530 Pin mesh-sdk at 14fd5cb6 (R4a's merge) and take the catalogue, overlay and validator from it, s to inventory (issue 1381, 4 of 6 import swaps)
go.mod and go.sum move to mesh-sdk 14fd5cb68fd1, the commit on its main that holds go/catalogue, go/overlay and go/validate (ADR 0453 clause 2); vendor/modules.txt follows in the last of the six commits.
2026-10-20 08:26:01 +00:00

77 lines
2.3 KiB
Go

//go:build integration
package inventory
import (
"testing"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// Where a consumer of data was bound is kept, and a move keeps where it was (novox/hq ADR 0232).
func TestABindingIsRecordedAndAMoveKeepsWhereItWas(t *testing.T) {
inv := fresh(t)
ctx := t.Context()
for _, n := range []string{"home", "anchor"} {
if _, err := inv.AddNode(ctx, n); err != nil {
t.Fatal(err)
}
}
home := catalogue.Chosen{Node: "home", Module: "store"}
anchor := catalogue.Chosen{Node: "anchor", Module: "store"}
if err := inv.RecordBindings(ctx, "home", []Binding{
{Consumer: "board", Provision: "postgres-database", Provider: home},
{Consumer: "game", Provision: "postgres-database", Provider: home},
}); err != nil {
t.Fatal(err)
}
got, err := inv.BindingsFor(ctx, "home")
if err != nil {
t.Fatal(err)
}
if got["board"]["postgres-database"] != home || got["game"]["postgres-database"] != home {
t.Fatalf("recorded %+v", got)
}
first, err := inv.Bindings(ctx)
if err != nil || len(first) != 2 {
t.Fatalf("%+v, %v", first, err)
}
// Sent again where it is: only marked sent.
if err := inv.RecordBindings(ctx, "home", []Binding{{Consumer: "board", Provision: "postgres-database", Provider: home}}); err != nil {
t.Fatal(err)
}
// Moved by a pin and sent: bound anew, with where it was.
if err := inv.RecordBindings(ctx, "home", []Binding{{Consumer: "game", Provision: "postgres-database", Provider: anchor}}); err != nil {
t.Fatal(err)
}
all, err := inv.Bindings(ctx)
if err != nil {
t.Fatal(err)
}
for _, b := range all {
switch b.Consumer {
case "board":
if b.Provider != home || b.MovedFrom != "" || !b.BoundAt.Equal(first[0].BoundAt) || b.Machine != "home" {
t.Errorf("a binding sent again where it is changed: %+v (was %+v)", b, first[0])
}
case "game":
if b.Provider != anchor || b.MovedFrom != "home/store" || !b.BoundAt.After(first[1].BoundAt) {
t.Errorf("a moved binding: %+v", b)
}
}
}
// A provider machine leaving keeps the record of where the data is.
if _, err := inv.store.Pool().Exec(ctx, `delete from node where name = 'anchor'`); err != nil {
t.Fatal(err)
}
got, err = inv.BindingsFor(ctx, "home")
if err != nil {
t.Fatal(err)
}
if got["game"]["postgres-database"] != anchor {
t.Fatalf("the record went with the provider's machine: %+v", got)
}
}