github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/stage/stage_test.go (about) 1 /* 2 Copyright 2018 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 stage 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/sirupsen/logrus" 24 25 "k8s.io/test-infra/prow/github" 26 ) 27 28 type fakeClient struct { 29 // current labels 30 labels []string 31 // labels that are added 32 added []string 33 // labels that are removed 34 removed []string 35 } 36 37 func (c *fakeClient) AddLabel(owner, repo string, number int, label string) error { 38 c.added = append(c.added, label) 39 c.labels = append(c.labels, label) 40 return nil 41 } 42 43 func (c *fakeClient) RemoveLabel(owner, repo string, number int, label string) error { 44 c.removed = append(c.removed, label) 45 46 // remove from existing labels 47 for k, v := range c.labels { 48 if label == v { 49 c.labels = append(c.labels[:k], c.labels[k+1:]...) 50 break 51 } 52 } 53 54 return nil 55 } 56 57 func (c *fakeClient) GetIssueLabels(owner, repo string, number int) ([]github.Label, error) { 58 la := []github.Label{} 59 for _, l := range c.labels { 60 la = append(la, github.Label{Name: l}) 61 } 62 return la, nil 63 } 64 65 func TestStageLabels(t *testing.T) { 66 var testcases = []struct { 67 name string 68 body string 69 added []string 70 removed []string 71 labels []string 72 }{ 73 { 74 name: "random command -> no-op", 75 body: "/random-command", 76 added: []string{}, 77 removed: []string{}, 78 labels: []string{}, 79 }, 80 { 81 name: "remove stage but don't specify state -> no-op", 82 body: "/remove-stage", 83 added: []string{}, 84 removed: []string{}, 85 labels: []string{}, 86 }, 87 { 88 name: "add stage but don't specify state -> no-op", 89 body: "/stage", 90 added: []string{}, 91 removed: []string{}, 92 labels: []string{}, 93 }, 94 { 95 name: "add stage random -> no-op", 96 body: "/stage random", 97 added: []string{}, 98 removed: []string{}, 99 labels: []string{}, 100 }, 101 { 102 name: "remove stage random -> no-op", 103 body: "/remove-stage random", 104 added: []string{}, 105 removed: []string{}, 106 labels: []string{}, 107 }, 108 { 109 name: "add alpha and beta with single command -> no-op", 110 body: "/stage alpha beta", 111 added: []string{}, 112 removed: []string{}, 113 labels: []string{}, 114 }, 115 { 116 name: "add alpha and random with single command -> no-op", 117 body: "/stage alpha random", 118 added: []string{}, 119 removed: []string{}, 120 labels: []string{}, 121 }, 122 { 123 name: "add alpha, don't have it -> alpha added", 124 body: "/stage alpha", 125 added: []string{stageAlpha}, 126 removed: []string{}, 127 labels: []string{}, 128 }, 129 { 130 name: "add beta, don't have it -> beta added", 131 body: "/stage beta", 132 added: []string{stageBeta}, 133 removed: []string{}, 134 labels: []string{}, 135 }, 136 { 137 name: "add stable, don't have it -> stable added", 138 body: "/stage stable", 139 added: []string{stageStable}, 140 removed: []string{}, 141 labels: []string{}, 142 }, 143 { 144 name: "remove alpha, have it -> alpha removed", 145 body: "/remove-stage alpha", 146 added: []string{}, 147 removed: []string{stageAlpha}, 148 labels: []string{stageAlpha}, 149 }, 150 { 151 name: "remove beta, have it -> beta removed", 152 body: "/remove-stage beta", 153 added: []string{}, 154 removed: []string{stageBeta}, 155 labels: []string{stageBeta}, 156 }, 157 { 158 name: "remove stable, have it -> stable removed", 159 body: "/remove-stage stable", 160 added: []string{}, 161 removed: []string{stageStable}, 162 labels: []string{stageStable}, 163 }, 164 { 165 name: "add alpha but have it -> no-op", 166 body: "/stage alpha", 167 added: []string{}, 168 removed: []string{}, 169 labels: []string{stageAlpha}, 170 }, 171 { 172 name: "add beta, have alpha -> beta added, alpha removed", 173 body: "/stage beta", 174 added: []string{stageBeta}, 175 removed: []string{stageAlpha}, 176 labels: []string{stageAlpha}, 177 }, 178 { 179 name: "add stable, have beta -> stable added, beta removed", 180 body: "/stage stable", 181 added: []string{stageStable}, 182 removed: []string{stageBeta}, 183 labels: []string{stageBeta}, 184 }, 185 { 186 name: "add stable, have alpha and beta -> stable added, alpha and beta removed", 187 body: "/stage stable", 188 added: []string{stageStable}, 189 removed: []string{stageAlpha, stageBeta}, 190 labels: []string{stageAlpha, stageBeta}, 191 }, 192 { 193 name: "remove alpha, then remove beta and then add stable -> alpha and beta removed, stable added", 194 body: "/remove-stage alpha\n/remove-stage beta\n/stage stable", 195 added: []string{stageStable}, 196 removed: []string{stageAlpha, stageBeta}, 197 labels: []string{stageAlpha, stageBeta}, 198 }, 199 } 200 for _, tc := range testcases { 201 fc := &fakeClient{ 202 labels: tc.labels, 203 added: []string{}, 204 removed: []string{}, 205 } 206 e := &github.GenericCommentEvent{ 207 Body: tc.body, 208 Action: github.GenericCommentActionCreated, 209 } 210 err := handle(fc, logrus.WithField("plugin", "fake-lifecyle"), e) 211 switch { 212 case err != nil: 213 t.Errorf("%s: unexpected error: %v", tc.name, err) 214 case !reflect.DeepEqual(tc.added, fc.added): 215 t.Errorf("%s: added %v != actual %v", tc.name, tc.added, fc.added) 216 case !reflect.DeepEqual(tc.removed, fc.removed): 217 t.Errorf("%s: removed %v != actual %v", tc.name, tc.removed, fc.removed) 218 } 219 } 220 }