Skip to content

Commit

Permalink
Add libvirttools testcase for 9pfs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Shvedunov committed Feb 11, 2019
1 parent 17d0661 commit 7d7c7bc
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 10 deletions.
16 changes: 13 additions & 3 deletions pkg/fs/fake/fakefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
129 changes: 129 additions & 0 deletions pkg/libvirttools/TestDomainDefinitions__9pfs_volume.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
- name: GetImagePathDigestAndVirtualSize
value: fake/image1
- name: 'storage: CreateStoragePool'
value: |-
<pool type="dir">
<name>volumes</name>
<target>
<path>/var/lib/virtlet/volumes</path>
</target>
</pool>
- name: 'storage: volumes: CreateStorageVol'
value: |-
<volume type="file">
<name>virtlet_root_231700d5-c9a6-5a49-738d-99a954c51550</name>
<allocation unit="b">0</allocation>
<capacity unit="b">424242</capacity>
<target>
<format type="qcow2"></format>
</target>
<backingStore>
<path>/fake/volume/path</path>
<format type="qcow2"></format>
</backingStore>
</volume>
- 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: |-
<domain type="kvm">
<name>virtlet-231700d5-c9a6-container1</name>
<uuid>231700d5-c9a6-5a49-738d-99a954c51550</uuid>
<memory unit="MiB">1024</memory>
<vcpu>1</vcpu>
<cputune>
<shares>0</shares>
<period>0</period>
<quota>0</quota>
</cputune>
<os>
<type>hvm</type>
<boot dev="hd"></boot>
</os>
<features>
<acpi></acpi>
</features>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/vmwrapper</emulator>
<disk type="file" device="disk">
<driver name="qemu" type="qcow2"></driver>
<source file="/var/lib/virtlet/volumes/virtlet_root_231700d5-c9a6-5a49-738d-99a954c51550"></source>
<target dev="sda" bus="scsi"></target>
<address type="drive" controller="0" bus="0" target="0" unit="0"></address>
</disk>
<disk type="file" device="cdrom">
<driver name="qemu" type="raw"></driver>
<source file="/var/lib/virtlet/config/config-231700d5-c9a6-5a49-738d-99a954c51550.iso"></source>
<target dev="sdc" bus="scsi"></target>
<readonly></readonly>
<address type="drive" controller="0" bus="0" target="0" unit="2"></address>
</disk>
<controller type="scsi" index="0" model="virtio-scsi">
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x0"></address>
</controller>
<controller type="pci" model="pci-root"></controller>
<filesystem type="mount" accessmode="squash">
<source dir="/mounts/virtlet_231700d5-c9a6-5a49-738d-99a954c51550_9pfs-vol_0"></source>
<target dir="foobar"></target>
</filesystem>
<serial type="unix">
<source mode="connect" path="/var/lib/libvirt/streamer.sock">
<reconnect enabled="yes" timeout="1"></reconnect>
</source>
<target port="0"></target>
</serial>
<input type="tablet" bus="usb"></input>
<graphics type="vnc" port="-1"></graphics>
<video>
<model type="cirrus"></model>
</video>
</devices>
<commandline xmlns="http://libvirt.org/schemas/domain/qemu/1.0">
<env name="VIRTLET_EMULATOR" value="/usr/bin/kvm"></env>
<env name="VIRTLET_NET_KEY" value="/tmp/fakenetns"></env>
<env name="VIRTLET_CONTAINER_ID" value="231700d5-c9a6-5a49-738d-99a954c51550"></env>
<env name="VIRTLET_CONTAINER_LOG_PATH" value="/var/log/pods/69eec606-0493-5825-73a4-c5e0c0236155/container1_42.log"></env>
</commandline>
</domain>
- 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
28 changes: 21 additions & 7 deletions pkg/libvirttools/virtualization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -326,6 +328,7 @@ func TestDoubleStartError(t *testing.T) {
type volMount struct {
name string
containerPath string
podSubpath string
}

type volDevice struct {
Expand Down Expand Up @@ -404,6 +407,7 @@ func TestDomainDefinitions(t *testing.T) {
{
name: "ceph",
containerPath: "/var/lib/whatever",
podSubpath: "volumes/virtlet~flexvolume_driver",
},
},
},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
})
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/virt/fake/fake_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
4 changes: 4 additions & 0 deletions pkg/virt/fake/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ var pathReplacements = [][2]string{
"/__pods__/",
"/var/lib/kubelet/pods/",
},
{
"/__fs__/",
"/",
},
}

func fixPath(s string) string {
Expand Down

0 comments on commit 7d7c7bc

Please sign in to comment.