github.com/abayer/test-infra@v0.0.5/prow/plugins/verify-owners/verify-owners_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 verifyowners
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/sirupsen/logrus"
    25  
    26  	"k8s.io/test-infra/prow/git/localgit"
    27  	"k8s.io/test-infra/prow/github"
    28  	"k8s.io/test-infra/prow/github/fakegithub"
    29  )
    30  
    31  var ownerFiles = map[string][]byte{
    32  	"emptyApprovers": []byte(`approvers:
    33  reviewers:
    34  - alice
    35  - bob
    36  labels:
    37  - label1
    38  `),
    39  	"emptyApproversFilters": []byte(`filters:
    40    ".*":
    41      approvers:
    42      reviewers:
    43      - alice
    44      - bob
    45      labels:
    46      - label1
    47  `),
    48  	"invalidSyntax": []byte(`approvers
    49  - jdoe
    50  reviewers:
    51  - alice
    52  - bob
    53  labels:
    54  - label1
    55  `),
    56  	"invalidSyntaxFilters": []byte(`filters:
    57    ".*":
    58      approvers
    59      - jdoe
    60      reviewers:
    61      - alice
    62      - bob
    63      labels:
    64      - label1
    65  `),
    66  	"invalidLabels": []byte(`approvers:
    67  - jdoe
    68  reviewers:
    69  - alice
    70  - bob
    71  labels:
    72  - lgtm
    73  `),
    74  	"invalidLabelsFilters": []byte(`filters:
    75    ".*":
    76      approvers:
    77      - jdoe
    78      reviewers:
    79      - alice
    80      - bob
    81      labels:
    82      - lgtm
    83  `),
    84  	"noApprovers": []byte(`reviewers:
    85  - alice
    86  - bob
    87  labels:
    88  - label1
    89  `),
    90  	"noApproversFilters": []byte(`filters:
    91    ".*":
    92      reviewers:
    93      - alice
    94      - bob
    95      labels:
    96      - label1
    97  `),
    98  	"valid": []byte(`approvers:
    99  - jdoe
   100  reviewers:
   101  - alice
   102  - bob
   103  labels:
   104  - label1
   105  `),
   106  	"validFilters": []byte(`filters:
   107    ".*":
   108      approvers:
   109      - jdoe
   110      reviewers:
   111      - alice
   112      - bob
   113      labels:
   114      - label1
   115  `),
   116  }
   117  
   118  func labelsAddedContain(arr []string, str string) bool {
   119  	for _, a := range arr {
   120  		// LabelsAdded format is owner/repo#number:label
   121  		b := strings.Split(a, ":")
   122  		if b[len(b)-1] == str {
   123  			return true
   124  		}
   125  	}
   126  	return false
   127  }
   128  
   129  func newFakeGithubClient(files []string, pr int) *fakegithub.FakeClient {
   130  	var changes []github.PullRequestChange
   131  	for _, file := range files {
   132  		changes = append(changes, github.PullRequestChange{Filename: file})
   133  	}
   134  	return &fakegithub.FakeClient{
   135  		PullRequestChanges: map[int][]github.PullRequestChange{pr: changes},
   136  		Reviews:            map[int][]github.Review{},
   137  	}
   138  }
   139  
   140  func TestHandle(t *testing.T) {
   141  	var tests = []struct {
   142  		name         string
   143  		filesChanged []string
   144  		ownersFile   string
   145  		shouldLabel  bool
   146  	}{
   147  		{
   148  			name:         "no OWNERS file",
   149  			filesChanged: []string{"a.go", "b.go"},
   150  			ownersFile:   "valid",
   151  			shouldLabel:  false,
   152  		},
   153  		{
   154  			name:         "no OWNERS file with filters",
   155  			filesChanged: []string{"a.go", "b.go"},
   156  			ownersFile:   "validFilters",
   157  			shouldLabel:  false,
   158  		},
   159  		{
   160  			name:         "good OWNERS file",
   161  			filesChanged: []string{"OWNERS", "b.go"},
   162  			ownersFile:   "valid",
   163  			shouldLabel:  false,
   164  		},
   165  		{
   166  			name:         "good OWNERS file with filters",
   167  			filesChanged: []string{"OWNERS", "b.go"},
   168  			ownersFile:   "validFilters",
   169  			shouldLabel:  false,
   170  		},
   171  		{
   172  			name:         "invalid syntax OWNERS file",
   173  			filesChanged: []string{"OWNERS", "b.go"},
   174  			ownersFile:   "invalidSyntax",
   175  			shouldLabel:  true,
   176  		},
   177  		{
   178  			name:         "invalid syntax OWNERS file with filters",
   179  			filesChanged: []string{"OWNERS", "b.go"},
   180  			ownersFile:   "invalidSyntaxFilters",
   181  			shouldLabel:  true,
   182  		},
   183  		{
   184  			name:         "forbidden labels in OWNERS file",
   185  			filesChanged: []string{"OWNERS", "b.go"},
   186  			ownersFile:   "invalidLabels",
   187  			shouldLabel:  true,
   188  		},
   189  		{
   190  			name:         "forbidden labels in OWNERS file with filters",
   191  			filesChanged: []string{"OWNERS", "b.go"},
   192  			ownersFile:   "invalidLabelsFilters",
   193  			shouldLabel:  true,
   194  		},
   195  		{
   196  			name:         "empty approvers in OWNERS file",
   197  			filesChanged: []string{"OWNERS", "b.go"},
   198  			ownersFile:   "emptyApprovers",
   199  			shouldLabel:  true,
   200  		},
   201  		{
   202  			name:         "empty approvers in OWNERS file with filters",
   203  			filesChanged: []string{"OWNERS", "b.go"},
   204  			ownersFile:   "emptyApproversFilters",
   205  			shouldLabel:  true,
   206  		},
   207  		{
   208  			name:         "no approvers in OWNERS file",
   209  			filesChanged: []string{"OWNERS", "b.go"},
   210  			ownersFile:   "noApprovers",
   211  			shouldLabel:  true,
   212  		},
   213  		{
   214  			name:         "no approvers in OWNERS file with filters",
   215  			filesChanged: []string{"OWNERS", "b.go"},
   216  			ownersFile:   "noApproversFilters",
   217  			shouldLabel:  true,
   218  		},
   219  		{
   220  			name:         "no approvers in pkg/OWNERS file",
   221  			filesChanged: []string{"pkg/OWNERS", "b.go"},
   222  			ownersFile:   "noApprovers",
   223  			shouldLabel:  false,
   224  		},
   225  		{
   226  			name:         "no approvers in pkg/OWNERS file with filters",
   227  			filesChanged: []string{"pkg/OWNERS", "b.go"},
   228  			ownersFile:   "noApproversFilters",
   229  			shouldLabel:  false,
   230  		},
   231  	}
   232  	lg, c, err := localgit.New()
   233  	if err != nil {
   234  		t.Fatalf("Making localgit: %v", err)
   235  	}
   236  	defer func() {
   237  		if err := lg.Clean(); err != nil {
   238  			t.Errorf("Cleaning up localgit: %v", err)
   239  		}
   240  		if err := c.Clean(); err != nil {
   241  			t.Errorf("Cleaning up client: %v", err)
   242  		}
   243  	}()
   244  	if err := lg.MakeFakeRepo("org", "repo"); err != nil {
   245  		t.Fatalf("Making fake repo: %v", err)
   246  	}
   247  	for i, test := range tests {
   248  		pr := i + 1
   249  		// make sure we're on master before branching
   250  		if err := lg.Checkout("org", "repo", "master"); err != nil {
   251  			t.Fatalf("Switching to master branch: %v", err)
   252  		}
   253  		if err := lg.CheckoutNewBranch("org", "repo", fmt.Sprintf("pull/%d/head", pr)); err != nil {
   254  			t.Fatalf("Checking out pull branch: %v", err)
   255  		}
   256  		pullFiles := map[string][]byte{}
   257  		for _, file := range test.filesChanged {
   258  			if strings.Contains(file, "OWNERS") {
   259  				pullFiles[file] = ownerFiles[test.ownersFile]
   260  			} else {
   261  				pullFiles[file] = []byte("foo")
   262  			}
   263  		}
   264  		if err := lg.AddCommit("org", "repo", pullFiles); err != nil {
   265  			t.Fatalf("Adding PR commit: %v", err)
   266  		}
   267  		pre := &github.PullRequestEvent{
   268  			Number:      pr,
   269  			PullRequest: github.PullRequest{User: github.User{Login: "author"}},
   270  			Repo:        github.Repo{FullName: "org/repo"},
   271  		}
   272  		fghc := newFakeGithubClient(test.filesChanged, pr)
   273  		if err := handle(fghc, c, logrus.WithField("plugin", pluginName), pre, []string{"approved", "lgtm"}); err != nil {
   274  			t.Fatalf("Handle PR: %v", err)
   275  		}
   276  		if !test.shouldLabel && labelsAddedContain(fghc.LabelsAdded, invalidOwnersLabel) {
   277  			t.Errorf("%s: didn't expect label %s in %s", test.name, invalidOwnersLabel, fghc.LabelsAdded)
   278  			continue
   279  		} else if test.shouldLabel && !labelsAddedContain(fghc.LabelsAdded, invalidOwnersLabel) {
   280  			t.Errorf("%s: expected label %s in %s", test.name, invalidOwnersLabel, fghc.LabelsAdded)
   281  			continue
   282  		}
   283  	}
   284  }