-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Shubham Gupta <[email protected]>
- Loading branch information
1 parent
fd68e31
commit b424775
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
}) | ||
} | ||
} |