Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-nijay committed Jan 9, 2025
1 parent 9c2d304 commit 9d6b4e4
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 4 deletions.
63 changes: 63 additions & 0 deletions pkg/k8sclient/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,66 @@ func (suite *CoreTestSuite) TestGetConfig() {
func TestCoreTestSuite(t *testing.T) {
suite.Run(t, new(CoreTestSuite))
}

func TestNewRemoteKubeClient(t *testing.T) {
t.Run("Testing with nil config", func(t *testing.T) {
_, err := NewRemoteKubeClient(nil, 0)
if err == nil {
t.Error("Expected error, but got nil")
}
})

t.Run("Testing with valid config", func(t *testing.T) {
config := &rest.Config{
Host: "https://example.com",
}
client, err := NewRemoteKubeClient(config, 0)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if client == nil {
t.Error("Expected client, but got nil")
}
})
}

func TestCreateNodeClient(t *testing.T) {
// Test case: Create node client successfully
c := &KubeClient{
ClientSet: &kubernetes.Clientset{},
timeout: 10,
}
node, err := c.CreateNodeClient()
if err != nil {
t.Errorf("CreateNodeClient() failed: %v", err)
}
if node == nil {
t.Error("CreateNodeClient() returned nil node")
}
if node.Interface == nil {
t.Error("CreateNodeClient() returned nil node interface")
}
if node.Timeout != c.timeout {
t.Errorf("CreateNodeClient() returned incorrect timeout: got %d, want %d", node.Timeout, c.timeout)
}
}

func TestCreateRGClient(t *testing.T) {
// Test case: Failed creation of replication group client
t.Run("Create RG client successfully", func(t *testing.T) {
kubeClient := &KubeClient{
Config: &rest.Config{},
ClientSet: &kubernetes.Clientset{},
timeout: 1,
}

rgClient, err := kubeClient.CreateRGClient()
if err == nil {
t.Errorf("Error not returned")
}

if rgClient != nil {
t.Error("Expected nil RG client, got nil")
}
})
}
22 changes: 22 additions & 0 deletions pkg/k8sclient/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package k8sclient

import (
"testing"
)

func TestRandomSuffix(t *testing.T) {
// Test case: Generate a random suffix
t.Run("Generate random suffix", func(t *testing.T) {
suffix := RandomSuffix()

// Check if the suffix is not empty
if suffix == "" {
t.Error("Suffix is empty")
}

// Check if the suffix is of length 8
if len(suffix) != 8 {
t.Errorf("Suffix length is not 8, got %d", len(suffix))
}
})
}
3 changes: 2 additions & 1 deletion pkg/utils/e2e_reporting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestE2eReportParser(t *testing.T) {
wantErr bool
}{
{"Check generated config", args{"testdata/execution_powerstore-nfs.xml"}, preResult, false},
{"Check generated config negative", args{"testdata/execution_powerstore-nfs-dummy.xml"}, preResult, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -49,7 +50,7 @@ func TestE2eReportParser(t *testing.T) {
t.Errorf("E2eReportParser() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
if !reflect.DeepEqual(got, tt.want) && !tt.wantErr {
t.Errorf("E2eReportParser() = %v, want %v", got, tt.want)
}
})
Expand Down
10 changes: 7 additions & 3 deletions pkg/utils/k8s_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,17 @@ type DriverConfig struct {

// ReadTestDriverConfig will read the driver config
func ReadTestDriverConfig(driverconfig string) string {
file, _ := os.ReadFile(filepath.Clean(driverconfig))
var DriverConfig DriverConfig
err := yaml.Unmarshal(file, &DriverConfig)
file, err := os.ReadFile(filepath.Clean(driverconfig))
if err != nil {
log.Errorf("Unable to read the driver config:%s", err.Error())
return ""
}
var DriverConfig DriverConfig
err = yaml.Unmarshal(file, &DriverConfig)
if err != nil {
log.Errorf("Unable to unmarshal the driver config:%s", err.Error())
return ""
}
return DriverConfig.StorageClass.Class
}

Expand Down
1 change: 1 addition & 0 deletions pkg/utils/k8s_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func TestReadTestDriverConfig(t *testing.T) {
want string
}{
{"get storage class name", args{"testdata/config-nfs.yaml"}, "powerstore-nfs"},
{"get storage class name negative", args{""}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
35 changes: 35 additions & 0 deletions pkg/utils/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package utils

import (
"context"
"github.com/sirupsen/logrus"
"testing"
)

func TestGetLoggerFromContext(t *testing.T) {
// Test case: Context with logger
t.Run("WithLogger", func(t *testing.T) {
logger := logrus.NewEntry(logrus.StandardLogger())
ctx := context.WithValue(context.Background(), LoggerContextKey, logger)
result := GetLoggerFromContext(ctx)
if result != logger {
t.Errorf("Expected %v, got %v", logger, result)
}
})
// Test case: Context without logger
t.Run("WithoutLogger", func(t *testing.T) {
ctx := context.Background()
result := GetLoggerFromContext(ctx)
if result == nil {
t.Error("Expected non-nil logger, got nil")
}
})
// Test case: Context with non-logger value
t.Run("WithNonLoggerValue", func(t *testing.T) {
ctx := context.WithValue(context.Background(), LoggerContextKey, "not a logger")
result := GetLoggerFromContext(ctx)
if result == nil {
t.Error("Expected non-nil logger, got nil")
}
})
}

0 comments on commit 9d6b4e4

Please sign in to comment.