Skip to content

Commit

Permalink
fix remote tests part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
bdrennz committed Nov 25, 2024
1 parent f94ab36 commit 9dd0683
Showing 1 changed file with 118 additions and 6 deletions.
124 changes: 118 additions & 6 deletions pkg/diagnostic/kms/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package kms_test

import (
"context"
"io/ioutil"
"net/http"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
k8stesting "k8s.io/client-go/testing"

Expand Down Expand Up @@ -49,6 +52,85 @@ func createMockEndpoints(clientset *fake.Clientset) {
})
}

// createTempKubeConfig creates a temporary Kubernetes configuration file
func createTempKubeConfig() (string, error) {
content := `
apiVersion: v1
clusters:
- cluster:
server: https://localhost:6443
name: local
contexts:
- context:
cluster: local
user: local
name: local
current-context: local
kind: Config
preferences: {}
users:
- name: local
user:
token: fake-token
`
tmpfile, err := ioutil.TempFile("", "kubeconfig")
if err != nil {
return "", err
}

if _, err := tmpfile.Write([]byte(content)); err != nil {
tmpfile.Close()
return "", err
}
if err := tmpfile.Close(); err != nil {
return "", err
}

return tmpfile.Name(), nil
}

func TestRunner_Run_Error(t *testing.T) {
// Create a temporary Kubernetes configuration file
kubeconfig, err := createTempKubeConfig()
if err != nil {
t.Fatalf("Failed to create temporary kubeconfig: %v", err)
}
defer os.Remove(kubeconfig)

// Set the KUBECONFIG environment variable to the temporary file
os.Setenv("KUBECONFIG", kubeconfig)

cfg := &config.Settings{
Prometheus: config.Prometheus{
KubeStateMetricsServiceEndpoint: mockURL,
},
}
clientset := fake.NewSimpleClientset()
createMockEndpoints(clientset)

provider := kms.NewProvider(context.Background(), cfg)
// Inject the mock clientset
if p, ok := provider.(interface{ SetClientset(kubernetes.Interface) }); ok {
p.SetClientset(clientset)
}

mock := test.NewHTTPMock()
mock.Expect(http.MethodGet, "kube_pod_info\nkube_node_info\n", http.StatusOK, nil)
client := mock.HTTPClient()

accessor := makeReport()

err = provider.Check(context.Background(), client, accessor, cfg)
assert.NoError(t, err)

accessor.ReadFromReport(func(s *status.ClusterStatus) {
assert.Len(t, s.Checks, 1)
for _, c := range s.Checks {
assert.True(t, c.Passing)
}
})
}

func TestChecker_CheckOK(t *testing.T) {
cfg := &config.Settings{
Prometheus: config.Prometheus{
Expand All @@ -57,7 +139,12 @@ func TestChecker_CheckOK(t *testing.T) {
}
clientset := fake.NewSimpleClientset()
createMockEndpoints(clientset)
provider := kms.NewProvider(context.Background(), cfg, clientset)

provider := kms.NewProvider(context.Background(), cfg)
// Inject the mock clientset
if p, ok := provider.(interface{ SetClientset(kubernetes.Interface) }); ok {
p.SetClientset(clientset)
}

mock := test.NewHTTPMock()
mock.Expect(http.MethodGet, "kube_pod_info\nkube_node_info\n", http.StatusOK, nil)
Expand All @@ -84,7 +171,12 @@ func TestChecker_CheckRetry(t *testing.T) {
}
clientset := fake.NewSimpleClientset()
createMockEndpoints(clientset)
provider := kms.NewProvider(context.Background(), cfg, clientset)

provider := kms.NewProvider(context.Background(), cfg)
// Inject the mock clientset
if p, ok := provider.(interface{ SetClientset(kubernetes.Interface) }); ok {
p.SetClientset(clientset)
}

// Update the test sleep interval to accelerate the test
kms.RetryInterval = 10 * time.Millisecond
Expand Down Expand Up @@ -118,7 +210,12 @@ func TestChecker_CheckRetryFailure(t *testing.T) {
}
clientset := fake.NewSimpleClientset()
createMockEndpoints(clientset)
provider := kms.NewProvider(context.Background(), cfg, clientset)

provider := kms.NewProvider(context.Background(), cfg)
// Inject the mock clientset
if p, ok := provider.(interface{ SetClientset(kubernetes.Interface) }); ok {
p.SetClientset(clientset)
}

// Update the test sleep interval to accelerate the test
kms.RetryInterval = 10 * time.Millisecond
Expand Down Expand Up @@ -151,7 +248,12 @@ func TestChecker_CheckMetricsValidation(t *testing.T) {
}
clientset := fake.NewSimpleClientset()
createMockEndpoints(clientset)
provider := kms.NewProvider(context.Background(), cfg, clientset)

provider := kms.NewProvider(context.Background(), cfg)
// Inject the mock clientset
if p, ok := provider.(interface{ SetClientset(kubernetes.Interface) }); ok {
p.SetClientset(clientset)
}

mock := test.NewHTTPMock()
mock.Expect(http.MethodGet, "kube_pod_info\nkube_node_info\n", http.StatusOK, nil)
Expand All @@ -178,7 +280,12 @@ func TestChecker_CheckHandles500Error(t *testing.T) {
}
clientset := fake.NewSimpleClientset()
createMockEndpoints(clientset)
provider := kms.NewProvider(context.Background(), cfg, clientset)

provider := kms.NewProvider(context.Background(), cfg)
// Inject the mock clientset
if p, ok := provider.(interface{ SetClientset(kubernetes.Interface) }); ok {
p.SetClientset(clientset)
}

mock := test.NewHTTPMock()
mock.Expect(http.MethodGet, "", http.StatusInternalServerError, nil)
Expand All @@ -205,7 +312,12 @@ func TestChecker_CheckHandlesTimeout(t *testing.T) {
}
clientset := fake.NewSimpleClientset()
createMockEndpoints(clientset)
provider := kms.NewProvider(context.Background(), cfg, clientset)

provider := kms.NewProvider(context.Background(), cfg)
// Inject the mock clientset
if p, ok := provider.(interface{ SetClientset(kubernetes.Interface) }); ok {
p.SetClientset(clientset)
}

mock := test.NewHTTPMock()
mock.Expect(http.MethodGet, "", http.StatusOK, nil)
Expand Down

0 comments on commit 9dd0683

Please sign in to comment.