Skip to content

Commit cd25601

Browse files
authored
Merge pull request #1792 from andyzhangx/add-ut
test: add unit test
2 parents 5784a6e + a90decb commit cd25601

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

pkg/util/util.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ func GetMountOptions(options []string) string {
9999
}
100100

101101
func MakeDir(pathname string, perm os.FileMode) error {
102-
err := os.MkdirAll(pathname, perm)
103-
if err != nil {
102+
if err := os.MkdirAll(pathname, perm); err != nil {
104103
if !os.IsExist(err) {
105104
return err
106105
}

pkg/util/util_test.go

+55-5
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ package util
1919
import (
2020
"fmt"
2121
"os"
22+
"path/filepath"
2223
"reflect"
2324
"testing"
2425
"time"
2526

2627
"github.com/stretchr/testify/assert"
2728
"go.uber.org/mock/gomock"
29+
"k8s.io/kubernetes/pkg/volume"
2830
)
2931

3032
func TestRoundUpBytes(t *testing.T) {
@@ -164,14 +166,19 @@ func TestGetMountOptions(t *testing.T) {
164166
}
165167

166168
func TestMakeDir(t *testing.T) {
167-
//Successfully create directory
168-
targetTest := "./target_test"
169+
// Successfully create directory
170+
targetTest := filepath.Join(os.TempDir(), "TestMakeDir")
169171
err := MakeDir(targetTest, 0777)
172+
defer func() {
173+
err := os.RemoveAll(targetTest)
174+
assert.NoError(t, err)
175+
}()
170176
assert.NoError(t, err)
171177

172-
// Remove the directory created
173-
err = os.RemoveAll(targetTest)
174-
assert.NoError(t, err)
178+
// create an existing directory
179+
if err = MakeDir(targetTest, 0755); err != nil {
180+
t.Errorf("Unexpected error: %v", err)
181+
}
175182
}
176183

177184
func TestConvertTagsToMap(t *testing.T) {
@@ -614,6 +621,11 @@ users:
614621
envVariableHasConfig: false,
615622
envVariableConfigIsValid: false,
616623
},
624+
{
625+
desc: "no-need-kubeconfig",
626+
kubeconfig: "no-need-kubeconfig",
627+
expectError: false,
628+
},
617629
}
618630

619631
for _, test := range tests {
@@ -625,6 +637,44 @@ users:
625637
}
626638
}
627639

640+
func TestVolumeMounter(t *testing.T) {
641+
path := "/mnt/data"
642+
attributes := volume.Attributes{}
643+
644+
mounter := &VolumeMounter{
645+
path: path,
646+
attributes: attributes,
647+
}
648+
649+
if mounter.GetPath() != path {
650+
t.Errorf("Expected path %s, but got %s", path, mounter.GetPath())
651+
}
652+
653+
if mounter.GetAttributes() != attributes {
654+
t.Errorf("Expected attributes %v, but got %v", attributes, mounter.GetAttributes())
655+
}
656+
657+
if err := mounter.CanMount(); err != nil {
658+
t.Errorf("Unexpected error: %v", err)
659+
}
660+
661+
if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
662+
t.Errorf("Unexpected error: %v", err)
663+
}
664+
665+
if err := mounter.SetUpAt("", volume.MounterArgs{}); err != nil {
666+
t.Errorf("Unexpected error: %v", err)
667+
}
668+
669+
metrics, err := mounter.GetMetrics()
670+
if err != nil {
671+
t.Errorf("Unexpected error: %v", err)
672+
}
673+
if metrics != nil {
674+
t.Errorf("Expected nil metrics, but got %v", metrics)
675+
}
676+
}
677+
628678
func TestSetVolumeOwnership(t *testing.T) {
629679
tmpVDir, err := os.MkdirTemp(os.TempDir(), "SetVolumeOwnership")
630680
if err != nil {

0 commit comments

Comments
 (0)