Skip to content

Commit

Permalink
client_test.go
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <[email protected]>
  • Loading branch information
shubham-cmyk committed Nov 8, 2023
1 parent fd68e31 commit b424775
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions k8sutils/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package k8sutils

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/client-go/rest"
)

func mockK8sConfigProvider() (*rest.Config, error) {
return &rest.Config{}, nil
}
func mockInvalidK8sConfigProvider() (*rest.Config, error) {
return nil, errors.New("invalid configuration")
}

func TestGenerateK8sClient(t *testing.T) {
tests := []struct {
name string
configProvider func() (*rest.Config, error)
wantErr bool
}{
{
name: "valid config",
configProvider: mockK8sConfigProvider,
wantErr: false,
},
{
name: "invalid config",
configProvider: mockInvalidK8sConfigProvider,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := generateK8sClient(tt.configProvider)
if tt.wantErr {
assert.Error(t, err, "generateK8sClient() should return an error for invalid config")
} else {
assert.NoError(t, err, "generateK8sClient() should not return an error for valid config")
assert.NotNil(t, client, "expected a non-nil Kubernetes client")
}
})
}
}

func TestGenerateK8sDynamicClient(t *testing.T) {
tests := []struct {
name string
configProvider func() (*rest.Config, error)
wantErr bool
}{
{
name: "valid config",
configProvider: mockK8sConfigProvider,
wantErr: false,
},
{
name: "invalid config",
configProvider: mockInvalidK8sConfigProvider,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := generateK8sDynamicClient(tt.configProvider)
if tt.wantErr {
assert.Error(t, err, "generateK8sDynamicClient() should return an error for invalid config")
} else {
assert.NoError(t, err, "generateK8sDynamicClient() should not return an error for valid config")
assert.NotNil(t, client, "expected a non-nil Kubernetes client")
}
})
}
}

0 comments on commit b424775

Please sign in to comment.