|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/open-source-cloud/devstack/internal/config" |
| 11 | + "github.com/open-source-cloud/devstack/internal/docker" |
| 12 | + "github.com/open-source-cloud/devstack/internal/generate" |
| 13 | + "github.com/open-source-cloud/devstack/internal/orchestrate" |
| 14 | + "github.com/open-source-cloud/devstack/internal/state" |
| 15 | + "github.com/open-source-cloud/devstack/internal/template" |
| 16 | + "github.com/open-source-cloud/devstack/internal/workspace" |
| 17 | + "github.com/open-source-cloud/devstack/templates" |
| 18 | +) |
| 19 | + |
| 20 | +// destroyFakeRunner records compose invocations so teardown can assert on them. |
| 21 | +type destroyFakeRunner struct{ cmds [][]string } |
| 22 | + |
| 23 | +func (f *destroyFakeRunner) Run(_ context.Context, _ []string, _, name string, args ...string) error { |
| 24 | + f.cmds = append(f.cmds, append([]string{name}, args...)) |
| 25 | + return nil |
| 26 | +} |
| 27 | +func (f *destroyFakeRunner) Output(_ context.Context, _ []string, _, name string, args ...string) ([]byte, error) { |
| 28 | + f.cmds = append(f.cmds, append([]string{name}, args...)) |
| 29 | + return nil, nil |
| 30 | +} |
| 31 | +func (f *destroyFakeRunner) saw(needles ...string) bool { |
| 32 | + for _, c := range f.cmds { |
| 33 | + joined := strings.Join(c, " ") |
| 34 | + all := true |
| 35 | + for _, n := range needles { |
| 36 | + if !strings.Contains(joined, n) { |
| 37 | + all = false |
| 38 | + break |
| 39 | + } |
| 40 | + } |
| 41 | + if all { |
| 42 | + return true |
| 43 | + } |
| 44 | + } |
| 45 | + return false |
| 46 | +} |
| 47 | + |
| 48 | +// destroyFixture builds a one-project workspace whose shared postgres is live and |
| 49 | +// referenced by the project, with a generated .devstack/ on disk. |
| 50 | +func destroyFixture(t *testing.T) (orchestrate.UpDeps, *destroyFakeRunner) { |
| 51 | + t.Helper() |
| 52 | + root := t.TempDir() |
| 53 | + write := func(rel, body string) { |
| 54 | + p := filepath.Join(root, rel) |
| 55 | + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + if err := os.WriteFile(p, []byte(body), 0o644); err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + } |
| 62 | + write("workspace.yaml", "apiVersion: devstack/v1\nkind: Workspace\nname: demo\nshared:\n postgres: { template: postgres, params: { version: \"16\" } }\nprojects:\n - { name: app, path: app }\n") |
| 63 | + write("app/devstack.yaml", "apiVersion: devstack/v1\nkind: Project\nname: app\nservices:\n web:\n template: node.vite\n uses: [workspace.shared.postgres]\n") |
| 64 | + // Generated artifacts that destroy must remove. |
| 65 | + write(filepath.Join("app", generate.GenDir, generate.ComposeFile), "services: {}\n") |
| 66 | + write(filepath.Join(generate.GenDir, "shared", generate.ComposeFile), "services: {}\n") |
| 67 | + |
| 68 | + m, err := config.LoadAt(root) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("load: %v", err) |
| 71 | + } |
| 72 | + db, err := state.Open(context.Background(), filepath.Join(root, "state"), "ctx") |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("state: %v", err) |
| 75 | + } |
| 76 | + t.Cleanup(func() { db.Close() }) |
| 77 | + |
| 78 | + mc := &docker.MockClient{ |
| 79 | + Context: "ctx", |
| 80 | + Containers: []docker.Container{{ |
| 81 | + ID: "pg1", Name: "devstack-shared-postgres-1", State: "running", |
| 82 | + Labels: map[string]string{generate.LabelManaged: "true", generate.LabelShared: "postgres"}, |
| 83 | + }}, |
| 84 | + } |
| 85 | + src := template.NewFSSource(templates.FS) |
| 86 | + lockPath := filepath.Join(root, "lock") |
| 87 | + fr := &destroyFakeRunner{} |
| 88 | + mgr := &workspace.Manager{Model: m, DB: db, Docker: mc, Source: src, LockPath: lockPath, Runner: fr} |
| 89 | + d := orchestrate.UpDeps{ |
| 90 | + Model: m, DB: db, Docker: mc, Manager: mgr, Source: src, LockPath: lockPath, Runner: fr, |
| 91 | + } |
| 92 | + return d, fr |
| 93 | +} |
| 94 | + |
| 95 | +func TestWorkspaceDestroyRegistered(t *testing.T) { |
| 96 | + root := NewRootCmd(Options{}) |
| 97 | + c, _, err := root.Find([]string{"workspace", "destroy"}) |
| 98 | + if err != nil || c.Name() != "destroy" || c.RunE == nil { |
| 99 | + t.Fatalf("workspace destroy not registered as a real command: %v", err) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestWorkspaceDestroyJSONRequiresYes(t *testing.T) { |
| 104 | + t.Chdir(t.TempDir()) |
| 105 | + var out strings.Builder |
| 106 | + root := NewRootCmd(Options{}) |
| 107 | + root.SetArgs([]string{"workspace", "destroy", "--json"}) |
| 108 | + root.SetOut(&out) |
| 109 | + root.SetErr(&out) |
| 110 | + // The --json-without-yes guard fires before any Docker/workspace access. |
| 111 | + if err := root.Execute(); err == nil { |
| 112 | + t.Fatal("workspace destroy --json without --yes must error") |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestDestroyWorkspaceTeardown(t *testing.T) { |
| 117 | + d, fr := destroyFixture(t) |
| 118 | + ctx := context.Background() |
| 119 | + |
| 120 | + // Seed live state: a ref row for the project + a port allocation it owns. |
| 121 | + if err := d.Manager.RegisterUp(ctx, "app"); err != nil { |
| 122 | + t.Fatalf("register up: %v", err) |
| 123 | + } |
| 124 | + if _, err := d.DB.AllocatePort("app", "web", 30000, 30100, func(int) bool { return true }); err != nil { |
| 125 | + t.Fatalf("alloc port: %v", err) |
| 126 | + } |
| 127 | + if n, _ := d.DB.RefCount("shared-postgres"); n != 1 { |
| 128 | + t.Fatalf("precondition: ref count = %d, want 1", n) |
| 129 | + } |
| 130 | + |
| 131 | + res := destroyWorkspace(ctx, d, []string{"app"}) |
| 132 | + if len(res.Errors) != 0 { |
| 133 | + t.Fatalf("destroy errors: %v", res.Errors) |
| 134 | + } |
| 135 | + |
| 136 | + // Project stack was composed down (never with --volumes). |
| 137 | + if !fr.saw("-p devstack-app", "down") { |
| 138 | + t.Errorf("project stack was not composed down: %v", fr.cmds) |
| 139 | + } |
| 140 | + if fr.saw("down", "--volumes") { |
| 141 | + t.Error("destroy must never pass --volumes (data preservation)") |
| 142 | + } |
| 143 | + // Project is in the result. |
| 144 | + if len(res.Projects) != 1 || res.Projects[0] != "app" { |
| 145 | + t.Errorf("projects = %v, want [app]", res.Projects) |
| 146 | + } |
| 147 | + // Ref rows + port rows for the project are gone. |
| 148 | + if n, _ := d.DB.RefCount("shared-postgres"); n != 0 { |
| 149 | + t.Errorf("ref count after destroy = %d, want 0", n) |
| 150 | + } |
| 151 | + if _, ok, _ := d.DB.PortFor("app", "web"); ok { |
| 152 | + t.Error("port allocation for app/web should be released") |
| 153 | + } |
| 154 | + // The now-orphaned shared service was warm-stopped (compose stop on the shared |
| 155 | + // stack), not downed. |
| 156 | + if !fr.saw("-p "+generate.SharedStackName, "stop") { |
| 157 | + t.Errorf("orphaned shared service was not warm-stopped: %v", fr.cmds) |
| 158 | + } |
| 159 | + if len(res.SharedStopped) != 1 || res.SharedStopped[0] != generate.SharedAlias("postgres") { |
| 160 | + t.Errorf("shared stopped = %v, want [%s]", res.SharedStopped, generate.SharedAlias("postgres")) |
| 161 | + } |
| 162 | + // Generated artifacts were removed. |
| 163 | + if _, err := os.Stat(filepath.Join(d.Model.Root, generate.GenDir)); !os.IsNotExist(err) { |
| 164 | + t.Error("workspace .devstack/ should be removed") |
| 165 | + } |
| 166 | + if _, err := os.Stat(filepath.Join(d.Model.ProjectDir("app"), generate.GenDir)); !os.IsNotExist(err) { |
| 167 | + t.Error("project .devstack/ should be removed") |
| 168 | + } |
| 169 | +} |
0 commit comments