From 76f48412dc68e1211d7102b4a20fd7af3346e8a1 Mon Sep 17 00:00:00 2001 From: Jan Wozniak Date: Sun, 26 Mar 2023 20:26:21 +0200 Subject: [PATCH] test: copy volume from snapshot unit tests --- pkg/nfs/controllerserver_test.go | 125 ++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 3 deletions(-) diff --git a/pkg/nfs/controllerserver_test.go b/pkg/nfs/controllerserver_test.go index 9fdb6b650..6c2b88c32 100644 --- a/pkg/nfs/controllerserver_test.go +++ b/pkg/nfs/controllerserver_test.go @@ -17,6 +17,8 @@ limitations under the License. package nfs import ( + "archive/tar" + "compress/gzip" "os" "path/filepath" "reflect" @@ -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", @@ -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", @@ -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) + } + } }) } }