oficina / alta Public

Clone
git clone https://amadan.net/oficina/alta

Plain git — no account needed to clone.

Download

Download this file

1package provision
2
3import (
4 "bytes"
5 "context"
6 "path/filepath"
7 "strings"
8 "testing"
9 "time"
10)
11
12func adoptStore(t *testing.T) *Store {
13 t.Helper()
14 db, err := OpenDB(filepath.Join(t.TempDir(), "alta.db"))
15 if err != nil {
16 t.Fatal(err)
17 }
18 t.Cleanup(func() { db.Close() })
19 s, err := NewStore(context.Background(), db, bytes.Repeat([]byte{4}, 32), func() time.Time { return time.Unix(2_000_000_000, 0) })
20 if err != nil {
21 t.Fatal(err)
22 }
23 return s
24}
25
26// The six existing instances keep the environments they run in, which
27// Alta never created: an adoption team owns none, so the §8 guard admits
28// no environment write or read for any app under it.
29func TestCreateAdoptionWritesNoEnvironmentRows(t *testing.T) {
30 ctx := context.Background()
31 s := adoptStore(t)
32 if err := s.CreateAdoption(ctx, "Oficina", ownerCanary, Slugs()); err != nil {
33 t.Fatal(err)
34 }
35 var n int
36 if err := s.DB().QueryRowContext(ctx, "SELECT count(*) FROM environments").Scan(&n); err != nil || n != 0 {
37 t.Fatalf("environments rows %d, %v", n, err)
38 }
39 team, err := s.Team(ctx, AdoptionLabel)
40 if err != nil {
41 t.Fatal(err)
42 }
43 if team.Name != "Oficina" || team.Environment != "" || team.Forked || len(team.Apps) != len(Apps) {
44 t.Fatalf("team %+v", team)
45 }
46 for _, a := range Apps {
47 if team.Apps[a.Slug] != "active" {
48 t.Fatalf("%s state %q", a.Slug, team.Apps[a.Slug])
49 }
50 }
51 if owner, ok, err := s.Owner(ctx, AdoptionLabel); err != nil || !ok || owner != ownerCanary {
52 t.Fatalf("owner %v %v", ok, err)
53 }
54 g := Guard{Store: s, Account: account}
55 for _, a := range Apps {
56 for _, env := range []string{"", AdoptionLabel, "production", a.Slug} {
57 for _, write := range []bool{false, true} {
58 if err := g.Environment(ctx, a.Slug, env, write); err == nil {
59 t.Fatalf("guard admitted %s environment %q (write %v)", a.Slug, env, write)
60 }
61 }
62 }
63 }
64 // The guard is not refusing everything: an ordinary team's own
65 // environment is still admitted.
66 if err := s.CreateTeam(ctx, "acme", "Acme", "acme-env", false, ownerCanary, []string{"docs"}); err != nil {
67 t.Fatal(err)
68 }
69 if err := g.Environment(ctx, "docs", "acme-env", true); err != nil {
70 t.Fatalf("ordinary environment refused: %v", err)
71 }
72}
73
74func TestCreateAdoptionRefusesBadInput(t *testing.T) {
75 ctx := context.Background()
76 for name, apps := range map[string][]string{
77 "none": nil,
78 "unknown": {"docs", "mail"},
79 "duplicate": {"docs", "docs"},
80 } {
81 s := adoptStore(t)
82 if err := s.CreateAdoption(ctx, "Oficina", ownerCanary, apps); err == nil {
83 t.Fatalf("%s: accepted %v", name, apps)
84 }
85 if used, _ := s.LabelUsed(ctx, AdoptionLabel); used {
86 t.Fatalf("%s: a refused adoption recorded the label", name)
87 }
88 }
89 s := adoptStore(t)
90 if err := s.CreateAdoption(ctx, "Oficina", ownerCanary, []string{"calendar"}); err != nil {
91 t.Fatal(err)
92 }
93 if err := s.CreateAdoption(ctx, "Oficina", ownerCanary, []string{"calendar"}); err == nil {
94 t.Fatal("a second adoption was recorded")
95 }
96}
97
98func TestAdoptionRunColumnScanned(t *testing.T) {
99 ctx := context.Background()
100 s := adoptStore(t)
101 ordinary, err := s.NewRun(ctx, "create-team", "acme", []string{"docs"}, RunOptions{Profile: ProfileOutside})
102 if err != nil {
103 t.Fatal(err)
104 }
105 id, err := s.NewAdoptionRun(ctx, "create-team", Slugs())
106 if err != nil {
107 t.Fatal(err)
108 }
109 r, err := s.Run(ctx, id)
110 if err != nil {
111 t.Fatal(err)
112 }
113 if !r.Adoption || r.Label != AdoptionLabel || r.Kind != "create-team" || strings.Join(r.Apps, ",") != strings.Join(Slugs(), ",") || r.State != "planned" {
114 t.Fatalf("adoption run %+v", r)
115 }
116 if r.Options != (RunOptions{Profile: ProfileOutside}) {
117 t.Fatalf("adoption run options %+v", r.Options)
118 }
119 var raw int
120 if err := s.DB().QueryRowContext(ctx, "SELECT adoption FROM runs WHERE id=?", id).Scan(&raw); err != nil || raw != 1 {
121 t.Fatalf("adoption column %d, %v", raw, err)
122 }
123 if o, err := s.Run(ctx, ordinary); err != nil || o.Adoption {
124 t.Fatalf("ordinary run %+v, %v", o, err)
125 }
126 runs, err := s.Runs(ctx, AdoptionLabel)
127 if err != nil || len(runs) != 1 || !runs[0].Adoption {
128 t.Fatalf("runs %+v, %v", runs, err)
129 }
130 live, err := s.LiveRuns(ctx)
131 if err != nil || len(live) != 2 || live[0].Adoption || !live[1].Adoption {
132 t.Fatalf("live runs %+v, %v", live, err)
133 }
134 // One live plan per team holds for adoption runs too.
135 next, err := s.NewAdoptionRun(ctx, "add-apps", []string{"calendar"})
136 if err != nil {
137 t.Fatal(err)
138 }
139 if r, _ := s.Run(ctx, id); r.State != "superseded" {
140 t.Fatalf("earlier adoption run %s", r.State)
141 }
142 if r, _ := s.Run(ctx, next); !r.Adoption || r.Kind != "add-apps" {
143 t.Fatalf("add-apps run %+v", r)
144 }
145}
146
147func TestNewAdoptionRunRefusesOtherKindsAndApps(t *testing.T) {
148 ctx := context.Background()
149 s := adoptStore(t)
150 for _, kind := range []string{"initialize", "retire-apps", ""} {
151 if _, err := s.NewAdoptionRun(ctx, kind, []string{"calendar"}); err == nil {
152 t.Fatalf("adoption run of kind %q", kind)
153 }
154 }
155 for _, apps := range [][]string{nil, {"mail"}, {"docs", "docs"}} {
156 if _, err := s.NewAdoptionRun(ctx, "add-apps", apps); err == nil {
157 t.Fatalf("adoption run for %v", apps)
158 }
159 }
160 var n int
161 if err := s.DB().QueryRowContext(ctx, "SELECT count(*) FROM runs").Scan(&n); err != nil || n != 0 {
162 t.Fatalf("refused runs recorded %d, %v", n, err)
163 }
164}
165
166// adopt seals its journal through the Store, and only under its own
167// purposes: it can never open Alta's owner address or plan bytes.
168func TestAdoptionSealIsConfinedToItsPurposes(t *testing.T) {
169 ctx := context.Background()
170 s := adoptStore(t)
171 if err := s.CreateAdoption(ctx, "Oficina", ownerCanary, []string{"calendar"}); err != nil {
172 t.Fatal(err)
173 }
174 var sealed []byte
175 if err := s.DB().QueryRowContext(ctx, "SELECT sealed_address FROM owner_inputs WHERE label=?", AdoptionLabel).Scan(&sealed); err != nil {
176 t.Fatal(err)
177 }
178 if _, err := s.OpenAdoption(ownerPurpose(AdoptionLabel), sealed); err == nil {
179 t.Fatal("adopt opened the owner address")
180 }
181 if _, err := s.SealAdoption(ownerPurpose(AdoptionLabel), []byte("x")); err == nil {
182 t.Fatal("adopt sealed under the owner purpose")
183 }
184 for _, p := range []string{"alta/adopt", "alta/adopt/", "alta/adoptx/t/k", "alta/owner/oficina"} {
185 if _, err := s.SealAdoption(p, []byte("x")); err == nil {
186 t.Fatalf("sealed under %q", p)
187 }
188 }
189 b, err := s.SealAdoption("alta/adopt/t/k", []byte("x"))
190 if err != nil {
191 t.Fatal(err)
192 }
193 if got, err := s.OpenAdoption("alta/adopt/t/k", b); err != nil || string(got) != "x" {
194 t.Fatalf("round trip %q, %v", got, err)
195 }
196 if _, err := s.OpenAdoption("alta/adopt/t/other", b); err == nil {
197 t.Fatal("opened under another key's purpose")
198 }
199}
200