github.com/abayer/test-infra@v0.0.5/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 ) 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 TestAddLifecycleLabels(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 lifecycle but don't specify state -> no-op", 82 body: "/remove-lifecycle", 83 added: []string{}, 84 removed: []string{}, 85 labels: []string{}, 86 }, 87 { 88 name: "add lifecycle but don't specify state -> no-op", 89 body: "/lifecycle", 90 added: []string{}, 91 removed: []string{}, 92 labels: []string{}, 93 }, 94 { 95 name: "add lifecycle random -> no-op", 96 body: "/lifecycle random", 97 added: []string{}, 98 removed: []string{}, 99 labels: []string{}, 100 }, 101 { 102 name: "remove lifecycle random -> no-op", 103 body: "/remove-lifecycle random", 104 added: []string{}, 105 removed: []string{}, 106 labels: []string{}, 107 }, 108 { 109 name: "add frozen and stale with single command -> no-op", 110 body: "/lifecycle frozen stale", 111 added: []string{}, 112 removed: []string{}, 113 labels: []string{}, 114 }, 115 { 116 name: "add frozen and random with single command -> no-op", 117 body: "/lifecycle frozen random", 118 added: []string{}, 119 removed: []string{}, 120 labels: []string{}, 121 }, 122 { 123 name: "add frozen, don't have it -> frozen added", 124 body: "/lifecycle frozen", 125 added: []string{lifecycleFrozenLabel}, 126 removed: []string{}, 127 labels: []string{}, 128 }, 129 { 130 name: "add stale, don't have it -> stale added", 131 body: "/lifecycle stale", 132 added: []string{lifecycleStaleLabel}, 133 removed: []string{}, 134 labels: []string{}, 135 }, 136 { 137 name: "add rotten, don't have it -> rotten added", 138 body: "/lifecycle rotten", 139 added: []string{lifecycleRottenLabel}, 140 removed: []string{}, 141 labels: []string{}, 142 }, 143 { 144 name: "remove frozen, have it -> frozen removed", 145 body: "/remove-lifecycle frozen", 146 added: []string{}, 147 removed: []string{lifecycleFrozenLabel}, 148 labels: []string{lifecycleFrozenLabel}, 149 }, 150 { 151 name: "remove stale, have it -> stale removed", 152 body: "/remove-lifecycle stale", 153 added: []string{}, 154 removed: []string{lifecycleStaleLabel}, 155 labels: []string{lifecycleStaleLabel}, 156 }, 157 { 158 name: "remove rotten, have it -> rotten removed", 159 body: "/remove-lifecycle rotten", 160 added: []string{}, 161 removed: []string{lifecycleRottenLabel}, 162 labels: []string{lifecycleRottenLabel}, 163 }, 164 { 165 name: "add frozen but have it -> no-op", 166 body: "/lifecycle frozen", 167 added: []string{}, 168 removed: []string{}, 169 labels: []string{lifecycleFrozenLabel}, 170 }, 171 { 172 name: "add stale, have active -> stale added, remove active", 173 body: "/lifecycle stale", 174 added: []string{lifecycleStaleLabel}, 175 removed: []string{lifecycleActiveLabel}, 176 labels: []string{lifecycleActiveLabel}, 177 }, 178 { 179 name: "add frozen, have rotten -> frozen added, rotten removed", 180 body: "/lifecycle frozen", 181 added: []string{lifecycleFrozenLabel}, 182 removed: []string{lifecycleRottenLabel}, 183 labels: []string{lifecycleRottenLabel}, 184 }, 185 { 186 name: "add rotten, have stale -> rotten added, stale removed", 187 body: "/lifecycle rotten", 188 added: []string{lifecycleRottenLabel}, 189 removed: []string{lifecycleStaleLabel}, 190 labels: []string{lifecycleStaleLabel}, 191 }, 192 { 193 name: "add frozen, have stale and rotten -> frozen added, stale and rotten removed", 194 body: "/lifecycle frozen", 195 added: []string{lifecycleFrozenLabel}, 196 removed: []string{lifecycleStaleLabel, lifecycleRottenLabel}, 197 labels: []string{lifecycleStaleLabel, lifecycleRottenLabel}, 198 }, 199 { 200 name: "remove stale, then remove rotten and then add frozen -> stale and rotten removed, frozen added", 201 body: "/remove-lifecycle stale\n/remove-lifecycle rotten\n/lifecycle frozen", 202 added: []string{lifecycleFrozenLabel}, 203 removed: []string{lifecycleStaleLabel, lifecycleRottenLabel}, 204 labels: []string{lifecycleStaleLabel, lifecycleRottenLabel}, 205 }, 206 } 207 for _, tc := range testcases { 208 fc := &fakeClient{ 209 labels: tc.labels, 210 added: []string{}, 211 removed: []string{}, 212 } 213 e := &github.GenericCommentEvent{ 214 Body: tc.body, 215 Action: github.GenericCommentActionCreated, 216 } 217 err := handle(fc, logrus.WithField("plugin", "fake-lifecyle"), e) 218 switch { 219 case err != nil: 220 t.Errorf("%s: unexpected error: %v", tc.name, err) 221 case !reflect.DeepEqual(tc.added, fc.added): 222 t.Errorf("%s: added %v != actual %v", tc.name, tc.added, fc.added) 223 case !reflect.DeepEqual(tc.removed, fc.removed): 224 t.Errorf("%s: removed %v != actual %v", tc.name, tc.removed, fc.removed) 225 } 226 } 227 }