Skip to content

Commit

Permalink
request: move cache checker into ut
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Fu <[email protected]>
  • Loading branch information
fuweid committed Jan 29, 2024
1 parent 5274e46 commit 75827af
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
21 changes: 0 additions & 21 deletions request/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
35 changes: 35 additions & 0 deletions request/client_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions request/testdata/dummy_nonexistent_kubeconfig.yaml
Original file line number Diff line number Diff line change
@@ -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: [email protected]
name: [email protected]
current-context: [email protected]
kind: Config
preferences: {}
users:
- name: [email protected]
user:
exec:
apiVersion: client.authentication.k8s.io/v1beta1
command: unknown
interactiveMode: IfAvailable
provideClusterInfo: false

0 comments on commit 75827af

Please sign in to comment.