Skip to content

Commit 91686b8

Browse files
committed
#275 Added tests that test the ScheduledScan ConcurrencyPolicy
Signed-off-by: Ilyes Ben Dlala <ilyes.bendlala@iteratec.com>
1 parent 9abe016 commit 91686b8

3 files changed

Lines changed: 76 additions & 6 deletions

File tree

‎operator/controllers/execution/scantype_controller_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var _ = Describe("ScanType controller", func() {
3636

3737
createNamespace(ctx, namespace)
3838
createScanType(ctx, namespace)
39-
scheduledScan := createScheduledScanWithInterval(ctx, namespace, true)
39+
scheduledScan := createScheduledScanWithInterval(ctx, namespace, true, 42*time.Hour, executionv1.ForbidConcurrent)
4040

4141
// ensure that the ScheduledScan has been triggered
4242
waitForScheduledScanToBeTriggered(ctx, namespace, timeout)
@@ -77,7 +77,7 @@ var _ = Describe("ScanType controller", func() {
7777

7878
createNamespace(ctx, namespace)
7979
createScanType(ctx, namespace)
80-
scheduledScan := createScheduledScanWithInterval(ctx, namespace, true)
80+
scheduledScan := createScheduledScanWithInterval(ctx, namespace, true, 42*time.Hour, executionv1.ForbidConcurrent)
8181

8282
// ensure that the ScheduledScan has been triggered
8383
waitForScheduledScanToBeTriggered(ctx, namespace, timeout)
@@ -107,7 +107,7 @@ var _ = Describe("ScanType controller", func() {
107107

108108
createNamespace(ctx, namespace)
109109
createScanType(ctx, namespace)
110-
scheduledScan := createScheduledScanWithInterval(ctx, namespace, false)
110+
scheduledScan := createScheduledScanWithInterval(ctx, namespace, false, 42*time.Hour, executionv1.ForbidConcurrent)
111111

112112
// ensure that the ScheduledScan has been triggered
113113
waitForScheduledScanToBeTriggered(ctx, namespace, timeout)

‎operator/controllers/execution/scheduledscan_controller_test.go‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package controllers
99

1010
import (
1111
"context"
12+
"time"
1213

1314
. "github.com/onsi/ginkgo"
1415
. "github.com/onsi/gomega"
@@ -75,7 +76,7 @@ var _ = Describe("ScheduledScan controller", func() {
7576

7677
createNamespace(ctx, namespace)
7778
createScanType(ctx, namespace)
78-
scheduledScan := createScheduledScanWithInterval(ctx, namespace, true)
79+
scheduledScan := createScheduledScanWithInterval(ctx, namespace, true, 42*time.Hour, executionv1.ForbidConcurrent)
7980

8081
var scanlist executionv1.ScanList
8182
// ensure that the ScheduledScan has been triggered
@@ -108,4 +109,72 @@ var _ = Describe("ScheduledScan controller", func() {
108109
Expect(scheduledScan.Status.Findings.FindingCategories).Should(Equal(map[string]uint64{"Open Port": 42}))
109110
})
110111
})
112+
113+
Context("A Scan is triggred due to a Scheduled Scan with a ConcurrencyPolicy", func() {
114+
It("A second scheduled scan should not start before the first one is finished if the concurency policy is set to ForbidConcurrent", func() {
115+
116+
ctx := context.Background()
117+
namespace := "scheduled-scan-triggerd-concurrency-forbid-test"
118+
createNamespace(ctx, namespace)
119+
createScanType(ctx, namespace)
120+
createScheduledScan(ctx, namespace, true, 1*time.Second, executionv1.ForbidConcurrent)
121+
122+
var scanlist executionv1.ScanList
123+
// ensure that the ScheduledScan has been triggered
124+
waitForScheduledScanToBeTriggered(ctx, namespace)
125+
k8sClient.List(ctx, &scanlist, client.InNamespace(namespace))
126+
127+
Expect(scanlist.Items).Should(HaveLen(1))
128+
time.Sleep(2 * time.Second)
129+
// make sure that no second scan has been triggered
130+
k8sClient.List(ctx, &scanlist, client.InNamespace(namespace))
131+
Expect(scanlist.Items).Should(HaveLen(1))
132+
133+
})
134+
135+
It("A second scheduled scan should start before the first one is finished if the concurency policy is set to AllowConcurrent", func() {
136+
137+
ctx := context.Background()
138+
namespace := "scheduled-scan-triggerd-concurrency-allow-test"
139+
createNamespace(ctx, namespace)
140+
createScanType(ctx, namespace)
141+
createScheduledScan(ctx, namespace, true, 1*time.Second, executionv1.AllowConcurrent)
142+
143+
var scanlist executionv1.ScanList
144+
// ensure that the ScheduledScan has been triggered
145+
waitForScheduledScanToBeTriggered(ctx, namespace)
146+
k8sClient.List(ctx, &scanlist, client.InNamespace(namespace))
147+
Expect(scanlist.Items).ShouldNot(BeEmpty())
148+
149+
time.Sleep(2 * time.Second)
150+
151+
// make sure more than one scan has been triggered
152+
k8sClient.List(ctx, &scanlist, client.InNamespace(namespace))
153+
Expect(scanlist.Items).ShouldNot(HaveLen(1))
154+
})
155+
156+
It("A second scheduled scan should replace the first one, before the first one is finished if the concurency policy is set to ReplaceConcurrent", func() {
157+
158+
ctx := context.Background()
159+
namespace := "scheduled-scan-triggerd-concurrency-replace-test"
160+
createNamespace(ctx, namespace)
161+
createScanType(ctx, namespace)
162+
createScheduledScan(ctx, namespace, true, 1*time.Second, executionv1.ReplaceConcurrent)
163+
164+
var scanlist executionv1.ScanList
165+
// ensure that the ScheduledScan has been triggered
166+
waitForScheduledScanToBeTriggered(ctx, namespace)
167+
k8sClient.List(ctx, &scanlist, client.InNamespace(namespace))
168+
Expect(scanlist.Items).Should(HaveLen(1))
169+
firstScanName := scanlist.Items[0].Name
170+
171+
time.Sleep(2 * time.Second)
172+
173+
// make sure the first scan has been replaced
174+
k8sClient.List(ctx, &scanlist, client.InNamespace(namespace))
175+
secondScanName := scanlist.Items[0].Name
176+
Expect(scanlist.Items).Should(HaveLen(1))
177+
Expect(firstScanName).ShouldNot(Equal(secondScanName))
178+
})
179+
})
111180
})

‎operator/controllers/execution/test_utils_test.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func createScanType(ctx context.Context, namespace string) {
6666
Expect(k8sClient.Create(ctx, scanType)).Should(Succeed())
6767
}
6868

69-
func createScheduledScanWithInterval(ctx context.Context, namespace string, retriggerOnScanTypeChange bool) executionv1.ScheduledScan {
69+
func createScheduledScanWithInterval(ctx context.Context, namespace string, retriggerOnScanTypeChange bool, interval time.Duration, concurrencyPolicy executionv1.ConcurrencyPolicy) executionv1.ScheduledScan {
7070
namespaceLocalResourceMode := executionv1.NamespaceLocal
7171

7272
scheduledScan := executionv1.ScheduledScan{
@@ -75,8 +75,9 @@ func createScheduledScanWithInterval(ctx context.Context, namespace string, retr
7575
Namespace: namespace,
7676
},
7777
Spec: executionv1.ScheduledScanSpec{
78-
Interval: metav1.Duration{Duration: 42 * time.Hour},
78+
Interval: metav1.Duration{Duration: interval},
7979
RetriggerOnScanTypeChange: retriggerOnScanTypeChange,
80+
ConcurrencyPolicy: concurrencyPolicy,
8081
ScanSpec: &executionv1.ScanSpec{
8182
ScanType: "nmap",
8283
ResourceMode: &namespaceLocalResourceMode,

0 commit comments

Comments
 (0)