...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/dra/plugin/plugin_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cm/dra/plugin

     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  	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