Skip to content

Commit

Permalink
test: copy volume from snapshot unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wozniakjan committed Mar 26, 2023
1 parent e32cb1d commit 76f4841
Showing 1 changed file with 122 additions and 3 deletions.
125 changes: 122 additions & 3 deletions pkg/nfs/controllerserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package nfs

import (
"archive/tar"
"compress/gzip"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -634,6 +636,8 @@ func TestCopyVolume(t *testing.T) {
req *csi.CreateVolumeRequest
dstVol *nfsVolume
expectErr bool
prepare func() error
cleanup func() error
}{
{
desc: "copy volume from valid volume",
Expand All @@ -654,6 +658,56 @@ func TestCopyVolume(t *testing.T) {
subDir: "subdir",
uuid: "dst-pv-name",
},
prepare: func() error { return os.MkdirAll("/tmp/src-pv-name/subdir", 0777) },
cleanup: func() error { return os.RemoveAll("/tmp/src-pv-name") },
},
{
desc: "copy volume from valid snapshot",
req: &csi.CreateVolumeRequest{
Name: "snapshot-name",
VolumeContentSource: &csi.VolumeContentSource{
Type: &csi.VolumeContentSource_Snapshot{
Snapshot: &csi.VolumeContentSource_SnapshotSource{
SnapshotId: "nfs-server.default.svc.cluster.local#share#snapshot-name#snapshot-name#src-pv-name",
},
},
},
},
dstVol: &nfsVolume{
id: "nfs-server.default.svc.cluster.local#share#subdir#dst-pv-name",
server: "//nfs-server.default.svc.cluster.local",
baseDir: "share",
subDir: "subdir",
uuid: "dst-pv-name",
},
prepare: func() error {
if err := os.MkdirAll("/tmp/snapshot-name/share/snapshot-name/", 0777); err != nil {
return err
}
file, err := os.Create("/tmp/snapshot-name/share/snapshot-name/src-pv-name.tar.gz")
if err != nil {
return err
}
defer file.Close()
gzipWriter := gzip.NewWriter(file)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
body := "test file"
hdr := &tar.Header{
Name: "test.txt",
Mode: 0777,
Size: int64(len(body)),
}
if err := tarWriter.WriteHeader(hdr); err != nil {
return err
}
if _, err := tarWriter.Write([]byte(body)); err != nil {
return err
}
return nil
},
cleanup: func() error { return os.RemoveAll("/tmp/snapshot-name") },
},
{
desc: "copy volume missing source id",
Expand Down Expand Up @@ -694,17 +748,82 @@ func TestCopyVolume(t *testing.T) {
},
expectErr: true,
},
}
if err := os.MkdirAll("/tmp/src-pv-name/subdir", 0777); err != nil {
t.Fatalf("Unexpected error when creating srcVolume: %v", err)
{
desc: "copy volume from broken snapshot",
req: &csi.CreateVolumeRequest{
Name: "snapshot-name",
VolumeContentSource: &csi.VolumeContentSource{
Type: &csi.VolumeContentSource_Snapshot{
Snapshot: &csi.VolumeContentSource_SnapshotSource{
SnapshotId: "nfs-server.default.svc.cluster.local#share#snapshot-name#snapshot-name#src-pv-name",
},
},
},
},
dstVol: &nfsVolume{
id: "nfs-server.default.svc.cluster.local#share#subdir#dst-pv-name",
server: "//nfs-server.default.svc.cluster.local",
baseDir: "share",
subDir: "subdir",
uuid: "dst-pv-name",
},
expectErr: true,
},
{
desc: "copy volume from missing snapshot",
req: &csi.CreateVolumeRequest{
Name: "snapshot-name",
VolumeContentSource: &csi.VolumeContentSource{
Type: &csi.VolumeContentSource_Snapshot{
Snapshot: &csi.VolumeContentSource_SnapshotSource{},
},
},
},
dstVol: &nfsVolume{
id: "nfs-server.default.svc.cluster.local#share#subdir#dst-pv-name",
server: "//nfs-server.default.svc.cluster.local",
baseDir: "share",
subDir: "subdir",
uuid: "dst-pv-name",
},
expectErr: true,
},
{
desc: "copy volume from snapshot into missing dst volume",
req: &csi.CreateVolumeRequest{
Name: "snapshot-name",
VolumeContentSource: &csi.VolumeContentSource{
Type: &csi.VolumeContentSource_Snapshot{
Snapshot: &csi.VolumeContentSource_SnapshotSource{},
},
},
},
dstVol: &nfsVolume{
server: "//nfs-server.default.svc.cluster.local",
baseDir: "share",
subDir: "subdir",
uuid: "dst-pv-name",
},
expectErr: true,
},
}
for _, test := range cases {
t.Run(test.desc, func(t *testing.T) {
if test.prepare != nil {
if err := test.prepare(); err != nil {
t.Errorf(`[test: %s] prepare failed: "%v"`, test.desc, err)
}
}
cs := initTestController(t)
err := cs.copyVolume(context.TODO(), test.req, test.dstVol)
if (err == nil) == test.expectErr {
t.Errorf(`[test: %s] Error expectation mismatch, expected error: "%v", received: %q`, test.desc, test.expectErr, err)
}
if test.cleanup != nil {
if err := test.cleanup(); err != nil {
t.Errorf(`[test: %s] cleanup failed: "%v"`, test.desc, err)
}
}
})
}
}

0 comments on commit 76f4841

Please sign in to comment.