github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/metaserver_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 metaserver 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 "k8s.io/apimachinery/pkg/runtime" 27 dynamicfake "k8s.io/client-go/dynamic/fake" 28 "k8s.io/client-go/kubernetes/fake" 29 30 internalfake "github.com/kubewharf/katalyst-api/pkg/client/clientset/versioned/fake" 31 "github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/options" 32 "github.com/kubewharf/katalyst-core/pkg/client" 33 "github.com/kubewharf/katalyst-core/pkg/config" 34 "github.com/kubewharf/katalyst-core/pkg/metaserver/agent" 35 "github.com/kubewharf/katalyst-core/pkg/metaserver/agent/cnr" 36 "github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric" 37 "github.com/kubewharf/katalyst-core/pkg/metaserver/agent/node" 38 "github.com/kubewharf/katalyst-core/pkg/metaserver/agent/pod" 39 "github.com/kubewharf/katalyst-core/pkg/metaserver/external" 40 dynamicconfig "github.com/kubewharf/katalyst-core/pkg/metaserver/kcc" 41 "github.com/kubewharf/katalyst-core/pkg/metaserver/spd" 42 "github.com/kubewharf/katalyst-core/pkg/metrics" 43 ) 44 45 func generateTestConfiguration(t *testing.T) *config.Configuration { 46 testConfiguration, err := options.NewOptions().Config() 47 require.NoError(t, err) 48 require.NotNil(t, testConfiguration) 49 return testConfiguration 50 } 51 52 func generateTestMetaServer(clientSet *client.GenericClientSet, conf *config.Configuration) *MetaServer { 53 return &MetaServer{ 54 MetaAgent: &agent.MetaAgent{ 55 PodFetcher: &pod.PodFetcherStub{}, 56 NodeFetcher: node.NewRemoteNodeFetcher(conf.BaseConfiguration, conf.NodeConfiguration, 57 clientSet.KubeClient.CoreV1().Nodes()), 58 CNRFetcher: cnr.NewCachedCNRFetcher(conf.BaseConfiguration, conf.CNRConfiguration, 59 clientSet.InternalClient.NodeV1alpha1().CustomNodeResources()), 60 MetricsFetcher: metric.NewMetricsFetcher(conf.BaseConfiguration, conf.MetricConfiguration, 61 metrics.DummyMetrics{}, &pod.PodFetcherStub{}), 62 AgentConf: conf.MetaServerConfiguration.AgentConfiguration, 63 }, 64 ConfigurationManager: &dynamicconfig.DummyConfigurationManager{}, 65 ServiceProfilingManager: &spd.DummyServiceProfilingManager{}, 66 ExternalManager: &external.DummyExternalManager{}, 67 } 68 } 69 70 func TestMetaServer_Run(t *testing.T) { 71 t.Parallel() 72 73 genericClient := &client.GenericClientSet{ 74 KubeClient: fake.NewSimpleClientset(), 75 InternalClient: internalfake.NewSimpleClientset(), 76 DynamicClient: dynamicfake.NewSimpleDynamicClient(runtime.NewScheme()), 77 } 78 conf := generateTestConfiguration(t) 79 meta := generateTestMetaServer(genericClient, conf) 80 81 meta.Lock() 82 assert.False(t, meta.start) 83 meta.Unlock() 84 85 go meta.Run(context.Background()) 86 87 time.Sleep(10 * time.Millisecond) 88 89 meta.Lock() 90 assert.True(t, meta.start) 91 meta.Unlock() 92 } 93 94 func TestMetaServer_SetServiceProfilingManager(t *testing.T) { 95 t.Parallel() 96 97 genericClient := &client.GenericClientSet{ 98 KubeClient: fake.NewSimpleClientset(), 99 InternalClient: internalfake.NewSimpleClientset(), 100 DynamicClient: dynamicfake.NewSimpleDynamicClient(runtime.NewScheme()), 101 } 102 conf := generateTestConfiguration(t) 103 meta := generateTestMetaServer(genericClient, conf) 104 105 err := meta.SetServiceProfilingManager(&spd.DummyServiceProfilingManager{}) 106 assert.NoError(t, err) 107 108 go meta.Run(context.Background()) 109 110 time.Sleep(3 * time.Second) 111 112 err = meta.SetServiceProfilingManager(&spd.DummyServiceProfilingManager{}) 113 assert.Error(t, err) 114 }