|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/kubeshop/tracetest/cli-e2etest/environment" |
| 9 | + "github.com/kubeshop/tracetest/cli-e2etest/helpers" |
| 10 | + "github.com/kubeshop/tracetest/cli-e2etest/testscenarios/types" |
| 11 | + "github.com/kubeshop/tracetest/cli-e2etest/tracetestcli" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func addGetAnalyzerPreReqs(t *testing.T, env environment.Manager) { |
| 16 | + cliConfig := env.GetCLIConfigPath(t) |
| 17 | + |
| 18 | + // When I try to set up a analyzer |
| 19 | + // Then it should be applied with success |
| 20 | + configPath := env.GetTestResourcePath(t, "new-analyzer") |
| 21 | + |
| 22 | + result := tracetestcli.Exec(t, fmt.Sprintf("apply analyzer --file %s", configPath), tracetestcli.WithCLIConfig(cliConfig)) |
| 23 | + helpers.RequireExitCodeEqual(t, result, 0) |
| 24 | +} |
| 25 | + |
| 26 | +func TestGetAnalyzer(t *testing.T) { |
| 27 | + // instantiate require with testing helper |
| 28 | + require := require.New(t) |
| 29 | + |
| 30 | + env := environment.CreateAndStart(t) |
| 31 | + defer env.Close(t) |
| 32 | + |
| 33 | + cliConfig := env.GetCLIConfigPath(t) |
| 34 | + |
| 35 | + t.Run("get with no analyzer initialized", func(t *testing.T) { |
| 36 | + // Given I am a Tracetest CLI user |
| 37 | + // And I have my server recently created |
| 38 | + // And no analyzer previously registered |
| 39 | + |
| 40 | + // When I try to get a analyzer on yaml mode |
| 41 | + // Then it should print a YAML with the default analyzer |
| 42 | + result := tracetestcli.Exec(t, "get analyzer --id current --output yaml", tracetestcli.WithCLIConfig(cliConfig)) |
| 43 | + require.Equal(0, result.ExitCode) |
| 44 | + |
| 45 | + analyzer := helpers.UnmarshalYAML[types.AnalyzerResource](t, result.StdOut) |
| 46 | + require.Equal("Analyzer", analyzer.Type) |
| 47 | + require.Equal("current", analyzer.Spec.Id) |
| 48 | + require.Equal("analyzer", analyzer.Spec.Name) |
| 49 | + require.True(analyzer.Spec.Enabled) |
| 50 | + require.Equal(analyzer.Spec.MinimumScore, 0) |
| 51 | + require.Len(analyzer.Spec.Plugins, 3) |
| 52 | + }) |
| 53 | + |
| 54 | + addGetAnalyzerPreReqs(t, env) |
| 55 | + |
| 56 | + t.Run("get with YAML format", func(t *testing.T) { |
| 57 | + // Given I am a Tracetest CLI user |
| 58 | + // And I have my server recently created |
| 59 | + // And I have a config already set |
| 60 | + |
| 61 | + // When I try to get a config on yaml mode |
| 62 | + // Then it should print a YAML |
| 63 | + result := tracetestcli.Exec(t, "get analyzer --id current --output yaml", tracetestcli.WithCLIConfig(cliConfig)) |
| 64 | + require.Equal(0, result.ExitCode) |
| 65 | + |
| 66 | + analyzer := helpers.UnmarshalYAML[types.AnalyzerResource](t, result.StdOut) |
| 67 | + require.Equal("Analyzer", analyzer.Type) |
| 68 | + require.Equal("current", analyzer.Spec.Id) |
| 69 | + require.Equal("analyzer", analyzer.Spec.Name) |
| 70 | + require.True(analyzer.Spec.Enabled) |
| 71 | + require.Equal(analyzer.Spec.MinimumScore, 95) |
| 72 | + require.Len(analyzer.Spec.Plugins, 3) |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("get with JSON format", func(t *testing.T) { |
| 76 | + // Given I am a Tracetest CLI user |
| 77 | + // And I have my server recently created |
| 78 | + // And I have a analyzer already set |
| 79 | + |
| 80 | + // When I try to get a analyzer on json mode |
| 81 | + // Then it should print a json |
| 82 | + result := tracetestcli.Exec(t, "get analyzer --id current --output json", tracetestcli.WithCLIConfig(cliConfig)) |
| 83 | + helpers.RequireExitCodeEqual(t, result, 0) |
| 84 | + |
| 85 | + analyzer := helpers.UnmarshalJSON[types.AnalyzerResource](t, result.StdOut) |
| 86 | + require.Equal("Analyzer", analyzer.Type) |
| 87 | + require.Equal("current", analyzer.Spec.Id) |
| 88 | + require.Equal("analyzer", analyzer.Spec.Name) |
| 89 | + require.True(analyzer.Spec.Enabled) |
| 90 | + require.Equal(analyzer.Spec.MinimumScore, 95) |
| 91 | + require.Len(analyzer.Spec.Plugins, 3) |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("get with pretty format", func(t *testing.T) { |
| 95 | + // Given I am a Tracetest CLI user |
| 96 | + // And I have my server recently created |
| 97 | + // And I have a analyzer already set |
| 98 | + |
| 99 | + // When I try to get a analyzer on pretty mode |
| 100 | + // Then it should print a table with 4 lines printed: header, separator, a analyzer item and empty line |
| 101 | + result := tracetestcli.Exec(t, "get analyzer --id current --output pretty", tracetestcli.WithCLIConfig(cliConfig)) |
| 102 | + helpers.RequireExitCodeEqual(t, result, 0) |
| 103 | + require.Contains(result.StdOut, "current") |
| 104 | + |
| 105 | + lines := strings.Split(result.StdOut, "\n") |
| 106 | + require.Len(lines, 4) |
| 107 | + }) |
| 108 | +} |
0 commit comments