github.com/abayer/test-infra@v0.0.5/prow/plugins/docs-no-retest/docs-no-retest_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 docsnoretest
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/util/sets"
    24  	"k8s.io/test-infra/prow/github"
    25  )
    26  
    27  type ghc struct {
    28  	*testing.T
    29  	labels    sets.String
    30  	prChanges []github.PullRequestChange
    31  
    32  	addLabelErr, removeLabelErr, getIssueLabelsErr, getPullRequestChangesErr error
    33  }
    34  
    35  func (c *ghc) AddLabel(_, _ string, _ int, targetLabel string) error {
    36  	c.T.Logf("AddLabel: %s", targetLabel)
    37  	c.labels.Insert(targetLabel)
    38  
    39  	return c.addLabelErr
    40  }
    41  
    42  func (c *ghc) RemoveLabel(_, _ string, _ int, targetLabel string) error {
    43  	c.T.Logf("RemoveLabel: %s", targetLabel)
    44  	c.labels.Delete(targetLabel)
    45  
    46  	return c.removeLabelErr
    47  }
    48  
    49  func (c *ghc) GetIssueLabels(_, _ string, _ int) (ls []github.Label, err error) {
    50  	c.T.Log("GetIssueLabels")
    51  	for label := range c.labels {
    52  		ls = append(ls, github.Label{Name: label})
    53  	}
    54  
    55  	err = c.getIssueLabelsErr
    56  	return
    57  }
    58  
    59  func (c *ghc) GetPullRequestChanges(_, _ string, _ int) ([]github.PullRequestChange, error) {
    60  	c.T.Log("GetPullRequestChanges")
    61  	return c.prChanges, c.getPullRequestChangesErr
    62  }
    63  
    64  func TestHandlePR(t *testing.T) {
    65  	var (
    66  		testError = errors.New("test error")
    67  	)
    68  
    69  	cases := []struct {
    70  		name             string
    71  		labels           sets.String
    72  		prChanges        []github.PullRequestChange
    73  		err              error
    74  		shouldSkipRetest bool
    75  		action           github.PullRequestEventAction
    76  		addLabelErr, removeLabelErr, getIssueLabelsErr,
    77  		getPullRequestChangesErr error
    78  	}{
    79  		// does not initially have label
    80  		{
    81  			name:   "change md, no label, needs label",
    82  			labels: sets.NewString(),
    83  			prChanges: []github.PullRequestChange{
    84  				{
    85  					Filename: "/path/to/file/README.md",
    86  				},
    87  			},
    88  			action:           github.PullRequestActionOpened,
    89  			shouldSkipRetest: true,
    90  		},
    91  		{
    92  			name:   "change svg, no label, needs label",
    93  			labels: sets.NewString(),
    94  			prChanges: []github.PullRequestChange{
    95  				{
    96  					Filename: "/path/to/file/graph.svg",
    97  				},
    98  			},
    99  			action:           github.PullRequestActionOpened,
   100  			shouldSkipRetest: true,
   101  		},
   102  		{
   103  			name:   "change OWNERS, no label, needs label",
   104  			labels: sets.NewString(),
   105  			prChanges: []github.PullRequestChange{
   106  				{
   107  					Filename: "/path/to/file/OWNERS",
   108  				},
   109  			},
   110  			action:           github.PullRequestActionOpened,
   111  			shouldSkipRetest: true,
   112  		},
   113  		{
   114  			name:   "change LICENSE, no label, needs label",
   115  			labels: sets.NewString(),
   116  			prChanges: []github.PullRequestChange{
   117  				{
   118  					Filename: "/path/to/file/LICENSE",
   119  				},
   120  			},
   121  			action:           github.PullRequestActionOpened,
   122  			shouldSkipRetest: true,
   123  		},
   124  		{
   125  			name:   "change SECURITY_CONTACTS, no label, needs label",
   126  			labels: sets.NewString(),
   127  			prChanges: []github.PullRequestChange{
   128  				{
   129  					Filename: "/path/to/file/SECURITY_CONTACTS",
   130  				},
   131  			},
   132  			action:           github.PullRequestActionOpened,
   133  			shouldSkipRetest: true,
   134  		},
   135  		{
   136  			name:   "change OWNERS_ALIASES, no label, needs label",
   137  			labels: sets.NewString(),
   138  			prChanges: []github.PullRequestChange{
   139  				{
   140  					Filename: "/path/to/file/OWNERS_ALIASES",
   141  				},
   142  			},
   143  			action:           github.PullRequestActionOpened,
   144  			shouldSkipRetest: true,
   145  		},
   146  		{
   147  			name:   "change non doc, no label, needs no label",
   148  			labels: sets.NewString(),
   149  			prChanges: []github.PullRequestChange{
   150  				{
   151  					Filename: "/path/to/file/foo.go",
   152  				},
   153  			},
   154  			action:           github.PullRequestActionOpened,
   155  			shouldSkipRetest: false,
   156  		},
   157  		{
   158  			name:   "change mix, no label, needs label",
   159  			labels: sets.NewString(),
   160  			prChanges: []github.PullRequestChange{
   161  				{
   162  					Filename: "/path/to/file/foo.go",
   163  				},
   164  				{
   165  					Filename: "/path/to/file/foo.md",
   166  				},
   167  			},
   168  			action:           github.PullRequestActionOpened,
   169  			shouldSkipRetest: false,
   170  		},
   171  		// initially has label
   172  		{
   173  			name:   "change md, has label, needs label",
   174  			labels: sets.NewString(labelSkipRetest),
   175  			prChanges: []github.PullRequestChange{
   176  				{
   177  					Filename: "/path/to/file/README.md",
   178  				},
   179  			},
   180  			action:           github.PullRequestActionOpened,
   181  			shouldSkipRetest: true,
   182  		},
   183  		{
   184  			name:   "change svg, has label, needs label",
   185  			labels: sets.NewString(labelSkipRetest),
   186  			prChanges: []github.PullRequestChange{
   187  				{
   188  					Filename: "/path/to/file/graph.svg",
   189  				},
   190  			},
   191  			action:           github.PullRequestActionOpened,
   192  			shouldSkipRetest: true,
   193  		},
   194  		{
   195  			name:   "change OWNERS, has label, needs label",
   196  			labels: sets.NewString(labelSkipRetest),
   197  			prChanges: []github.PullRequestChange{
   198  				{
   199  					Filename: "/path/to/file/OWNERS",
   200  				},
   201  			},
   202  			action:           github.PullRequestActionOpened,
   203  			shouldSkipRetest: true,
   204  		},
   205  		{
   206  			name:   "change LICENSE, has label, needs label",
   207  			labels: sets.NewString(labelSkipRetest),
   208  			prChanges: []github.PullRequestChange{
   209  				{
   210  					Filename: "/path/to/file/LICENSE",
   211  				},
   212  			},
   213  			action:           github.PullRequestActionOpened,
   214  			shouldSkipRetest: true,
   215  		},
   216  		{
   217  			name:   "change SECURITY_CONTACTS, has label, needs label",
   218  			labels: sets.NewString(labelSkipRetest),
   219  			prChanges: []github.PullRequestChange{
   220  				{
   221  					Filename: "/path/to/file/SECURITY_CONTACTS",
   222  				},
   223  			},
   224  			action:           github.PullRequestActionOpened,
   225  			shouldSkipRetest: true,
   226  		},
   227  		{
   228  			name:   "change OWNERS_ALIASES, has label, needs label",
   229  			labels: sets.NewString(labelSkipRetest),
   230  			prChanges: []github.PullRequestChange{
   231  				{
   232  					Filename: "/path/to/file/OWNERS_ALIASES",
   233  				},
   234  			},
   235  			action:           github.PullRequestActionOpened,
   236  			shouldSkipRetest: true,
   237  		},
   238  		{
   239  			name:   "change non doc, has label, needs no label",
   240  			labels: sets.NewString(labelSkipRetest),
   241  			prChanges: []github.PullRequestChange{
   242  				{
   243  					Filename: "/path/to/file/foo.go",
   244  				},
   245  			},
   246  			action:           github.PullRequestActionOpened,
   247  			shouldSkipRetest: false,
   248  		},
   249  		{
   250  			name:   "change mix, has label, needs label",
   251  			labels: sets.NewString(labelSkipRetest),
   252  			prChanges: []github.PullRequestChange{
   253  				{
   254  					Filename: "/path/to/file/foo.go",
   255  				},
   256  				{
   257  					Filename: "/path/to/file/foo.md",
   258  				},
   259  			},
   260  			action:           github.PullRequestActionOpened,
   261  			shouldSkipRetest: false,
   262  		},
   263  		// check action
   264  		{
   265  			name:   "action opened",
   266  			labels: sets.NewString(),
   267  			prChanges: []github.PullRequestChange{
   268  				{
   269  					Filename: "/path/to/file/foo.md",
   270  				},
   271  			},
   272  			action:           github.PullRequestActionOpened,
   273  			shouldSkipRetest: true,
   274  		},
   275  		{
   276  			name:   "action reopened",
   277  			labels: sets.NewString(),
   278  			prChanges: []github.PullRequestChange{
   279  				{
   280  					Filename: "/path/to/file/foo.md",
   281  				},
   282  			},
   283  			action:           github.PullRequestActionReopened,
   284  			shouldSkipRetest: true,
   285  		},
   286  		{
   287  			name:   "action synchronize",
   288  			labels: sets.NewString(),
   289  			prChanges: []github.PullRequestChange{
   290  				{
   291  					Filename: "/path/to/file/foo.md",
   292  				},
   293  			},
   294  			action:           github.PullRequestActionSynchronize,
   295  			shouldSkipRetest: true,
   296  		},
   297  		{
   298  			name:   "action closed",
   299  			labels: sets.NewString(),
   300  			prChanges: []github.PullRequestChange{
   301  				{
   302  					Filename: "/path/to/file/foo.md",
   303  				},
   304  			},
   305  			action:           github.PullRequestActionClosed,
   306  			shouldSkipRetest: false, // since it is closed, should not change
   307  		},
   308  		// error handling
   309  		{
   310  			name:   "error getting pull request changes",
   311  			labels: sets.NewString(),
   312  			prChanges: []github.PullRequestChange{
   313  				{
   314  					Filename: "/path/to/file/foo.go",
   315  				},
   316  			},
   317  			getPullRequestChangesErr: testError,
   318  			err:              testError,
   319  			action:           github.PullRequestActionOpened,
   320  			shouldSkipRetest: false,
   321  		},
   322  		{
   323  			name:   "error getting labels",
   324  			labels: sets.NewString(),
   325  			prChanges: []github.PullRequestChange{
   326  				{
   327  					Filename: "/path/to/file/foo.go",
   328  				},
   329  			},
   330  			getIssueLabelsErr: testError,
   331  			err:               testError,
   332  			action:            github.PullRequestActionOpened,
   333  			shouldSkipRetest:  false,
   334  		},
   335  		{
   336  			name:   "error adding label",
   337  			labels: sets.NewString(),
   338  			prChanges: []github.PullRequestChange{
   339  				{
   340  					Filename: "/path/to/file/foo.md",
   341  				},
   342  			},
   343  			addLabelErr:      testError,
   344  			err:              testError,
   345  			action:           github.PullRequestActionOpened,
   346  			shouldSkipRetest: true,
   347  		},
   348  		{
   349  			name:   "error removing label",
   350  			labels: sets.NewString(labelSkipRetest),
   351  			prChanges: []github.PullRequestChange{
   352  				{
   353  					Filename: "/path/to/file/foo.go",
   354  				},
   355  			},
   356  			removeLabelErr:   testError,
   357  			err:              testError,
   358  			action:           github.PullRequestActionOpened,
   359  			shouldSkipRetest: false,
   360  		},
   361  	}
   362  
   363  	for _, c := range cases {
   364  		t.Run(c.name, func(t *testing.T) {
   365  			client := &ghc{
   366  				labels:                   c.labels,
   367  				prChanges:                c.prChanges,
   368  				addLabelErr:              c.addLabelErr,
   369  				removeLabelErr:           c.removeLabelErr,
   370  				getIssueLabelsErr:        c.getIssueLabelsErr,
   371  				getPullRequestChangesErr: c.getPullRequestChangesErr,
   372  				T: t,
   373  			}
   374  
   375  			event := github.PullRequestEvent{
   376  				Action: c.action,
   377  				Number: 101,
   378  				PullRequest: github.PullRequest{
   379  					Number: 101,
   380  					Base: github.PullRequestBranch{
   381  						SHA: "abcd",
   382  						Repo: github.Repo{
   383  							Owner: github.User{
   384  								Login: "kubernetes",
   385  							},
   386  							Name: "kubernetes",
   387  						},
   388  					},
   389  				},
   390  			}
   391  
   392  			err := handlePR(client, event)
   393  
   394  			if err != nil && c.err == nil {
   395  				t.Errorf("test case \"%s\": unexpected handlePR error: %v", c.name, err)
   396  			}
   397  
   398  			if err == nil && c.err != nil {
   399  				t.Errorf("test case \"%s\": handlePR wanted error %v, got nil", c.name, err)
   400  			}
   401  
   402  			if !client.labels.Has(labelSkipRetest) && c.shouldSkipRetest {
   403  				t.Errorf("test case \"%s\": github client is missing expected label %s", c.name, labelSkipRetest)
   404  			} else if client.labels.Has(labelSkipRetest) && !c.shouldSkipRetest {
   405  				t.Errorf("test case \"%s\": github client unexpectedly has label %s", c.name, labelSkipRetest)
   406  			}
   407  		})
   408  	}
   409  }