github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/orm/deviceprovider/kubelet/provider_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 kubelet
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"google.golang.org/grpc"
    27  	v1 "k8s.io/kubelet/pkg/apis/podresources/v1"
    28  )
    29  
    30  func TestGetDevice(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	mockProvider := &Provider{
    34  		client: &mockPodResourcesListerClient{
    35  			ListPodResourcesResponse: &v1.ListPodResourcesResponse{
    36  				PodResources: []*v1.PodResources{},
    37  			},
    38  		},
    39  	}
    40  
    41  	resp := mockProvider.GetDevices()
    42  	assert.NotNil(t, resp)
    43  
    44  	nilPodResourcesProvider := &Provider{
    45  		client: &mockPodResourcesListerClient{
    46  			ListPodResourcesResponse: &v1.ListPodResourcesResponse{},
    47  		},
    48  	}
    49  	resp = nilPodResourcesProvider.GetDevices()
    50  	assert.Nil(t, resp)
    51  
    52  	nilRespProvider := &Provider{
    53  		client: &mockPodResourcesListerClient{
    54  			ListPodResourcesResponse: nil,
    55  		},
    56  	}
    57  	resp = nilRespProvider.GetDevices()
    58  	assert.Nil(t, resp)
    59  
    60  	errPodResourceProvider := &Provider{
    61  		client: &errPodResourceListerClient{},
    62  	}
    63  	resp = errPodResourceProvider.GetDevices()
    64  	assert.Nil(t, resp)
    65  }
    66  
    67  func TestGetAllocatableDevices(t *testing.T) {
    68  	t.Parallel()
    69  
    70  	mockProvider := &Provider{
    71  		client: &mockPodResourcesListerClient{
    72  			AllocatableResourcesResponse: &v1.AllocatableResourcesResponse{
    73  				Devices: []*v1.ContainerDevices{},
    74  			},
    75  		},
    76  	}
    77  	resp := mockProvider.GetAllocatableDevices()
    78  	assert.NotNil(t, resp)
    79  
    80  	nilRespProvider := &Provider{
    81  		client: &mockPodResourcesListerClient{
    82  			AllocatableResourcesResponse: nil,
    83  		},
    84  	}
    85  	resp = nilRespProvider.GetAllocatableDevices()
    86  	assert.Nil(t, resp)
    87  
    88  	nilDeviceProvider := &Provider{
    89  		client: &mockPodResourcesListerClient{
    90  			AllocatableResourcesResponse: &v1.AllocatableResourcesResponse{},
    91  		},
    92  	}
    93  	resp = nilDeviceProvider.GetAllocatableDevices()
    94  	assert.Nil(t, resp)
    95  
    96  	errProvider := &Provider{
    97  		client: &errPodResourceListerClient{},
    98  	}
    99  	resp = errProvider.GetAllocatableDevices()
   100  	assert.Nil(t, resp)
   101  }
   102  
   103  func TestNewProvider(t *testing.T) {
   104  	t.Parallel()
   105  
   106  	p, err := NewProvider([]string{}, getMockClientFunc)
   107  	assert.NoError(t, err)
   108  	assert.NotNil(t, p)
   109  }
   110  
   111  type mockPodResourcesListerClient struct {
   112  	*v1.ListPodResourcesResponse
   113  	*v1.AllocatableResourcesResponse
   114  }
   115  
   116  func (f *mockPodResourcesListerClient) List(ctx context.Context, in *v1.ListPodResourcesRequest, opts ...grpc.CallOption) (*v1.ListPodResourcesResponse, error) {
   117  	return f.ListPodResourcesResponse, nil
   118  }
   119  
   120  func (f *mockPodResourcesListerClient) GetAllocatableResources(ctx context.Context, in *v1.AllocatableResourcesRequest, opts ...grpc.CallOption) (*v1.AllocatableResourcesResponse, error) {
   121  	return f.AllocatableResourcesResponse, nil
   122  }
   123  
   124  type errPodResourceListerClient struct{}
   125  
   126  func (e *errPodResourceListerClient) List(ctx context.Context, in *v1.ListPodResourcesRequest, opts ...grpc.CallOption) (*v1.ListPodResourcesResponse, error) {
   127  	return nil, fmt.Errorf("err")
   128  }
   129  
   130  func (e *errPodResourceListerClient) GetAllocatableResources(ctx context.Context, in *v1.AllocatableResourcesRequest, opts ...grpc.CallOption) (*v1.AllocatableResourcesResponse, error) {
   131  	return nil, fmt.Errorf("err")
   132  }
   133  
   134  func getMockClientFunc(socket string, connectionTimeout time.Duration, maxMsgSize int) (v1.PodResourcesListerClient, *grpc.ClientConn, error) {
   135  	return &mockPodResourcesListerClient{}, nil, nil
   136  }