...
1
16
17 package plugin
18
19 import (
20 "testing"
21
22 "github.com/stretchr/testify/assert"
23 v1 "k8s.io/api/core/v1"
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 )
26
27 func getFakeNode() (*v1.Node, error) {
28 return &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: "worker"}}, nil
29 }
30
31 func TestRegistrationHandler_ValidatePlugin(t *testing.T) {
32 newRegistrationHandler := func() *RegistrationHandler {
33 return NewRegistrationHandler(nil, getFakeNode)
34 }
35
36 for _, test := range []struct {
37 description string
38 handler func() *RegistrationHandler
39 pluginName string
40 endpoint string
41 versions []string
42 shouldError bool
43 }{
44 {
45 description: "no versions provided",
46 handler: newRegistrationHandler,
47 shouldError: true,
48 },
49 {
50 description: "unsupported version",
51 handler: newRegistrationHandler,
52 versions: []string{"v2.0.0"},
53 shouldError: true,
54 },
55 {
56 description: "plugin already registered with a higher supported version",
57 handler: func() *RegistrationHandler {
58 handler := newRegistrationHandler()
59 if err := handler.RegisterPlugin("this-plugin-already-exists-and-has-a-long-name-so-it-doesnt-collide", "", []string{"v1.1.0"}, nil); err != nil {
60 t.Fatal(err)
61 }
62 return handler
63 },
64 pluginName: "this-plugin-already-exists-and-has-a-long-name-so-it-doesnt-collide",
65 versions: []string{"v1.0.0"},
66 shouldError: true,
67 },
68 {
69 description: "should validate the plugin",
70 handler: newRegistrationHandler,
71 pluginName: "this-is-a-dummy-plugin-with-a-long-name-so-it-doesnt-collide",
72 versions: []string{"v1.3.0"},
73 },
74 } {
75 t.Run(test.description, func(t *testing.T) {
76 handler := test.handler()
77 err := handler.ValidatePlugin(test.pluginName, test.endpoint, test.versions)
78 if test.shouldError {
79 assert.Error(t, err)
80 } else {
81 assert.Nil(t, err)
82 }
83 })
84 }
85
86 t.Cleanup(func() {
87 handler := newRegistrationHandler()
88 handler.DeRegisterPlugin("this-plugin-already-exists-and-has-a-long-name-so-it-doesnt-collide")
89 handler.DeRegisterPlugin("this-is-a-dummy-plugin-with-a-long-name-so-it-doesnt-collide")
90 })
91 }
92
View as plain text