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