@@ -9,6 +9,7 @@ package controllers
99
1010import (
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})
0 commit comments