mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 22:17:14 +00:00
Merge pull request #80418 from cwdsuzhou/July/fix_csi_attacher_ut
Fix csi attacher unit tests using t.Run()
This commit is contained in:
commit
1cb3b5807e
@ -190,40 +190,42 @@ func TestAttacherAttach(t *testing.T) {
|
|||||||
|
|
||||||
// attacher loop
|
// attacher loop
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Logf("test case: %s", tc.name)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, nil)
|
t.Logf("test case: %s", tc.name)
|
||||||
defer os.RemoveAll(tmpDir)
|
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, nil)
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
attacher, err := plug.NewAttacher()
|
attacher, err := plug.NewAttacher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create new attacher: %v", err)
|
t.Fatalf("failed to create new attacher: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
csiAttacher := attacher.(*csiAttacher)
|
csiAttacher := attacher.(*csiAttacher)
|
||||||
|
|
||||||
go func(spec *volume.Spec, nodename string, fail bool) {
|
go func(spec *volume.Spec, nodename string, fail bool) {
|
||||||
attachID, err := csiAttacher.Attach(spec, types.NodeName(nodename))
|
attachID, err := csiAttacher.Attach(spec, types.NodeName(nodename))
|
||||||
if !fail && err != nil {
|
if !fail && err != nil {
|
||||||
t.Errorf("expecting no failure, but got err: %v", err)
|
t.Errorf("expecting no failure, but got err: %v", err)
|
||||||
}
|
}
|
||||||
if fail && err == nil {
|
if fail && err == nil {
|
||||||
t.Errorf("expecting failure, but got no err")
|
t.Errorf("expecting failure, but got no err")
|
||||||
}
|
}
|
||||||
if attachID != "" {
|
if attachID != "" {
|
||||||
t.Errorf("expecting empty attachID, got %v", attachID)
|
t.Errorf("expecting empty attachID, got %v", attachID)
|
||||||
}
|
}
|
||||||
}(tc.spec, tc.nodeName, tc.shouldFail)
|
}(tc.spec, tc.nodeName, tc.shouldFail)
|
||||||
|
|
||||||
var status storage.VolumeAttachmentStatus
|
var status storage.VolumeAttachmentStatus
|
||||||
if tc.injectAttacherError {
|
if tc.injectAttacherError {
|
||||||
status.Attached = false
|
status.Attached = false
|
||||||
status.AttachError = &storage.VolumeError{
|
status.AttachError = &storage.VolumeError{
|
||||||
Message: "attacher error",
|
Message: "attacher error",
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
status.Attached = true
|
||||||
}
|
}
|
||||||
} else {
|
markVolumeAttached(t, csiAttacher.k8s, fakeWatcher, tc.attachID, status)
|
||||||
status.Attached = true
|
})
|
||||||
}
|
|
||||||
markVolumeAttached(t, csiAttacher.k8s, fakeWatcher, tc.attachID, status)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,36 +272,38 @@ func TestAttacherAttachWithInline(t *testing.T) {
|
|||||||
|
|
||||||
// attacher loop
|
// attacher loop
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Logf("test case: %s", tc.name)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, nil)
|
t.Logf("test case: %s", tc.name)
|
||||||
defer os.RemoveAll(tmpDir)
|
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, nil)
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
attacher, err := plug.NewAttacher()
|
attacher, err := plug.NewAttacher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create new attacher: %v", err)
|
t.Fatalf("failed to create new attacher: %v", err)
|
||||||
}
|
}
|
||||||
csiAttacher := attacher.(*csiAttacher)
|
csiAttacher := attacher.(*csiAttacher)
|
||||||
|
|
||||||
go func(spec *volume.Spec, nodename string, fail bool) {
|
go func(spec *volume.Spec, nodename string, fail bool) {
|
||||||
attachID, err := csiAttacher.Attach(spec, types.NodeName(nodename))
|
attachID, err := csiAttacher.Attach(spec, types.NodeName(nodename))
|
||||||
if fail != (err != nil) {
|
if fail != (err != nil) {
|
||||||
t.Errorf("expecting no failure, but got err: %v", err)
|
t.Errorf("expecting no failure, but got err: %v", err)
|
||||||
}
|
}
|
||||||
if attachID != "" {
|
if attachID != "" {
|
||||||
t.Errorf("expecting empty attachID, got %v", attachID)
|
t.Errorf("expecting empty attachID, got %v", attachID)
|
||||||
}
|
}
|
||||||
}(tc.spec, tc.nodeName, tc.shouldFail)
|
}(tc.spec, tc.nodeName, tc.shouldFail)
|
||||||
|
|
||||||
var status storage.VolumeAttachmentStatus
|
var status storage.VolumeAttachmentStatus
|
||||||
if tc.injectAttacherError {
|
if tc.injectAttacherError {
|
||||||
status.Attached = false
|
status.Attached = false
|
||||||
status.AttachError = &storage.VolumeError{
|
status.AttachError = &storage.VolumeError{
|
||||||
Message: "attacher error",
|
Message: "attacher error",
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
status.Attached = true
|
||||||
}
|
}
|
||||||
} else {
|
markVolumeAttached(t, csiAttacher.k8s, fakeWatcher, tc.attachID, status)
|
||||||
status.Attached = true
|
})
|
||||||
}
|
|
||||||
markVolumeAttached(t, csiAttacher.k8s, fakeWatcher, tc.attachID, status)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -670,52 +674,54 @@ func TestAttacherWaitForVolumeAttachment(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, tc := range testCases {
|
for i, tc := range testCases {
|
||||||
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, nil)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
defer os.RemoveAll(tmpDir)
|
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, nil)
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
attacher, err := plug.NewAttacher()
|
attacher, err := plug.NewAttacher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create new attacher: %v", err)
|
t.Fatalf("failed to create new attacher: %v", err)
|
||||||
}
|
|
||||||
csiAttacher := attacher.(*csiAttacher)
|
|
||||||
t.Logf("running test: %v", tc.name)
|
|
||||||
pvName := fmt.Sprintf("test-pv-%d", i)
|
|
||||||
volID := fmt.Sprintf("test-vol-%d", i)
|
|
||||||
attachID := getAttachmentName(volID, testDriver, nodeName)
|
|
||||||
attachment := makeTestAttachment(attachID, nodeName, pvName)
|
|
||||||
attachment.Status.Attached = tc.initAttached
|
|
||||||
attachment.Status.AttachError = tc.initAttachErr
|
|
||||||
_, err = csiAttacher.k8s.StorageV1().VolumeAttachments().Create(attachment)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to attach: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
trigerWatchEventTime := tc.trigerWatchEventTime
|
|
||||||
finalAttached := tc.finalAttached
|
|
||||||
finalAttachErr := tc.finalAttachErr
|
|
||||||
// after timeout, fakeWatcher will be closed by csiAttacher.waitForVolumeAttachment
|
|
||||||
if tc.trigerWatchEventTime > 0 && tc.trigerWatchEventTime < tc.timeout {
|
|
||||||
go func() {
|
|
||||||
time.Sleep(trigerWatchEventTime)
|
|
||||||
attachment := makeTestAttachment(attachID, nodeName, pvName)
|
|
||||||
attachment.Status.Attached = finalAttached
|
|
||||||
attachment.Status.AttachError = finalAttachErr
|
|
||||||
fakeWatcher.Modify(attachment)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
retID, err := csiAttacher.waitForVolumeAttachment(volID, attachID, tc.timeout)
|
|
||||||
if tc.shouldFail && err == nil {
|
|
||||||
t.Error("expecting failure, but err is nil")
|
|
||||||
}
|
|
||||||
if tc.initAttachErr != nil {
|
|
||||||
if tc.initAttachErr.Message != err.Error() {
|
|
||||||
t.Errorf("expecting error [%v], got [%v]", tc.initAttachErr.Message, err.Error())
|
|
||||||
}
|
}
|
||||||
}
|
csiAttacher := attacher.(*csiAttacher)
|
||||||
if err == nil && retID != attachID {
|
t.Logf("running test: %v", tc.name)
|
||||||
t.Errorf("attacher.WaitForAttach not returning attachment ID")
|
pvName := fmt.Sprintf("test-pv-%d", i)
|
||||||
}
|
volID := fmt.Sprintf("test-vol-%d", i)
|
||||||
|
attachID := getAttachmentName(volID, testDriver, nodeName)
|
||||||
|
attachment := makeTestAttachment(attachID, nodeName, pvName)
|
||||||
|
attachment.Status.Attached = tc.initAttached
|
||||||
|
attachment.Status.AttachError = tc.initAttachErr
|
||||||
|
_, err = csiAttacher.k8s.StorageV1().VolumeAttachments().Create(attachment)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to attach: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
trigerWatchEventTime := tc.trigerWatchEventTime
|
||||||
|
finalAttached := tc.finalAttached
|
||||||
|
finalAttachErr := tc.finalAttachErr
|
||||||
|
// after timeout, fakeWatcher will be closed by csiAttacher.waitForVolumeAttachment
|
||||||
|
if tc.trigerWatchEventTime > 0 && tc.trigerWatchEventTime < tc.timeout {
|
||||||
|
go func() {
|
||||||
|
time.Sleep(trigerWatchEventTime)
|
||||||
|
attachment := makeTestAttachment(attachID, nodeName, pvName)
|
||||||
|
attachment.Status.Attached = finalAttached
|
||||||
|
attachment.Status.AttachError = finalAttachErr
|
||||||
|
fakeWatcher.Modify(attachment)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
retID, err := csiAttacher.waitForVolumeAttachment(volID, attachID, tc.timeout)
|
||||||
|
if tc.shouldFail && err == nil {
|
||||||
|
t.Error("expecting failure, but err is nil")
|
||||||
|
}
|
||||||
|
if tc.initAttachErr != nil {
|
||||||
|
if tc.initAttachErr.Message != err.Error() {
|
||||||
|
t.Errorf("expecting error [%v], got [%v]", tc.initAttachErr.Message, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err == nil && retID != attachID {
|
||||||
|
t.Errorf("attacher.WaitForAttach not returning attachment ID")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -911,50 +917,52 @@ func TestAttacherDetach(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Logf("running test: %v", tc.name)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
plug, fakeWatcher, tmpDir, client := newTestWatchPlugin(t, nil)
|
t.Logf("running test: %v", tc.name)
|
||||||
defer os.RemoveAll(tmpDir)
|
plug, fakeWatcher, tmpDir, client := newTestWatchPlugin(t, nil)
|
||||||
if tc.reactor != nil {
|
defer os.RemoveAll(tmpDir)
|
||||||
client.PrependReactor("*", "*", tc.reactor)
|
if tc.reactor != nil {
|
||||||
}
|
client.PrependReactor("*", "*", tc.reactor)
|
||||||
|
}
|
||||||
|
|
||||||
attacher, err0 := plug.NewAttacher()
|
attacher, err0 := plug.NewAttacher()
|
||||||
if err0 != nil {
|
if err0 != nil {
|
||||||
t.Fatalf("failed to create new attacher: %v", err0)
|
t.Fatalf("failed to create new attacher: %v", err0)
|
||||||
}
|
}
|
||||||
csiAttacher := attacher.(*csiAttacher)
|
csiAttacher := attacher.(*csiAttacher)
|
||||||
|
|
||||||
pv := makeTestPV("test-pv", 10, testDriver, tc.volID)
|
pv := makeTestPV("test-pv", 10, testDriver, tc.volID)
|
||||||
spec := volume.NewSpecFromPersistentVolume(pv, pv.Spec.PersistentVolumeSource.CSI.ReadOnly)
|
spec := volume.NewSpecFromPersistentVolume(pv, pv.Spec.PersistentVolumeSource.CSI.ReadOnly)
|
||||||
attachment := makeTestAttachment(tc.attachID, nodeName, "test-pv")
|
attachment := makeTestAttachment(tc.attachID, nodeName, "test-pv")
|
||||||
_, err := csiAttacher.k8s.StorageV1().VolumeAttachments().Create(attachment)
|
_, err := csiAttacher.k8s.StorageV1().VolumeAttachments().Create(attachment)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to attach: %v", err)
|
t.Fatalf("failed to attach: %v", err)
|
||||||
}
|
}
|
||||||
volumeName, err := plug.GetVolumeName(spec)
|
volumeName, err := plug.GetVolumeName(spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("test case %s failed: %v", tc.name, err)
|
t.Errorf("test case %s failed: %v", tc.name, err)
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
fakeWatcher.Delete(attachment)
|
fakeWatcher.Delete(attachment)
|
||||||
}()
|
}()
|
||||||
err = csiAttacher.Detach(volumeName, types.NodeName(nodeName))
|
err = csiAttacher.Detach(volumeName, types.NodeName(nodeName))
|
||||||
if tc.shouldFail && err == nil {
|
if tc.shouldFail && err == nil {
|
||||||
t.Fatal("expecting failure, but err = nil")
|
t.Fatal("expecting failure, but err = nil")
|
||||||
}
|
}
|
||||||
if !tc.shouldFail && err != nil {
|
if !tc.shouldFail && err != nil {
|
||||||
t.Fatalf("unexpected err: %v", err)
|
|
||||||
}
|
|
||||||
attach, err := csiAttacher.k8s.StorageV1().VolumeAttachments().Get(tc.attachID, meta.GetOptions{})
|
|
||||||
if err != nil {
|
|
||||||
if !apierrs.IsNotFound(err) {
|
|
||||||
t.Fatalf("unexpected err: %v", err)
|
t.Fatalf("unexpected err: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
attach, err := csiAttacher.k8s.StorageV1().VolumeAttachments().Get(tc.attachID, meta.GetOptions{})
|
||||||
if attach == nil {
|
if err != nil {
|
||||||
t.Errorf("expecting attachment not to be nil, but it is")
|
if !apierrs.IsNotFound(err) {
|
||||||
|
t.Fatalf("unexpected err: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if attach == nil {
|
||||||
|
t.Errorf("expecting attachment not to be nil, but it is")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1087,73 +1095,75 @@ func TestAttacherMountDevice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Logf("Running test case: %s", tc.testName)
|
t.Run(tc.testName, func(t *testing.T) {
|
||||||
|
t.Logf("Running test case: %s", tc.testName)
|
||||||
|
|
||||||
// Setup
|
// Setup
|
||||||
// Create a new attacher
|
// Create a new attacher
|
||||||
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, nil)
|
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, nil)
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
attacher, err0 := plug.NewAttacher()
|
attacher, err0 := plug.NewAttacher()
|
||||||
if err0 != nil {
|
if err0 != nil {
|
||||||
t.Fatalf("failed to create new attacher: %v", err0)
|
t.Fatalf("failed to create new attacher: %v", err0)
|
||||||
}
|
|
||||||
csiAttacher := attacher.(*csiAttacher)
|
|
||||||
csiAttacher.csiClient = setupClient(t, tc.stageUnstageSet)
|
|
||||||
|
|
||||||
if tc.deviceMountPath != "" {
|
|
||||||
tc.deviceMountPath = filepath.Join(tmpDir, tc.deviceMountPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
nodeName := string(csiAttacher.plugin.host.GetNodeName())
|
|
||||||
attachID := getAttachmentName(tc.volName, testDriver, nodeName)
|
|
||||||
|
|
||||||
// Set up volume attachment
|
|
||||||
attachment := makeTestAttachment(attachID, nodeName, pvName)
|
|
||||||
_, err := csiAttacher.k8s.StorageV1().VolumeAttachments().Create(attachment)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to attach: %v", err)
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
fakeWatcher.Delete(attachment)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Run
|
|
||||||
err = csiAttacher.MountDevice(tc.spec, tc.devicePath, tc.deviceMountPath)
|
|
||||||
|
|
||||||
// Verify
|
|
||||||
if err != nil {
|
|
||||||
if !tc.shouldFail {
|
|
||||||
t.Errorf("test should not fail, but error occurred: %v", err)
|
|
||||||
}
|
}
|
||||||
continue
|
csiAttacher := attacher.(*csiAttacher)
|
||||||
}
|
csiAttacher.csiClient = setupClient(t, tc.stageUnstageSet)
|
||||||
if err == nil && tc.shouldFail {
|
|
||||||
t.Errorf("test should fail, but no error occurred")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify call goes through all the way
|
if tc.deviceMountPath != "" {
|
||||||
numStaged := 1
|
tc.deviceMountPath = filepath.Join(tmpDir, tc.deviceMountPath)
|
||||||
if !tc.stageUnstageSet {
|
}
|
||||||
numStaged = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
cdc := csiAttacher.csiClient.(*fakeCsiDriverClient)
|
nodeName := string(csiAttacher.plugin.host.GetNodeName())
|
||||||
staged := cdc.nodeClient.GetNodeStagedVolumes()
|
attachID := getAttachmentName(tc.volName, testDriver, nodeName)
|
||||||
if len(staged) != numStaged {
|
|
||||||
t.Errorf("got wrong number of staged volumes, expecting %v got: %v", numStaged, len(staged))
|
// Set up volume attachment
|
||||||
}
|
attachment := makeTestAttachment(attachID, nodeName, pvName)
|
||||||
if tc.stageUnstageSet {
|
_, err := csiAttacher.k8s.StorageV1().VolumeAttachments().Create(attachment)
|
||||||
vol, ok := staged[tc.volName]
|
if err != nil {
|
||||||
if !ok {
|
t.Fatalf("failed to attach: %v", err)
|
||||||
t.Errorf("could not find staged volume: %s", tc.volName)
|
|
||||||
}
|
}
|
||||||
if vol.Path != tc.deviceMountPath {
|
go func() {
|
||||||
t.Errorf("expected mount path: %s. got: %s", tc.deviceMountPath, vol.Path)
|
fakeWatcher.Delete(attachment)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Run
|
||||||
|
err = csiAttacher.MountDevice(tc.spec, tc.devicePath, tc.deviceMountPath)
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
if err != nil {
|
||||||
|
if !tc.shouldFail {
|
||||||
|
t.Errorf("test should not fail, but error occurred: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(vol.MountFlags, tc.spec.PersistentVolume.Spec.MountOptions) {
|
if err == nil && tc.shouldFail {
|
||||||
t.Errorf("expected mount options: %v, got: %v", tc.spec.PersistentVolume.Spec.MountOptions, vol.MountFlags)
|
t.Errorf("test should fail, but no error occurred")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// Verify call goes through all the way
|
||||||
|
numStaged := 1
|
||||||
|
if !tc.stageUnstageSet {
|
||||||
|
numStaged = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
cdc := csiAttacher.csiClient.(*fakeCsiDriverClient)
|
||||||
|
staged := cdc.nodeClient.GetNodeStagedVolumes()
|
||||||
|
if len(staged) != numStaged {
|
||||||
|
t.Errorf("got wrong number of staged volumes, expecting %v got: %v", numStaged, len(staged))
|
||||||
|
}
|
||||||
|
if tc.stageUnstageSet {
|
||||||
|
vol, ok := staged[tc.volName]
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("could not find staged volume: %s", tc.volName)
|
||||||
|
}
|
||||||
|
if vol.Path != tc.deviceMountPath {
|
||||||
|
t.Errorf("expected mount path: %s. got: %s", tc.deviceMountPath, vol.Path)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(vol.MountFlags, tc.spec.PersistentVolume.Spec.MountOptions) {
|
||||||
|
t.Errorf("expected mount options: %v, got: %v", tc.spec.PersistentVolume.Spec.MountOptions, vol.MountFlags)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1230,70 +1240,72 @@ func TestAttacherMountDeviceWithInline(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Logf("Running test case: %s", tc.testName)
|
t.Run(tc.testName, func(t *testing.T) {
|
||||||
|
t.Logf("Running test case: %s", tc.testName)
|
||||||
|
|
||||||
// Setup
|
// Setup
|
||||||
// Create a new attacher
|
// Create a new attacher
|
||||||
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, nil)
|
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, nil)
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
attacher, err0 := plug.NewAttacher()
|
attacher, err0 := plug.NewAttacher()
|
||||||
if err0 != nil {
|
if err0 != nil {
|
||||||
t.Fatalf("failed to create new attacher: %v", err0)
|
t.Fatalf("failed to create new attacher: %v", err0)
|
||||||
}
|
|
||||||
csiAttacher := attacher.(*csiAttacher)
|
|
||||||
csiAttacher.csiClient = setupClient(t, tc.stageUnstageSet)
|
|
||||||
|
|
||||||
if tc.deviceMountPath != "" {
|
|
||||||
tc.deviceMountPath = filepath.Join(tmpDir, tc.deviceMountPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
nodeName := string(csiAttacher.plugin.host.GetNodeName())
|
|
||||||
attachID := getAttachmentName(tc.volName, testDriver, nodeName)
|
|
||||||
|
|
||||||
// Set up volume attachment
|
|
||||||
attachment := makeTestAttachment(attachID, nodeName, pvName)
|
|
||||||
_, err := csiAttacher.k8s.StorageV1().VolumeAttachments().Create(attachment)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to attach: %v", err)
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
fakeWatcher.Delete(attachment)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Run
|
|
||||||
err = csiAttacher.MountDevice(tc.spec, tc.devicePath, tc.deviceMountPath)
|
|
||||||
|
|
||||||
// Verify
|
|
||||||
if err != nil {
|
|
||||||
if !tc.shouldFail {
|
|
||||||
t.Errorf("test should not fail, but error occurred: %v", err)
|
|
||||||
}
|
}
|
||||||
continue
|
csiAttacher := attacher.(*csiAttacher)
|
||||||
}
|
csiAttacher.csiClient = setupClient(t, tc.stageUnstageSet)
|
||||||
if err == nil && tc.shouldFail {
|
|
||||||
t.Errorf("test should fail, but no error occurred")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify call goes through all the way
|
if tc.deviceMountPath != "" {
|
||||||
numStaged := 1
|
tc.deviceMountPath = filepath.Join(tmpDir, tc.deviceMountPath)
|
||||||
if !tc.stageUnstageSet {
|
}
|
||||||
numStaged = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
cdc := csiAttacher.csiClient.(*fakeCsiDriverClient)
|
nodeName := string(csiAttacher.plugin.host.GetNodeName())
|
||||||
staged := cdc.nodeClient.GetNodeStagedVolumes()
|
attachID := getAttachmentName(tc.volName, testDriver, nodeName)
|
||||||
if len(staged) != numStaged {
|
|
||||||
t.Errorf("got wrong number of staged volumes, expecting %v got: %v", numStaged, len(staged))
|
// Set up volume attachment
|
||||||
}
|
attachment := makeTestAttachment(attachID, nodeName, pvName)
|
||||||
if tc.stageUnstageSet {
|
_, err := csiAttacher.k8s.StorageV1().VolumeAttachments().Create(attachment)
|
||||||
vol, ok := staged[tc.volName]
|
if err != nil {
|
||||||
if !ok {
|
t.Fatalf("failed to attach: %v", err)
|
||||||
t.Errorf("could not find staged volume: %s", tc.volName)
|
|
||||||
}
|
}
|
||||||
if vol.Path != tc.deviceMountPath {
|
go func() {
|
||||||
t.Errorf("expected mount path: %s. got: %s", tc.deviceMountPath, vol.Path)
|
fakeWatcher.Delete(attachment)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Run
|
||||||
|
err = csiAttacher.MountDevice(tc.spec, tc.devicePath, tc.deviceMountPath)
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
if err != nil {
|
||||||
|
if !tc.shouldFail {
|
||||||
|
t.Errorf("test should not fail, but error occurred: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
if err == nil && tc.shouldFail {
|
||||||
|
t.Errorf("test should fail, but no error occurred")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify call goes through all the way
|
||||||
|
numStaged := 1
|
||||||
|
if !tc.stageUnstageSet {
|
||||||
|
numStaged = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
cdc := csiAttacher.csiClient.(*fakeCsiDriverClient)
|
||||||
|
staged := cdc.nodeClient.GetNodeStagedVolumes()
|
||||||
|
if len(staged) != numStaged {
|
||||||
|
t.Errorf("got wrong number of staged volumes, expecting %v got: %v", numStaged, len(staged))
|
||||||
|
}
|
||||||
|
if tc.stageUnstageSet {
|
||||||
|
vol, ok := staged[tc.volName]
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("could not find staged volume: %s", tc.volName)
|
||||||
|
}
|
||||||
|
if vol.Path != tc.deviceMountPath {
|
||||||
|
t.Errorf("expected mount path: %s. got: %s", tc.deviceMountPath, vol.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1357,91 +1369,93 @@ func TestAttacherUnmountDevice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Logf("Running test case: %s", tc.testName)
|
t.Run(tc.testName, func(t *testing.T) {
|
||||||
// Setup
|
t.Logf("Running test case: %s", tc.testName)
|
||||||
// Create a new attacher
|
// Setup
|
||||||
plug, _, tmpDir, _ := newTestWatchPlugin(t, nil)
|
// Create a new attacher
|
||||||
defer os.RemoveAll(tmpDir)
|
plug, _, tmpDir, _ := newTestWatchPlugin(t, nil)
|
||||||
attacher, err0 := plug.NewAttacher()
|
defer os.RemoveAll(tmpDir)
|
||||||
if err0 != nil {
|
attacher, err0 := plug.NewAttacher()
|
||||||
t.Fatalf("failed to create new attacher: %v", err0)
|
if err0 != nil {
|
||||||
}
|
t.Fatalf("failed to create new attacher: %v", err0)
|
||||||
csiAttacher := attacher.(*csiAttacher)
|
|
||||||
csiAttacher.csiClient = setupClient(t, tc.stageUnstageSet)
|
|
||||||
|
|
||||||
if tc.deviceMountPath != "" {
|
|
||||||
tc.deviceMountPath = filepath.Join(tmpDir, tc.deviceMountPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the volume to NodeStagedVolumes
|
|
||||||
cdc := csiAttacher.csiClient.(*fakeCsiDriverClient)
|
|
||||||
cdc.nodeClient.AddNodeStagedVolume(tc.volID, tc.deviceMountPath, nil)
|
|
||||||
|
|
||||||
// Make JSON for this object
|
|
||||||
if tc.deviceMountPath != "" {
|
|
||||||
if err := os.MkdirAll(tc.deviceMountPath, 0755); err != nil {
|
|
||||||
t.Fatalf("error creating directory %s: %s", tc.deviceMountPath, err)
|
|
||||||
}
|
}
|
||||||
}
|
csiAttacher := attacher.(*csiAttacher)
|
||||||
dir := filepath.Dir(tc.deviceMountPath)
|
csiAttacher.csiClient = setupClient(t, tc.stageUnstageSet)
|
||||||
if tc.jsonFile != "" {
|
|
||||||
dataPath := filepath.Join(dir, volDataFileName)
|
if tc.deviceMountPath != "" {
|
||||||
if err := ioutil.WriteFile(dataPath, []byte(tc.jsonFile), 0644); err != nil {
|
tc.deviceMountPath = filepath.Join(tmpDir, tc.deviceMountPath)
|
||||||
t.Fatalf("error creating %s: %s", dataPath, err)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if tc.createPV {
|
|
||||||
// Make the PV for this object
|
|
||||||
pvName := filepath.Base(dir)
|
|
||||||
pv := makeTestPV(pvName, 5, "csi", tc.volID)
|
|
||||||
_, err := csiAttacher.k8s.CoreV1().PersistentVolumes().Create(pv)
|
|
||||||
if err != nil && !tc.shouldFail {
|
|
||||||
t.Fatalf("Failed to create PV: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run
|
// Add the volume to NodeStagedVolumes
|
||||||
err := csiAttacher.UnmountDevice(tc.deviceMountPath)
|
cdc := csiAttacher.csiClient.(*fakeCsiDriverClient)
|
||||||
// Verify
|
cdc.nodeClient.AddNodeStagedVolume(tc.volID, tc.deviceMountPath, nil)
|
||||||
if err != nil {
|
|
||||||
if !tc.shouldFail {
|
|
||||||
t.Errorf("test should not fail, but error occurred: %v", err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err == nil && tc.shouldFail {
|
|
||||||
t.Errorf("test should fail, but no error occurred")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify call goes through all the way
|
// Make JSON for this object
|
||||||
expectedSet := 0
|
if tc.deviceMountPath != "" {
|
||||||
if !tc.stageUnstageSet {
|
if err := os.MkdirAll(tc.deviceMountPath, 0755); err != nil {
|
||||||
expectedSet = 1
|
t.Fatalf("error creating directory %s: %s", tc.deviceMountPath, err)
|
||||||
}
|
|
||||||
staged := cdc.nodeClient.GetNodeStagedVolumes()
|
|
||||||
if len(staged) != expectedSet {
|
|
||||||
t.Errorf("got wrong number of staged volumes, expecting %v got: %v", expectedSet, len(staged))
|
|
||||||
}
|
|
||||||
|
|
||||||
_, ok := staged[tc.volID]
|
|
||||||
if ok && tc.stageUnstageSet {
|
|
||||||
t.Errorf("found unexpected staged volume: %s", tc.volID)
|
|
||||||
} else if !ok && !tc.stageUnstageSet {
|
|
||||||
t.Errorf("could not find expected staged volume: %s", tc.volID)
|
|
||||||
}
|
|
||||||
|
|
||||||
if tc.jsonFile != "" && !tc.shouldFail {
|
|
||||||
dataPath := filepath.Join(dir, volDataFileName)
|
|
||||||
if _, err := os.Stat(dataPath); !os.IsNotExist(err) {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("error checking file %s: %s", dataPath, err)
|
|
||||||
} else {
|
|
||||||
t.Errorf("json file %s should not exists, but it does", dataPath)
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
t.Logf("json file %s was correctly removed", dataPath)
|
|
||||||
}
|
}
|
||||||
}
|
dir := filepath.Dir(tc.deviceMountPath)
|
||||||
|
if tc.jsonFile != "" {
|
||||||
|
dataPath := filepath.Join(dir, volDataFileName)
|
||||||
|
if err := ioutil.WriteFile(dataPath, []byte(tc.jsonFile), 0644); err != nil {
|
||||||
|
t.Fatalf("error creating %s: %s", dataPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tc.createPV {
|
||||||
|
// Make the PV for this object
|
||||||
|
pvName := filepath.Base(dir)
|
||||||
|
pv := makeTestPV(pvName, 5, "csi", tc.volID)
|
||||||
|
_, err := csiAttacher.k8s.CoreV1().PersistentVolumes().Create(pv)
|
||||||
|
if err != nil && !tc.shouldFail {
|
||||||
|
t.Fatalf("Failed to create PV: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run
|
||||||
|
err := csiAttacher.UnmountDevice(tc.deviceMountPath)
|
||||||
|
// Verify
|
||||||
|
if err != nil {
|
||||||
|
if !tc.shouldFail {
|
||||||
|
t.Errorf("test should not fail, but error occurred: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err == nil && tc.shouldFail {
|
||||||
|
t.Errorf("test should fail, but no error occurred")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify call goes through all the way
|
||||||
|
expectedSet := 0
|
||||||
|
if !tc.stageUnstageSet {
|
||||||
|
expectedSet = 1
|
||||||
|
}
|
||||||
|
staged := cdc.nodeClient.GetNodeStagedVolumes()
|
||||||
|
if len(staged) != expectedSet {
|
||||||
|
t.Errorf("got wrong number of staged volumes, expecting %v got: %v", expectedSet, len(staged))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, ok := staged[tc.volID]
|
||||||
|
if ok && tc.stageUnstageSet {
|
||||||
|
t.Errorf("found unexpected staged volume: %s", tc.volID)
|
||||||
|
} else if !ok && !tc.stageUnstageSet {
|
||||||
|
t.Errorf("could not find expected staged volume: %s", tc.volID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tc.jsonFile != "" && !tc.shouldFail {
|
||||||
|
dataPath := filepath.Join(dir, volDataFileName)
|
||||||
|
if _, err := os.Stat(dataPath); !os.IsNotExist(err) {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error checking file %s: %s", dataPath, err)
|
||||||
|
} else {
|
||||||
|
t.Errorf("json file %s should not exists, but it does", dataPath)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Logf("json file %s was correctly removed", dataPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user