github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/updateconfig/updateconfig_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 updateconfig 18 19 import ( 20 "fmt" 21 "strings" 22 "testing" 23 24 "github.com/sirupsen/logrus" 25 26 "k8s.io/test-infra/prow/github" 27 "k8s.io/test-infra/prow/github/fakegithub" 28 "k8s.io/test-infra/prow/kube" 29 ) 30 31 type fakeKubeClient struct { 32 maps map[string]kube.ConfigMap 33 } 34 35 func (c *fakeKubeClient) ReplaceConfigMap(name string, config kube.ConfigMap) (kube.ConfigMap, error) { 36 if config.Metadata.Name != name { 37 return kube.ConfigMap{}, fmt.Errorf("name %s does not match configmap name %s", name, config.Metadata.Name) 38 } 39 c.maps[name] = config 40 return c.maps[name], nil 41 } 42 43 func TestUpdateConfig(t *testing.T) { 44 basicPR := github.PullRequest{ 45 Number: 1, 46 Base: github.PullRequestBranch{ 47 Repo: github.Repo{ 48 Owner: github.User{ 49 Login: "kubernetes", 50 }, 51 Name: "kubernetes", 52 }, 53 }, 54 User: github.User{ 55 Login: "foo", 56 }, 57 } 58 59 testcases := []struct { 60 name string 61 prAction github.PullRequestEventAction 62 merged bool 63 mergeCommit string 64 changes []github.PullRequestChange 65 configUpdate bool 66 pluginsUpdate bool 67 }{ 68 { 69 name: "Opened PR, no update", 70 prAction: github.PullRequestActionOpened, 71 merged: false, 72 changes: []github.PullRequestChange{ 73 { 74 Filename: "prow/config.yaml", 75 Additions: 1, 76 }, 77 }, 78 }, 79 { 80 name: "Opened PR, not merged, no update", 81 merged: false, 82 changes: []github.PullRequestChange{ 83 { 84 Filename: "prow/config.yaml", 85 Additions: 1, 86 }, 87 }, 88 }, 89 { 90 name: "Closed PR, no prow changes, no update", 91 prAction: github.PullRequestActionClosed, 92 merged: false, 93 changes: []github.PullRequestChange{ 94 { 95 Filename: "foo.txt", 96 Additions: 1, 97 }, 98 }, 99 }, 100 { 101 name: "For whatever reason no merge commit SHA", 102 prAction: github.PullRequestActionClosed, 103 merged: true, 104 changes: []github.PullRequestChange{ 105 { 106 Filename: "prow/config.yaml", 107 Additions: 1, 108 }, 109 }, 110 }, 111 { 112 name: "changed config.yaml, 1 update", 113 prAction: github.PullRequestActionClosed, 114 merged: true, 115 mergeCommit: "12345", 116 changes: []github.PullRequestChange{ 117 { 118 Filename: "prow/config.yaml", 119 Additions: 1, 120 }, 121 }, 122 configUpdate: true, 123 }, 124 { 125 name: "changed plugins.yaml, 1 update", 126 prAction: github.PullRequestActionClosed, 127 merged: true, 128 mergeCommit: "12345", 129 changes: []github.PullRequestChange{ 130 { 131 Filename: "prow/plugins.yaml", 132 Additions: 1, 133 }, 134 }, 135 pluginsUpdate: true, 136 }, 137 { 138 name: "changed config.yaml and plugins.yaml, 2 update", 139 prAction: github.PullRequestActionClosed, 140 merged: true, 141 mergeCommit: "12345", 142 changes: []github.PullRequestChange{ 143 { 144 Filename: "prow/plugins.yaml", 145 Additions: 1, 146 }, 147 { 148 Filename: "prow/config.yaml", 149 Additions: 1, 150 }, 151 }, 152 configUpdate: true, 153 pluginsUpdate: true, 154 }, 155 } 156 157 for _, tc := range testcases { 158 log := logrus.WithField("plugin", pluginName) 159 event := github.PullRequestEvent{ 160 Action: tc.prAction, 161 Number: basicPR.Number, 162 PullRequest: basicPR, 163 } 164 event.PullRequest.Merged = tc.merged 165 event.PullRequest.MergeSHA = &tc.mergeCommit 166 167 fgc := &fakegithub.FakeClient{ 168 PullRequests: map[int]*github.PullRequest{ 169 basicPR.Number: &basicPR, 170 }, 171 PullRequestChanges: map[int][]github.PullRequestChange{ 172 basicPR.Number: tc.changes, 173 }, 174 IssueComments: map[int][]github.IssueComment{}, 175 RemoteFiles: map[string]map[string]string{ 176 "prow/config.yaml": { 177 "master": "old-config", 178 "12345": "new-config", 179 }, 180 "prow/plugins.yaml": { 181 "master": "old-plugins", 182 "12345": "new-plugins", 183 }, 184 }, 185 } 186 fkc := &fakeKubeClient{ 187 maps: map[string]kube.ConfigMap{}, 188 } 189 190 if err := handle(fgc, fkc, log, event, "prow/config.yaml", "prow/plugins.yaml"); err != nil { 191 t.Fatal(err) 192 } 193 194 if tc.configUpdate || tc.pluginsUpdate { 195 if len(fgc.IssueComments[basicPR.Number]) != 1 { 196 t.Fatalf("tc %s : Expect 1 comment, actually got %d", tc.name, len(fgc.IssueComments[basicPR.Number])) 197 } 198 199 comment := fgc.IssueComments[basicPR.Number][0].Body 200 if tc.configUpdate && !strings.Contains(comment, "I updated Prow config for you!") { 201 t.Fatalf("tc %s : Expect comment %s to contain 'I updated Prow config for you!'", comment, fgc.IssueComments[basicPR.Number][0].Body) 202 } 203 204 if tc.pluginsUpdate && !strings.Contains(comment, "I updated Prow plugins config for you!") { 205 t.Fatalf("tc %s : Expect comment %s to contain 'I updated Prow plugins config for you!'", comment, fgc.IssueComments[basicPR.Number][0].Body) 206 } 207 } 208 209 if tc.configUpdate { 210 if config, ok := fkc.maps["config"]; !ok { 211 t.Fatalf("tc %s : Should have updated configmap for 'config'", tc.name) 212 } else if config.Data["config"] != "new-config" { 213 t.Fatalf("tc %s : Expect get config 'new-config', got '%s'", tc.name, config.Data["config"]) 214 } 215 } 216 217 if tc.pluginsUpdate { 218 if plugins, ok := fkc.maps["plugins"]; !ok { 219 t.Fatalf("tc %s : Should have updated configmap for 'plugins'", tc.name) 220 } else if plugins.Data["plugins"] != "new-plugins" { 221 t.Fatalf("tc %s : Expect get config 'new-plugins', got '%s'", tc.name, plugins.Data["plugins"]) 222 } 223 } 224 } 225 }