Files
mesh-controller/cmd/mesh-controller/registry_verbs_integration_test.go
T

76 lines
2.8 KiB
Go

//go:build integration
package main
import (
"context"
"fmt"
"slices"
"strings"
"testing"
"time"
"github.com/novox/mesh-controller/internal/inventory"
)
func TestADryRunCollectAsksOnlyAndChangesNothing(t *testing.T) {
f := &fakeStore{}
store := f.serve(t)
kept := []string{archiveRef("web", "tools", 1)}
eligible := []string{ref("web", "app", 2), archiveRef("web", "tools", 3)}
a := runCollect(context.Background(), nil, store, eligible, kept, false,
sweepBounds{most: 10, budget: 5 * time.Second})
if !a.DryRun || a.Eligible != 2 || !slices.Equal(a.WouldLetGo, eligible) || len(a.LetGo) != 0 {
t.Errorf("a dry run answered %+v", a)
}
if a.KeptArchives != 1 || a.Held != 0 || a.Unheld != 1 {
t.Errorf("the kept archive is unheld in the fake store, and was said as %+v", a)
}
for _, r := range f.seen() {
if !strings.HasPrefix(r, "HEAD ") {
t.Errorf("a dry run asked the store %s", r)
}
}
}
func TestARealCollectHoldsEveryKeptArchiveBeforeItLetsAnythingGo(t *testing.T) {
inv := inventory.ForTest(t)
f := &fakeStore{}
store := f.serve(t)
kept := []string{archiveRef("web", "tools", 1)}
eligible := []string{ref("web", "app", 2), ref("web", "app", 3), ref("web", "app", 4)}
madeBy(t, inv, eligible)
a := runCollect(t.Context(), inv, store, eligible, kept, true, sweepBounds{most: 2, budget: 5 * time.Second})
if a.DryRun || a.HoldersWritten != 1 || a.Held != 1 {
t.Errorf("the kept archive was not held first: %+v", a)
}
if !slices.Equal(a.LetGo, eligible[:2]) || a.Left != 1 || a.Stopped == "" {
t.Errorf("bounded at two: let go %v, left %d, stopped %q", a.LetGo, a.Left, a.Stopped)
}
requests := f.seen()
firstPut := slices.IndexFunc(requests, func(r string) bool { return strings.HasPrefix(r, "PUT ") && strings.Contains(r, "/manifests/") })
firstDelete := slices.IndexFunc(requests, func(r string) bool { return strings.HasPrefix(r, "DELETE ") })
if firstPut < 0 || firstDelete < 0 || firstPut > firstDelete {
t.Errorf("the holder was not put before the first delete: %v", requests)
}
// What was let go is recorded collected: the sweep no longer offers it.
left, err := inv.ToCollect(t.Context())
if err != nil {
t.Fatal(err)
}
if !slices.Equal(left, eligible[2:]) {
t.Errorf("after letting go of two, the records offer %v", left)
}
}
func TestARealCollectStopsAtTheStoresFirstRefusal(t *testing.T) {
inv := inventory.ForTest(t)
f := &fakeStore{refuse: fmt.Sprintf("%064x", 3)}
store := f.serve(t)
eligible := []string{ref("web", "app", 2), ref("web", "app", 3), ref("web", "app", 4)}
a := runCollect(t.Context(), inv, store, eligible, nil, true, sweepBounds{most: 10, budget: 5 * time.Second})
if !slices.Equal(a.LetGo, eligible[:1]) || a.Left != 2 || !strings.Contains(a.Stopped, "kept") {
t.Errorf("after a refusal: let go %v, left %d, stopped %q", a.LetGo, a.Left, a.Stopped)
}
}