Skip to content

Commit aaed2c5

Browse files
committed
Refactor auto-discovery controller suite tests to remove duplicate code
Correct a minor typographical error in service_scan_controller.go regarding the RequeueAfter value. Signed-off-by: Boris Shek <boris.shek@iteratec.com>
1 parent 4a2e5a2 commit aaed2c5

3 files changed

Lines changed: 48 additions & 248 deletions

File tree

auto-discovery/kubernetes/controllers/service_scan_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (r *ServiceScanReconciler) Reconcile(ctx context.Context, req ctrl.Request)
187187
// Requeue to allow scan to be created when the user installs the scanType
188188
return ctrl.Result{
189189
Requeue: true,
190-
RequeueAfter: Config.ServiceAutoDiscovery.PassiveReconcileInterval.Duration,
190+
RequeueAfter: r.Config.ServiceAutoDiscovery.PassiveReconcileInterval.Duration,
191191
}, nil
192192
} else if err != nil {
193193
return ctrl.Result{

auto-discovery/kubernetes/controllers/suite_test.go

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"path/filepath"
1010
"testing"
11-
"time"
1211

1312
. "github.com/onsi/ginkgo"
1413
. "github.com/onsi/gomega"
@@ -21,11 +20,8 @@ import (
2120

2221
ctrl "sigs.k8s.io/controller-runtime"
2322

24-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25-
2623
//+kubebuilder:scaffold:imports
2724

28-
config "github.com/secureCodeBox/secureCodeBox/auto-discovery/kubernetes/pkg/config"
2925
executionv1 "github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1"
3026
)
3127

@@ -72,105 +68,13 @@ var _ = BeforeSuite(func() {
7268
})
7369
Expect(err).ToNot(HaveOccurred())
7470

75-
config := config.AutoDiscoveryConfig{
76-
Cluster: config.ClusterConfig{
77-
Name: "test-cluster",
78-
},
79-
ServiceAutoDiscovery: config.ServiceAutoDiscoveryConfig{
80-
PassiveReconcileInterval: metav1.Duration{Duration: 1 * time.Second},
81-
ScanConfigs: []config.ScanConfig{
82-
{
83-
Name: "test-scan-0",
84-
RepeatInterval: metav1.Duration{Duration: time.Hour},
85-
Annotations: map[string]string{},
86-
Labels: map[string]string{},
87-
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
88-
ScanType: "nmap",
89-
HookSelector: metav1.LabelSelector{
90-
MatchLabels: map[string]string{
91-
"foo": "bar",
92-
},
93-
},
94-
},
95-
{
96-
Name: "test-scan-1",
97-
RepeatInterval: metav1.Duration{Duration: time.Hour},
98-
Annotations: map[string]string{},
99-
Labels: map[string]string{},
100-
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
101-
ScanType: "nmap",
102-
HookSelector: metav1.LabelSelector{
103-
MatchLabels: map[string]string{
104-
"foo": "bar",
105-
},
106-
},
107-
},
108-
},
109-
},
110-
ContainerAutoDiscovery: config.ContainerAutoDiscoveryConfig{
111-
PassiveReconcileInterval: metav1.Duration{Duration: 1 * time.Second},
112-
ImagePullSecretConfig: config.ImagePullSecretConfig{
113-
MapImagePullSecretsToEnvironmentVariables: true,
114-
UsernameEnvironmentVariableName: "username",
115-
PasswordNameEnvironmentVariableName: "password",
116-
},
117-
ScanConfigs: []config.ScanConfig{
118-
{
119-
Name: "test-scan",
120-
RepeatInterval: metav1.Duration{Duration: time.Hour},
121-
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
122-
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
123-
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
124-
ScanType: "nmap",
125-
HookSelector: metav1.LabelSelector{
126-
MatchExpressions: []metav1.LabelSelectorRequirement{
127-
{
128-
Operator: metav1.LabelSelectorOpIn,
129-
Key: "foo",
130-
Values: []string{"bar", "baz"},
131-
},
132-
{
133-
Operator: metav1.LabelSelectorOpDoesNotExist,
134-
Key: "foo",
135-
},
136-
},
137-
},
138-
},
139-
{
140-
Name: "test-scan-two",
141-
RepeatInterval: metav1.Duration{Duration: time.Hour},
142-
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
143-
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
144-
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
145-
ScanType: "nmap",
146-
HookSelector: metav1.LabelSelector{
147-
MatchExpressions: []metav1.LabelSelectorRequirement{
148-
{
149-
Operator: metav1.LabelSelectorOpIn,
150-
Key: "foo",
151-
Values: []string{"bar", "baz"},
152-
},
153-
{
154-
Operator: metav1.LabelSelectorOpDoesNotExist,
155-
Key: "foo",
156-
},
157-
},
158-
},
159-
},
160-
},
161-
},
162-
ResourceInclusion: config.ResourceInclusionConfig{
163-
Mode: config.EnabledPerResource,
164-
},
165-
}
166-
16771
// working config
16872
err = (&ServiceScanReconciler{
16973
Client: k8sManager.GetClient(),
17074
Scheme: k8sManager.GetScheme(),
17175
Recorder: k8sManager.GetEventRecorderFor("ServiceScanController"),
17276
Log: ctrl.Log.WithName("controllers").WithName("ServiceScanController"),
173-
Config: config,
77+
Config: AutoDiscoveryConfigMock,
17478
}).SetupWithManager(k8sManager)
17579
Expect(err).ToNot(HaveOccurred())
17680

@@ -180,7 +84,7 @@ var _ = BeforeSuite(func() {
18084
Scheme: k8sManager.GetScheme(),
18185
Recorder: k8sManager.GetEventRecorderFor("ContainerScanController"),
18286
Log: ctrl.Log.WithName("controllers").WithName("ContainerScanController"),
183-
Config: config,
87+
Config: AutoDiscoveryConfigMock,
18488
}).SetupWithManager(k8sManager)
18589
Expect(err).ToNot(HaveOccurred())
18690

auto-discovery/kubernetes/controllers/suite_test_util.go

Lines changed: 45 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -11,174 +11,70 @@ import (
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212
)
1313

14-
var Config = config.AutoDiscoveryConfig{
15-
Cluster: config.ClusterConfig{
16-
Name: "test-cluster",
17-
},
18-
ServiceAutoDiscovery: config.ServiceAutoDiscoveryConfig{
19-
PassiveReconcileInterval: metav1.Duration{Duration: 1 * time.Second},
20-
ScanConfigs: []config.ScanConfig{
21-
{
22-
Name: "test-scan-0",
23-
RepeatInterval: metav1.Duration{Duration: time.Hour},
24-
Annotations: map[string]string{},
25-
Labels: map[string]string{},
26-
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
27-
ScanType: "nmap",
28-
HookSelector: metav1.LabelSelector{
29-
MatchLabels: map[string]string{
30-
"foo": "bar",
31-
},
32-
},
33-
},
34-
{
35-
Name: "test-scan-1",
36-
RepeatInterval: metav1.Duration{Duration: time.Hour},
37-
Annotations: map[string]string{},
38-
Labels: map[string]string{},
39-
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
40-
ScanType: "nmap",
41-
HookSelector: metav1.LabelSelector{
42-
MatchLabels: map[string]string{
43-
"foo": "bar",
44-
},
45-
},
14+
// newServiceScanConfigMock creates a mock scan configuration specifically for service auto-discovery
15+
func newServiceScanConfigMock(name string) config.ScanConfig {
16+
return config.ScanConfig{
17+
Name: name,
18+
RepeatInterval: metav1.Duration{Duration: time.Hour},
19+
Annotations: map[string]string{},
20+
Labels: map[string]string{},
21+
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
22+
ScanType: "nmap",
23+
HookSelector: metav1.LabelSelector{
24+
MatchLabels: map[string]string{
25+
"foo": "bar",
4626
},
4727
},
48-
},
49-
ContainerAutoDiscovery: config.ContainerAutoDiscoveryConfig{
50-
PassiveReconcileInterval: metav1.Duration{Duration: 1 * time.Second},
51-
ScanConfigs: []config.ScanConfig{
52-
{
53-
Name: "test-scan",
54-
RepeatInterval: metav1.Duration{Duration: time.Hour},
55-
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
56-
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
57-
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
58-
ScanType: "nmap",
59-
HookSelector: metav1.LabelSelector{
60-
MatchExpressions: []metav1.LabelSelectorRequirement{
61-
{
62-
Operator: metav1.LabelSelectorOpIn,
63-
Key: "foo",
64-
Values: []string{"bar", "baz"},
65-
},
66-
{
67-
Operator: metav1.LabelSelectorOpDoesNotExist,
68-
Key: "foo",
69-
},
70-
},
28+
}
29+
}
30+
31+
// newContainerScanConfigMock creates a mock scan configuration specifically for container auto-discovery
32+
func newContainerScanConfigMock(name string) config.ScanConfig {
33+
return config.ScanConfig{
34+
Name: name,
35+
RepeatInterval: metav1.Duration{Duration: time.Hour},
36+
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
37+
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
38+
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
39+
ScanType: "nmap",
40+
HookSelector: metav1.LabelSelector{
41+
MatchExpressions: []metav1.LabelSelectorRequirement{
42+
{
43+
Operator: metav1.LabelSelectorOpIn,
44+
Key: "foo",
45+
Values: []string{"bar", "baz"},
7146
},
72-
},
73-
{
74-
Name: "test-scan-two",
75-
RepeatInterval: metav1.Duration{Duration: time.Hour},
76-
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
77-
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
78-
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
79-
ScanType: "nmap",
80-
HookSelector: metav1.LabelSelector{
81-
MatchExpressions: []metav1.LabelSelectorRequirement{
82-
{
83-
Operator: metav1.LabelSelectorOpIn,
84-
Key: "foo",
85-
Values: []string{"bar", "baz"},
86-
},
87-
{
88-
Operator: metav1.LabelSelectorOpDoesNotExist,
89-
Key: "foo",
90-
},
91-
},
47+
{
48+
Operator: metav1.LabelSelectorOpDoesNotExist,
49+
Key: "foo",
9250
},
9351
},
9452
},
95-
},
96-
ResourceInclusion: config.ResourceInclusionConfig{
97-
Mode: config.EnabledPerResource,
98-
},
53+
}
9954
}
10055

101-
// broken config has two scans (per autodiscovery) defined with the same name which will trigger an error during controller setup
102-
var BrokenConfig = config.AutoDiscoveryConfig{
56+
// AutoDiscoveryConfigMock holds the complete mock configuration
57+
var AutoDiscoveryConfigMock = config.AutoDiscoveryConfig{
10358
Cluster: config.ClusterConfig{
10459
Name: "test-cluster",
10560
},
10661
ServiceAutoDiscovery: config.ServiceAutoDiscoveryConfig{
10762
PassiveReconcileInterval: metav1.Duration{Duration: 1 * time.Second},
10863
ScanConfigs: []config.ScanConfig{
109-
{
110-
Name: "test-scan",
111-
RepeatInterval: metav1.Duration{Duration: time.Hour},
112-
Annotations: map[string]string{},
113-
Labels: map[string]string{},
114-
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
115-
ScanType: "nmap",
116-
HookSelector: metav1.LabelSelector{
117-
MatchLabels: map[string]string{
118-
"foo": "bar",
119-
},
120-
},
121-
},
122-
{
123-
Name: "test-scan",
124-
RepeatInterval: metav1.Duration{Duration: time.Hour},
125-
Annotations: map[string]string{},
126-
Labels: map[string]string{},
127-
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
128-
ScanType: "nmap",
129-
HookSelector: metav1.LabelSelector{
130-
MatchLabels: map[string]string{
131-
"foo": "bar",
132-
},
133-
},
134-
},
64+
newServiceScanConfigMock("test-scan-0"),
65+
newServiceScanConfigMock("test-scan-1"),
13566
},
13667
},
13768
ContainerAutoDiscovery: config.ContainerAutoDiscoveryConfig{
13869
PassiveReconcileInterval: metav1.Duration{Duration: 1 * time.Second},
70+
ImagePullSecretConfig: config.ImagePullSecretConfig{
71+
MapImagePullSecretsToEnvironmentVariables: true,
72+
UsernameEnvironmentVariableName: "username",
73+
PasswordNameEnvironmentVariableName: "password",
74+
},
13975
ScanConfigs: []config.ScanConfig{
140-
{
141-
Name: "test-scan",
142-
RepeatInterval: metav1.Duration{Duration: time.Hour},
143-
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
144-
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
145-
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
146-
ScanType: "nmap",
147-
HookSelector: metav1.LabelSelector{
148-
MatchExpressions: []metav1.LabelSelectorRequirement{
149-
{
150-
Operator: metav1.LabelSelectorOpIn,
151-
Key: "foo",
152-
Values: []string{"bar", "baz"},
153-
},
154-
{
155-
Operator: metav1.LabelSelectorOpDoesNotExist,
156-
Key: "foo",
157-
},
158-
},
159-
},
160-
},
161-
{
162-
Name: "test-scan",
163-
RepeatInterval: metav1.Duration{Duration: time.Hour},
164-
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
165-
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
166-
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
167-
ScanType: "nmap",
168-
HookSelector: metav1.LabelSelector{
169-
MatchExpressions: []metav1.LabelSelectorRequirement{
170-
{
171-
Operator: metav1.LabelSelectorOpIn,
172-
Key: "foo",
173-
Values: []string{"bar", "baz"},
174-
},
175-
{
176-
Operator: metav1.LabelSelectorOpDoesNotExist,
177-
Key: "foo",
178-
},
179-
},
180-
},
181-
},
76+
newContainerScanConfigMock("test-scan"),
77+
newContainerScanConfigMock("test-scan-two"),
18278
},
18379
},
18480
ResourceInclusion: config.ResourceInclusionConfig{

0 commit comments

Comments
 (0)