github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/cmd/horologium/main_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "testing" 21 "time" 22 23 "k8s.io/test-infra/prow/config" 24 "k8s.io/test-infra/prow/kube" 25 ) 26 27 type fakeKube struct { 28 jobs []kube.ProwJob 29 created bool 30 } 31 32 func (fk *fakeKube) ListProwJobs(ls map[string]string) ([]kube.ProwJob, error) { 33 return fk.jobs, nil 34 } 35 36 func (fk *fakeKube) CreateProwJob(j kube.ProwJob) (kube.ProwJob, error) { 37 fk.created = true 38 return j, nil 39 } 40 41 // Assumes there is one periodic job called "p" with an interval of one minute. 42 func TestSync(t *testing.T) { 43 testcases := []struct { 44 testName string 45 46 jobName string 47 jobComplete bool 48 jobStartTimeAgo time.Duration 49 50 shouldStart bool 51 }{ 52 { 53 testName: "no job", 54 shouldStart: true, 55 }, 56 { 57 testName: "job with other name", 58 jobName: "not-j", 59 jobComplete: true, 60 jobStartTimeAgo: time.Hour, 61 shouldStart: true, 62 }, 63 { 64 testName: "old, complete job", 65 jobName: "j", 66 jobComplete: true, 67 jobStartTimeAgo: time.Hour, 68 shouldStart: true, 69 }, 70 { 71 testName: "old, incomplete job", 72 jobName: "j", 73 jobComplete: false, 74 jobStartTimeAgo: time.Hour, 75 shouldStart: false, 76 }, 77 { 78 testName: "new, complete job", 79 jobName: "j", 80 jobComplete: true, 81 jobStartTimeAgo: time.Second, 82 shouldStart: false, 83 }, 84 { 85 testName: "new, incomplete job", 86 jobName: "j", 87 jobComplete: false, 88 jobStartTimeAgo: time.Second, 89 shouldStart: false, 90 }, 91 } 92 for _, tc := range testcases { 93 cfg := config.Config{ 94 Periodics: []config.Periodic{{Name: "j"}}, 95 } 96 cfg.Periodics[0].SetInterval(time.Minute) 97 98 var jobs []kube.ProwJob 99 now := time.Now() 100 if tc.jobName != "" { 101 jobs = []kube.ProwJob{{ 102 Spec: kube.ProwJobSpec{ 103 Type: kube.PeriodicJob, 104 Job: tc.jobName, 105 }, 106 Status: kube.ProwJobStatus{ 107 StartTime: now.Add(-tc.jobStartTimeAgo), 108 }, 109 }} 110 if tc.jobComplete { 111 jobs[0].Status.CompletionTime = now.Add(-time.Millisecond) 112 } 113 } 114 kc := &fakeKube{jobs: jobs} 115 if err := sync(kc, &cfg, now); err != nil { 116 t.Fatalf("For case %s, didn't expect error: %v", tc.testName, err) 117 } 118 if tc.shouldStart != kc.created { 119 t.Errorf("For case %s, did the wrong thing.", tc.testName) 120 } 121 } 122 }