github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/cron/cron_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 cron 18 19 import ( 20 "testing" 21 22 "gopkg.in/robfig/cron.v2" 23 24 "k8s.io/test-infra/prow/config" 25 ) 26 27 func TestSync(t *testing.T) { 28 c := New() 29 initial := &config.Config{ 30 JobConfig: config.JobConfig{ 31 Periodics: []config.Periodic{ 32 { 33 Name: "interval", 34 Interval: "1m", 35 }, 36 { 37 Name: "cron", 38 Cron: "@every 1m", 39 }, 40 }, 41 }, 42 } 43 44 addAndUpdate := &config.Config{ 45 JobConfig: config.JobConfig{ 46 Periodics: []config.Periodic{ 47 { 48 Name: "interval", 49 Interval: "1m", 50 }, 51 { 52 Name: "cron", 53 Cron: "@every 1m", 54 }, 55 { 56 Name: "cron-2", 57 Cron: "@every 1m", 58 }, 59 }, 60 }, 61 } 62 63 deleted := &config.Config{ 64 JobConfig: config.JobConfig{ 65 Periodics: []config.Periodic{ 66 { 67 Name: "interval", 68 Interval: "1m", 69 }, 70 { 71 Name: "cron-2", 72 Cron: "@every 1h", 73 }, 74 }, 75 }, 76 } 77 78 if err := c.SyncConfig(initial); err != nil { 79 t.Fatalf("error first sync config: %v", err) 80 } 81 82 if c.HasJob("interval") { 83 t.Error("1st sync, should not have job 'interval'") 84 } 85 86 var cronID cron.EntryID 87 if !c.HasJob("cron") { 88 t.Error("1st sync, should have job 'cron'") 89 } else { 90 cronID = c.jobs["cron"].entryID 91 } 92 93 if err := c.SyncConfig(addAndUpdate); err != nil { 94 t.Fatalf("error sync cfg: %v", err) 95 } 96 97 if !c.HasJob("cron") { 98 t.Error("2nd sync, should have job 'cron'") 99 } else { 100 newCronID := c.jobs["cron"].entryID 101 if newCronID != cronID { 102 t.Errorf("2nd sync, entryID for 'cron' should not be updated") 103 } 104 } 105 106 if !c.HasJob("cron-2") { 107 t.Error("2nd sync, should have job 'cron-2'") 108 } else { 109 cronID = c.jobs["cron-2"].entryID 110 } 111 112 if err := c.SyncConfig(deleted); err != nil { 113 t.Fatalf("error sync cfg: %v", err) 114 } 115 116 if c.HasJob("cron") { 117 t.Error("3rd sync, should not have job 'cron'") 118 } 119 120 if !c.HasJob("cron-2") { 121 t.Error("3rd sync, should have job 'cron-2'") 122 } else { 123 newCronID := c.jobs["cron-2"].entryID 124 if newCronID == cronID { 125 t.Errorf("3rd sync, entryID for 'cron-2' should be updated") 126 } 127 } 128 } 129 130 func TestTrigger(t *testing.T) { 131 c := New() 132 cfg := &config.Config{ 133 JobConfig: config.JobConfig{ 134 Periodics: []config.Periodic{ 135 { 136 Name: "cron", 137 Cron: "* 8 * * *", 138 }, 139 { 140 Name: "periodic", 141 Cron: "@every 1h", 142 }, 143 }, 144 }, 145 } 146 147 if err := c.SyncConfig(cfg); err != nil { 148 t.Fatalf("error sync config: %v", err) 149 } 150 151 periodic := false 152 for _, job := range c.QueuedJobs() { 153 if job == "cron" { 154 t.Errorf("should not have triggered job 'cron'") 155 } else if job == "periodic" { 156 periodic = true 157 } 158 } 159 160 if !periodic { 161 t.Errorf("should have triggered job 'periodic'") 162 } 163 164 // force trigger 165 for _, entry := range c.cronAgent.Entries() { 166 entry.Job.Run() 167 } 168 169 periodic = false 170 cron := false 171 for _, job := range c.QueuedJobs() { 172 if job == "cron" { 173 cron = true 174 } else if job == "periodic" { 175 periodic = true 176 } 177 } 178 179 if !cron { 180 t.Error("should have triggered job 'cron'") 181 } 182 if !periodic { 183 t.Error("should have triggered job 'periodic'") 184 } 185 }