|
1 | 1 | package cli |
2 | 2 |
|
3 | | -import ( |
4 | | - "bytes" |
5 | | - "strings" |
6 | | - "testing" |
7 | | - |
8 | | - "github.com/spf13/cobra" |
9 | | - |
10 | | - "github.com/open-source-cloud/devstack/internal/orchestrate" |
11 | | -) |
12 | | - |
13 | | -func exposeFixture() []orchestrate.ExposedPort { |
14 | | - return []orchestrate.ExposedPort{ |
15 | | - {Instance: "postgres", Engine: "postgres", Alias: "shared-postgres", Label: "postgres", Host: "127.0.0.1", Port: 55432, Container: 5432, Primary: true, URL: "postgres://devstack:devstack@127.0.0.1:55432/postgres?sslmode=disable"}, |
16 | | - {Instance: "minio", Engine: "minio", Alias: "shared-minio", Label: "console", Host: "127.0.0.1", Port: 59001, Container: 9001, Primary: false, URL: "http://127.0.0.1:59001"}, |
17 | | - } |
18 | | -} |
19 | | - |
20 | | -func TestRenderExposed_Table(t *testing.T) { |
21 | | - var buf bytes.Buffer |
22 | | - cmd := &cobra.Command{} |
23 | | - cmd.SetOut(&buf) |
24 | | - if err := renderExposed(cmd, &GlobalOpts{}, exposeFixture()); err != nil { |
25 | | - t.Fatal(err) |
26 | | - } |
27 | | - out := buf.String() |
28 | | - for _, want := range []string{"shared-postgres", "55432", "shared-minio (console)", "59001", "per-project database:"} { |
29 | | - if !strings.Contains(out, want) { |
30 | | - t.Errorf("table missing %q:\n%s", want, out) |
| 3 | +import "testing" |
| 4 | + |
| 5 | +// TestExposePortsRegistered asserts expose/ports are available both at the top |
| 6 | +// level (the promotion) and under `shared` (backward-compatible aliases). |
| 7 | +func TestExposePortsRegistered(t *testing.T) { |
| 8 | + root := NewRootCmd(Options{}) |
| 9 | + for _, path := range [][]string{ |
| 10 | + {"expose"}, {"ports"}, |
| 11 | + {"shared", "expose"}, {"shared", "ports"}, |
| 12 | + } { |
| 13 | + c, _, err := root.Find(path) |
| 14 | + if err != nil || c.RunE == nil { |
| 15 | + t.Errorf("%v not registered as a real command: %v", path, err) |
31 | 16 | } |
32 | 17 | } |
33 | | -} |
34 | | - |
35 | | -func TestRenderExposed_JSON(t *testing.T) { |
36 | | - var buf bytes.Buffer |
37 | | - cmd := &cobra.Command{} |
38 | | - cmd.SetOut(&buf) |
39 | | - if err := renderExposed(cmd, &GlobalOpts{JSON: true}, exposeFixture()); err != nil { |
40 | | - t.Fatal(err) |
41 | | - } |
42 | | - out := buf.String() |
43 | | - if !strings.Contains(out, "\"exposed\"") || !strings.Contains(out, "\"port\": 55432") { |
44 | | - t.Errorf("json missing fields:\n%s", out) |
45 | | - } |
46 | | -} |
47 | | - |
48 | | -func TestRenderExposed_Quiet(t *testing.T) { |
49 | | - var buf bytes.Buffer |
50 | | - cmd := &cobra.Command{} |
51 | | - cmd.SetOut(&buf) |
52 | | - if err := renderExposed(cmd, &GlobalOpts{Quiet: true}, exposeFixture()); err != nil { |
53 | | - t.Fatal(err) |
54 | | - } |
55 | | - out := strings.TrimSpace(buf.String()) |
56 | | - // Quiet emits only the connection URLs, one per line. |
57 | | - lines := strings.Split(out, "\n") |
58 | | - if len(lines) != 2 || !strings.HasPrefix(lines[0], "postgres://") { |
59 | | - t.Errorf("quiet should print only URLs, got:\n%s", out) |
60 | | - } |
61 | | -} |
62 | | - |
63 | | -// TestSharedExposeCommandsRegistered guards that `shared expose` and |
64 | | -// `shared ports` are wired into the shared command tree. |
65 | | -func TestSharedExposeCommandsRegistered(t *testing.T) { |
66 | | - sh := newSharedCmd(&GlobalOpts{}) |
67 | | - have := map[string]bool{} |
68 | | - for _, c := range sh.Commands() { |
69 | | - have[c.Name()] = true |
70 | | - } |
71 | | - for _, want := range []string{"expose", "ports", "status", "gc", "doctor"} { |
72 | | - if !have[want] { |
73 | | - t.Errorf("shared subcommand %q not registered", want) |
74 | | - } |
| 18 | + // The top-level expose carries its --off flag. |
| 19 | + c, _, _ := root.Find([]string{"expose"}) |
| 20 | + if c.Flags().Lookup("off") == nil { |
| 21 | + t.Error("expose missing --off flag") |
75 | 22 | } |
76 | 23 | } |
0 commit comments