github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/orm/endpoint/endpoint_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 endpoint
    18  
    19  import (
    20  	"context"
    21  	"path"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  	pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1"
    26  )
    27  
    28  var eSocketName = "mock.sock"
    29  
    30  func TestNewEndpoint(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	socket := path.Join("/tmp", "TestNewEndpoint"+eSocketName)
    34  
    35  	p, e := eSetup(t, socket, "mock")
    36  	defer eCleanup(t, p, e)
    37  }
    38  
    39  func TestAllocate(t *testing.T) {
    40  	t.Parallel()
    41  
    42  	socket := path.Join("/tmp", "TestAllocate"+eSocketName)
    43  	p, e := eSetup(t, socket, "mock")
    44  	defer eCleanup(t, p, e)
    45  
    46  	req := generateResourceRequest()
    47  	resp := generateResourceResponse()
    48  
    49  	p.SetAllocFunc(func(r *pluginapi.ResourceRequest) (*pluginapi.ResourceAllocationResponse, error) {
    50  		return resp, nil
    51  	})
    52  
    53  	respOut, err := e.Allocate(context.TODO(), req)
    54  	require.NoError(t, err)
    55  	require.Equal(t, resp, respOut)
    56  }
    57  
    58  func TestNewStoppedEndpointImpl(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	ei := NewStoppedEndpointImpl("cpu")
    62  	require.NotNil(t, ei)
    63  }
    64  
    65  func TestRemovePod(t *testing.T) {
    66  	t.Parallel()
    67  
    68  	socket := path.Join("/tmp", "TestRemovePod"+eSocketName)
    69  	p, e := eSetup(t, socket, "mock")
    70  	defer eCleanup(t, p, e)
    71  
    72  	req := generateRemovePodRequest()
    73  	resp, err := e.RemovePod(context.TODO(), req)
    74  	require.NoError(t, err)
    75  	require.NotNil(t, resp)
    76  }
    77  
    78  func TestGetResourceAllocation(t *testing.T) {
    79  	t.Parallel()
    80  
    81  	socket := path.Join("/tmp", "TestGetResourceAllocation"+eSocketName)
    82  	p, e := eSetup(t, socket, "mock")
    83  	defer eCleanup(t, p, e)
    84  
    85  	resp := generateGetResourceAllocationInfoResponse()
    86  	p.SetGetAllocFunc(func(r *pluginapi.GetResourcesAllocationRequest) (*pluginapi.GetResourcesAllocationResponse, error) {
    87  		return resp, nil
    88  	})
    89  
    90  	actual, err := e.GetResourceAllocation(context.TODO(), &pluginapi.GetResourcesAllocationRequest{})
    91  	require.NoError(t, err)
    92  	require.Equal(t, actual, resp)
    93  }
    94  
    95  func TestGetTopologyAwareAllocatableResources(t *testing.T) {
    96  	t.Parallel()
    97  
    98  	socket := path.Join("/tmp", "GetTopologyAwareAllocatableResources"+eSocketName)
    99  	p, e := eSetup(t, socket, "mock")
   100  	defer eCleanup(t, p, e)
   101  
   102  	resp := generateGetTopologyAwareAllocatableResources()
   103  	p.SetGetTopologyAwareAllocatableResourcesFunc(func(r *pluginapi.GetTopologyAwareAllocatableResourcesRequest) (*pluginapi.GetTopologyAwareAllocatableResourcesResponse, error) {
   104  		return resp, nil
   105  	})
   106  
   107  	actual, err := e.GetTopologyAwareAllocatableResources(context.TODO(), &pluginapi.GetTopologyAwareAllocatableResourcesRequest{})
   108  	require.NoError(t, err)
   109  	require.Equal(t, resp, actual)
   110  }
   111  
   112  func TestGetTopologyAwareResources(t *testing.T) {
   113  	t.Parallel()
   114  
   115  	socket := path.Join("/tmp", "GetTopologyAwareResources"+eSocketName)
   116  	p, e := eSetup(t, socket, "mock")
   117  	defer eCleanup(t, p, e)
   118  
   119  	resp := generateGetTopologyAwareResources()
   120  	p.SetGetTopologyAwareResourcesFunc(func(r *pluginapi.GetTopologyAwareResourcesRequest) (*pluginapi.GetTopologyAwareResourcesResponse, error) {
   121  		return resp, nil
   122  	})
   123  
   124  	actual, err := e.GetTopologyAwareResources(context.TODO(), &pluginapi.GetTopologyAwareResourcesRequest{})
   125  	require.NoError(t, err)
   126  	require.Equal(t, resp, actual)
   127  }
   128  
   129  func TestClient(t *testing.T) {
   130  	t.Parallel()
   131  
   132  	socket := path.Join("/tmp", "TestClient"+eSocketName)
   133  	p, e := eSetup(t, socket, "mock")
   134  	defer eCleanup(t, p, e)
   135  
   136  	client := e.Client()
   137  	require.NotNil(t, client)
   138  }
   139  
   140  func generateResourceRequest() *pluginapi.ResourceRequest {
   141  	return &pluginapi.ResourceRequest{
   142  		PodUid:        "mock_pod",
   143  		PodNamespace:  "mock_pod_ns",
   144  		PodName:       "mock_pod_name",
   145  		ContainerName: "mock_con_name",
   146  		// IsInitContainer: false,
   147  		PodRole:      "mock_role",
   148  		PodType:      "mock_type",
   149  		ResourceName: "mock_res",
   150  		Hint: &pluginapi.TopologyHint{
   151  			Nodes:     []uint64{0, 1},
   152  			Preferred: true,
   153  		},
   154  		ResourceRequests: map[string]float64{
   155  			"mock_res": 2,
   156  		},
   157  	}
   158  }
   159  
   160  func generateResourceResponse() *pluginapi.ResourceAllocationResponse {
   161  	return &pluginapi.ResourceAllocationResponse{
   162  		PodUid:        "mock_pod",
   163  		PodNamespace:  "mock_pod_ns",
   164  		PodName:       "mock_pod_name",
   165  		ContainerName: "mock_con_name",
   166  		// IsInitContainer: false,
   167  		PodRole:      "mock_role",
   168  		PodType:      "mock_type",
   169  		ResourceName: "mock_res",
   170  		AllocationResult: &pluginapi.ResourceAllocation{
   171  			ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{
   172  				"mock_res": generateResourceAllocationInfo(),
   173  			},
   174  		},
   175  	}
   176  }
   177  
   178  func generateResourceAllocationInfo() *pluginapi.ResourceAllocationInfo {
   179  	return &pluginapi.ResourceAllocationInfo{
   180  		OciPropertyName:   "CpusetCpus",
   181  		IsNodeResource:    true,
   182  		IsScalarResource:  true,
   183  		AllocatedQuantity: 3,
   184  		AllocationResult:  "5-6,10",
   185  		Envs:              map[string]string{"mock_key": "mock_env"},
   186  		Annotations:       map[string]string{"mock_key": "mock_ano"},
   187  		ResourceHints:     &pluginapi.ListOfTopologyHints{},
   188  	}
   189  }
   190  
   191  func generateRemovePodRequest() *pluginapi.RemovePodRequest {
   192  	return &pluginapi.RemovePodRequest{
   193  		PodUid: "mock_pod",
   194  	}
   195  }
   196  
   197  func generateGetResourceAllocationInfoResponse() *pluginapi.GetResourcesAllocationResponse {
   198  	return &pluginapi.GetResourcesAllocationResponse{
   199  		PodResources: map[string]*pluginapi.ContainerResources{
   200  			"mock_pod": {
   201  				ContainerResources: map[string]*pluginapi.ResourceAllocation{
   202  					"mock_container": {
   203  						ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{
   204  							"mock_res": generateResourceAllocationInfo(),
   205  						},
   206  					},
   207  				},
   208  			},
   209  		},
   210  	}
   211  }
   212  
   213  func generateGetTopologyAwareAllocatableResources() *pluginapi.GetTopologyAwareAllocatableResourcesResponse {
   214  	return &pluginapi.GetTopologyAwareAllocatableResourcesResponse{
   215  		AllocatableResources: map[string]*pluginapi.AllocatableTopologyAwareResource{
   216  			"cpu": {
   217  				IsScalarResource:              true,
   218  				AggregatedAllocatableQuantity: 8,
   219  				AggregatedCapacityQuantity:    8,
   220  				TopologyAwareAllocatableQuantityList: []*pluginapi.TopologyAwareQuantity{
   221  					{
   222  						ResourceValue: 8,
   223  						Node:          0,
   224  						TopologyLevel: pluginapi.TopologyLevel_NUMA,
   225  					},
   226  				},
   227  				TopologyAwareCapacityQuantityList: []*pluginapi.TopologyAwareQuantity{
   228  					{
   229  						ResourceValue: 8,
   230  						Node:          0,
   231  						TopologyLevel: pluginapi.TopologyLevel_NUMA,
   232  					},
   233  				},
   234  			},
   235  			"memory": {
   236  				IsScalarResource:              true,
   237  				AggregatedAllocatableQuantity: 33395208192,
   238  				AggregatedCapacityQuantity:    33395208192,
   239  				TopologyAwareAllocatableQuantityList: []*pluginapi.TopologyAwareQuantity{
   240  					{
   241  						ResourceValue: 33395208192,
   242  						Node:          0,
   243  						TopologyLevel: pluginapi.TopologyLevel_NUMA,
   244  					},
   245  				},
   246  				TopologyAwareCapacityQuantityList: []*pluginapi.TopologyAwareQuantity{
   247  					{
   248  						ResourceValue: 33395208192,
   249  						Node:          0,
   250  						TopologyLevel: pluginapi.TopologyLevel_NUMA,
   251  					},
   252  				},
   253  			},
   254  		},
   255  	}
   256  }
   257  
   258  func generateGetTopologyAwareResources() *pluginapi.GetTopologyAwareResourcesResponse {
   259  	return &pluginapi.GetTopologyAwareResourcesResponse{
   260  		PodUid:       "pod-Uid",
   261  		PodName:      "pod-name",
   262  		PodNamespace: "pod-namespace",
   263  		ContainerTopologyAwareResources: &pluginapi.ContainerTopologyAwareResources{
   264  			ContainerName: "container-name",
   265  			AllocatedResources: map[string]*pluginapi.TopologyAwareResource{
   266  				"cpu": {
   267  					IsScalarResource:           true,
   268  					AggregatedQuantity:         4,
   269  					OriginalAggregatedQuantity: 4,
   270  					TopologyAwareQuantityList: []*pluginapi.TopologyAwareQuantity{
   271  						{
   272  							ResourceValue: 4,
   273  							Node:          0,
   274  							TopologyLevel: pluginapi.TopologyLevel_NUMA,
   275  						},
   276  					},
   277  					OriginalTopologyAwareQuantityList: []*pluginapi.TopologyAwareQuantity{
   278  						{
   279  							ResourceValue: 4,
   280  							Node:          0,
   281  							TopologyLevel: pluginapi.TopologyLevel_NUMA,
   282  						},
   283  					},
   284  				},
   285  			},
   286  		},
   287  	}
   288  }
   289  
   290  func eSetup(t *testing.T, socket, resourceName string) (*Stub, *EndpointImpl) {
   291  	p := NewResourcePluginStub(socket, resourceName, false)
   292  
   293  	err := p.Start()
   294  	require.NoError(t, err)
   295  
   296  	e, err := NewEndpointImpl(socket, resourceName)
   297  	require.NoError(t, err)
   298  
   299  	return p, e
   300  }
   301  
   302  func eCleanup(t *testing.T, p *Stub, e *EndpointImpl) {
   303  	p.Stop()
   304  	e.Stop()
   305  }