|
| 1 | +/* |
| 2 | +Copyright © 2024 Juliano Martinez <[email protected]> |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/spf13/viper" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | +) |
| 27 | + |
| 28 | +func TestRootCmd_Exists(t *testing.T) { |
| 29 | + assert.NotNil(t, rootCmd) |
| 30 | + assert.Equal(t, "vault-audit-filter", rootCmd.Use) |
| 31 | +} |
| 32 | + |
| 33 | +func TestRootCmd_HasSubcommands(t *testing.T) { |
| 34 | + commands := rootCmd.Commands() |
| 35 | + assert.GreaterOrEqual(t, len(commands), 2) |
| 36 | + |
| 37 | + var hasSetup, hasAuditServer bool |
| 38 | + for _, cmd := range commands { |
| 39 | + if cmd.Use == "setup" { |
| 40 | + hasSetup = true |
| 41 | + } |
| 42 | + if cmd.Use == "auditServer" { |
| 43 | + hasAuditServer = true |
| 44 | + } |
| 45 | + } |
| 46 | + assert.True(t, hasSetup, "setup command should be registered") |
| 47 | + assert.True(t, hasAuditServer, "auditServer command should be registered") |
| 48 | +} |
| 49 | + |
| 50 | +func TestRootCmd_PersistentFlags(t *testing.T) { |
| 51 | + flags := rootCmd.PersistentFlags() |
| 52 | + |
| 53 | + configFlag := flags.Lookup("config") |
| 54 | + assert.NotNil(t, configFlag) |
| 55 | + |
| 56 | + vaultAddressFlag := flags.Lookup("vault.address") |
| 57 | + assert.NotNil(t, vaultAddressFlag) |
| 58 | + assert.Equal(t, "http://127.0.0.1:8200", vaultAddressFlag.DefValue) |
| 59 | + |
| 60 | + vaultTokenFlag := flags.Lookup("vault.token") |
| 61 | + assert.NotNil(t, vaultTokenFlag) |
| 62 | + |
| 63 | + vaultAuditPathFlag := flags.Lookup("vault.audit_path") |
| 64 | + assert.NotNil(t, vaultAuditPathFlag) |
| 65 | + |
| 66 | + vaultAuditAddressFlag := flags.Lookup("vault.audit_address") |
| 67 | + assert.NotNil(t, vaultAuditAddressFlag) |
| 68 | + assert.Equal(t, "127.0.0.1:1269", vaultAuditAddressFlag.DefValue) |
| 69 | +} |
| 70 | + |
| 71 | +func TestInitConfig_WithConfigFile(t *testing.T) { |
| 72 | + // Create a temporary config file |
| 73 | + tmpDir := t.TempDir() |
| 74 | + configPath := filepath.Join(tmpDir, "test-config.yaml") |
| 75 | + |
| 76 | + configContent := ` |
| 77 | +vault: |
| 78 | + address: "http://test-vault:8200" |
| 79 | + token: "test-token" |
| 80 | +rule_groups: |
| 81 | + - name: test |
| 82 | + rules: |
| 83 | + - "Request.Operation == 'read'" |
| 84 | +` |
| 85 | + err := os.WriteFile(configPath, []byte(configContent), 0644) |
| 86 | + assert.NoError(t, err) |
| 87 | + |
| 88 | + // Reset viper and set config file |
| 89 | + viper.Reset() |
| 90 | + cfgFile = configPath |
| 91 | + initConfig() |
| 92 | + |
| 93 | + assert.Equal(t, "http://test-vault:8200", viper.GetString("vault.address")) |
| 94 | + assert.Equal(t, "test-token", viper.GetString("vault.token")) |
| 95 | + |
| 96 | + ruleGroups := viper.Get("rule_groups") |
| 97 | + assert.NotNil(t, ruleGroups) |
| 98 | + |
| 99 | + // Reset for other tests |
| 100 | + cfgFile = "" |
| 101 | + viper.Reset() |
| 102 | +} |
| 103 | + |
| 104 | +func TestInitConfig_NoRuleGroups(t *testing.T) { |
| 105 | + viper.Reset() |
| 106 | + cfgFile = "" |
| 107 | + |
| 108 | + // initConfig should log that no rules are defined |
| 109 | + // We just verify it doesn't panic |
| 110 | + initConfig() |
| 111 | + |
| 112 | + // Reset for other tests |
| 113 | + viper.Reset() |
| 114 | +} |
| 115 | + |
| 116 | +func TestExecute_Help(t *testing.T) { |
| 117 | + // Test that Execute doesn't panic with help flag |
| 118 | + oldArgs := os.Args |
| 119 | + defer func() { os.Args = oldArgs }() |
| 120 | + |
| 121 | + os.Args = []string{"vault-audit-filter", "--help"} |
| 122 | + |
| 123 | + // Capture output |
| 124 | + rootCmd.SetOut(&bytes.Buffer{}) |
| 125 | + rootCmd.SetErr(&bytes.Buffer{}) |
| 126 | + |
| 127 | + // This should not panic |
| 128 | + // We don't call Execute() directly as it calls os.Exit |
| 129 | + err := rootCmd.Help() |
| 130 | + assert.NoError(t, err) |
| 131 | +} |
0 commit comments