github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/mungers/sig-mention-handler_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 mungers
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"reflect"
    23  	"runtime"
    24  	"testing"
    25  
    26  	githubapi "github.com/google/go-github/github"
    27  	"k8s.io/test-infra/mungegithub/github"
    28  	github_test "k8s.io/test-infra/mungegithub/github/testing"
    29  )
    30  
    31  const (
    32  	helpWanted          = "help-wanted"
    33  	open                = "open"
    34  	sigApps             = "sig/apps"
    35  	committeeSteering   = "committee/steering"
    36  	wgContainerIdentity = "wg/container-identity"
    37  	username            = "Ali"
    38  	needsSigLabel       = "needs-sig"
    39  )
    40  
    41  func TestSigMentionHandler(t *testing.T) {
    42  	runtime.GOMAXPROCS(runtime.NumCPU())
    43  
    44  	tests := []struct {
    45  		name     string
    46  		issue    *githubapi.Issue
    47  		expected []githubapi.Label
    48  	}{
    49  		{
    50  			name: "ignore PRs",
    51  			issue: &githubapi.Issue{
    52  				State:            githubapi.String(open),
    53  				Labels:           []githubapi.Label{{Name: githubapi.String(helpWanted)}},
    54  				PullRequestLinks: &githubapi.PullRequestLinks{},
    55  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
    56  				Number:           intPtr(1),
    57  			},
    58  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}},
    59  		},
    60  		{
    61  			name:     "issue is null",
    62  			issue:    nil,
    63  			expected: nil,
    64  		},
    65  		{
    66  			name: "issue state is null",
    67  			issue: &githubapi.Issue{
    68  				State:            nil,
    69  				Labels:           []githubapi.Label{{Name: githubapi.String(helpWanted)}},
    70  				PullRequestLinks: nil,
    71  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
    72  				Number:           intPtr(1),
    73  			},
    74  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}},
    75  		},
    76  		{
    77  			name: "issue is closed",
    78  			issue: &githubapi.Issue{
    79  				State:            githubapi.String("closed"),
    80  				Labels:           []githubapi.Label{{Name: githubapi.String(helpWanted)}},
    81  				PullRequestLinks: nil,
    82  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
    83  				Number:           intPtr(1),
    84  			},
    85  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}},
    86  		},
    87  		{
    88  			name: "issue has sig/foo label, no needs-sig label",
    89  			issue: &githubapi.Issue{
    90  				State: githubapi.String(open),
    91  				Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)},
    92  					{Name: githubapi.String(sigApps)}},
    93  				PullRequestLinks: nil,
    94  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
    95  				Number:           intPtr(1),
    96  			},
    97  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)},
    98  				{Name: githubapi.String(sigApps)}},
    99  		},
   100  		{
   101  			name: "issue has no sig/foo label, no needs-sig label",
   102  			issue: &githubapi.Issue{
   103  				State:            githubapi.String(open),
   104  				Labels:           []githubapi.Label{{Name: githubapi.String(helpWanted)}},
   105  				PullRequestLinks: nil,
   106  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
   107  				Number:           intPtr(1),
   108  			},
   109  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   110  				{Name: githubapi.String(needsSigLabel)}},
   111  		},
   112  		{
   113  			name: "issue has needs-sig label, no sig/foo label",
   114  			issue: &githubapi.Issue{
   115  				State: githubapi.String(open),
   116  				Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   117  					{Name: githubapi.String(needsSigLabel)}},
   118  				PullRequestLinks: nil,
   119  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
   120  				Number:           intPtr(1),
   121  			},
   122  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   123  				{Name: githubapi.String(needsSigLabel)}},
   124  		},
   125  		{
   126  			name: "issue has both needs-sig label and sig/foo label",
   127  			issue: &githubapi.Issue{
   128  				State: githubapi.String(open),
   129  				Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   130  					{Name: githubapi.String(needsSigLabel)},
   131  					{Name: githubapi.String(sigApps)}},
   132  				PullRequestLinks: nil,
   133  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
   134  				Number:           intPtr(1),
   135  			},
   136  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   137  				{Name: githubapi.String(sigApps)}},
   138  		},
   139  		{
   140  			name: "issue has committee/foo label, no needs-sig label",
   141  			issue: &githubapi.Issue{
   142  				State: githubapi.String(open),
   143  				Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   144  					{Name: githubapi.String(committeeSteering)}},
   145  				PullRequestLinks: nil,
   146  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
   147  				Number:           intPtr(1),
   148  			},
   149  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   150  				{Name: githubapi.String(committeeSteering)}},
   151  		},
   152  		{
   153  			name: "issue has both needs-sig label and committee/foo label",
   154  			issue: &githubapi.Issue{
   155  				State: githubapi.String(open),
   156  				Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   157  					{Name: githubapi.String(needsSigLabel)},
   158  					{Name: githubapi.String(committeeSteering)}},
   159  				PullRequestLinks: nil,
   160  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
   161  				Number:           intPtr(1),
   162  			},
   163  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   164  				{Name: githubapi.String(committeeSteering)}},
   165  		},
   166  		{
   167  			name: "issue has wg/foo label, no needs-sig label",
   168  			issue: &githubapi.Issue{
   169  				State: githubapi.String(open),
   170  				Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   171  					{Name: githubapi.String(wgContainerIdentity)}},
   172  				PullRequestLinks: nil,
   173  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
   174  				Number:           intPtr(1),
   175  			},
   176  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   177  				{Name: githubapi.String(wgContainerIdentity)}},
   178  		},
   179  		{
   180  			name: "issue has both needs-sig label and wg/foo label",
   181  			issue: &githubapi.Issue{
   182  				State: githubapi.String(open),
   183  				Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   184  					{Name: githubapi.String(needsSigLabel)},
   185  					{Name: githubapi.String(wgContainerIdentity)}},
   186  				PullRequestLinks: nil,
   187  				Assignee:         &githubapi.User{Login: githubapi.String(username)},
   188  				Number:           intPtr(1),
   189  			},
   190  			expected: []githubapi.Label{{Name: githubapi.String(helpWanted)},
   191  				{Name: githubapi.String(wgContainerIdentity)}},
   192  		},
   193  	}
   194  
   195  	for testNum, test := range tests {
   196  		client, server, mux := github_test.InitServer(t, test.issue, nil, nil, nil, nil, nil, nil)
   197  		config := &github.Config{
   198  			Org:     "o",
   199  			Project: "r",
   200  		}
   201  		config.SetClient(client)
   202  
   203  		mux.HandleFunc(fmt.Sprintf("/repos/o/r/issues/1/labels/%s", needsSigLabel), func(w http.ResponseWriter, r *http.Request) {
   204  			w.WriteHeader(http.StatusOK)
   205  		})
   206  
   207  		obj := &github.MungeObject{}
   208  		var err error
   209  
   210  		if test.issue != nil {
   211  			obj, err = config.GetObject(*test.issue.Number)
   212  
   213  			if err != nil {
   214  				t.Fatalf("%d: unable to get issue: %v", testNum, *test.issue.Number)
   215  			}
   216  
   217  			obj.Issue = test.issue
   218  		}
   219  
   220  		s := SigMentionHandler{}
   221  		s.Munge(obj)
   222  
   223  		if obj.Issue == nil {
   224  			if test.expected != nil {
   225  				t.Errorf("%d:%s: expected: %v, saw: %v", testNum, test.name, test.expected, nil)
   226  			} else {
   227  				continue
   228  			}
   229  		}
   230  
   231  		if !reflect.DeepEqual(test.expected, obj.Issue.Labels) {
   232  			t.Errorf("%d:%s: expected: %v, saw: %v", testNum, test.name, test.expected, obj.Issue.Labels)
   233  		}
   234  
   235  		server.Close()
   236  	}
   237  }