|
| 1 | +package tools_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/esnet/gdg/cli" |
| 8 | + "github.com/esnet/gdg/cli/domain" |
| 9 | + "github.com/esnet/gdg/internal/ports/mocks" |
| 10 | + "github.com/esnet/gdg/pkg/test_tooling" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +// TestS3ListShowsEngines exercises "gdg tools contexts s3 list" against the |
| 15 | +// testing.yml config (which defines a "test" storage engine) and verifies that |
| 16 | +// the table output contains the expected column headers and engine data. |
| 17 | +func TestS3ListShowsEngines(t *testing.T) { |
| 18 | + rootSvc := cli.NewRootService() |
| 19 | + execMe := func(_ *mocks.GrafanaService, optionMockSvc func() domain.RootOption) error { |
| 20 | + return cli.Execute(rootSvc, []string{"tools", "contexts", "s3", "list"}, optionMockSvc()) |
| 21 | + } |
| 22 | + outStr, closeReader := test_tooling.SetupAndExecuteMockingServices(t, execMe) |
| 23 | + defer closeReader() |
| 24 | + |
| 25 | + lower := strings.ToLower(outStr) |
| 26 | + |
| 27 | + // Column headers (go-pretty StyleLight uppercases them, so compare lowercase) |
| 28 | + assert.Contains(t, lower, "label") |
| 29 | + assert.Contains(t, lower, "cloud_type") |
| 30 | + assert.Contains(t, lower, "endpoint") |
| 31 | + assert.Contains(t, lower, "bucket") |
| 32 | + |
| 33 | + // Data from testing.yml storage_engine.test |
| 34 | + assert.Contains(t, outStr, "test") |
| 35 | + assert.Contains(t, outStr, "http://localhost:9000") |
| 36 | +} |
| 37 | + |
| 38 | +// TestS3ListViaStorageAlias verifies that the "storage" alias on the s3 command |
| 39 | +// and the "ctx" alias on the contexts command both resolve correctly. |
| 40 | +func TestS3ListViaStorageAlias(t *testing.T) { |
| 41 | + rootSvc := cli.NewRootService() |
| 42 | + execMe := func(_ *mocks.GrafanaService, optionMockSvc func() domain.RootOption) error { |
| 43 | + return cli.Execute(rootSvc, []string{"tools", "ctx", "storage", "list"}, optionMockSvc()) |
| 44 | + } |
| 45 | + outStr, closeReader := test_tooling.SetupAndExecuteMockingServices(t, execMe) |
| 46 | + defer closeReader() |
| 47 | + |
| 48 | + // Aliases should produce the same table as the canonical command |
| 49 | + assert.Contains(t, strings.ToLower(outStr), "label") |
| 50 | + assert.Contains(t, outStr, "http://localhost:9000") |
| 51 | +} |
| 52 | + |
| 53 | +// TestS3ParentShowsHelp verifies that running "gdg tools contexts s3" with no |
| 54 | +// subcommand prints the help listing all three child commands. |
| 55 | +func TestS3ParentShowsHelp(t *testing.T) { |
| 56 | + rootSvc := cli.NewRootService() |
| 57 | + execMe := func(_ *mocks.GrafanaService, optionMockSvc func() domain.RootOption) error { |
| 58 | + return cli.Execute(rootSvc, []string{"tools", "contexts", "s3"}, optionMockSvc()) |
| 59 | + } |
| 60 | + outStr, closeReader := test_tooling.SetupAndExecuteMockingServices(t, execMe) |
| 61 | + defer closeReader() |
| 62 | + |
| 63 | + lower := strings.ToLower(outStr) |
| 64 | + assert.Contains(t, lower, "new") |
| 65 | + assert.Contains(t, lower, "list") |
| 66 | + assert.Contains(t, lower, "delete") |
| 67 | +} |
| 68 | + |
| 69 | +// TestS3DeleteNoArgsShowsHelp verifies that running "gdg tools contexts s3 delete" |
| 70 | +// without a label argument prints the command's usage help rather than returning an error. |
| 71 | +func TestS3DeleteNoArgsShowsHelp(t *testing.T) { |
| 72 | + rootSvc := cli.NewRootService() |
| 73 | + execMe := func(_ *mocks.GrafanaService, optionMockSvc func() domain.RootOption) error { |
| 74 | + return cli.Execute(rootSvc, []string{"tools", "contexts", "s3", "delete"}, optionMockSvc()) |
| 75 | + } |
| 76 | + outStr, closeReader := test_tooling.SetupAndExecuteMockingServices(t, execMe) |
| 77 | + defer closeReader() |
| 78 | + |
| 79 | + lower := strings.ToLower(outStr) |
| 80 | + // Cobra help output always includes "usage" and the command name |
| 81 | + assert.Contains(t, lower, "usage") |
| 82 | + assert.Contains(t, lower, "delete") |
| 83 | +} |
0 commit comments