|
| 1 | +package admin |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +// ----------------------------------------------------- |
| 14 | +// Mocked operational function |
| 15 | +// ----------------------------------------------------- |
| 16 | +func executeClusterStats(_ context.Context, _ interface{}, shard, replicas int) ([]string, error) { |
| 17 | + clusters := []struct { |
| 18 | + Server string |
| 19 | + Shard int |
| 20 | + ConnectionStatus string |
| 21 | + NamespacesCount int |
| 22 | + AppsCount int |
| 23 | + ResourcesCount int |
| 24 | + }{ |
| 25 | + {"https://cluster1", 0, "Healthy", 2, 3, 10}, |
| 26 | + {"https://cluster2", 1, "Degraded", 1, 1, 5}, |
| 27 | + } |
| 28 | + |
| 29 | + lines := []string{"SERVER\tSHARD\tCONNECTION\tNAMESPACES COUNT\tAPPS COUNT\tRESOURCES COUNT"} |
| 30 | + for _, c := range clusters { |
| 31 | + line := fmt.Sprintf("%s\t%d\t%s\t%d\t%d\t%d", |
| 32 | + c.Server, c.Shard, c.ConnectionStatus, c.NamespacesCount, c.AppsCount, c.ResourcesCount) |
| 33 | + lines = append(lines, line) |
| 34 | + } |
| 35 | + |
| 36 | + return lines, nil |
| 37 | +} |
| 38 | + |
| 39 | +// ----------------------------------------------------- |
| 40 | +// Unit test for the operational function |
| 41 | +// ----------------------------------------------------- |
| 42 | +func TestExecuteClusterStats(t *testing.T) { |
| 43 | + lines, err := executeClusterStats(context.Background(), nil, 0, 1) |
| 44 | + assert.NoError(t, err) |
| 45 | + assert.GreaterOrEqual(t, len(lines), 2) |
| 46 | + assert.Contains(t, lines[0], "SERVER") |
| 47 | + assert.Contains(t, lines[1], "cluster1") |
| 48 | +} |
| 49 | + |
| 50 | +// ----------------------------------------------------- |
| 51 | +// Fake Cobra command for testing RunE |
| 52 | +// ----------------------------------------------------- |
| 53 | +func fakeClusterStatsCommand() *cobra.Command { |
| 54 | + cmd := &cobra.Command{ |
| 55 | + Use: "cluster-stats", |
| 56 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 57 | + lines, err := executeClusterStats(context.Background(), nil, 0, 1) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + for _, line := range lines { |
| 62 | + fmt.Fprintln(cmd.OutOrStdout(), line) |
| 63 | + } |
| 64 | + return nil |
| 65 | + }, |
| 66 | + } |
| 67 | + return cmd |
| 68 | +} |
| 69 | + |
| 70 | +// ----------------------------------------------------- |
| 71 | +// Integration test for the RunE CLI behavior |
| 72 | +// ----------------------------------------------------- |
| 73 | +func TestClusterStatsCommand_RunE_Mock(t *testing.T) { |
| 74 | + cmd := fakeClusterStatsCommand() |
| 75 | + |
| 76 | + buf := new(bytes.Buffer) |
| 77 | + cmd.SetOut(buf) |
| 78 | + cmd.SetErr(buf) |
| 79 | + cmd.SetArgs([]string{}) |
| 80 | + |
| 81 | + err := cmd.Execute() |
| 82 | + assert.NoError(t, err) |
| 83 | + |
| 84 | + output := buf.String() |
| 85 | + assert.Contains(t, output, "SERVER") |
| 86 | + assert.Contains(t, output, "Healthy") |
| 87 | + assert.Contains(t, output, "Degraded") |
| 88 | +} |
0 commit comments