github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/kcc/config_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 kcc
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    27  	dynamicfake "k8s.io/client-go/dynamic/fake"
    28  
    29  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    30  
    31  	"github.com/kubewharf/katalyst-api/pkg/apis/config/v1alpha1"
    32  	internalfake "github.com/kubewharf/katalyst-api/pkg/client/clientset/versioned/fake"
    33  	"github.com/kubewharf/katalyst-core/pkg/client"
    34  	"github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic/crd"
    35  	"github.com/kubewharf/katalyst-core/pkg/config/agent/global"
    36  	metaconfig "github.com/kubewharf/katalyst-core/pkg/config/agent/metaserver"
    37  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/cnc"
    38  )
    39  
    40  var testTargetGVR = crd.AdminQoSConfigurationGVR
    41  
    42  func generateTestGenericClientSet(objects ...runtime.Object) *client.GenericClientSet {
    43  	scheme := runtime.NewScheme()
    44  	utilruntime.Must(v1alpha1.AddToScheme(scheme))
    45  	return &client.GenericClientSet{
    46  		KubeClient:     nil,
    47  		InternalClient: internalfake.NewSimpleClientset(objects...),
    48  		DynamicClient:  dynamicfake.NewSimpleDynamicClient(scheme, objects...),
    49  	}
    50  }
    51  
    52  func constructKatalystCustomConfigLoader() ConfigurationLoader {
    53  	nodeName := "test-node"
    54  	c := &v1alpha1.CustomNodeConfig{
    55  		ObjectMeta: metav1.ObjectMeta{
    56  			Name: nodeName,
    57  		},
    58  		Status: v1alpha1.CustomNodeConfigStatus{
    59  			KatalystCustomConfigList: []v1alpha1.TargetConfig{
    60  				{
    61  					ConfigName:      "default",
    62  					ConfigNamespace: "test-namespace",
    63  					ConfigType:      testTargetGVR,
    64  					Hash:            "e39c2dd73aac",
    65  				},
    66  			},
    67  		},
    68  	}
    69  
    70  	aqc := &v1alpha1.AdminQoSConfiguration{
    71  		ObjectMeta: metav1.ObjectMeta{
    72  			Name:      "default",
    73  			Namespace: "test-namespace",
    74  		},
    75  		Spec: v1alpha1.AdminQoSConfigurationSpec{
    76  			Config: v1alpha1.AdminQoSConfig{
    77  				EvictionConfig: &v1alpha1.EvictionConfig{
    78  					ReclaimedResourcesEvictionConfig: &v1alpha1.ReclaimedResourcesEvictionConfig{
    79  						EvictionThreshold: map[v1.ResourceName]float64{
    80  							v1.ResourceCPU:    1.2,
    81  							v1.ResourceMemory: 1.3,
    82  						},
    83  					},
    84  				},
    85  			},
    86  		},
    87  	}
    88  
    89  	clientSet := generateTestGenericClientSet(c, aqc)
    90  	cncFetcher := cnc.NewCachedCNCFetcher(
    91  		&global.BaseConfiguration{NodeName: nodeName},
    92  		&metaconfig.CNCConfiguration{CustomNodeConfigCacheTTL: 1 * time.Second},
    93  		clientSet.InternalClient.ConfigV1alpha1().CustomNodeConfigs())
    94  
    95  	return NewKatalystCustomConfigLoader(clientSet, 1*time.Second, cncFetcher)
    96  }
    97  
    98  func Test_katalystCustomConfigLoader_LoadConfig(t *testing.T) {
    99  	t.Parallel()
   100  
   101  	type args struct {
   102  		ctx  context.Context
   103  		gvr  metav1.GroupVersionResource
   104  		conf interface{}
   105  	}
   106  	tests := []struct {
   107  		name    string
   108  		args    args
   109  		wantErr bool
   110  	}{
   111  		{
   112  			name: "test-1",
   113  			args: args{
   114  				ctx:  context.TODO(),
   115  				gvr:  testTargetGVR,
   116  				conf: &v1alpha1.AdminQoSConfiguration{},
   117  			},
   118  		},
   119  	}
   120  	for _, tt := range tests {
   121  		tt := tt
   122  		t.Run(tt.name, func(t *testing.T) {
   123  			t.Parallel()
   124  			c := constructKatalystCustomConfigLoader()
   125  			if err := c.LoadConfig(tt.args.ctx, tt.args.gvr, tt.args.conf); (err != nil) != tt.wantErr {
   126  				t.Errorf("LoadConfig() error = %v, wantErr %v", err, tt.wantErr)
   127  			}
   128  		})
   129  	}
   130  }