k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/util/cdi/cdi_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 cdi
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"k8s.io/kubernetes/pkg/kubelet/container"
    24  )
    25  
    26  func TestGenerateAnnotations(t *testing.T) {
    27  	testCases := []struct {
    28  		description         string
    29  		deviceIDs           []string
    30  		expecteError        error
    31  		expectedAnnotations []container.Annotation
    32  	}{
    33  		{
    34  			description: "no devices",
    35  			deviceIDs:   []string{},
    36  		},
    37  		{
    38  			description:         "one device",
    39  			deviceIDs:           []string{"vendor.com/class=device1"},
    40  			expectedAnnotations: []container.Annotation{{Name: "cdi.k8s.io/test-driver-name_test-claim-uid", Value: "vendor.com/class=device1"}},
    41  		},
    42  	}
    43  
    44  	as := assert.New(t)
    45  	for _, tc := range testCases {
    46  		t.Run(tc.description, func(t *testing.T) {
    47  			annotations, err := GenerateAnnotations("test-claim-uid", "test-driver-name", tc.deviceIDs)
    48  			as.ErrorIs(err, tc.expecteError)
    49  			as.Equal(tc.expectedAnnotations, annotations)
    50  		})
    51  	}
    52  }