mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Include unit test
This commit is contained in:
parent
df3119e815
commit
15da65d9ae
@ -21,6 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
@ -1112,15 +1113,16 @@ func TestAttacherMountDevice(t *testing.T) {
|
|||||||
transientError := volumetypes.NewTransientOperationFailure("")
|
transientError := volumetypes.NewTransientOperationFailure("")
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
testName string
|
testName string
|
||||||
volName string
|
volName string
|
||||||
devicePath string
|
devicePath string
|
||||||
deviceMountPath string
|
deviceMountPath string
|
||||||
stageUnstageSet bool
|
stageUnstageSet bool
|
||||||
shouldFail bool
|
shouldFail bool
|
||||||
createAttachment bool
|
createAttachment bool
|
||||||
exitError error
|
populateDeviceMountPath bool
|
||||||
spec *volume.Spec
|
exitError error
|
||||||
|
spec *volume.Spec
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
testName: "normal PV",
|
testName: "normal PV",
|
||||||
@ -1210,9 +1212,24 @@ func TestAttacherMountDevice(t *testing.T) {
|
|||||||
exitError: nonFinalError,
|
exitError: nonFinalError,
|
||||||
shouldFail: true,
|
shouldFail: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
testName: "failure PV with existing data",
|
||||||
|
volName: "test-vol1",
|
||||||
|
devicePath: "path1",
|
||||||
|
deviceMountPath: "path2",
|
||||||
|
stageUnstageSet: true,
|
||||||
|
createAttachment: true,
|
||||||
|
populateDeviceMountPath: true,
|
||||||
|
shouldFail: true,
|
||||||
|
spec: volume.NewSpecFromPersistentVolume(makeTestPV(pvName, 10, testDriver, "test-vol1"), true),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
user, _ := user.Current()
|
||||||
|
if tc.populateDeviceMountPath && user.Uid == "0" {
|
||||||
|
t.Skipf("Skipping intentional failure on existing data when running as root.")
|
||||||
|
}
|
||||||
t.Run(tc.testName, func(t *testing.T) {
|
t.Run(tc.testName, func(t *testing.T) {
|
||||||
t.Logf("Running test case: %s", tc.testName)
|
t.Logf("Running test case: %s", tc.testName)
|
||||||
|
|
||||||
@ -1254,6 +1271,25 @@ func TestAttacherMountDevice(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parent := filepath.Dir(tc.deviceMountPath)
|
||||||
|
filePath := filepath.Join(parent, "newfile")
|
||||||
|
if tc.populateDeviceMountPath {
|
||||||
|
// We need to create the deviceMountPath before we Mount,
|
||||||
|
// so that we can correctly create the file without errors.
|
||||||
|
err := os.MkdirAll(tc.deviceMountPath, 0750)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error attempting to create the directory")
|
||||||
|
}
|
||||||
|
_, err = os.Create(filePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error attempting to populate file on parent path: %v", err)
|
||||||
|
}
|
||||||
|
err = os.Chmod(parent, 0555)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error attempting to modify directory permissions: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Run
|
// Run
|
||||||
err := csiAttacher.MountDevice(tc.spec, tc.devicePath, tc.deviceMountPath)
|
err := csiAttacher.MountDevice(tc.spec, tc.devicePath, tc.deviceMountPath)
|
||||||
|
|
||||||
@ -1262,6 +1298,22 @@ func TestAttacherMountDevice(t *testing.T) {
|
|||||||
if !tc.shouldFail {
|
if !tc.shouldFail {
|
||||||
t.Errorf("test should not fail, but error occurred: %v", err)
|
t.Errorf("test should not fail, but error occurred: %v", err)
|
||||||
}
|
}
|
||||||
|
if tc.populateDeviceMountPath {
|
||||||
|
// We're expecting saveVolumeData to fail, which is responsible
|
||||||
|
// for creating this file. It shouldn't exist.
|
||||||
|
_, err := os.Stat(parent + "/" + volDataFileName)
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
t.Errorf("vol_data.json should not exist: %v", err)
|
||||||
|
}
|
||||||
|
_, err = os.Stat(filePath)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
t.Errorf("expecting file to exist after err received: %v", err)
|
||||||
|
}
|
||||||
|
err = os.Chmod(parent, 0777)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to modify permissions after test: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err == nil && tc.shouldFail {
|
if err == nil && tc.shouldFail {
|
||||||
|
Loading…
Reference in New Issue
Block a user