github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/libvirttools/block_volumesource_test.go (about) 1 /* 2 Copyright 2018 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package libvirttools 18 19 import ( 20 "encoding/xml" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "reflect" 25 "testing" 26 27 libvirtxml "github.com/libvirt/libvirt-go-xml" 28 29 "github.com/Mirantis/virtlet/pkg/metadata/types" 30 ) 31 32 func TestBlockVolumeSource(t *testing.T) { 33 tmpDir, err := ioutil.TempDir("", "block-vol-test-") 34 if err != nil { 35 t.Fatalf("TempDir() returned: %v", err) 36 } 37 defer os.RemoveAll(tmpDir) 38 39 // On Mac, /tmp is a symlink and it may make this test fail 40 // unless we eval it 41 baseDir, err := filepath.EvalSymlinks(tmpDir) 42 if err != nil { 43 t.Fatal(err) 44 } 45 46 devs := []string{ 47 filepath.Join(baseDir, "test1"), 48 filepath.Join(baseDir, "test2"), 49 } 50 for _, dev := range devs { 51 if f, err := os.OpenFile(dev, os.O_RDONLY|os.O_CREATE, 0666); err != nil { 52 t.Fatal(err) 53 } else { 54 f.Close() 55 } 56 } 57 symlinkName := filepath.Join(baseDir, "test2link") 58 if err := os.Symlink(devs[1], symlinkName); err != nil { 59 t.Fatal(err) 60 } 61 62 volumes, err := GetBlockVolumes(&types.VMConfig{ 63 VolumeDevices: []types.VMVolumeDevice{ 64 { 65 DevicePath: "/dev/test1", 66 HostPath: devs[0], 67 }, 68 { 69 DevicePath: "/dev/test1", 70 HostPath: symlinkName, 71 }, 72 }, 73 }, nil) 74 if err != nil { 75 t.Fatalf("GetRootVolume returned an error: %v", err) 76 } 77 78 expectedDisks := []*libvirtxml.DomainDisk{ 79 { 80 Device: "disk", 81 Source: &libvirtxml.DomainDiskSource{Block: &libvirtxml.DomainDiskSourceBlock{Dev: devs[0]}}, 82 Driver: &libvirtxml.DomainDiskDriver{Name: "qemu", Type: "raw"}, 83 }, 84 { 85 Device: "disk", 86 Source: &libvirtxml.DomainDiskSource{Block: &libvirtxml.DomainDiskSourceBlock{Dev: devs[1]}}, 87 Driver: &libvirtxml.DomainDiskDriver{Name: "qemu", Type: "raw"}, 88 }, 89 } 90 var disks []*libvirtxml.DomainDisk 91 for _, vol := range volumes { 92 if disk, _, err := vol.Setup(); err != nil { 93 t.Fatal(err) 94 } else { 95 disks = append(disks, disk) 96 } 97 } 98 if !reflect.DeepEqual(expectedDisks, disks) { 99 expected, err := xml.MarshalIndent(expectedDisks, "", " ") 100 if err != nil { 101 t.Fatal(err) 102 } 103 actual, err := xml.MarshalIndent(disks, "", " ") 104 if err != nil { 105 t.Fatal(err) 106 } 107 t.Errorf("Bad disk defs generated. Expected:\n%s\nGot:\n%s", expected, actual) 108 } 109 // TODO: symlink 110 }