|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/deckhouse/deckhouse/pkg/log" |
| 12 | + |
| 13 | + "github.com/deckhouse/module-sdk/internal/controller" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_HooksRun_NoErrorOrUsageOutput(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + args []string |
| 20 | + description string |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "missing argument", |
| 24 | + args: []string{"hooks", "run"}, |
| 25 | + description: "should not output Error or Usage when argument is missing", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "invalid argument format", |
| 29 | + args: []string{"hooks", "run", "not-a-number"}, |
| 30 | + description: "should not output Error or Usage when argument is not a number", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "invalid hook index", |
| 34 | + args: []string{"hooks", "run", "999"}, |
| 35 | + description: "should not output Error or Usage when hook index is invalid", |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.name, func(t *testing.T) { |
| 41 | + // Capture stdout and stderr |
| 42 | + var stdout, stderr bytes.Buffer |
| 43 | + |
| 44 | + // Create a mock controller |
| 45 | + cfg := &controller.Config{ |
| 46 | + ModuleName: "test-module", |
| 47 | + HookConfig: &controller.HookConfig{ |
| 48 | + BindingContextPath: "in/binding_context.json", |
| 49 | + ValuesPath: "in/values_path.json", |
| 50 | + ConfigValuesPath: "in/config_values_path.json", |
| 51 | + HookConfigPath: "out/hook_config.json", |
| 52 | + MetricsPath: "out/metrics.json", |
| 53 | + KubernetesPath: "out/kubernetes.json", |
| 54 | + ValuesJSONPath: "out/values.json", |
| 55 | + ConfigValuesJSONPath: "out/config_values.json", |
| 56 | + CreateFilesByYourself: true, |
| 57 | + }, |
| 58 | + LogLevel: log.LevelFatal, |
| 59 | + } |
| 60 | + |
| 61 | + // Create logger that writes to buffer instead of stderr |
| 62 | + logBuf := bytes.NewBuffer(nil) |
| 63 | + logger := log.Default() |
| 64 | + logger.SetOutput(logBuf) |
| 65 | + |
| 66 | + hookController := controller.NewHookController(cfg, logger) |
| 67 | + |
| 68 | + // Create command structure with our test logger |
| 69 | + cmd := newCMD(hookController, logger) |
| 70 | + |
| 71 | + // Build complete command structure |
| 72 | + rootCmd := cmd.buildCommand() |
| 73 | + rootCmd.SetOut(&stdout) |
| 74 | + rootCmd.SetErr(&stderr) |
| 75 | + |
| 76 | + // Set up test arguments |
| 77 | + rootCmd.SetArgs(tt.args) |
| 78 | + |
| 79 | + // Execute command |
| 80 | + _ = rootCmd.Execute() |
| 81 | + |
| 82 | + // Combine outputs (stdout, stderr, and log output) |
| 83 | + output := stdout.String() + stderr.String() + logBuf.String() |
| 84 | + // Check that Error: (from cobra) is not in output |
| 85 | + // We check for "Error:" with capital E, which is how cobra outputs errors |
| 86 | + // JSON logs may contain "error" field, but not "Error:" prefix from cobra |
| 87 | + assert.NotContains(t, output, "Error:", tt.description+": should not contain 'Error:' from cobra") |
| 88 | + |
| 89 | + // Check that Usage: (from cobra) is not in output |
| 90 | + // We check for "Usage:" with capital U, which is how cobra outputs usage |
| 91 | + assert.NotContains(t, output, "Usage:", tt.description+": should not contain 'Usage:' from cobra") |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func Test_HooksConfig_NoErrorOrUsageOutput(t *testing.T) { |
| 97 | + // Capture stdout and stderr |
| 98 | + var stdout, stderr bytes.Buffer |
| 99 | + |
| 100 | + // Create a mock controller |
| 101 | + cfg := &controller.Config{ |
| 102 | + ModuleName: "test-module", |
| 103 | + HookConfig: &controller.HookConfig{ |
| 104 | + BindingContextPath: "in/binding_context.json", |
| 105 | + ValuesPath: "in/values_path.json", |
| 106 | + ConfigValuesPath: "in/config_values_path.json", |
| 107 | + HookConfigPath: "out/hook_config.json", |
| 108 | + MetricsPath: "out/metrics.json", |
| 109 | + KubernetesPath: "out/kubernetes.json", |
| 110 | + ValuesJSONPath: "out/values.json", |
| 111 | + ConfigValuesJSONPath: "out/config_values.json", |
| 112 | + CreateFilesByYourself: true, |
| 113 | + }, |
| 114 | + LogLevel: log.LevelFatal, |
| 115 | + } |
| 116 | + |
| 117 | + // Create logger that writes to buffer instead of stderr |
| 118 | + logBuf := bytes.Buffer{} |
| 119 | + logger := log.Default() |
| 120 | + logger.SetOutput(&logBuf) |
| 121 | + |
| 122 | + hookController := controller.NewHookController(cfg, logger) |
| 123 | + |
| 124 | + // Create command structure with our test logger |
| 125 | + cmd := newCMD(hookController, logger) |
| 126 | + |
| 127 | + // Build complete command structure |
| 128 | + rootCmd := cmd.buildCommand() |
| 129 | + rootCmd.SetOut(&stdout) |
| 130 | + rootCmd.SetErr(&stderr) |
| 131 | + |
| 132 | + // Set up test arguments |
| 133 | + rootCmd.SetArgs([]string{"hooks", "config"}) |
| 134 | + |
| 135 | + // Execute command |
| 136 | + err := rootCmd.Execute() |
| 137 | + |
| 138 | + // Combine outputs (stdout, stderr, and log output) |
| 139 | + output := stdout.String() + stderr.String() + logBuf.String() |
| 140 | + |
| 141 | + // Check that Error: (from cobra) is not in output |
| 142 | + // We check for "Error:" with capital E, which is how cobra outputs errors |
| 143 | + // JSON logs may contain "error" field, but not "Error:" prefix from cobra |
| 144 | + assert.NotContains(t, output, "Error:", "config command should not contain 'Error:' from cobra") |
| 145 | + |
| 146 | + // Check that Usage: (from cobra) is not in output |
| 147 | + // We check for "Usage:" with capital U, which is how cobra outputs usage |
| 148 | + assert.NotContains(t, output, "Usage:", "config command should not contain 'Usage:' from cobra") |
| 149 | + |
| 150 | + // Check that output is valid JSON (if there are hooks and no error) |
| 151 | + // Only check stdout for JSON, not stderr or logs |
| 152 | + if err == nil && len(stdout.String()) > 0 { |
| 153 | + require.True(t, strings.HasPrefix(strings.TrimSpace(stdout.String()), "{"), "config command should output valid JSON") |
| 154 | + } |
| 155 | +} |
0 commit comments