carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1//go:build unix
2
3package hostbox
4
5import (
6 "context"
7 "errors"
8 "os"
9 "path/filepath"
10 "testing"
11 "time"
12
13 "github.com/carlosframework/platform/internal/hostclaim"
14 "github.com/carlosframework/platform/internal/hostguard"
15 "github.com/carlosframework/platform/internal/hostwrite"
16 "github.com/carlosframework/platform/internal/provision"
17 "github.com/carlosframework/platform/internal/registry"
18 "github.com/carlosframework/platform/internal/release"
19)
20
21// The production adapter, driven by the real reconcile passes, hands the
22// RecordSelection it derived to the record check. That check aborts the
23// install, so nothing is written; what it saw is the layout the claim
24// inventory would have been stamped with.
25func TestAdapterSelectionLayoutFollowsRoutePaths(t *testing.T) {
26 stop := errors.New("selection captured")
27 for _, tc := range []struct {
28 name string
29 sidecar bool
30 mode string
31 want string
32 }{
33 {"instance minted nested", false, release.LayoutModeNested, "nested"},
34 {"instance minted flat", false, release.LayoutModeFlat, "flat"},
35 {"sidecar stays flat on a nested box", true, release.LayoutModeNested, "flat"},
36 } {
37 t.Run(tc.name, func(t *testing.T) {
38 ctx := t.Context()
39 f := newAdmissionFixture(t, tc.sidecar)
40 reg, err := registry.Open(filepath.Join(t.TempDir(), "registry.db"))
41 if err != nil {
42 t.Fatal(err)
43 }
44 defer reg.Close()
45 inventory, err := OpenStopInventoryDB(filepath.Join(t.TempDir(), "inventory.db"))
46 if err != nil {
47 t.Fatal(err)
48 }
49 defer inventory.Close()
50 var seen []RecordSelection
51 adapter := ReconcileAdapter{
52 Registry: reg, Inventory: inventory, Box: "box", Enrollment: "en", DirectoryRevision: "1",
53 Reconciler: Reconciler{
54 Claims: f.store, Box: "box", Region: "ie",
55 Guard: hostguard.Guard{DataDir: f.data, LockDir: t.TempDir(), Claims: hostguard.StoreReader{Store: f.store}},
56 Writes: hostwrite.Config{Enabled: true},
57 Now: func() time.Time { return time.Unix(100, 0) },
58 CheckRecord: func(_ context.Context, s RecordSelection) error { seen = append(seen, s); return stop },
59 CheckLocal: func(context.Context, hostclaim.Observed, RecordSelection) error { return nil },
60 },
61 }
62 cfg := provision.ReconcileConfig{DataDir: f.data, Region: "ie"}
63 cfg.Layout = func(context.Context, release.Store) (string, error) { return tc.mode, nil }
64 adapter.Configure(&cfg)
65 if !cfg.ClaimEnabled {
66 t.Fatal("adapter did not enable claim admission")
67 }
68 if err := f.pass(ctx, f.store, reg, cfg); err != nil {
69 t.Fatal(err)
70 }
71 if len(seen) != 1 {
72 t.Fatalf("record check ran %d times, want 1", len(seen))
73 }
74 if seen[0].Layout != tc.want || seen[0].Host != f.host || seen[0].Family != f.family {
75 t.Fatalf("selection = %+v, want layout %q for %s %s", seen[0], tc.want, f.family, f.host)
76 }
77 if row, err := reg.Get(f.host); err != nil || row != nil {
78 t.Fatalf("aborted install wrote a route: %v", err)
79 }
80 if _, err := os.Stat(filepath.Join(f.data, "hostguard", f.host+".json")); !errors.Is(err, os.ErrNotExist) {
81 t.Fatalf("aborted install activated the guard: %v", err)
82 }
83 })
84 }
85}
86