github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/assign-fixes_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  	"io/ioutil"
    23  	"net/http"
    24  	"runtime"
    25  	"testing"
    26  
    27  	github_util "k8s.io/test-infra/mungegithub/github"
    28  	github_test "k8s.io/test-infra/mungegithub/github/testing"
    29  
    30  	"github.com/golang/glog"
    31  	"github.com/google/go-github/github"
    32  )
    33  
    34  var (
    35  	_ = fmt.Printf
    36  	_ = glog.Errorf
    37  )
    38  
    39  func TestAssignFixes(t *testing.T) {
    40  	runtime.GOMAXPROCS(runtime.NumCPU())
    41  
    42  	tests := []struct {
    43  		name       string
    44  		assignee   string
    45  		pr         *github.PullRequest
    46  		prIssue    *github.Issue
    47  		prBody     string
    48  		fixesIssue *github.Issue
    49  	}{
    50  		{
    51  			name:       "fixes an issue",
    52  			assignee:   "dev45",
    53  			pr:         github_test.PullRequest("dev45", false, true, true),
    54  			prIssue:    github_test.Issue("fred", 7779, []string{}, true),
    55  			prBody:     "does stuff and fixes #8889.",
    56  			fixesIssue: github_test.Issue("jill", 8889, []string{}, true),
    57  		},
    58  	}
    59  	for _, test := range tests {
    60  		test.prIssue.Body = &test.prBody
    61  		client, server, mux := github_test.InitServer(t, test.prIssue, test.pr, nil, nil, nil, nil, nil)
    62  		path := fmt.Sprintf("/repos/o/r/issues/%d", *test.fixesIssue.Number)
    63  		mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
    64  			data, err := json.Marshal(test.fixesIssue)
    65  			if err != nil {
    66  				t.Errorf("%v", err)
    67  			}
    68  			if r.Method != "PATCH" && r.Method != "GET" {
    69  				t.Errorf("Unexpected method: expected: GET/PATCH got: %s", r.Method)
    70  			}
    71  			if r.Method == "PATCH" {
    72  				body, _ := ioutil.ReadAll(r.Body)
    73  
    74  				type IssuePatch struct {
    75  					Assignee string
    76  				}
    77  				var ip IssuePatch
    78  				err := json.Unmarshal(body, &ip)
    79  				if err != nil {
    80  					fmt.Println("error:", err)
    81  				}
    82  				if ip.Assignee != test.assignee {
    83  					t.Errorf("Patching the incorrect Assignee %v instead of %v", ip.Assignee, test.assignee)
    84  				}
    85  			}
    86  			w.WriteHeader(http.StatusOK)
    87  			w.Write(data)
    88  		})
    89  
    90  		config := &github_util.Config{
    91  			Org:     "o",
    92  			Project: "r",
    93  		}
    94  		config.SetClient(client)
    95  
    96  		c := AssignFixesMunger{}
    97  		err := c.Initialize(config, nil)
    98  		if err != nil {
    99  			t.Fatalf("%v", err)
   100  		}
   101  
   102  		err = c.EachLoop()
   103  		if err != nil {
   104  			t.Fatalf("%v", err)
   105  		}
   106  
   107  		obj, err := config.GetObject(*test.prIssue.Number)
   108  		if err != nil {
   109  			t.Fatalf("%v", err)
   110  		}
   111  
   112  		c.Munge(obj)
   113  		server.Close()
   114  	}
   115  }