-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdashboard_describe_test.go
More file actions
139 lines (115 loc) · 4.62 KB
/
dashboard_describe_test.go
File metadata and controls
139 lines (115 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package dashboard
import (
"os"
"testing"
"github.com/spf13/cobra"
sts "github.com/stackvista/stackstate-cli/generated/stackstate_api"
"github.com/stackvista/stackstate-cli/internal/di"
"github.com/stretchr/testify/assert"
)
func setupDescribeCmd(t *testing.T) (*di.MockDeps, *cobra.Command) {
cli := di.NewMockDeps(t)
cmd := DashboardDescribeCommand(&cli.Deps)
return &cli, cmd
}
func createTestDashboard() sts.DashboardReadSchema {
fullDashboard := sts.DashboardReadFullSchema{
Id: firstDashboardId,
Name: firstDashboardName,
Identifier: firstDashboardIdentifier,
Description: firstDashboardDescription,
Scope: firstDashboardScope,
Dashboard: sts.PersesDashboard{
Spec: &sts.PersesDashboardSpec{
Layouts: []sts.PersesLayout{
{
Kind: "Grid",
Spec: sts.PersesLayoutSpec{
Items: []sts.PersesGridItem{},
},
},
},
Panels: &map[string]sts.PersesPanel{},
},
},
}
return sts.DashboardReadFullSchemaAsDashboardReadSchema(&fullDashboard)
}
func TestDashboardDescribe(t *testing.T) {
cli, cmd := setupDescribeCmd(t)
expectedDashboard := createTestDashboard()
cli.MockClient.ApiMocks.DashboardsApi.GetDashboardResponse.Result = expectedDashboard
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, "--id", "1")
assert.Nil(t, err)
// Verify that the command printed the dashboard YAML
assert.Len(t, *cli.MockPrinter.PrintLnCalls, 1)
printedOutput := (*cli.MockPrinter.PrintLnCalls)[0]
assert.Contains(t, printedOutput, `id: 1`)
assert.Contains(t, printedOutput, `name: aDashboard`)
assert.Contains(t, printedOutput, `identifier: urn:custom:dashboard:aDashboard`)
}
func TestDashboardDescribeWithIdentifier(t *testing.T) {
cli, cmd := setupDescribeCmd(t)
expectedDashboard := createTestDashboard()
cli.MockClient.ApiMocks.DashboardsApi.GetDashboardResponse.Result = expectedDashboard
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, "--identifier", "urn:custom:dashboard:aDashboard")
assert.Nil(t, err)
// Verify the API was called with the correct identifier
assert.Len(t, *cli.MockClient.ApiMocks.DashboardsApi.GetDashboardCalls, 1)
assert.Equal(t, "urn:custom:dashboard:aDashboard", (*cli.MockClient.ApiMocks.DashboardsApi.GetDashboardCalls)[0].PdashboardIdOrUrn)
}
func TestDashboardDescribeJson(t *testing.T) {
cli, cmd := setupDescribeCmd(t)
expectedDashboard := createTestDashboard()
cli.MockClient.ApiMocks.DashboardsApi.GetDashboardResponse.Result = expectedDashboard
di.ExecuteCommandWithContext(&cli.Deps, cmd, "--id", "1", "-o", "json") //nolint:errcheck
assert.Len(t, *cli.MockPrinter.PrintJsonCalls, 1)
jsonOutput := (*cli.MockPrinter.PrintJsonCalls)[0]
assert.Contains(t, jsonOutput, "data")
assert.Contains(t, jsonOutput, "format")
assert.Equal(t, "json", jsonOutput["format"])
assert.False(t, cli.MockPrinter.HasNonJsonCalls)
}
func TestDashboardDescribeToFile(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "test_dashboard_")
if err != nil {
panic(err)
}
defer os.Remove(file.Name())
cli, cmd := setupDescribeCmd(t)
expectedDashboard := createTestDashboard()
cli.MockClient.ApiMocks.DashboardsApi.GetDashboardResponse.Result = expectedDashboard
_, err = di.ExecuteCommandWithContext(&cli.Deps, cmd, "--id", "1", "--file", file.Name())
assert.Nil(t, err)
// Verify success message was printed
assert.Len(t, *cli.MockPrinter.SuccessCalls, 1)
assert.Contains(t, (*cli.MockPrinter.SuccessCalls)[0], "dashboard exported to:")
assert.Contains(t, (*cli.MockPrinter.SuccessCalls)[0], file.Name())
// Verify file contents
body, err := os.ReadFile(file.Name())
assert.Nil(t, err)
assert.Contains(t, string(body), `id: 1`)
assert.Contains(t, string(body), `name: aDashboard`)
}
func TestDashboardDescribeToFileJson(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "test_dashboard_")
if err != nil {
panic(err)
}
defer os.Remove(file.Name())
cli, cmd := setupDescribeCmd(t)
expectedDashboard := createTestDashboard()
cli.MockClient.ApiMocks.DashboardsApi.GetDashboardResponse.Result = expectedDashboard
di.ExecuteCommandWithContext(&cli.Deps, cmd, "--id", "1", "--file", file.Name(), "-o", "json") //nolint:errcheck
// Verify JSON response with filepath
assert.Len(t, *cli.MockPrinter.PrintJsonCalls, 1)
jsonOutput := (*cli.MockPrinter.PrintJsonCalls)[0]
assert.Equal(t, file.Name(), jsonOutput["filepath"])
assert.False(t, cli.MockPrinter.HasNonJsonCalls)
}
func TestDashboardDescribeMissingArgs(t *testing.T) {
cli, cmd := setupDescribeCmd(t)
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, "")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "one of the required flags {id | identifier} not set")
}