github.com/kubewharf/katalyst-core@v0.5.3/pkg/client/genericclient_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 client is the package that generate K8S kubeConfig and clientSet; and 18 // any new CRD and its corresponding clientSet should be added here. 19 // besides, this package is the only package that update/patch actions should happen. 20 package client // import "github.com/kubewharf/katalyst-core/pkg/client" 21 22 import ( 23 "testing" 24 25 "github.com/stretchr/testify/assert" 26 "k8s.io/apimachinery/pkg/labels" 27 "k8s.io/apimachinery/pkg/runtime/schema" 28 29 cmfake "k8s.io/metrics/pkg/client/custom_metrics/fake" 30 emfake "k8s.io/metrics/pkg/client/external_metrics/fake" 31 ) 32 33 func TestMetricsClient(t *testing.T) { 34 t.Parallel() 35 36 client := GenericClientSet{ 37 CustomClient: &cmfake.FakeCustomMetricsClient{}, 38 ExternalClient: &emfake.FakeExternalMetricsClient{}, 39 } 40 41 objectLabel := labels.Everything() 42 metricsLabel := labels.Everything() 43 44 var err error 45 46 _, err = client.CustomClient.RootScopedMetrics().GetForObject(schema.GroupKind{}, 47 "none-exist-pod", "none-exist-metric", metricsLabel) 48 assert.ErrorContains(t, err, "returned 0 results") 49 50 _, err = client.CustomClient.RootScopedMetrics().GetForObjects(schema.GroupKind{}, 51 objectLabel, "none-exist-metric", metricsLabel) 52 assert.Nil(t, err) 53 54 _, err = client.CustomClient.NamespacedMetrics("none-exist-ns").GetForObject(schema.GroupKind{}, 55 "none-exist-pod", "none-exist-metric", metricsLabel) 56 assert.ErrorContains(t, err, "returned 0 results") 57 58 _, err = client.CustomClient.NamespacedMetrics("none-exist-ns").GetForObjects(schema.GroupKind{}, 59 objectLabel, "none-exist-metric", metricsLabel) 60 assert.Nil(t, err) 61 62 _, err = client.ExternalClient.NamespacedMetrics("none-exist-ns").List("none-exist-external-metric", metricsLabel) 63 assert.Nil(t, err) 64 }