@@ -36,6 +36,7 @@ import (
3636)
3737
3838var (
39+ allNs = ""
3940 testNs = "foo"
4041 simpleStep = tb .Step (testNs , tb .StepCommand ("/mycmd" ))
4142 simpleTask = tb .Task ("test-task" , tb .TaskSpec (simpleStep ))
@@ -109,7 +110,7 @@ func TestTaskRunCheckTimeouts(t *testing.T) {
109110 }
110111
111112 th .SetTaskRunCallbackFunc (f )
112- th .CheckTimeouts (c .Kube , c .Pipeline )
113+ th .CheckTimeouts (allNs , c .Kube , c .Pipeline )
113114
114115 for _ , tc := range []struct {
115116 name string
@@ -157,6 +158,84 @@ func TestTaskRunCheckTimeouts(t *testing.T) {
157158
158159}
159160
161+ func TestTaskRunSingleNamespaceCheckTimeouts (t * testing.T ) {
162+ taskRunTimedout := tb .TaskRun ("test-taskrun-run-timedout-foo" , tb .TaskRunNamespace (testNs ), tb .TaskRunSpec (
163+ tb .TaskRunTaskRef (simpleTask .Name , tb .TaskRefAPIVersion ("a1" )),
164+ tb .TaskRunTimeout (1 * time .Second ),
165+ ), tb .TaskRunStatus (tb .StatusCondition (apis.Condition {
166+ Type : apis .ConditionSucceeded ,
167+ Status : corev1 .ConditionUnknown }),
168+ tb .TaskRunStartTime (time .Now ().Add (- 10 * time .Second )),
169+ ))
170+
171+ taskRunTimedoutOtherNS := tb .TaskRun ("test-taskrun-run-timedout-bar" , tb .TaskRunNamespace ("otherNS" ), tb .TaskRunSpec (
172+ tb .TaskRunTaskRef (simpleTask .Name , tb .TaskRefAPIVersion ("a1" )),
173+ tb .TaskRunTimeout (1 * time .Second ),
174+ ), tb .TaskRunStatus (tb .StatusCondition (apis.Condition {
175+ Type : apis .ConditionSucceeded ,
176+ Status : corev1 .ConditionUnknown }),
177+ tb .TaskRunStartTime (time .Now ().Add (- 10 * time .Second )),
178+ ))
179+
180+ d := test.Data {
181+ TaskRuns : []* v1alpha1.TaskRun {taskRunTimedout , taskRunTimedoutOtherNS },
182+ Tasks : []* v1alpha1.Task {simpleTask },
183+ Namespaces : []* corev1.Namespace {{
184+ ObjectMeta : metav1.ObjectMeta {
185+ Name : testNs ,
186+ },
187+ }},
188+ }
189+ ctx , _ := ttesting .SetupFakeContext (t )
190+ c , _ := test .SeedTestData (t , ctx , d )
191+ stopCh := make (chan struct {})
192+ defer close (stopCh )
193+ observer , _ := observer .New (zap .InfoLevel )
194+
195+ th := NewTimeoutHandler (stopCh , zap .New (observer ).Sugar ())
196+ gotCallback := sync.Map {}
197+ f := func (tr interface {}) {
198+ trNew := tr .(* v1alpha1.TaskRun )
199+ gotCallback .Store (trNew .Name , struct {}{})
200+ }
201+
202+ th .SetTaskRunCallbackFunc (f )
203+ th .CheckTimeouts (testNs , c .Kube , c .Pipeline )
204+
205+ for _ , tc := range []struct {
206+ name string
207+ taskRun * v1alpha1.TaskRun
208+ expectCallback bool
209+ }{{
210+ name : "timedout" ,
211+ taskRun : taskRunTimedout ,
212+ expectCallback : true ,
213+ }, {
214+ name : "timedout" ,
215+ taskRun : taskRunTimedoutOtherNS ,
216+ expectCallback : false ,
217+ }} {
218+ t .Run (tc .name , func (t * testing.T ) {
219+ if err := wait .PollImmediate (100 * time .Millisecond , 3 * time .Second , func () (bool , error ) {
220+ if tc .expectCallback {
221+ if _ , ok := gotCallback .Load (tc .taskRun .Name ); ok {
222+ return true , nil
223+ }
224+ return false , nil
225+ }
226+ // not expecting callback
227+ if _ , ok := gotCallback .Load (tc .taskRun .Name ); ok {
228+ return false , fmt .Errorf ("did not expect call back for %s why" , tc .taskRun .Name )
229+ }
230+ return true , nil
231+ }); err != nil {
232+ t .Fatalf ("Expected %s callback to be %t but got error: %s" , tc .name , tc .expectCallback , err )
233+ }
234+ })
235+ }
236+
237+ }
238+
160239func TestPipelinRunCheckTimeouts (t * testing.T ) {
161240 simplePipeline := tb .Pipeline ("test-pipeline" , tb .PipelineNamespace (testNs ), tb .PipelineSpec (
162241 tb .PipelineTask ("hello-world-1" , "hello-world" ),
@@ -235,7 +314,7 @@ func TestPipelinRunCheckTimeouts(t *testing.T) {
235314 }
236315
237316 th .SetPipelineRunCallbackFunc (f )
238- th .CheckTimeouts (c .Kube , c .Pipeline )
317+ th .CheckTimeouts (allNs , c .Kube , c .Pipeline )
239318 for _ , tc := range []struct {
240319 name string
241320 pr * v1alpha1.PipelineRun
@@ -314,7 +393,7 @@ func TestWithNoFunc(t *testing.T) {
314393 t .Fatal ("Expected CheckTimeouts function not to panic" )
315394 }
316395 }()
317- testHandler .CheckTimeouts (c .Kube , c .Pipeline )
396+ testHandler .CheckTimeouts (allNs , c .Kube , c .Pipeline )
318397
319398}
320399
0 commit comments