| 1 | package hostbox |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/hostclaim" |
| 12 | "github.com/carlosframework/platform/internal/registry" |
| 13 | ) |
| 14 | |
| 15 | func TestFreeCensusProvesAbsenceWithoutInitializing(t *testing.T) { |
| 16 | ctx := context.Background() |
| 17 | db, err := OpenStopInventoryDB(filepath.Join(t.TempDir(), "inventory.db")) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | defer db.Close() |
| 22 | reg, err := registry.Open(filepath.Join(t.TempDir(), "registry.db")) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | defer reg.Close() |
| 27 | launches, err := OpenLaunchJournal(filepath.Join(t.TempDir(), "launches.db")) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | defer launches.Close() |
| 32 | c := EmptyInstallationCensus{Inventory: db, Registry: reg, Launches: launches, Box: "box", Enrollment: "enrollment", DirectoryRevision: "revision", Roots: map[string]string{"data": t.TempDir(), "opt": t.TempDir(), "units": t.TempDir()}} |
| 33 | obs := hostclaim.Observed{Host: "shop.example", Claim: &hostclaim.Claim{V: 2, Host: "shop.example", Gen: 1}} |
| 34 | selected := RecordSelection{Host: obs.Host, Incarnation: "prior-inc"} |
| 35 | if err = c.CheckLocal(ctx, obs, selected); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if err = c.Initialize(ctx, obs, selected); !errors.Is(err, ErrChanged) { |
| 39 | t.Fatalf("free claim initialized: %v", err) |
| 40 | } |
| 41 | if _, err = db.ReadStopInventory(ctx, obs.Host); !errors.Is(err, sql.ErrNoRows) { |
| 42 | t.Fatalf("cleanup census wrote inventory: %v", err) |
| 43 | } |
| 44 | // Prior complete inventory never suppresses physical examination for a Free claim. |
| 45 | if err = db.WriteStopInventory(ctx, StopInventory{Complete: true, Host: obs.Host, Incarnation: selected.Incarnation, InstallationStamp: 1, Box: "box", Enrollment: "enrollment", DirectoryRevision: "revision"}); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | path := filepath.Join(c.Roots["opt"], "unclassified") |
| 49 | if err = os.WriteFile(path, []byte("leftover"), 0600); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if err = c.CheckLocal(ctx, obs, selected); !errors.Is(err, ErrTypedInventory) { |
| 53 | t.Fatalf("complete metadata hid artifact: %v", err) |
| 54 | } |
| 55 | if err = os.Remove(path); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | if err = c.CheckLocal(ctx, obs, selected); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | obs.Claim.Op = hostclaim.Op{Kind: hostclaim.KindPurging, ID: "purge"} |
| 62 | if err = c.CheckLocal(ctx, obs, selected); err == nil { |
| 63 | t.Fatal("holderless active purge counted as Free") |
| 64 | } |
| 65 | } |
| 66 | |