oficina / home Public

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

Plain git — no account needed to clone.

Download

Download this file

1package operator
2
3import (
4 "bytes"
5 "context"
6 "path/filepath"
7 "testing"
8 "testing/fstest"
9 "time"
10
11 sharedseal "amadan.net/oficina/home/handoff/seal"
12 rdb "amadan.net/rastrillo/rastrillo/db"
13 "amadan.net/rastrillo/rastrillo/migrate"
14)
15
16func TestExistingPlanMigrationPreservesOldPendingAndOpen(t *testing.T) {
17 for _, role := range []string{"credential", "directory"} {
18 t.Run(role, func(t *testing.T) {
19 ctx := context.Background()
20 db, err := rdb.Open(filepath.Join(t.TempDir(), "old.db"), nil)
21 if err != nil {
22 t.Fatal(err)
23 }
24 defer db.Close()
25 old, err := httpMigrations.ReadFile("http_migrations/" + role + "/migrations/0001_operator_http.sql")
26 if err != nil {
27 t.Fatal(err)
28 }
29 fs := fstest.MapFS{"migrations/0001_operator_http.sql": &fstest.MapFile{Data: old}}
30 if _, err = migrate.Apply(ctx, db, migrate.MustFromFS(fs, "oficina_"+role+"_operator_http")); err != nil {
31 t.Fatal(err)
32 }
33 now := time.Unix(2000000000, 0)
34 for i, state := range []string{"pending", "open"} {
35 var window any
36 if state == "open" {
37 window = now.Unix() + 600
38 }
39 if role == "credential" {
40 _, err = db.Writer().Exec("INSERT INTO operator_plans(plan_id,state,kind,apps,expires_at,window_expires_at) VALUES(?,?,'add-apps','docs',?,?)", token(byte(i+20)), state, now.Unix()+600, window)
41 } else {
42 _, err = db.Writer().Exec("INSERT INTO operator_plans(plan_id,state,kind,label,apps,expires_at,window_expires_at) VALUES(?,?,'add-apps','alpha','docs',?,?)", token(byte(i+20)), state, now.Unix()+600, window)
43 }
44 if err != nil {
45 t.Fatal(err)
46 }
47 }
48 if _, err = migrate.Apply(ctx, db, httpSchema(role)); err != nil {
49 t.Fatal("upgrade", err)
50 }
51 sealer, _ := sharedseal.New(bytes.Repeat([]byte{6}, 32), "oficina/operator")
52 store, _ := NewPlanStore(db.Writer(), role, sealer)
53 pending, err := store.Pending(ctx, now)
54 if err != nil || pending.ExistingTeam != "" || pending.Kind != "add-apps" {
55 t.Fatal("old plan changed", err)
56 }
57 conn, err := db.Writer().Conn(ctx)
58 if err != nil {
59 t.Fatal(err)
60 }
61 opened, _, err := window(ctx, conn, role, now.Unix())
62 conn.Close()
63 if err != nil || opened.ExistingTeam != "" || opened.State != "open" {
64 t.Fatal("old window changed", err)
65 }
66 if err = store.Sweep(ctx, now.Add(600*time.Second)); err != nil || countRows(t, db.Writer(), "operator_plans") != 0 {
67 t.Fatal("expiry", err)
68 }
69 })
70 }
71}
72