Skip to content

Commit 0bdc038

Browse files
committed
#275 Added tests that test the ScheduledScan ConcurrencyPolicy
Signed-off-by: Ilyes Ben Dlala <ilyes.bendlala@iteratec.com>
1 parent 249054c commit 0bdc038

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
@@ -33,7 +33,7 @@ var _ = Describe("ScanType controller", func() {
3333

3434
createNamespace(ctx, namespace)
3535
createScanType(ctx, namespace)
36-
scheduledScan := createScheduledScan(ctx, namespace, true)
36+
scheduledScan := createScheduledScan(ctx, namespace, true, 42*time.Hour, executionv1.ForbidConcurrent)
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, true)
77+
scheduledScan := createScheduledScan(ctx, namespace, true, 42*time.Hour, executionv1.ForbidConcurrent)
7878

7979
// ensure that the ScheduledScan has been triggered
8080
waitForScheduledScanToBeTriggered(ctx, namespace)
@@ -104,7 +104,7 @@ var _ = Describe("ScanType controller", func() {
104104

105105
createNamespace(ctx, namespace)
106106
createScanType(ctx, namespace)
107-
scheduledScan := createScheduledScan(ctx, namespace, false)
107+
scheduledScan := createScheduledScan(ctx, namespace, false, 42*time.Hour, executionv1.ForbidConcurrent)
108108

109109
// ensure that the ScheduledScan has been triggered
110110
waitForScheduledScanToBeTriggered(ctx, namespace)

operator/controllers/execution/scheduledscan_controller_test.go

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

66
import (
77
"context"
8+
"time"
89

910
. "github.com/onsi/ginkgo"
1011
. "github.com/onsi/gomega"
@@ -71,7 +72,7 @@ var _ = Describe("ScheduledScan controller", func() {
7172

7273
createNamespace(ctx, namespace)
7374
createScanType(ctx, namespace)
74-
scheduledScan := createScheduledScan(ctx, namespace, true)
75+
scheduledScan := createScheduledScan(ctx, namespace, true, 42*time.Hour, executionv1.ForbidConcurrent)
7576

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

operator/controllers/execution/test_utils_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ 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, retriggerOnScanTypeChange bool) executionv1.ScheduledScan {
66+
func createScheduledScan(ctx context.Context, namespace string, retriggerOnScanTypeChange bool, interval time.Duration, concurrencyPolicy executionv1.ConcurrencyPolicy) executionv1.ScheduledScan {
6767
namespaceLocalResourceMode := executionv1.NamespaceLocal
6868

6969
scheduledScan := executionv1.ScheduledScan{
@@ -72,8 +72,9 @@ func createScheduledScan(ctx context.Context, namespace string, retriggerOnScanT
7272
Namespace: namespace,
7373
},
7474
Spec: executionv1.ScheduledScanSpec{
75-
Interval: metav1.Duration{Duration: 42 * time.Hour},
75+
Interval: metav1.Duration{Duration: interval},
7676
RetriggerOnScanTypeChange: retriggerOnScanTypeChange,
77+
ConcurrencyPolicy: concurrencyPolicy,
7778
ScanSpec: &executionv1.ScanSpec{
7879
ScanType: "nmap",
7980
ResourceMode: &namespaceLocalResourceMode,

0 commit comments

Comments
 (0)