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