110 lines
3.3 KiB
Go
110 lines
3.3 KiB
Go
//go:build integration
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
"github.com/novox/mesh-controller/internal/broker"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
sdkdelivery "git.novox.be/novox/mesh-sdk/go/delivery"
|
|
"github.com/novox/mesh-controller/internal/builderkeys"
|
|
"github.com/novox/mesh-controller/internal/inventory"
|
|
"github.com/novox/mesh-controller/internal/link"
|
|
)
|
|
|
|
// Helpers the integration tests of the deleted plan machinery shared with tests that remain.
|
|
|
|
// noListener hears no node: these tests send merges only.
|
|
type noListener struct{ link.Listener }
|
|
|
|
func becomes(t *testing.T, within time.Duration, what string, is func() bool) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(within)
|
|
for time.Now().Before(deadline) {
|
|
if is() {
|
|
return
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
t.Fatalf("%s did not happen within %s", what, within)
|
|
}
|
|
|
|
// noBuildAsked once failed a test that asked build-agent for a build; nothing can now, so it says nothing.
|
|
func noBuildAsked(t *testing.T) { t.Helper() }
|
|
|
|
func twoTiers(t *testing.T, open *stores) {
|
|
t.Helper()
|
|
for _, name := range []string{"a", "b"} {
|
|
if err := open.inventory.RegisterModule(t.Context(), catalogue.Manifest{Module: name, Version: "1"},
|
|
inventory.Source{Repository: "novox/" + name, Seat: "git", Ref: "main", BuiltFrom: "c0ffee", Head: "c0ffee"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func inventoryHolds(t *testing.T, inv *inventory.Inventory, module string) (inventory.Entry, bool) {
|
|
t.Helper()
|
|
entries, err := inv.Catalogued(t.Context())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, e := range entries {
|
|
if e.Manifest.Module == module {
|
|
return e, true
|
|
}
|
|
}
|
|
return inventory.Entry{}, false
|
|
}
|
|
|
|
// planKeysOf is the indexer and the builder as a walk reads them, for a test: values by module and commit, keys by
|
|
// module.
|
|
type planKeysOf struct {
|
|
values map[string]sdkdelivery.Module // by module + "@" + commit
|
|
keys map[string]builderkeys.Key
|
|
}
|
|
|
|
func (k planKeysOf) ModuleAt(_ context.Context, module, commit string) (sdkdelivery.Module, error) {
|
|
if v, ok := k.values[module+"@"+commit]; ok {
|
|
return v, nil
|
|
}
|
|
return sdkdelivery.Module{}, builderkeys.ErrNoValue
|
|
}
|
|
|
|
func (k planKeysOf) Build(_ context.Context, module, fp string) (builderkeys.Key, error) {
|
|
if key, ok := k.keys[module]; ok && key.Fingerprint == fp {
|
|
return key, nil
|
|
}
|
|
return builderkeys.Key{Module: module, Fingerprint: fp}, nil
|
|
}
|
|
|
|
// putRunningHave writes a node-engine's have of a module: running and healthy, at a fingerprint and composition of its own.
|
|
func putRunningHave(t *testing.T, js *broker.JetStream, node, module string) {
|
|
t.Helper()
|
|
ctx := t.Context()
|
|
api, err := jetstream.New(js.Conn())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
kv, err := api.CreateOrUpdateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: sdkdelivery.HaveBucket})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key, err := sdkdelivery.HaveKey(node, module)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value, err := sdkdelivery.EncodeHave(sdkdelivery.Have{FP: sdkdelivery.SourcePrefix2 + strings.Repeat("a", 64),
|
|
Composed: "sha256:" + strings.Repeat("b", 64), Phase: sdkdelivery.PhaseRunning, Health: sdkdelivery.HealthOK})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := kv.Put(ctx, key, value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|