package provision import ( "bytes" "context" "path/filepath" "strings" "testing" "time" ) func adoptStore(t *testing.T) *Store { t.Helper() db, err := OpenDB(filepath.Join(t.TempDir(), "alta.db")) if err != nil { t.Fatal(err) } t.Cleanup(func() { db.Close() }) s, err := NewStore(context.Background(), db, bytes.Repeat([]byte{4}, 32), func() time.Time { return time.Unix(2_000_000_000, 0) }) if err != nil { t.Fatal(err) } return s } // The six existing instances keep the environments they run in, which // Alta never created: an adoption team owns none, so the ยง8 guard admits // no environment write or read for any app under it. func TestCreateAdoptionWritesNoEnvironmentRows(t *testing.T) { ctx := context.Background() s := adoptStore(t) if err := s.CreateAdoption(ctx, "Oficina", ownerCanary, Slugs()); err != nil { t.Fatal(err) } var n int if err := s.DB().QueryRowContext(ctx, "SELECT count(*) FROM environments").Scan(&n); err != nil || n != 0 { t.Fatalf("environments rows %d, %v", n, err) } team, err := s.Team(ctx, AdoptionLabel) if err != nil { t.Fatal(err) } if team.Name != "Oficina" || team.Environment != "" || team.Forked || len(team.Apps) != len(Apps) { t.Fatalf("team %+v", team) } for _, a := range Apps { if team.Apps[a.Slug] != "active" { t.Fatalf("%s state %q", a.Slug, team.Apps[a.Slug]) } } if owner, ok, err := s.Owner(ctx, AdoptionLabel); err != nil || !ok || owner != ownerCanary { t.Fatalf("owner %v %v", ok, err) } g := Guard{Store: s, Account: account} for _, a := range Apps { for _, env := range []string{"", AdoptionLabel, "production", a.Slug} { for _, write := range []bool{false, true} { if err := g.Environment(ctx, a.Slug, env, write); err == nil { t.Fatalf("guard admitted %s environment %q (write %v)", a.Slug, env, write) } } } } // The guard is not refusing everything: an ordinary team's own // environment is still admitted. if err := s.CreateTeam(ctx, "acme", "Acme", "acme-env", false, ownerCanary, []string{"docs"}); err != nil { t.Fatal(err) } if err := g.Environment(ctx, "docs", "acme-env", true); err != nil { t.Fatalf("ordinary environment refused: %v", err) } } func TestCreateAdoptionRefusesBadInput(t *testing.T) { ctx := context.Background() for name, apps := range map[string][]string{ "none": nil, "unknown": {"docs", "mail"}, "duplicate": {"docs", "docs"}, } { s := adoptStore(t) if err := s.CreateAdoption(ctx, "Oficina", ownerCanary, apps); err == nil { t.Fatalf("%s: accepted %v", name, apps) } if used, _ := s.LabelUsed(ctx, AdoptionLabel); used { t.Fatalf("%s: a refused adoption recorded the label", name) } } s := adoptStore(t) if err := s.CreateAdoption(ctx, "Oficina", ownerCanary, []string{"calendar"}); err != nil { t.Fatal(err) } if err := s.CreateAdoption(ctx, "Oficina", ownerCanary, []string{"calendar"}); err == nil { t.Fatal("a second adoption was recorded") } } func TestAdoptionRunColumnScanned(t *testing.T) { ctx := context.Background() s := adoptStore(t) ordinary, err := s.NewRun(ctx, "create-team", "acme", []string{"docs"}, RunOptions{Profile: ProfileOutside}) if err != nil { t.Fatal(err) } id, err := s.NewAdoptionRun(ctx, "create-team", Slugs()) if err != nil { t.Fatal(err) } r, err := s.Run(ctx, id) if err != nil { t.Fatal(err) } if !r.Adoption || r.Label != AdoptionLabel || r.Kind != "create-team" || strings.Join(r.Apps, ",") != strings.Join(Slugs(), ",") || r.State != "planned" { t.Fatalf("adoption run %+v", r) } if r.Options != (RunOptions{Profile: ProfileOutside}) { t.Fatalf("adoption run options %+v", r.Options) } var raw int if err := s.DB().QueryRowContext(ctx, "SELECT adoption FROM runs WHERE id=?", id).Scan(&raw); err != nil || raw != 1 { t.Fatalf("adoption column %d, %v", raw, err) } if o, err := s.Run(ctx, ordinary); err != nil || o.Adoption { t.Fatalf("ordinary run %+v, %v", o, err) } runs, err := s.Runs(ctx, AdoptionLabel) if err != nil || len(runs) != 1 || !runs[0].Adoption { t.Fatalf("runs %+v, %v", runs, err) } live, err := s.LiveRuns(ctx) if err != nil || len(live) != 2 || live[0].Adoption || !live[1].Adoption { t.Fatalf("live runs %+v, %v", live, err) } // One live plan per team holds for adoption runs too. next, err := s.NewAdoptionRun(ctx, "add-apps", []string{"calendar"}) if err != nil { t.Fatal(err) } if r, _ := s.Run(ctx, id); r.State != "superseded" { t.Fatalf("earlier adoption run %s", r.State) } if r, _ := s.Run(ctx, next); !r.Adoption || r.Kind != "add-apps" { t.Fatalf("add-apps run %+v", r) } } func TestNewAdoptionRunRefusesOtherKindsAndApps(t *testing.T) { ctx := context.Background() s := adoptStore(t) for _, kind := range []string{"initialize", "retire-apps", ""} { if _, err := s.NewAdoptionRun(ctx, kind, []string{"calendar"}); err == nil { t.Fatalf("adoption run of kind %q", kind) } } for _, apps := range [][]string{nil, {"mail"}, {"docs", "docs"}} { if _, err := s.NewAdoptionRun(ctx, "add-apps", apps); err == nil { t.Fatalf("adoption run for %v", apps) } } var n int if err := s.DB().QueryRowContext(ctx, "SELECT count(*) FROM runs").Scan(&n); err != nil || n != 0 { t.Fatalf("refused runs recorded %d, %v", n, err) } } // adopt seals its journal through the Store, and only under its own // purposes: it can never open Alta's owner address or plan bytes. func TestAdoptionSealIsConfinedToItsPurposes(t *testing.T) { ctx := context.Background() s := adoptStore(t) if err := s.CreateAdoption(ctx, "Oficina", ownerCanary, []string{"calendar"}); err != nil { t.Fatal(err) } var sealed []byte if err := s.DB().QueryRowContext(ctx, "SELECT sealed_address FROM owner_inputs WHERE label=?", AdoptionLabel).Scan(&sealed); err != nil { t.Fatal(err) } if _, err := s.OpenAdoption(ownerPurpose(AdoptionLabel), sealed); err == nil { t.Fatal("adopt opened the owner address") } if _, err := s.SealAdoption(ownerPurpose(AdoptionLabel), []byte("x")); err == nil { t.Fatal("adopt sealed under the owner purpose") } for _, p := range []string{"alta/adopt", "alta/adopt/", "alta/adoptx/t/k", "alta/owner/oficina"} { if _, err := s.SealAdoption(p, []byte("x")); err == nil { t.Fatalf("sealed under %q", p) } } b, err := s.SealAdoption("alta/adopt/t/k", []byte("x")) if err != nil { t.Fatal(err) } if got, err := s.OpenAdoption("alta/adopt/t/k", b); err != nil || string(got) != "x" { t.Fatalf("round trip %q, %v", got, err) } if _, err := s.OpenAdoption("alta/adopt/t/other", b); err == nil { t.Fatal("opened under another key's purpose") } }