github.com/abayer/test-infra@v0.0.5/prow/plugins/hold/hold_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 hold 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/sirupsen/logrus" 24 25 "k8s.io/test-infra/prow/github" 26 "k8s.io/test-infra/prow/github/fakegithub" 27 ) 28 29 func TestHandle(t *testing.T) { 30 var tests = []struct { 31 name string 32 body string 33 hasLabel bool 34 shouldLabel bool 35 shouldUnlabel bool 36 }{ 37 { 38 name: "nothing to do", 39 body: "noise", 40 hasLabel: false, 41 shouldLabel: false, 42 shouldUnlabel: false, 43 }, 44 { 45 name: "requested hold", 46 body: "/hold", 47 hasLabel: false, 48 shouldLabel: true, 49 shouldUnlabel: false, 50 }, 51 { 52 name: "requested hold, label already exists", 53 body: "/hold", 54 hasLabel: true, 55 shouldLabel: false, 56 shouldUnlabel: false, 57 }, 58 { 59 name: "requested hold cancel", 60 body: "/hold cancel", 61 hasLabel: true, 62 shouldLabel: false, 63 shouldUnlabel: true, 64 }, 65 { 66 name: "requested hold cancel, label already gone", 67 body: "/hold cancel", 68 hasLabel: false, 69 shouldLabel: false, 70 shouldUnlabel: false, 71 }, 72 } 73 74 for _, tc := range tests { 75 fc := &fakegithub.FakeClient{ 76 IssueComments: make(map[int][]github.IssueComment), 77 } 78 79 e := &github.GenericCommentEvent{ 80 Action: github.GenericCommentActionCreated, 81 Body: tc.body, 82 Number: 1, 83 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 84 } 85 hasLabel := func(label string, issueLabels []github.Label) bool { 86 return tc.hasLabel 87 } 88 89 if err := handle(fc, logrus.WithField("plugin", pluginName), e, hasLabel); err != nil { 90 t.Errorf("For case %s, didn't expect error from hold: %v", tc.name, err) 91 continue 92 } 93 94 fakeLabel := fmt.Sprintf("org/repo#1:%s", label) 95 if tc.shouldLabel { 96 if len(fc.LabelsAdded) != 1 || fc.LabelsAdded[0] != fakeLabel { 97 t.Errorf("For case %s: expected to add %q label but instead added: %v", tc.name, label, fc.LabelsAdded) 98 } 99 } else if len(fc.LabelsAdded) > 0 { 100 t.Errorf("For case %s, expected to not add %q label but added: %v", tc.name, label, fc.LabelsAdded) 101 } 102 if tc.shouldUnlabel { 103 if len(fc.LabelsRemoved) != 1 || fc.LabelsRemoved[0] != fakeLabel { 104 t.Errorf("For case %s: expected to remove %q label but instead removed: %v", tc.name, label, fc.LabelsRemoved) 105 } 106 } else if len(fc.LabelsRemoved) > 0 { 107 t.Errorf("For case %s, expected to not remove %q label but removed: %v", tc.name, label, fc.LabelsRemoved) 108 } 109 } 110 }