github.com/abayer/test-infra@v0.0.5/prow/plugins/plugins_test.go (about) 1 /* 2 Copyright 2016 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 plugins 18 19 import ( 20 "errors" 21 "reflect" 22 "testing" 23 ) 24 25 func TestGetPlugins(t *testing.T) { 26 var testcases = []struct { 27 name string 28 pluginMap map[string][]string // this is read from the plugins.yaml file typically. 29 owner string 30 repo string 31 expectedPlugins []string 32 }{ 33 { 34 name: "All plugins enabled for org should be returned for any org/repo query", 35 pluginMap: map[string][]string{ 36 "org1": {"plugin1", "plugin2"}, 37 }, 38 owner: "org1", 39 repo: "repo", 40 expectedPlugins: []string{"plugin1", "plugin2"}, 41 }, 42 { 43 name: "All plugins enabled for org/repo should be returned for a org/repo query", 44 pluginMap: map[string][]string{ 45 "org1": {"plugin1", "plugin2"}, 46 "org1/repo": {"plugin3"}, 47 }, 48 owner: "org1", 49 repo: "repo", 50 expectedPlugins: []string{"plugin1", "plugin2", "plugin3"}, 51 }, 52 { 53 name: "Plugins for org1/repo should not be returned for org2/repo query", 54 pluginMap: map[string][]string{ 55 "org1": {"plugin1", "plugin2"}, 56 "org1/repo": {"plugin3"}, 57 }, 58 owner: "org2", 59 repo: "repo", 60 expectedPlugins: nil, 61 }, 62 { 63 name: "Plugins for org1 should not be returned for org2/repo query", 64 pluginMap: map[string][]string{ 65 "org1": {"plugin1", "plugin2"}, 66 "org2/repo": {"plugin3"}, 67 }, 68 owner: "org2", 69 repo: "repo", 70 expectedPlugins: []string{"plugin3"}, 71 }, 72 } 73 for _, tc := range testcases { 74 pa := PluginAgent{configuration: &Configuration{Plugins: tc.pluginMap}} 75 76 plugins := pa.getPlugins(tc.owner, tc.repo) 77 if len(plugins) != len(tc.expectedPlugins) { 78 t.Errorf("Different number of plugins for case \"%s\". Got %v, expected %v", tc.name, plugins, tc.expectedPlugins) 79 } else { 80 for i := range plugins { 81 if plugins[i] != tc.expectedPlugins[i] { 82 t.Errorf("Different plugin for case \"%s\": Got %v expected %v", tc.name, plugins, tc.expectedPlugins) 83 } 84 } 85 } 86 } 87 } 88 89 func TestValidateExternalPlugins(t *testing.T) { 90 tests := []struct { 91 name string 92 plugins map[string][]ExternalPlugin 93 expectedErr error 94 }{ 95 { 96 name: "valid config", 97 plugins: map[string][]ExternalPlugin{ 98 "kubernetes/test-infra": { 99 { 100 Name: "cherrypick", 101 }, 102 { 103 Name: "configupdater", 104 }, 105 { 106 Name: "tetris", 107 }, 108 }, 109 "kubernetes": { 110 { 111 Name: "coffeemachine", 112 }, 113 { 114 Name: "blender", 115 }, 116 }, 117 }, 118 expectedErr: nil, 119 }, 120 { 121 name: "invalid config", 122 plugins: map[string][]ExternalPlugin{ 123 "kubernetes/test-infra": { 124 { 125 Name: "cherrypick", 126 }, 127 { 128 Name: "configupdater", 129 }, 130 { 131 Name: "tetris", 132 }, 133 }, 134 "kubernetes": { 135 { 136 Name: "coffeemachine", 137 }, 138 { 139 Name: "tetris", 140 }, 141 }, 142 }, 143 expectedErr: errors.New("invalid plugin configuration:\n\texternal plugins [tetris] are duplicated for kubernetes/test-infra and kubernetes"), 144 }, 145 } 146 147 for _, test := range tests { 148 t.Logf("Running scenario %q", test.name) 149 150 err := validateExternalPlugins(test.plugins) 151 if !reflect.DeepEqual(err, test.expectedErr) { 152 t.Errorf("unexpected error: %v, expected: %v", err, test.expectedErr) 153 } 154 } 155 } 156 157 func TestSetDefault_Maps(t *testing.T) { 158 cases := []struct { 159 name string 160 config ConfigUpdater 161 expected map[string]ConfigMapSpec 162 }{ 163 { 164 name: "nothing", 165 expected: map[string]ConfigMapSpec{ 166 "prow/config.yaml": {Name: "config"}, 167 "prow/plugins.yaml": {Name: "plugins"}, 168 }, 169 }, 170 { 171 name: "basic", 172 config: ConfigUpdater{ 173 Maps: map[string]ConfigMapSpec{ 174 "hello.yaml": {Name: "my-cm"}, 175 "world.yaml": {Name: "you-cm"}, 176 }, 177 }, 178 expected: map[string]ConfigMapSpec{ 179 "hello.yaml": {Name: "my-cm"}, 180 "world.yaml": {Name: "you-cm"}, 181 }, 182 }, 183 { 184 name: "deprecated config", 185 config: ConfigUpdater{ 186 ConfigFile: "foo.yaml", 187 }, 188 expected: map[string]ConfigMapSpec{ 189 "foo.yaml": {Name: "config"}, 190 "prow/plugins.yaml": {Name: "plugins"}, 191 }, 192 }, 193 { 194 name: "deprecated plugins", 195 config: ConfigUpdater{ 196 PluginFile: "bar.yaml", 197 }, 198 expected: map[string]ConfigMapSpec{ 199 "bar.yaml": {Name: "plugins"}, 200 "prow/config.yaml": {Name: "config"}, 201 }, 202 }, 203 { 204 name: "deprecated both", 205 config: ConfigUpdater{ 206 ConfigFile: "foo.yaml", 207 PluginFile: "bar.yaml", 208 }, 209 expected: map[string]ConfigMapSpec{ 210 "foo.yaml": {Name: "config"}, 211 "bar.yaml": {Name: "plugins"}, 212 }, 213 }, 214 { 215 name: "both current and deprecated", 216 config: ConfigUpdater{ 217 Maps: map[string]ConfigMapSpec{ 218 "config.yaml": {Name: "overwrite-config"}, 219 "plugins.yaml": {Name: "overwrite-plugins"}, 220 "unconflicting.yaml": {Name: "ignored"}, 221 }, 222 ConfigFile: "config.yaml", 223 PluginFile: "plugins.yaml", 224 }, 225 expected: map[string]ConfigMapSpec{ 226 "config.yaml": {Name: "overwrite-config"}, 227 "plugins.yaml": {Name: "overwrite-plugins"}, 228 "unconflicting.yaml": {Name: "ignored"}, 229 }, 230 }, 231 } 232 for _, tc := range cases { 233 cfg := Configuration{ 234 ConfigUpdater: tc.config, 235 } 236 cfg.setDefaults() 237 actual := cfg.ConfigUpdater.Maps 238 if len(actual) != len(tc.expected) { 239 t.Errorf("%s: actual and expected have different keys: %v %v", tc.name, actual, tc.expected) 240 continue 241 } 242 for k, n := range tc.expected { 243 if an := actual[k]; an != n { 244 t.Errorf("%s - %s: expected %s != actual %s", tc.name, k, n, an) 245 } 246 } 247 } 248 } 249 250 func TestSetDefaults(t *testing.T) { 251 tests := []struct { 252 name string 253 254 trustedOrg string 255 joinOrgURL string 256 257 expectedTrustedOrg string 258 expectedJoinOrgURL string 259 }{ 260 { 261 name: "url defaults to org", 262 263 trustedOrg: "kubernetes", 264 joinOrgURL: "", 265 266 expectedTrustedOrg: "kubernetes", 267 expectedJoinOrgURL: "https://github.com/orgs/kubernetes/people", 268 }, 269 { 270 name: "both org and url are set", 271 272 trustedOrg: "kubernetes", 273 joinOrgURL: "https://git.k8s.io/community/community-membership.md#member", 274 275 expectedTrustedOrg: "kubernetes", 276 expectedJoinOrgURL: "https://git.k8s.io/community/community-membership.md#member", 277 }, 278 { 279 name: "only url is set", 280 281 trustedOrg: "", 282 joinOrgURL: "https://git.k8s.io/community/community-membership.md#member", 283 284 expectedTrustedOrg: "", 285 expectedJoinOrgURL: "https://git.k8s.io/community/community-membership.md#member", 286 }, 287 { 288 name: "nothing is set", 289 290 trustedOrg: "", 291 joinOrgURL: "", 292 293 expectedTrustedOrg: "", 294 expectedJoinOrgURL: "", 295 }, 296 } 297 298 for _, test := range tests { 299 c := &Configuration{ 300 Triggers: []Trigger{ 301 { 302 TrustedOrg: test.trustedOrg, 303 JoinOrgURL: test.joinOrgURL, 304 }, 305 }, 306 } 307 308 c.setDefaults() 309 310 if c.Triggers[0].TrustedOrg != test.expectedTrustedOrg { 311 t.Errorf("unexpected trusted_org: %s, expected: %s", c.Triggers[0].TrustedOrg, test.expectedTrustedOrg) 312 } 313 if c.Triggers[0].JoinOrgURL != test.expectedJoinOrgURL { 314 t.Errorf("unexpected join_org_url: %s, expected: %s", c.Triggers[0].JoinOrgURL, test.expectedJoinOrgURL) 315 } 316 } 317 }