|
17 | 17 | package plugin |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
20 | 22 | "testing" |
21 | 23 | ) |
22 | 24 |
|
@@ -377,3 +379,136 @@ func TestPluginGraph(t *testing.T) { |
377 | 379 | cmpOrdered(t, ordered, testcase.expectedURI) |
378 | 380 | } |
379 | 381 | } |
| 382 | + |
| 383 | +func TestGetPlugins(t *testing.T) { |
| 384 | + otherError := fmt.Errorf("other error") |
| 385 | + plugins := NewPluginSet() |
| 386 | + for _, p := range []*Plugin{ |
| 387 | + testPlugin("type1", "id1", "id1", nil), |
| 388 | + testPlugin("type1", "id2", "id2", ErrSkipPlugin), |
| 389 | + testPlugin("type2", "id3", "id3", ErrSkipPlugin), |
| 390 | + testPlugin("type3", "id4", "id4", nil), |
| 391 | + testPlugin("type4", "id5", "id5", nil), |
| 392 | + testPlugin("type4", "id6", "id6", nil), |
| 393 | + testPlugin("type5", "id7", "id7", otherError), |
| 394 | + } { |
| 395 | + plugins.Add(p) |
| 396 | + } |
| 397 | + |
| 398 | + ic := InitContext{ |
| 399 | + plugins: plugins, |
| 400 | + } |
| 401 | + |
| 402 | + for _, tc := range []struct { |
| 403 | + pluginType string |
| 404 | + err error |
| 405 | + }{ |
| 406 | + {"type1", nil}, |
| 407 | + {"type2", ErrPluginNotFound}, |
| 408 | + {"type3", nil}, |
| 409 | + {"type4", ErrPluginMultipleInstances}, |
| 410 | + {"type5", otherError}, |
| 411 | + } { |
| 412 | + t.Run("GetSingle", func(t *testing.T) { |
| 413 | + instance, err := ic.GetSingle(Type(tc.pluginType)) |
| 414 | + if err != nil { |
| 415 | + if tc.err == nil { |
| 416 | + t.Fatalf("unexpected error %v", err) |
| 417 | + } else if !errors.Is(err, tc.err) { |
| 418 | + t.Fatalf("unexpected error %v, expected %v", err, tc.err) |
| 419 | + } |
| 420 | + return |
| 421 | + } else if tc.err != nil { |
| 422 | + t.Fatalf("expected error %v, got no error", tc.err) |
| 423 | + } |
| 424 | + _, ok := instance.(string) |
| 425 | + if !ok { |
| 426 | + t.Fatalf("unexpected instance value %v", instance) |
| 427 | + } |
| 428 | + }) |
| 429 | + } |
| 430 | + |
| 431 | + for _, tc := range []struct { |
| 432 | + pluginType string |
| 433 | + expected []string |
| 434 | + err error |
| 435 | + }{ |
| 436 | + {"type1", []string{"id1"}, nil}, |
| 437 | + {"type2", nil, ErrPluginNotFound}, |
| 438 | + {"type3", []string{"id4"}, nil}, |
| 439 | + {"type4", []string{"id5", "id6"}, nil}, |
| 440 | + {"type5", nil, otherError}, |
| 441 | + } { |
| 442 | + t.Run("GetByType", func(t *testing.T) { |
| 443 | + m, err := ic.GetByType(Type(tc.pluginType)) |
| 444 | + if err != nil { |
| 445 | + if tc.err == nil { |
| 446 | + t.Fatalf("unexpected error %v", err) |
| 447 | + } else if !errors.Is(err, tc.err) { |
| 448 | + t.Fatalf("unexpected error %v, expected %v", err, tc.err) |
| 449 | + } |
| 450 | + return |
| 451 | + } else if tc.err != nil { |
| 452 | + t.Fatalf("expected error %v, got no error", tc.err) |
| 453 | + } |
| 454 | + |
| 455 | + if len(m) != len(tc.expected) { |
| 456 | + t.Fatalf("unexpected result %v, expected %v", m, tc.expected) |
| 457 | + } |
| 458 | + for _, v := range tc.expected { |
| 459 | + instance, ok := m[v] |
| 460 | + if !ok { |
| 461 | + t.Errorf("missing value for %q", v) |
| 462 | + continue |
| 463 | + } |
| 464 | + if instance.(string) != v { |
| 465 | + t.Errorf("unexpected value %v, expected %v", instance, v) |
| 466 | + } |
| 467 | + } |
| 468 | + }) |
| 469 | + } |
| 470 | + |
| 471 | + for _, tc := range []struct { |
| 472 | + pluginType string |
| 473 | + id string |
| 474 | + err error |
| 475 | + }{ |
| 476 | + {"type1", "id1", nil}, |
| 477 | + {"type1", "id2", ErrSkipPlugin}, |
| 478 | + {"type2", "id3", ErrSkipPlugin}, |
| 479 | + {"type3", "id4", nil}, |
| 480 | + {"type4", "id5", nil}, |
| 481 | + {"type4", "id6", nil}, |
| 482 | + {"type5", "id7", otherError}, |
| 483 | + } { |
| 484 | + t.Run("GetByID", func(t *testing.T) { |
| 485 | + instance, err := ic.GetByID(Type(tc.pluginType), tc.id) |
| 486 | + if err != nil { |
| 487 | + if tc.err == nil { |
| 488 | + t.Fatalf("unexpected error %v", err) |
| 489 | + } else if !errors.Is(err, tc.err) { |
| 490 | + t.Fatalf("unexpected error %v, expected %v", err, tc.err) |
| 491 | + } |
| 492 | + return |
| 493 | + } else if tc.err != nil { |
| 494 | + t.Fatalf("expected error %v, got no error", tc.err) |
| 495 | + } |
| 496 | + |
| 497 | + if instance.(string) != tc.id { |
| 498 | + t.Errorf("unexpected value %v, expected %v", instance, tc.id) |
| 499 | + } |
| 500 | + }) |
| 501 | + } |
| 502 | + |
| 503 | +} |
| 504 | + |
| 505 | +func testPlugin(t Type, id string, i interface{}, err error) *Plugin { |
| 506 | + return &Plugin{ |
| 507 | + Registration: Registration{ |
| 508 | + Type: t, |
| 509 | + ID: id, |
| 510 | + }, |
| 511 | + instance: i, |
| 512 | + err: err, |
| 513 | + } |
| 514 | +} |
0 commit comments