k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/dra/plugin/plugin_test.go (about)

     1  /*
     2  Copyright 2023 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 plugin
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestRegistrationHandler_ValidatePlugin(t *testing.T) {
    26  	for _, test := range []struct {
    27  		description string
    28  		handler     func() *RegistrationHandler
    29  		pluginName  string
    30  		endpoint    string
    31  		versions    []string
    32  		shouldError bool
    33  	}{
    34  		{
    35  			description: "no versions provided",
    36  			handler:     NewRegistrationHandler,
    37  			shouldError: true,
    38  		},
    39  		{
    40  			description: "unsupported version",
    41  			handler:     NewRegistrationHandler,
    42  			versions:    []string{"v2.0.0"},
    43  			shouldError: true,
    44  		},
    45  		{
    46  			description: "plugin already registered with a higher supported version",
    47  			handler: func() *RegistrationHandler {
    48  				handler := NewRegistrationHandler()
    49  				if err := handler.RegisterPlugin("this-plugin-already-exists-and-has-a-long-name-so-it-doesnt-collide", "", []string{"v1.1.0"}); err != nil {
    50  					t.Fatal(err)
    51  				}
    52  				return handler
    53  			},
    54  			pluginName:  "this-plugin-already-exists-and-has-a-long-name-so-it-doesnt-collide",
    55  			versions:    []string{"v1.0.0"},
    56  			shouldError: true,
    57  		},
    58  		{
    59  			description: "should validate the plugin",
    60  			handler:     NewRegistrationHandler,
    61  			pluginName:  "this-is-a-dummy-plugin-with-a-long-name-so-it-doesnt-collide",
    62  			versions:    []string{"v1.3.0"},
    63  		},
    64  	} {
    65  		t.Run(test.description, func(t *testing.T) {
    66  			handler := test.handler()
    67  			err := handler.ValidatePlugin(test.pluginName, test.endpoint, test.versions)
    68  			if test.shouldError {
    69  				assert.Error(t, err)
    70  			} else {
    71  				assert.Nil(t, err)
    72  			}
    73  		})
    74  	}
    75  
    76  	t.Cleanup(func() {
    77  		handler := NewRegistrationHandler()
    78  		handler.DeRegisterPlugin("this-plugin-already-exists-and-has-a-long-name-so-it-doesnt-collide")
    79  		handler.DeRegisterPlugin("this-is-a-dummy-plugin-with-a-long-name-so-it-doesnt-collide")
    80  	})
    81  }