github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/owner-label_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  	"encoding/json"
    21  	"net/http"
    22  	"strings"
    23  	"testing"
    24  
    25  	github_util "k8s.io/test-infra/mungegithub/github"
    26  	github_test "k8s.io/test-infra/mungegithub/github/testing"
    27  
    28  	"github.com/google/go-github/github"
    29  	"k8s.io/apimachinery/pkg/util/sets"
    30  )
    31  
    32  type testOwnerLabeler struct{}
    33  
    34  func (t testOwnerLabeler) AllPossibleOwnerLabels() sets.String {
    35  	return sets.NewString()
    36  }
    37  
    38  func (t testOwnerLabeler) FindLabelsForPath(path string) sets.String {
    39  	if strings.Contains(path, "docs") {
    40  		return sets.NewString("kind/design")
    41  	}
    42  	return sets.NewString()
    43  }
    44  
    45  func TestOwnerLabelMunge(t *testing.T) {
    46  	const testBotName = "dummy"
    47  	tests := []struct {
    48  		files       []*github.CommitFile
    49  		events      []*github.IssueEvent
    50  		mustHave    []string
    51  		mustNotHave []string
    52  	}{
    53  		{
    54  			files:       commitFiles([]string{"docs/proposals"}),
    55  			events:      BotAddedDesign(testBotName),
    56  			mustHave:    []string{"kind/design"},
    57  			mustNotHave: []string{},
    58  		},
    59  	}
    60  	for testNum, test := range tests {
    61  		client, server, mux := github_test.InitServer(t, docsProposalIssue(testBotName), ValidPR(), test.events, nil, nil, nil, test.files)
    62  		mux.HandleFunc("/repos/o/r/issues/1/labels/kind/design", func(w http.ResponseWriter, r *http.Request) {
    63  			w.WriteHeader(http.StatusOK)
    64  			w.Write([]byte{})
    65  		})
    66  		mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
    67  			w.WriteHeader(http.StatusOK)
    68  			out := []github.Label{{}}
    69  			data, err := json.Marshal(out)
    70  			if err != nil {
    71  				t.Errorf("Unexpected error: %v", err)
    72  			}
    73  			w.Write(data)
    74  
    75  		})
    76  
    77  		config := &github_util.Config{
    78  			Org:     "o",
    79  			Project: "r",
    80  		}
    81  		config.SetClient(client)
    82  		config.BotName = testBotName
    83  
    84  		o := OwnerLabelMunger{labeler: testOwnerLabeler{}}
    85  
    86  		obj, err := config.GetObject(1)
    87  		if err != nil {
    88  			t.Fatalf("%v", err)
    89  		}
    90  
    91  		o.Munge(obj)
    92  
    93  		for _, l := range test.mustHave {
    94  			if !obj.HasLabel(l) {
    95  				t.Errorf("%d: Did not find label %q, labels: %v", testNum, l, obj.Issue.Labels)
    96  			}
    97  		}
    98  		for _, l := range test.mustNotHave {
    99  			if obj.HasLabel(l) {
   100  				t.Errorf("%d: Found label %q and should not have, labels: %v", testNum, l, obj.Issue.Labels)
   101  			}
   102  		}
   103  		server.Close()
   104  	}
   105  }