-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathscans_test.go
More file actions
219 lines (196 loc) · 6.13 KB
/
scans_test.go
File metadata and controls
219 lines (196 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"context"
"errors"
"testing"
v1 "github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
type MockClientProvider struct {
client.Client
namespace string
err error
}
func (m *MockClientProvider) GetClient(_ *genericclioptions.ConfigFlags) (client.Client, string, error) {
return m.Client, m.namespace, m.err
}
type testcase struct {
name string
args []string
expectedError error
expectedScan *expectedScan
}
type expectedScan struct {
name string
scanType string
namespace string
parameters []string
}
func TestScanCommand(t *testing.T) {
testcases := []testcase{
{
name: "Should create nmap scan with a single parameter",
args: []string{"scan", "nmap", "--", "scanme.nmap.org"},
expectedError: nil,
expectedScan: &expectedScan{
name: "nmap",
scanType: "nmap",
namespace: "default",
parameters: []string{"scanme.nmap.org"},
},
},
{
name: "Should create nmap scan with multiple parameters",
args: []string{"scan", "nmap", "--", "scanme.nmap.org", "-p", "90"},
expectedError: nil,
expectedScan: &expectedScan{
name: "nmap",
scanType: "nmap",
namespace: "default",
parameters: []string{"scanme.nmap.org", "-p", "90"},
},
},
{
name: "Should use --name flag as the name of the scan if provided",
args: []string{"scan", "--name", "scanme-nmap-org", "nmap", "--", "scanme.nmap.org"},
expectedError: nil,
expectedScan: &expectedScan{
scanType: "nmap",
name: "scanme-nmap-org",
namespace: "default",
parameters: []string{"scanme.nmap.org"},
},
},
{
name: "Should create nmap in a custom namespace",
args: []string{"scan", "--namespace", "foobar", "nmap", "--", "scanme.nmap.org"},
expectedError: nil,
expectedScan: &expectedScan{
name: "nmap",
scanType: "nmap",
namespace: "foobar",
parameters: []string{"scanme.nmap.org"},
},
},
{
name: "Flags provided after the `--` seperator should be passed as parameters, not flags",
args: []string{"scan", "--namespace", "foobar", "kubeaudit", "--", "--namespace", "some-other-namespace"},
expectedError: nil,
expectedScan: &expectedScan{
name: "kubeaudit",
scanType: "kubeaudit",
namespace: "foobar",
parameters: []string{"--namespace", "some-other-namespace"},
},
},
{
name: "Should throw an error when no parameters are provided",
args: []string{"scan", "nmap"},
expectedError: errors.New("you must use '--' to separate scan parameters"),
expectedScan: nil,
},
{
name: "Should throw an error when no `--` separator is used before the scan parameters",
args: []string{"scan", "nmap", "scanme.nmap.org"},
expectedError: errors.New("you must use '--' to separate scan parameters"),
expectedScan: nil,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
scheme := runtime.NewScheme()
utilruntime.Must(v1.AddToScheme(scheme))
client := fake.NewClientBuilder().WithScheme(scheme).Build()
clientProvider = &MockClientProvider{
Client: client,
namespace: "default",
err: nil,
}
rootCmd := NewRootCommand()
rootCmd.SetArgs(tc.args)
rootCmd.SilenceUsage = true
err := rootCmd.Execute()
assert.Equal(t, tc.expectedError, err, "error returned by scan should match")
if tc.expectedScan != nil {
scans := &v1.ScanList{}
listErr := client.List(context.Background(), scans)
assert.Nil(t, listErr, "failed to list scans")
assert.Len(t, scans.Items, 1, "expected 1 scan to be created")
scan := scans.Items[0]
assert.Equal(t, tc.expectedScan.name, scan.Name)
assert.Equal(t, tc.expectedScan.namespace, scan.Namespace)
assert.Equal(t, tc.expectedScan.scanType, scan.Spec.ScanType)
assert.Equal(t, tc.expectedScan.parameters, scan.Spec.Parameters)
}
})
}
}
func TestScanCommandCompletion(t *testing.T) {
testcases := []struct {
name string
namespace string
expectedTypes []string
setup func(client.Client)
expectedError bool
}{
{
name: "Should return all scan types in the namespace",
namespace: "default",
expectedTypes: []string{"nmap", "zap"},
setup: func(client client.Client) {
scanTypes := []v1.ScanType{
{ObjectMeta: metav1.ObjectMeta{Name: "nmap", Namespace: "default"}},
{ObjectMeta: metav1.ObjectMeta{Name: "zap", Namespace: "default"}},
}
for _, st := range scanTypes {
err := client.Create(context.Background(), &st)
if err != nil {
t.Fatalf("Failed to create scan type: %v", err)
}
}
},
expectedError: false,
},
{
name: "Should return empty list when no scan types exist",
namespace: "empty-ns",
expectedTypes: []string{},
setup: func(client.Client) {},
expectedError: false,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
scheme := runtime.NewScheme()
utilruntime.Must(v1.AddToScheme(scheme))
client := fake.NewClientBuilder().WithScheme(scheme).Build()
if tc.setup != nil {
tc.setup(client)
}
cmd := NewScanCommand()
clientProvider = &TestClientProvider{
Client: client,
namespace: tc.namespace,
err: nil,
}
kubeconfigArgs = genericclioptions.NewConfigFlags(true)
completions, directive := cmd.ValidArgsFunction(cmd, []string{}, "")
if tc.expectedError {
assert.Equal(t, cobra.ShellCompDirectiveError, directive)
} else {
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
assert.ElementsMatch(t, tc.expectedTypes, completions)
}
})
}
}