github.com/johandry/terraform@v0.11.12-beta1/plugin/discovery/meta_set_test.go (about) 1 package discovery 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 ) 8 9 func TestPluginMetaSetManipulation(t *testing.T) { 10 metas := []PluginMeta{ 11 { 12 Name: "foo", 13 Version: "1.0.0", 14 Path: "test-foo", 15 }, 16 { 17 Name: "bar", 18 Version: "2.0.0", 19 Path: "test-bar", 20 }, 21 { 22 Name: "baz", 23 Version: "2.0.0", 24 Path: "test-bar", 25 }, 26 } 27 s := make(PluginMetaSet) 28 29 if count := s.Count(); count != 0 { 30 t.Fatalf("set has Count %d before any items added", count) 31 } 32 33 // Can we add metas? 34 for _, p := range metas { 35 s.Add(p) 36 if !s.Has(p) { 37 t.Fatalf("%q not in set after adding it", p.Name) 38 } 39 } 40 41 if got, want := s.Count(), len(metas); got != want { 42 t.Fatalf("set has Count %d after all items added; want %d", got, want) 43 } 44 45 // Can we still retrieve earlier ones after we added later ones? 46 for _, p := range metas { 47 if !s.Has(p) { 48 t.Fatalf("%q not in set after all adds", p.Name) 49 } 50 } 51 52 // Can we remove metas? 53 for _, p := range metas { 54 s.Remove(p) 55 if s.Has(p) { 56 t.Fatalf("%q still in set after removing it", p.Name) 57 } 58 } 59 60 if count := s.Count(); count != 0 { 61 t.Fatalf("set has Count %d after all items removed", count) 62 } 63 } 64 65 func TestPluginMetaSetValidateVersions(t *testing.T) { 66 metas := []PluginMeta{ 67 { 68 Name: "foo", 69 Version: "1.0.0", 70 Path: "test-foo", 71 }, 72 { 73 Name: "bar", 74 Version: "0.0.1", 75 Path: "test-bar", 76 }, 77 { 78 Name: "baz", 79 Version: "bananas", 80 Path: "test-bar", 81 }, 82 } 83 s := make(PluginMetaSet) 84 85 for _, p := range metas { 86 s.Add(p) 87 } 88 89 valid, invalid := s.ValidateVersions() 90 if count := valid.Count(); count != 2 { 91 t.Errorf("valid set has %d metas; want 2", count) 92 } 93 if count := invalid.Count(); count != 1 { 94 t.Errorf("valid set has %d metas; want 1", count) 95 } 96 97 if !valid.Has(metas[0]) { 98 t.Errorf("'foo' not in valid set") 99 } 100 if !valid.Has(metas[1]) { 101 t.Errorf("'bar' not in valid set") 102 } 103 if !invalid.Has(metas[2]) { 104 t.Errorf("'baz' not in invalid set") 105 } 106 107 if invalid.Has(metas[0]) { 108 t.Errorf("'foo' in invalid set") 109 } 110 if invalid.Has(metas[1]) { 111 t.Errorf("'bar' in invalid set") 112 } 113 if valid.Has(metas[2]) { 114 t.Errorf("'baz' in valid set") 115 } 116 117 } 118 119 func TestPluginMetaSetWithName(t *testing.T) { 120 tests := []struct { 121 metas []PluginMeta 122 name string 123 wantCount int 124 }{ 125 { 126 []PluginMeta{}, 127 "foo", 128 0, 129 }, 130 { 131 []PluginMeta{ 132 { 133 Name: "foo", 134 Version: "0.0.1", 135 Path: "foo", 136 }, 137 }, 138 "foo", 139 1, 140 }, 141 { 142 []PluginMeta{ 143 { 144 Name: "foo", 145 Version: "0.0.1", 146 Path: "foo", 147 }, 148 }, 149 "bar", 150 0, 151 }, 152 } 153 154 for i, test := range tests { 155 t.Run(fmt.Sprintf("Test%02d", i), func(t *testing.T) { 156 s := make(PluginMetaSet) 157 for _, p := range test.metas { 158 s.Add(p) 159 } 160 filtered := s.WithName(test.name) 161 if gotCount := filtered.Count(); gotCount != test.wantCount { 162 t.Errorf("got count %d in %#v; want %d", gotCount, filtered, test.wantCount) 163 } 164 }) 165 } 166 } 167 168 func TestPluginMetaSetByName(t *testing.T) { 169 metas := []PluginMeta{ 170 { 171 Name: "foo", 172 Version: "1.0.0", 173 Path: "test-foo", 174 }, 175 { 176 Name: "foo", 177 Version: "2.0.0", 178 Path: "test-foo-2", 179 }, 180 { 181 Name: "bar", 182 Version: "0.0.1", 183 Path: "test-bar", 184 }, 185 { 186 Name: "baz", 187 Version: "1.2.0", 188 Path: "test-bar", 189 }, 190 } 191 s := make(PluginMetaSet) 192 193 for _, p := range metas { 194 s.Add(p) 195 } 196 197 byName := s.ByName() 198 if got, want := len(byName), 3; got != want { 199 t.Errorf("%d keys in ByName map; want %d", got, want) 200 } 201 if got, want := len(byName["foo"]), 2; got != want { 202 t.Errorf("%d metas for 'foo'; want %d", got, want) 203 } 204 if got, want := len(byName["bar"]), 1; got != want { 205 t.Errorf("%d metas for 'bar'; want %d", got, want) 206 } 207 if got, want := len(byName["baz"]), 1; got != want { 208 t.Errorf("%d metas for 'baz'; want %d", got, want) 209 } 210 211 if !byName["foo"].Has(metas[0]) { 212 t.Errorf("%#v missing from 'foo' set", metas[0]) 213 } 214 if !byName["foo"].Has(metas[1]) { 215 t.Errorf("%#v missing from 'foo' set", metas[1]) 216 } 217 if !byName["bar"].Has(metas[2]) { 218 t.Errorf("%#v missing from 'bar' set", metas[2]) 219 } 220 if !byName["baz"].Has(metas[3]) { 221 t.Errorf("%#v missing from 'baz' set", metas[3]) 222 } 223 } 224 225 func TestPluginMetaSetNewest(t *testing.T) { 226 tests := []struct { 227 versions []string 228 want string 229 }{ 230 { 231 []string{ 232 "0.0.1", 233 }, 234 "0.0.1", 235 }, 236 { 237 []string{ 238 "0.0.1", 239 "0.0.2", 240 }, 241 "0.0.2", 242 }, 243 { 244 []string{ 245 "1.0.0", 246 "1.0.0-beta1", 247 }, 248 "1.0.0", 249 }, 250 { 251 []string{ 252 "0.0.1", 253 "1.0.0", 254 }, 255 "1.0.0", 256 }, 257 } 258 259 for _, test := range tests { 260 t.Run(strings.Join(test.versions, "|"), func(t *testing.T) { 261 s := make(PluginMetaSet) 262 for _, version := range test.versions { 263 s.Add(PluginMeta{ 264 Name: "foo", 265 Version: VersionStr(version), 266 Path: "foo-V" + version, 267 }) 268 } 269 270 newest := s.Newest() 271 if newest.Version != VersionStr(test.want) { 272 t.Errorf("version is %q; want %q", newest.Version, test.want) 273 } 274 }) 275 } 276 } 277 278 func TestPluginMetaSetConstrainVersions(t *testing.T) { 279 metas := []PluginMeta{ 280 { 281 Name: "foo", 282 Version: "1.0.0", 283 Path: "test-foo", 284 }, 285 { 286 Name: "foo", 287 Version: "2.0.0", 288 Path: "test-foo-2", 289 }, 290 { 291 Name: "foo", 292 Version: "3.0.0", 293 Path: "test-foo-2", 294 }, 295 { 296 Name: "bar", 297 Version: "0.0.5", 298 Path: "test-bar", 299 }, 300 { 301 Name: "baz", 302 Version: "0.0.1", 303 Path: "test-bar", 304 }, 305 } 306 s := make(PluginMetaSet) 307 308 for _, p := range metas { 309 s.Add(p) 310 } 311 312 byName := s.ConstrainVersions(PluginRequirements{ 313 "foo": &PluginConstraints{Versions: ConstraintStr(">=2.0.0").MustParse()}, 314 "bar": &PluginConstraints{Versions: ConstraintStr(">=0.0.0").MustParse()}, 315 "baz": &PluginConstraints{Versions: ConstraintStr(">=1.0.0").MustParse()}, 316 "fun": &PluginConstraints{Versions: ConstraintStr(">5.0.0").MustParse()}, 317 }) 318 if got, want := len(byName), 3; got != want { 319 t.Errorf("%d keys in map; want %d", got, want) 320 } 321 322 if got, want := len(byName["foo"]), 2; got != want { 323 t.Errorf("%d metas for 'foo'; want %d", got, want) 324 } 325 if got, want := len(byName["bar"]), 1; got != want { 326 t.Errorf("%d metas for 'bar'; want %d", got, want) 327 } 328 if got, want := len(byName["baz"]), 0; got != want { 329 t.Errorf("%d metas for 'baz'; want %d", got, want) 330 } 331 // "fun" is not in the map at all, because we have no metas for that name 332 333 if !byName["foo"].Has(metas[1]) { 334 t.Errorf("%#v missing from 'foo' set", metas[1]) 335 } 336 if !byName["foo"].Has(metas[2]) { 337 t.Errorf("%#v missing from 'foo' set", metas[2]) 338 } 339 if !byName["bar"].Has(metas[3]) { 340 t.Errorf("%#v missing from 'bar' set", metas[3]) 341 } 342 343 } 344 345 func TestPluginMetaSetOverridePaths(t *testing.T) { 346 347 metas := []PluginMeta{ 348 { 349 Name: "foo", 350 Version: "1.0.0", 351 Path: "test-foo-1", 352 }, 353 { 354 Name: "foo", 355 Version: "2.0.0", 356 Path: "test-foo-2", 357 }, 358 { 359 Name: "foo", 360 Version: "3.0.0", 361 Path: "test-foo-3", 362 }, 363 { 364 Name: "bar", 365 Version: "0.0.5", 366 Path: "test-bar-5", 367 }, 368 { 369 Name: "bar", 370 Version: "0.0.6", 371 Path: "test-bar-6", 372 }, 373 { 374 Name: "baz", 375 Version: "0.0.1", 376 Path: "test-bar", 377 }, 378 } 379 s := make(PluginMetaSet) 380 381 for _, p := range metas { 382 s.Add(p) 383 } 384 385 ns := s.OverridePaths(map[string]string{ 386 "foo": "override-foo", 387 "fun": "override-fun", 388 }) 389 390 if got, want := ns.Count(), 5; got != want { 391 t.Errorf("got %d metas; want %d", got, want) 392 } 393 394 if !ns.Has(metas[3]) { 395 t.Errorf("new set is missing %#v", metas[3]) 396 } 397 if !ns.Has(metas[4]) { 398 t.Errorf("new set is missing %#v", metas[4]) 399 } 400 if !ns.Has(metas[5]) { 401 t.Errorf("new set is missing %#v", metas[5]) 402 } 403 if !ns.Has(PluginMeta{ 404 Name: "foo", 405 Version: VersionZero, 406 Path: "override-foo", 407 }) { 408 t.Errorf("new set is missing 'foo' override") 409 } 410 if !ns.Has(PluginMeta{ 411 Name: "fun", 412 Version: VersionZero, 413 Path: "override-fun", 414 }) { 415 t.Errorf("new set is missing 'fun' override") 416 } 417 }