Skip to content

Commit

Permalink
test: add unit test
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
andyzhangx committed Jan 15, 2025
1 parent 5784a6e commit 8486bcb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
3 changes: 1 addition & 2 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ func GetMountOptions(options []string) string {
}

func MakeDir(pathname string, perm os.FileMode) error {
err := os.MkdirAll(pathname, perm)
if err != nil {
if err := os.MkdirAll(pathname, perm); err != nil {
if !os.IsExist(err) {
return err
}
Expand Down
61 changes: 55 additions & 6 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ package util
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"k8s.io/kubernetes/pkg/volume"
)

func TestRoundUpBytes(t *testing.T) {
Expand Down Expand Up @@ -164,14 +166,18 @@ func TestGetMountOptions(t *testing.T) {
}

func TestMakeDir(t *testing.T) {
//Successfully create directory
targetTest := "./target_test"
// Successfully create directory
targetTest := filepath.Join(os.TempDir(), "TestMakeDir")
err := MakeDir(targetTest, 0777)

Check failure on line 171 in pkg/util/util_test.go

View workflow job for this annotation

GitHub Actions / Go Lint

ineffectual assignment to err (ineffassign)
assert.NoError(t, err)
defer func() {
err := os.RemoveAll(targetTest)
assert.NoError(t, err)
}()

// Remove the directory created
err = os.RemoveAll(targetTest)
assert.NoError(t, err)
// create an existing directory
if err = MakeDir(targetTest, 0755); err != nil {
t.Errorf("Unexpected error: %v", err)
}
}

func TestConvertTagsToMap(t *testing.T) {
Expand Down Expand Up @@ -614,6 +620,11 @@ users:
envVariableHasConfig: false,
envVariableConfigIsValid: false,
},
{
desc: "no-need-kubeconfig",
kubeconfig: "no-need-kubeconfig",
expectError: false,
},
}

for _, test := range tests {
Expand All @@ -625,6 +636,44 @@ users:
}
}

func TestVolumeMounter(t *testing.T) {
path := "/mnt/data"
attributes := volume.Attributes{}

mounter := &VolumeMounter{
path: path,
attributes: attributes,
}

if mounter.GetPath() != path {
t.Errorf("Expected path %s, but got %s", path, mounter.GetPath())
}

if mounter.GetAttributes() != attributes {
t.Errorf("Expected attributes %v, but got %v", attributes, mounter.GetAttributes())
}

if err := mounter.CanMount(); err != nil {
t.Errorf("Unexpected error: %v", err)
}

if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
t.Errorf("Unexpected error: %v", err)
}

if err := mounter.SetUpAt("", volume.MounterArgs{}); err != nil {
t.Errorf("Unexpected error: %v", err)
}

metrics, err := mounter.GetMetrics()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if metrics != nil {
t.Errorf("Expected nil metrics, but got %v", metrics)
}
}

func TestSetVolumeOwnership(t *testing.T) {
tmpVDir, err := os.MkdirTemp(os.TempDir(), "SetVolumeOwnership")
if err != nil {
Expand Down

0 comments on commit 8486bcb

Please sign in to comment.