Skip to content

Commit 909986b

Browse files
committed
Added test for RetriggerOnScanTypeChange set to False for ScheduledScans
Signed-off-by: Ilyes Ben Dlala <ilyes.bendlala@iteratec.com>
1 parent ce6dcc1 commit 909986b

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

operator/controllers/execution/scantype_controller_test.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var _ = Describe("ScanType controller", func() {
3333

3434
createNamespace(ctx, namespace)
3535
createScanType(ctx, namespace)
36-
scheduledScan := createScheduledScan(ctx, namespace)
36+
scheduledScan := createScheduledScan(ctx, namespace, true)
3737

3838
// ensure that the ScheduledScan has been triggered
3939
waitForScheduledScanToBeTriggered(ctx, namespace)
@@ -74,7 +74,7 @@ var _ = Describe("ScanType controller", func() {
7474

7575
createNamespace(ctx, namespace)
7676
createScanType(ctx, namespace)
77-
scheduledScan := createScheduledScan(ctx, namespace)
77+
scheduledScan := createScheduledScan(ctx, namespace, true)
7878

7979
// ensure that the ScheduledScan has been triggered
8080
waitForScheduledScanToBeTriggered(ctx, namespace)
@@ -96,6 +96,47 @@ var _ = Describe("ScanType controller", func() {
9696
}, timeout, interval).Should(BeTrue(), "Scan was restarted without need")
9797
})
9898
})
99+
100+
Context("Should not trigger rescan when RetriggerOnScanTypeChange is set to False", func() {
101+
It("Should restart a scheduledScan when RetriggerOnScanTypeChange is set to True", func() {
102+
ctx := context.Background()
103+
namespace := "scantype-retrigger-on-scantype-false-test"
104+
105+
createNamespace(ctx, namespace)
106+
createScanType(ctx, namespace)
107+
scheduledScan := createScheduledScan(ctx, namespace, false)
108+
109+
// ensure that the ScheduledScan has been triggered
110+
waitForScheduledScanToBeTriggered(ctx, namespace)
111+
k8sClient.Get(ctx, types.NamespacedName{Name: "test-scan", Namespace: namespace}, &scheduledScan)
112+
initialExecutionTime := *scheduledScan.Status.LastScheduleTime
113+
114+
// wait at least one second to ensure that the unix timestamps are at least one second apart.
115+
time.Sleep(1 * time.Second)
116+
117+
By("Update ScanType to trigger rescan")
118+
var scanType executionv1.ScanType
119+
k8sClient.Get(ctx, types.NamespacedName{Name: "nmap", Namespace: namespace}, &scanType)
120+
if scanType.ObjectMeta.Annotations == nil {
121+
scanType.ObjectMeta.Annotations = map[string]string{}
122+
}
123+
scanType.ObjectMeta.Annotations["foobar.securecodebox.io/example"] = "barfoo"
124+
err := k8sClient.Update(ctx, &scanType)
125+
if err != nil {
126+
panic(err)
127+
}
128+
129+
By("Controller should set the lastScheduled Timestamp to the past to force a re-scan")
130+
Eventually(func() bool {
131+
err := k8sClient.Get(ctx, types.NamespacedName{Name: "test-scan", Namespace: namespace}, &scheduledScan)
132+
if errors.IsNotFound(err) {
133+
panic("ScheduledScan should be present for this check!")
134+
}
135+
136+
return scheduledScan.Status.LastScheduleTime.Unix() == initialExecutionTime.Unix()
137+
}, timeout, interval).Should(BeTrue())
138+
})
139+
})
99140
})
100141

101142
func waitForScheduledScanToBeTriggered(ctx context.Context, namespace string) {

operator/controllers/execution/test_utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ func createScanType(ctx context.Context, namespace string) {
6363
Expect(k8sClient.Create(ctx, scanType)).Should(Succeed())
6464
}
6565

66-
func createScheduledScan(ctx context.Context, namespace string) executionv1.ScheduledScan {
66+
func createScheduledScan(ctx context.Context, namespace string, retriggerOnScanTypeChange bool) executionv1.ScheduledScan {
6767
scheduledScan := executionv1.ScheduledScan{
6868
ObjectMeta: metav1.ObjectMeta{
6969
Name: "test-scan",
7070
Namespace: namespace,
7171
},
7272
Spec: executionv1.ScheduledScanSpec{
7373
Interval: metav1.Duration{Duration: 42 * time.Hour},
74-
RetriggerOnScanTypeChange: true,
74+
RetriggerOnScanTypeChange: retriggerOnScanTypeChange,
7575
ScanSpec: &executionv1.ScanSpec{
7676
ScanType: "nmap",
7777
Parameters: []string{"scanme.nmap.org"},

0 commit comments

Comments
 (0)