github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/cherrypick-auto-approve_test.go (about)

     1  /*
     2  Copyright 2016 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  	"encoding/json"
    21  	"fmt"
    22  	"net/http"
    23  	"runtime"
    24  	"testing"
    25  
    26  	github_util "k8s.io/test-infra/mungegithub/github"
    27  	github_test "k8s.io/test-infra/mungegithub/github/testing"
    28  
    29  	"github.com/golang/glog"
    30  	"github.com/google/go-github/github"
    31  )
    32  
    33  var (
    34  	_ = fmt.Printf
    35  	_ = glog.Errorf
    36  )
    37  
    38  func TestCherrypickAuthApprove(t *testing.T) {
    39  	const testBotName = "dummy"
    40  	runtime.GOMAXPROCS(runtime.NumCPU())
    41  
    42  	tests := []struct {
    43  		name                string
    44  		issue               *github.Issue
    45  		issueBody           string
    46  		prBranch            string
    47  		parentIssue         *github.Issue
    48  		milestone           *github.Milestone
    49  		shouldHaveLabel     string
    50  		shouldHaveMilestone string
    51  		shouldNotHaveLabel  string
    52  		shouldNotHaveMile   string
    53  	}{
    54  		{
    55  			name:                "Add cpApproved and milestone",
    56  			issue:               github_test.Issue(testBotName, 1, []string{}, true),
    57  			issueBody:           "Cherry pick of #2 on release-1.2.",
    58  			prBranch:            "release-1.2",
    59  			parentIssue:         github_test.Issue(testBotName, 2, []string{cpApprovedLabel}, true),
    60  			milestone:           &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)},
    61  			shouldHaveLabel:     cpApprovedLabel,
    62  			shouldHaveMilestone: "v1.2",
    63  		},
    64  		{
    65  			name:                "Add milestone",
    66  			issue:               github_test.Issue(testBotName, 1, []string{cpApprovedLabel}, true),
    67  			issueBody:           "Cherry pick of #2 on release-1.2.",
    68  			prBranch:            "release-1.2",
    69  			parentIssue:         github_test.Issue(testBotName, 2, []string{cpApprovedLabel}, true),
    70  			milestone:           &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)},
    71  			shouldHaveLabel:     cpApprovedLabel,
    72  			shouldHaveMilestone: "v1.2",
    73  		},
    74  		{
    75  			name:               "Do not add because parent not have",
    76  			issue:              github_test.Issue(testBotName, 1, []string{}, true),
    77  			issueBody:          "Cherry pick of #2 on release-1.2.",
    78  			prBranch:           "release-1.2",
    79  			parentIssue:        github_test.Issue(testBotName, 2, []string{}, true),
    80  			milestone:          &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)},
    81  			shouldNotHaveLabel: cpApprovedLabel,
    82  			shouldNotHaveMile:  "v1.2",
    83  		},
    84  		{
    85  			name:               "PR against wrong branch",
    86  			issue:              github_test.Issue(testBotName, 1, []string{}, true),
    87  			issueBody:          "Cherry pick of #2 on release-1.2.",
    88  			prBranch:           "release-1.1",
    89  			parentIssue:        github_test.Issue(testBotName, 2, []string{cpApprovedLabel}, true),
    90  			milestone:          &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)},
    91  			shouldNotHaveLabel: cpApprovedLabel,
    92  			shouldNotHaveMile:  "v1.2",
    93  		},
    94  		{
    95  			name:               "Parent milestone against other branch",
    96  			issue:              github_test.Issue(testBotName, 1, []string{}, true),
    97  			issueBody:          "Cherry pick of #2 on release-1.2.",
    98  			prBranch:           "release-1.2",
    99  			parentIssue:        github_test.Issue(testBotName, 2, []string{cpApprovedLabel}, true),
   100  			milestone:          &github.Milestone{Title: stringPtr("v1.1"), Number: intPtr(1)},
   101  			shouldNotHaveLabel: cpApprovedLabel,
   102  			shouldNotHaveMile:  "v1.1",
   103  		},
   104  		{
   105  			name:               "Parent has no milestone",
   106  			issue:              github_test.Issue(testBotName, 1, []string{}, true),
   107  			issueBody:          "Cherry pick of #2 on release-1.2.",
   108  			prBranch:           "release-1.2",
   109  			parentIssue:        github_test.Issue(testBotName, 2, []string{cpApprovedLabel}, true),
   110  			shouldNotHaveLabel: cpApprovedLabel,
   111  			shouldNotHaveMile:  "v1.2",
   112  		},
   113  	}
   114  	for testNum, test := range tests {
   115  		test.issue.Body = &test.issueBody
   116  
   117  		pr := ValidPR()
   118  		pr.Base.Ref = &test.prBranch
   119  		client, server, mux := github_test.InitServer(t, test.issue, pr, nil, nil, nil, nil, nil)
   120  
   121  		path := fmt.Sprintf("/repos/o/r/issues/%d/labels", *test.issue.Number)
   122  		mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
   123  			w.WriteHeader(http.StatusOK)
   124  			out := []github.Label{{}}
   125  			data, err := json.Marshal(out)
   126  			if err != nil {
   127  				t.Errorf("Unexpected error: %v", err)
   128  			}
   129  			w.Write(data)
   130  		})
   131  
   132  		path = "/repos/o/r/milestones"
   133  		mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
   134  			w.WriteHeader(http.StatusOK)
   135  			out := []github.Milestone{}
   136  			if test.milestone != nil {
   137  				out = append(out, *test.milestone)
   138  			}
   139  			data, err := json.Marshal(out)
   140  			if err != nil {
   141  				t.Errorf("Unexpected error: %v", err)
   142  			}
   143  			w.Write(data)
   144  		})
   145  
   146  		test.parentIssue.Milestone = test.milestone
   147  		path = fmt.Sprintf("/repos/o/r/issues/%d", *test.parentIssue.Number)
   148  		mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
   149  			data, err := json.Marshal(test.parentIssue)
   150  			if err != nil {
   151  				t.Errorf("%v", err)
   152  			}
   153  			if r.Method != "GET" {
   154  				t.Errorf("Unexpected method: expected: GET got: %s", r.Method)
   155  			}
   156  			w.WriteHeader(http.StatusOK)
   157  			w.Write(data)
   158  		})
   159  
   160  		config := &github_util.Config{
   161  			Org:     "o",
   162  			Project: "r",
   163  		}
   164  		config.SetClient(client)
   165  		config.BotName = testBotName
   166  
   167  		c := CherrypickAutoApprove{}
   168  		err := c.Initialize(config, nil)
   169  		if err != nil {
   170  			t.Fatalf("%v", err)
   171  		}
   172  
   173  		err = c.EachLoop()
   174  		if err != nil {
   175  			t.Fatalf("%v", err)
   176  		}
   177  
   178  		obj, err := config.GetObject(*test.issue.Number)
   179  		if err != nil {
   180  			t.Fatalf("%v", err)
   181  		}
   182  
   183  		c.Munge(obj)
   184  		if test.shouldHaveLabel != "" && !obj.HasLabel(test.shouldHaveLabel) {
   185  			t.Errorf("%d:%q: missing label %q", testNum, test.name, test.shouldHaveLabel)
   186  		}
   187  		milestone, ok := obj.ReleaseMilestone()
   188  		if !ok {
   189  			t.Errorf("%d:%q: error getting obj.ReleaseMilestone", testNum, test.name)
   190  		}
   191  		if test.shouldHaveMilestone != "" && milestone != test.shouldHaveMilestone {
   192  			t.Errorf("%d:%q: missing milestone %q", testNum, test.name, test.shouldHaveMilestone)
   193  		}
   194  		if test.shouldNotHaveLabel != "" && obj.HasLabel(test.shouldNotHaveLabel) {
   195  			t.Errorf("%d:%q: extra label %q", testNum, test.name, test.shouldNotHaveLabel)
   196  		}
   197  		if test.shouldNotHaveMile != "" && milestone == test.shouldNotHaveMile {
   198  			t.Errorf("%d:%q: extra milestone %q", testNum, test.name, test.shouldNotHaveMile)
   199  		}
   200  
   201  		server.Close()
   202  	}
   203  }