| 1 | package hostbox |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/hostclaim" |
| 6 | "github.com/carlosframework/platform/internal/registry" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestEmptyCensusRefusesUnclassifiedArtifactsAndNeverWritesInPrecheck(t *testing.T) { |
| 13 | ctx := context.Background() |
| 14 | base := t.TempDir() |
| 15 | db, err := OpenStopInventoryDB(filepath.Join(base, "inventory.db")) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | defer db.Close() |
| 20 | reg, err := registry.Open(filepath.Join(base, "registry.db")) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | defer reg.Close() |
| 25 | launches, err := OpenLaunchJournal(filepath.Join(base, "launch.db")) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | defer launches.Close() |
| 30 | roots := map[string]string{} |
| 31 | for _, name := range []string{"data", "opt", "units"} { |
| 32 | roots[name] = filepath.Join(base, name) |
| 33 | if err = os.Mkdir(roots[name], 0700); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | } |
| 37 | c := EmptyInstallationCensus{Inventory: db, Registry: reg, Launches: launches, Box: "box", Enrollment: "enrollment", DirectoryRevision: "revision", Roots: roots} |
| 38 | obs := hostclaim.Observed{Host: "shop.example", Claim: &hostclaim.Claim{Holder: &hostclaim.Holder{Incarnation: "inc", Stamp: 1}}} |
| 39 | selected := RecordSelection{Host: obs.Host, Incarnation: "inc"} |
| 40 | if err = c.CheckLocal(ctx, obs, selected); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if _, err = db.ReadInstallation(ctx, obs.Host, "inc", 1); err == nil { |
| 44 | t.Fatal("precheck wrote inventory") |
| 45 | } |
| 46 | if err = os.WriteFile(filepath.Join(roots["opt"], "unexplained"), []byte("old"), 0600); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if err = c.Initialize(ctx, obs, selected); err == nil { |
| 50 | t.Fatal("assigned unknown artifact to new installation") |
| 51 | } |
| 52 | if err = os.Remove(filepath.Join(roots["opt"], "unexplained")); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if err = c.Initialize(ctx, obs, selected); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | in, err := db.ReadInstallation(ctx, obs.Host, "inc", 1) |
| 59 | if err != nil || !in.Complete || len(in.Entries) != 0 { |
| 60 | t.Fatalf("missing explicit empty census: %+v %v", in, err) |
| 61 | } |
| 62 | obs.Claim.Holder.Stamp = 2 |
| 63 | if err = c.Initialize(ctx, obs, selected); err == nil { |
| 64 | t.Fatal("old ownership silently reassigned") |
| 65 | } |
| 66 | } |
| 67 | |