@@ -5,6 +5,7 @@ package controllers
55
66import (
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})
0 commit comments