|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/cyberark/conjur-inspect/pkg/check" |
| 9 | + "github.com/cyberark/conjur-inspect/pkg/test" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestContainerEtcHostsDescribe(t *testing.T) { |
| 15 | + provider := &test.ContainerProvider{} |
| 16 | + ceh := &ContainerEtcHosts{Provider: provider} |
| 17 | + assert.Equal(t, "Test Container Provider /etc/hosts", ceh.Describe()) |
| 18 | +} |
| 19 | + |
| 20 | +func TestContainerEtcHostsRunSuccessful(t *testing.T) { |
| 21 | + hostsContent := "127.0.0.1\tlocalhost\n::1\tlocalhost\n172.17.0.2\tconjur\n" |
| 22 | + |
| 23 | + provider := &test.ContainerProvider{ |
| 24 | + ExecResponses: map[string]test.ExecResponse{ |
| 25 | + "cat /etc/hosts": { |
| 26 | + Stdout: strings.NewReader(hostsContent), |
| 27 | + Stderr: strings.NewReader(""), |
| 28 | + Error: nil, |
| 29 | + }, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + ceh := &ContainerEtcHosts{Provider: provider} |
| 34 | + runContext := test.NewRunContext("container123") |
| 35 | + |
| 36 | + results := ceh.Run(&runContext) |
| 37 | + |
| 38 | + // Should return empty results on success |
| 39 | + assert.Empty(t, results) |
| 40 | + |
| 41 | + // Verify the file was saved to the output store |
| 42 | + items, err := runContext.OutputStore.Items() |
| 43 | + require.NoError(t, err) |
| 44 | + require.Len(t, items, 1) |
| 45 | + info, err := items[0].Info() |
| 46 | + require.NoError(t, err) |
| 47 | + // Test provider name is "Test Container Provider" -> lowercase -> "test container provider" |
| 48 | + assert.Equal(t, "test container provider-etc-hosts.txt", info.Name()) |
| 49 | +} |
| 50 | + |
| 51 | +func TestContainerEtcHostsEmptyContainerID(t *testing.T) { |
| 52 | + provider := &test.ContainerProvider{} |
| 53 | + |
| 54 | + ceh := &ContainerEtcHosts{Provider: provider} |
| 55 | + runContext := test.NewRunContext("") |
| 56 | + results := ceh.Run(&runContext) |
| 57 | + |
| 58 | + // Should return empty results when no container ID |
| 59 | + assert.Empty(t, results) |
| 60 | + |
| 61 | + // Verify no output was saved |
| 62 | + items, err := runContext.OutputStore.Items() |
| 63 | + require.NoError(t, err) |
| 64 | + require.Len(t, items, 0) |
| 65 | +} |
| 66 | + |
| 67 | +func TestContainerEtcHostsRuntimeNotAvailable(t *testing.T) { |
| 68 | + provider := &test.ContainerProvider{} |
| 69 | + |
| 70 | + ceh := &ContainerEtcHosts{Provider: provider} |
| 71 | + runContext := test.NewRunContext("container123") |
| 72 | + |
| 73 | + // Set runtime as not available |
| 74 | + runContext.ContainerRuntimeAvailability = map[string]check.RuntimeAvailability{ |
| 75 | + "test container provider": { |
| 76 | + Available: false, |
| 77 | + Error: fmt.Errorf("runtime not found"), |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + results := ceh.Run(&runContext) |
| 82 | + |
| 83 | + // Should return empty results when runtime not available |
| 84 | + assert.Empty(t, results) |
| 85 | + |
| 86 | + // Verify no output was saved |
| 87 | + items, err := runContext.OutputStore.Items() |
| 88 | + require.NoError(t, err) |
| 89 | + require.Len(t, items, 0) |
| 90 | +} |
| 91 | + |
| 92 | +func TestContainerEtcHostsRuntimeNotAvailableWithVerboseErrors(t *testing.T) { |
| 93 | + provider := &test.ContainerProvider{} |
| 94 | + |
| 95 | + ceh := &ContainerEtcHosts{Provider: provider} |
| 96 | + runContext := test.NewRunContext("container123") |
| 97 | + runContext.VerboseErrors = true |
| 98 | + |
| 99 | + // Set runtime as not available |
| 100 | + runContext.ContainerRuntimeAvailability = map[string]check.RuntimeAvailability{ |
| 101 | + "test container provider": { |
| 102 | + Available: false, |
| 103 | + Error: fmt.Errorf("runtime not found"), |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + results := ceh.Run(&runContext) |
| 108 | + |
| 109 | + // Should return error result with verbose errors enabled |
| 110 | + require.Len(t, results, 1) |
| 111 | + assert.Equal(t, check.StatusError, results[0].Status) |
| 112 | + assert.Contains(t, results[0].Message, "container runtime not available") |
| 113 | +} |
| 114 | + |
| 115 | +func TestContainerEtcHostsExecError(t *testing.T) { |
| 116 | + provider := &test.ContainerProvider{ |
| 117 | + ExecResponses: map[string]test.ExecResponse{ |
| 118 | + "cat /etc/hosts": { |
| 119 | + Stdout: strings.NewReader(""), |
| 120 | + Stderr: strings.NewReader("cat: /etc/hosts: Permission denied"), |
| 121 | + Error: fmt.Errorf("exit status 1"), |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + ceh := &ContainerEtcHosts{Provider: provider} |
| 127 | + runContext := test.NewRunContext("container123") |
| 128 | + |
| 129 | + results := ceh.Run(&runContext) |
| 130 | + |
| 131 | + // Should return empty results without verbose errors |
| 132 | + assert.Empty(t, results) |
| 133 | + |
| 134 | + // Verify no output was saved |
| 135 | + items, err := runContext.OutputStore.Items() |
| 136 | + require.NoError(t, err) |
| 137 | + require.Len(t, items, 0) |
| 138 | +} |
| 139 | + |
| 140 | +func TestContainerEtcHostsExecErrorWithVerboseErrors(t *testing.T) { |
| 141 | + provider := &test.ContainerProvider{ |
| 142 | + ExecResponses: map[string]test.ExecResponse{ |
| 143 | + "cat /etc/hosts": { |
| 144 | + Stdout: strings.NewReader(""), |
| 145 | + Stderr: strings.NewReader("cat: /etc/hosts: Permission denied"), |
| 146 | + Error: fmt.Errorf("exit status 1"), |
| 147 | + }, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + ceh := &ContainerEtcHosts{Provider: provider} |
| 152 | + runContext := test.NewRunContext("container123") |
| 153 | + runContext.VerboseErrors = true |
| 154 | + |
| 155 | + results := ceh.Run(&runContext) |
| 156 | + |
| 157 | + // Should return error result with verbose errors enabled |
| 158 | + require.Len(t, results, 1) |
| 159 | + assert.Equal(t, check.StatusError, results[0].Status) |
| 160 | + assert.Contains(t, results[0].Message, "failed to read /etc/hosts") |
| 161 | + assert.Contains(t, results[0].Message, "Permission denied") |
| 162 | +} |
0 commit comments