k8s.io/kubernetes@v1.29.3/pkg/kubelet/apis/podresources/server_v1alpha1_test.go (about) 1 /* 2 Copyright 2018 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 podresources 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/golang/mock/gomock" 24 v1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/types" 27 podresourcesv1 "k8s.io/kubelet/pkg/apis/podresources/v1" 28 "k8s.io/kubelet/pkg/apis/podresources/v1alpha1" 29 podresourcetest "k8s.io/kubernetes/pkg/kubelet/apis/podresources/testing" 30 ) 31 32 func TestListPodResourcesV1alpha1(t *testing.T) { 33 podName := "pod-name" 34 podNamespace := "pod-namespace" 35 podUID := types.UID("pod-uid") 36 containerName := "container-name" 37 38 devs := []*podresourcesv1.ContainerDevices{ 39 { 40 ResourceName: "resource", 41 DeviceIds: []string{"dev0", "dev1"}, 42 }, 43 } 44 45 mockCtrl := gomock.NewController(t) 46 defer mockCtrl.Finish() 47 48 for _, tc := range []struct { 49 desc string 50 pods []*v1.Pod 51 devices []*podresourcesv1.ContainerDevices 52 expectedResponse *v1alpha1.ListPodResourcesResponse 53 }{ 54 { 55 desc: "no pods", 56 pods: []*v1.Pod{}, 57 devices: []*podresourcesv1.ContainerDevices{}, 58 expectedResponse: &v1alpha1.ListPodResourcesResponse{}, 59 }, 60 { 61 desc: "pod without devices", 62 pods: []*v1.Pod{ 63 { 64 ObjectMeta: metav1.ObjectMeta{ 65 Name: podName, 66 Namespace: podNamespace, 67 UID: podUID, 68 }, 69 Spec: v1.PodSpec{ 70 Containers: []v1.Container{ 71 { 72 Name: containerName, 73 }, 74 }, 75 }, 76 }, 77 }, 78 devices: []*podresourcesv1.ContainerDevices{}, 79 expectedResponse: &v1alpha1.ListPodResourcesResponse{ 80 PodResources: []*v1alpha1.PodResources{ 81 { 82 Name: podName, 83 Namespace: podNamespace, 84 Containers: []*v1alpha1.ContainerResources{ 85 { 86 Name: containerName, 87 Devices: []*v1alpha1.ContainerDevices{}, 88 }, 89 }, 90 }, 91 }, 92 }, 93 }, 94 { 95 desc: "pod with devices", 96 pods: []*v1.Pod{ 97 { 98 ObjectMeta: metav1.ObjectMeta{ 99 Name: podName, 100 Namespace: podNamespace, 101 UID: podUID, 102 }, 103 Spec: v1.PodSpec{ 104 Containers: []v1.Container{ 105 { 106 Name: containerName, 107 }, 108 }, 109 }, 110 }, 111 }, 112 devices: devs, 113 expectedResponse: &v1alpha1.ListPodResourcesResponse{ 114 PodResources: []*v1alpha1.PodResources{ 115 { 116 Name: podName, 117 Namespace: podNamespace, 118 Containers: []*v1alpha1.ContainerResources{ 119 { 120 Name: containerName, 121 Devices: v1DevicesToAlphaV1(devs), 122 }, 123 }, 124 }, 125 }, 126 }, 127 }, 128 } { 129 t.Run(tc.desc, func(t *testing.T) { 130 mockDevicesProvider := podresourcetest.NewMockDevicesProvider(mockCtrl) 131 mockPodsProvider := podresourcetest.NewMockPodsProvider(mockCtrl) 132 133 mockPodsProvider.EXPECT().GetPods().Return(tc.pods).AnyTimes() 134 mockDevicesProvider.EXPECT().GetDevices(string(podUID), containerName).Return(tc.devices).AnyTimes() 135 mockDevicesProvider.EXPECT().UpdateAllocatedDevices().Return().AnyTimes() 136 137 providers := PodResourcesProviders{ 138 Pods: mockPodsProvider, 139 Devices: mockDevicesProvider, 140 } 141 server := NewV1alpha1PodResourcesServer(providers) 142 resp, err := server.List(context.TODO(), &v1alpha1.ListPodResourcesRequest{}) 143 if err != nil { 144 t.Errorf("want err = %v, got %q", nil, err) 145 } 146 if tc.expectedResponse.String() != resp.String() { 147 t.Errorf("want resp = %s, got %s", tc.expectedResponse.String(), resp.String()) 148 } 149 }) 150 } 151 }