github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/trigger/push_test.go (about)

     1  /*
     2  Copyright 2018 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 trigger
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/sirupsen/logrus"
    23  	"k8s.io/test-infra/prow/config"
    24  	"k8s.io/test-infra/prow/github"
    25  	"k8s.io/test-infra/prow/github/fakegithub"
    26  )
    27  
    28  func TestHandlePE(t *testing.T) {
    29  	testCases := []struct {
    30  		name      string
    31  		pe        github.PushEvent
    32  		jobsToRun int
    33  	}{
    34  		{
    35  			name: "branch deleted",
    36  			pe: github.PushEvent{
    37  				Ref: "master",
    38  				Repo: github.Repo{
    39  					FullName: "org/repo",
    40  				},
    41  				Deleted: true,
    42  			},
    43  			jobsToRun: 0,
    44  		},
    45  		{
    46  			name: "no matching files",
    47  			pe: github.PushEvent{
    48  				Ref: "master",
    49  				Commits: []github.Commit{
    50  					{
    51  						Added: []string{"example.txt"},
    52  					},
    53  				},
    54  				Repo: github.Repo{
    55  					FullName: "org/repo",
    56  				},
    57  			},
    58  		},
    59  		{
    60  			name: "one matching file",
    61  			pe: github.PushEvent{
    62  				Ref: "master",
    63  				Commits: []github.Commit{
    64  					{
    65  						Added:    []string{"example.txt"},
    66  						Modified: []string{"hack.sh"},
    67  					},
    68  				},
    69  				Repo: github.Repo{
    70  					FullName: "org/repo",
    71  				},
    72  			},
    73  			jobsToRun: 1,
    74  		},
    75  		{
    76  			name: "no change matcher",
    77  			pe: github.PushEvent{
    78  				Ref: "master",
    79  				Commits: []github.Commit{
    80  					{
    81  						Added: []string{"example.txt"},
    82  					},
    83  				},
    84  				Repo: github.Repo{
    85  					FullName: "org2/repo2",
    86  				},
    87  			},
    88  			jobsToRun: 1,
    89  		},
    90  	}
    91  	for _, tc := range testCases {
    92  		g := &fakegithub.FakeClient{}
    93  		kc := &fkc{}
    94  		c := Client{
    95  			GitHubClient: g,
    96  			KubeClient:   kc,
    97  			Config:       &config.Config{},
    98  			Logger:       logrus.WithField("plugin", pluginName),
    99  		}
   100  		postsubmits := map[string][]config.Postsubmit{
   101  			"org/repo": {
   102  				{
   103  					JobBase: config.JobBase{
   104  						Name: "pass-butter",
   105  					},
   106  					RegexpChangeMatcher: config.RegexpChangeMatcher{
   107  						RunIfChanged: "\\.sh$",
   108  					},
   109  				},
   110  			},
   111  			"org2/repo2": {
   112  				{
   113  					JobBase: config.JobBase{
   114  						Name: "pass-salt",
   115  					},
   116  				},
   117  			},
   118  		}
   119  		if err := c.Config.SetPostsubmits(postsubmits); err != nil {
   120  			t.Fatalf("failed to set postsubmits: %v", err)
   121  		}
   122  		err := handlePE(c, tc.pe)
   123  		if err != nil {
   124  			t.Errorf("test %q: handlePE returned unexpected error %v", tc.name, err)
   125  		}
   126  		if len(kc.started) != tc.jobsToRun {
   127  			t.Errorf("test %q: expected %d jobs to run, got %d", tc.name, tc.jobsToRun, len(kc.started))
   128  		}
   129  	}
   130  }