diff --git a/request/client.go b/request/client.go index d431247..6aed87c 100644 --- a/request/client.go +++ b/request/client.go @@ -9,30 +9,9 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" - "k8s.io/client-go/tools/metrics" - "k8s.io/klog/v2" "k8s.io/kubectl/pkg/scheme" ) -// FIXME(weifu): -// -// Write UT to cover it instead of hook -type transportCacheTracker struct{} - -// Increment implements k8s.io/client-go/tools/metrics.TransportCreateCallsMetric interface. -func (t *transportCacheTracker) Increment(result string) { - if result != "uncacheable" { - klog.Fatal("unexpected use cache transport") - } - klog.V(3).Infof("transport cache: %s", result) -} - -func init() { - metrics.Register(metrics.RegisterOpts{ - TransportCreateCalls: &transportCacheTracker{}, - }) -} - // NewClients creates N rest.Interface. // // FIXME(weifu): diff --git a/request/client_test.go b/request/client_test.go new file mode 100644 index 0000000..059e07c --- /dev/null +++ b/request/client_test.go @@ -0,0 +1,35 @@ +package request + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "k8s.io/client-go/tools/metrics" +) + +type transportCacheTracker struct{} + +// Increment implements k8s.io/client-go/tools/metrics.TransportCreateCallsMetric interface. +func (t *transportCacheTracker) Increment(result string) { + if result != "uncacheable" { + panic(fmt.Errorf("unexpected use cache transport: %s", result)) + } + fmt.Printf("transport cache: %s\n", result) +} + +func init() { + metrics.Register(metrics.RegisterOpts{ + TransportCreateCalls: &transportCacheTracker{}, + }) +} + +func TestNewClientShouldNotPanic(t *testing.T) { + defer func() { + if r := recover(); r != nil { + t.Fatalf("should not reuse transport: %v", r) + } + }() + _, err := NewClients("testdata/dummy_nonexistent_kubeconfig.yaml", 10) + assert.NoError(t, err) +} diff --git a/request/testdata/dummy_nonexistent_kubeconfig.yaml b/request/testdata/dummy_nonexistent_kubeconfig.yaml new file mode 100644 index 0000000..172680e --- /dev/null +++ b/request/testdata/dummy_nonexistent_kubeconfig.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +clusters: +- cluster: + server: https://unit-test.kperf.io + name: unit-test.kperf.io +contexts: +- context: + cluster: unit-test.kperf.io + user: testing@unit-test.kperf.io + name: testing@unit-test.kperf.io +current-context: testing@unit-test.kperf.io +kind: Config +preferences: {} +users: +- name: testing@unit-test.kperf.io + user: + exec: + apiVersion: client.authentication.k8s.io/v1beta1 + command: unknown + interactiveMode: IfAvailable + provideClusterInfo: false