From 7d7c7bcd0183b587f29c224c607b7a66ae318bca Mon Sep 17 00:00:00 2001 From: Ivan Shvedunov Date: Fri, 8 Feb 2019 23:41:22 +0300 Subject: [PATCH] Add libvirttools testcase for 9pfs --- pkg/fs/fake/fakefs.go | 16 ++- ...estDomainDefinitions__9pfs_volume.out.yaml | 129 ++++++++++++++++++ pkg/libvirttools/virtualization_test.go | 28 +++- pkg/virt/fake/fake_domain.go | 6 + pkg/virt/fake/paths.go | 4 + 5 files changed, 173 insertions(+), 10 deletions(-) create mode 100755 pkg/libvirttools/TestDomainDefinitions__9pfs_volume.out.yaml diff --git a/pkg/fs/fake/fakefs.go b/pkg/fs/fake/fakefs.go index 955b7dbee..7e09d9d54 100644 --- a/pkg/fs/fake/fakefs.go +++ b/pkg/fs/fake/fakefs.go @@ -29,6 +29,16 @@ import ( testutils "github.com/Mirantis/virtlet/pkg/utils/testing" ) +const pathMarker = "/__fs__/" + +func fixPath(s string) string { + p := strings.Index(s, pathMarker) + if p < 0 { + return s + } + return "/" + s[p+len(pathMarker):] +} + type fakeDelimitedReader struct { rec testutils.Recorder fileData string @@ -83,7 +93,7 @@ func (fs *FakeFileSystem) validateMountPath(target string) { // Mount implements the Mount method of FileSystem interface. func (fs *FakeFileSystem) Mount(source string, target string, fstype string, bind bool) error { fs.validateMountPath(target) - fs.rec.Rec("Mount", []interface{}{source, target, fstype, bind}) + fs.rec.Rec("Mount", []interface{}{fixPath(source), fixPath(target), fstype, bind}) // We want to check directory contents both before & after mount, // see comment in FlexVolumeDriver.mount() in flexvolume.go. @@ -113,7 +123,7 @@ func (fs *FakeFileSystem) Mount(source string, target string, fstype string, bin func (fs *FakeFileSystem) Unmount(target string, detach bool) error { // we make sure that path is under our tmpdir before wiping it fs.validateMountPath(target) - fs.rec.Rec("Unmount", []interface{}{target, detach}) + fs.rec.Rec("Unmount", []interface{}{fixPath(target), detach}) paths, err := filepath.Glob(filepath.Join(target, "*")) if err != nil { @@ -140,7 +150,7 @@ func (fs *FakeFileSystem) IsPathAnNs(path string) bool { // ChownForEmulator implements ChownForEmulator method of FileSystem interface. func (fs *FakeFileSystem) ChownForEmulator(filePath string, recursive bool) error { - fs.rec.Rec("ChownForEmulator", []interface{}{filePath, recursive}) + fs.rec.Rec("ChownForEmulator", []interface{}{fixPath(filePath), recursive}) return nil } diff --git a/pkg/libvirttools/TestDomainDefinitions__9pfs_volume.out.yaml b/pkg/libvirttools/TestDomainDefinitions__9pfs_volume.out.yaml new file mode 100755 index 000000000..b1cceafde --- /dev/null +++ b/pkg/libvirttools/TestDomainDefinitions__9pfs_volume.out.yaml @@ -0,0 +1,129 @@ +- name: GetImagePathDigestAndVirtualSize + value: fake/image1 +- name: 'storage: CreateStoragePool' + value: |- + + volumes + + /var/lib/virtlet/volumes + + +- name: 'storage: volumes: CreateStorageVol' + value: |- + + virtlet_root_231700d5-c9a6-5a49-738d-99a954c51550 + 0 + 424242 + + + + + /fake/volume/path + + + +- name: ChownForEmulator + value: + - /mounts/virtlet_231700d5-c9a6-5a49-738d-99a954c51550_9pfs-vol_0 + - false +- name: Mount + value: + - /kubelet-root/69eec606-0493-5825-73a4-c5e0c0236155/volumes/kubernetes.io~rbd/9pfs-vol + - /mounts/virtlet_231700d5-c9a6-5a49-738d-99a954c51550_9pfs-vol_0 + - bind + - true +- name: ChownForEmulator + value: + - /mounts/virtlet_231700d5-c9a6-5a49-738d-99a954c51550_9pfs-vol_0 + - false +- name: 'domain conn: DefineDomain' + value: |- + + virtlet-231700d5-c9a6-container1 + 231700d5-c9a6-5a49-738d-99a954c51550 + 1024 + 1 + + 0 + 0 + 0 + + + hvm + + + + + + destroy + restart + restart + + /vmwrapper + + + + +
+
+ + + + + +
+
+ +
+
+ + + + + + + + + + + + + + +
+ + + + + + +
+- name: 'domain conn: virtlet-231700d5-c9a6-container1: Create' +- name: 'domain conn: virtlet-231700d5-c9a6-container1: iso image' + value: + meta-data: '{"instance-id":"testName_0.default","local-hostname":"testName_0"}' + network-config: | + version: 1 + user-data: | + #cloud-config + mounts: + - - foobar + - /var/lib/foobar + - 9p + - trans=virtio + write_files: + - content: | + #!/bin/sh + if ! mountpoint /var/lib/foobar; then mkdir -p /var/lib/foobar && mount -t 9p -o trans=virtio foobar /var/lib/foobar; fi + path: /etc/cloud/mount-volumes.sh + permissions: "0755" +- name: 'domain conn: virtlet-231700d5-c9a6-container1: Destroy' +- name: 'domain conn: virtlet-231700d5-c9a6-container1: Undefine' +- name: 'storage: volumes: RemoveVolumeByName' + value: virtlet_root_231700d5-c9a6-5a49-738d-99a954c51550 +- name: Unmount + value: + - /mounts/virtlet_231700d5-c9a6-5a49-738d-99a954c51550_9pfs-vol_0 + - true diff --git a/pkg/libvirttools/virtualization_test.go b/pkg/libvirttools/virtualization_test.go index 98fc7f537..94fdd9d28 100644 --- a/pkg/libvirttools/virtualization_test.go +++ b/pkg/libvirttools/virtualization_test.go @@ -87,17 +87,19 @@ func newContainerTester(t *testing.T, rec *testutils.TopLevelRecorder, cmds []fa } imageManager := newFakeImageManager(ct.rec) - ct.kubeletRootDir = filepath.Join(ct.tmpDir, "kubelet-root") + ct.kubeletRootDir = filepath.Join(ct.tmpDir, "__fs__/kubelet-root") + mountDir := filepath.Join(ct.tmpDir, "__fs__/mounts") virtConfig := VirtualizationConfig{ - VolumePoolName: "volumes", - RawDevices: []string{"loop*"}, - KubeletRootDir: ct.kubeletRootDir, - StreamerSocketPath: "/var/lib/libvirt/streamer.sock", + VolumePoolName: "volumes", + RawDevices: []string{"loop*"}, + KubeletRootDir: ct.kubeletRootDir, + StreamerSocketPath: "/var/lib/libvirt/streamer.sock", + SharedFilesystemPath: mountDir, } fakeCommander := fakeutils.NewCommander(rec, cmds) fakeCommander.ReplaceTempPath("__pods__", "/fakedev") - fs := fakefs.NewFakeFileSystem(t, rec, "", files) + fs := fakefs.NewFakeFileSystem(t, rec, mountDir, files) ct.virtTool = NewVirtualizationTool( ct.domainConn, ct.storageConn, imageManager, ct.metadataStore, @@ -326,6 +328,7 @@ func TestDoubleStartError(t *testing.T) { type volMount struct { name string containerPath string + podSubpath string } type volDevice struct { @@ -404,6 +407,7 @@ func TestDomainDefinitions(t *testing.T) { { name: "ceph", containerPath: "/var/lib/whatever", + podSubpath: "volumes/virtlet~flexvolume_driver", }, }, }, @@ -462,6 +466,16 @@ func TestDomainDefinitions(t *testing.T) { }, }, }, + { + name: "9pfs volume", + mounts: []volMount{ + { + name: "9pfs-vol", + containerPath: "/var/lib/foobar", + podSubpath: "volumes/kubernetes.io~rbd", + }, + }, + }, // TODO: add test cases for rootfs / persistent rootfs file injection } { t.Run(tc.name, func(t *testing.T) { @@ -490,7 +504,7 @@ func TestDomainDefinitions(t *testing.T) { var mounts []types.VMMount for _, m := range tc.mounts { mounts = append(mounts, types.VMMount{ - HostPath: filepath.Join(ct.kubeletRootDir, sandbox.Uid, "volumes/virtlet~flexvolume_driver", m.name), + HostPath: filepath.Join(ct.kubeletRootDir, sandbox.Uid, m.podSubpath, m.name), ContainerPath: m.containerPath, }) } diff --git a/pkg/virt/fake/fake_domain.go b/pkg/virt/fake/fake_domain.go index 53800f21c..d3772c0a9 100644 --- a/pkg/virt/fake/fake_domain.go +++ b/pkg/virt/fake/fake_domain.go @@ -411,4 +411,10 @@ func removeVolatilePathsFromDomainDef(def *libvirtxml.Domain) { } *toUpdate = fixPath(*toUpdate) } + + for _, fs := range def.Devices.Filesystems { + if fs.Source != nil && fs.Source.Mount != nil { + fs.Source.Mount.Dir = fixPath(fs.Source.Mount.Dir) + } + } } diff --git a/pkg/virt/fake/paths.go b/pkg/virt/fake/paths.go index 07dc95cc4..858e995a1 100644 --- a/pkg/virt/fake/paths.go +++ b/pkg/virt/fake/paths.go @@ -29,6 +29,10 @@ var pathReplacements = [][2]string{ "/__pods__/", "/var/lib/kubelet/pods/", }, + { + "/__fs__/", + "/", + }, } func fixPath(s string) string {