Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/dashboard/dashboard_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func RunDashboardDescribeCommand(args *DescribeArgs) di.CmdWithApiFn {
return common.NewResponseError(err, resp)
}

yamlData, err := yaml.Marshal(dashboard)
yamlData, err := yaml.Marshal(dashboard.DashboardReadFullSchema)
if err != nil {
return common.NewExecutionError(fmt.Errorf("failed to marshal dashboard: %v", err))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dashboard/dashboard_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func RunDashboardEditCommand(args *EditArgs) di.CmdWithApiFn {
}

// Convert dashboard to pretty JSON for editing
originalYAML, err := yaml.Marshal(dashboard)
originalYAML, err := yaml.Marshal(dashboard.DashboardReadFullSchema)
if err != nil {
return common.NewExecutionError(fmt.Errorf("failed to marshal dashboard to YAML: %v", err))
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/sts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@ func STSCommand(cli *di.Deps) *cobra.Command {
cmd.AddCommand(TopologySyncCommand(cli))
cmd.AddCommand(AgentCommand(cli))
cmd.AddCommand(UserSessionCommand(cli))
cmd.AddCommand(DashboardCommand(cli))

// Experimental commands for otel mapping
if os.Getenv("STS_EXPERIMENTAL_OTEL_MAPPING") != "" {
cmd.AddCommand(OtelComponentMappingCommand(cli))
cmd.AddCommand(OtelRelationtMappingCommand(cli))
}

// Only add dashboard command if experimental feature is enabled
if os.Getenv("STS_EXPERIMENTAL_DASHBOARD") != "" {
cmd.AddCommand(DashboardCommand(cli))
}

return cmd
}
171 changes: 29 additions & 142 deletions cmd/sts_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
package cmd

import (
"os"
"testing"

"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/internal/di"
"github.com/stretchr/testify/assert"
)

const dashboardCommand = "dashboard"

// Helper function to manage STS_EXPERIMENTAL_DASHBOARD environment variable
func withDashboardEnv(value string, fn func()) {
originalValue := os.Getenv("STS_EXPERIMENTAL_DASHBOARD")
defer func() {
if originalValue == "" {
_ = os.Unsetenv("STS_EXPERIMENTAL_DASHBOARD")
} else {
_ = os.Setenv("STS_EXPERIMENTAL_DASHBOARD", originalValue)
}
}()

if value == "" {
_ = os.Unsetenv("STS_EXPERIMENTAL_DASHBOARD")
} else {
_ = os.Setenv("STS_EXPERIMENTAL_DASHBOARD", value)
}

fn()
}

func TestSTSCommand(t *testing.T) {
cli := di.NewMockDeps(t)
cmd := STSCommand(&cli.Deps)
Expand All @@ -42,135 +18,46 @@ func TestSTSCommand(t *testing.T) {
}

func TestSTSCommandContainsExpectedSubcommands(t *testing.T) {
withDashboardEnv("", func() {
cli := di.NewMockDeps(t)
cmd := STSCommand(&cli.Deps)

expectedCommands := []string{
"context",
"version",
"script",
"settings",
"stackpack",
"monitor",
"service-token",
"health",
"license",
"graph",
"rbac",
"topic",
"topology-sync",
"agent",
"user-session",
}
cli := di.NewMockDeps(t)
cmd := STSCommand(&cli.Deps)

// Verify expected commands are present
for _, expectedCmd := range expectedCommands {
found := false
for _, subCmd := range cmd.Commands() {
if subCmd.Use == expectedCmd {
found = true
break
}
}
assert.True(t, found, "Expected command '%s' not found", expectedCmd)
}
expectedCommands := []string{
"context",
"version",
"script",
"settings",
"stackpack",
"monitor",
"service-token",
"health",
"license",
"graph",
"rbac",
"topic",
"topology-sync",
"agent",
"user-session",
"dashboard",
}

// Verify dashboard command is NOT present when env var is not set
dashboardFound := false
// Verify expected commands are present
for _, expectedCmd := range expectedCommands {
found := false
for _, subCmd := range cmd.Commands() {
if subCmd.Use == dashboardCommand {
dashboardFound = true
if subCmd.Use == expectedCmd {
found = true
break
}
}
assert.False(t, dashboardFound, "Dashboard command should not be present when STS_EXPERIMENTAL_DASHBOARD is not set")
})
}

func TestSTSCommandDashboardExperimentalFeature(t *testing.T) {
tests := []struct {
name string
envVarValue string
shouldIncludeDashboard bool
}{
{
name: "Dashboard command not included when env var is empty",
envVarValue: "",
shouldIncludeDashboard: false,
},
{
name: "Dashboard command included when env var is set to 'true'",
envVarValue: "true",
shouldIncludeDashboard: true,
},
{
name: "Dashboard command included when env var is set to any non-empty value",
envVarValue: "any-value",
shouldIncludeDashboard: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
withDashboardEnv(tt.envVarValue, func() {
cli := di.NewMockDeps(t)
cmd := STSCommand(&cli.Deps)

// Check if dashboard command is present
dashboardFound := false
for _, subCmd := range cmd.Commands() {
if subCmd.Use == dashboardCommand {
dashboardFound = true
break
}
}

if tt.shouldIncludeDashboard {
assert.True(t, dashboardFound, "Dashboard command should be present when STS_EXPERIMENTAL_DASHBOARD='%s'", tt.envVarValue)
} else {
assert.False(t, dashboardFound, "Dashboard command should not be present when STS_EXPERIMENTAL_DASHBOARD='%s'", tt.envVarValue)
}
})
})
assert.True(t, found, "Expected command '%s' not found", expectedCmd)
}
}

func TestSTSCommandStructure(t *testing.T) {
withDashboardEnv("1", func() {
cli := di.NewMockDeps(t)
cmd := STSCommand(&cli.Deps)

// Verify the command has the expected number of subcommands (15 regular + 1 dashboard)
assert.Len(t, cmd.Commands(), 16, "Expected 16 subcommands when dashboard is enabled")

// Verify that dashboard command is included and properly configured
var dashboardCmd *cobra.Command
for _, subCmd := range cmd.Commands() {
if subCmd.Use == dashboardCommand {
dashboardCmd = subCmd
break
}
}
assert.NotNil(t, dashboardCmd, "Dashboard command should be present")
assert.Equal(t, dashboardCommand, dashboardCmd.Use)
assert.NotEmpty(t, dashboardCmd.Short)
})
}

func TestSTSCommandWithoutDashboard(t *testing.T) {
withDashboardEnv("", func() {
cli := di.NewMockDeps(t)
cmd := STSCommand(&cli.Deps)

// Verify the command has the expected number of subcommands (15 regular, no dashboard)
assert.Len(t, cmd.Commands(), 15, "Expected 15 subcommands when dashboard is disabled")
cli := di.NewMockDeps(t)
cmd := STSCommand(&cli.Deps)

// Double-check that no command has "dashboard" as its Use field
for _, subCmd := range cmd.Commands() {
assert.NotEqual(t, dashboardCommand, subCmd.Use, "Dashboard command should not be present")
}
})
assert.Len(t, cmd.Commands(), 16, "Expected 16 subcommands")
}

func TestSTSCommandUsageTemplate(t *testing.T) {
Expand Down