github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/container/kvm/libvirt/domainxml_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package libvirt_test 5 6 import ( 7 "encoding/xml" 8 9 "github.com/juju/errors" 10 "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 . "github.com/juju/juju/container/kvm/libvirt" 15 ) 16 17 // gocheck boilerplate. 18 type domainXMLSuite struct { 19 testing.IsolationSuite 20 } 21 22 var _ = gc.Suite(&domainXMLSuite{}) 23 24 var amd64DomainStr = ` 25 <domain type="kvm"> 26 <name>juju-someid</name> 27 <vcpu>2</vcpu> 28 <currentMemory unit="MiB">1024</currentMemory> 29 <memory unit="MiB">1024</memory> 30 <os> 31 <type>hvm</type> 32 </os> 33 <devices> 34 <disk device="disk" type="file"> 35 <driver type="qcow2" name="qemu"></driver> 36 <source file="/some/path"></source> 37 <target dev="vda"></target> 38 </disk> 39 <disk device="disk" type="file"> 40 <driver type="raw" name="qemu"></driver> 41 <source file="/another/path"></source> 42 <target dev="vdb"></target> 43 </disk> 44 <interface type="bridge"> 45 <mac address="00:00:00:00:00:00"></mac> 46 <model type="virtio"></model> 47 <source bridge="parent-dev"></source> 48 <guest dev="device-name"></guest> 49 </interface> 50 <serial type="pty"> 51 <source path="/dev/pts/2"></source> 52 <target port="0"></target> 53 </serial> 54 <console type="pty" tty="/dev/pts/2"> 55 <source path="/dev/pts/2"></source> 56 <target port="0"></target> 57 </console> 58 </devices> 59 </domain>`[1:] 60 61 var arm64DomainStr = ` 62 <domain type="kvm"> 63 <name>juju-someid</name> 64 <vcpu>2</vcpu> 65 <currentMemory unit="MiB">1024</currentMemory> 66 <memory unit="MiB">1024</memory> 67 <os> 68 <type arch="aarch64" machine="virt">hvm</type> 69 <loader readonly="yes" type="pflash">/shared/readonly.fd</loader> 70 </os> 71 <features> 72 <gic version="host"></gic> 73 </features> 74 <cpu mode="custom" match="exact"> 75 <model fallback="allow">cortex-a53</model> 76 </cpu> 77 <devices> 78 <disk device="disk" type="file"> 79 <driver type="qcow2" name="qemu"></driver> 80 <source file="/some/path"></source> 81 <target dev="vda"></target> 82 </disk> 83 <disk device="disk" type="file"> 84 <driver type="raw" name="qemu"></driver> 85 <source file="/another/path"></source> 86 <target dev="vdb"></target> 87 </disk> 88 <interface type="bridge"> 89 <mac address="00:00:00:00:00:00"></mac> 90 <model type="virtio"></model> 91 <source bridge="parent-dev"></source> 92 <guest dev="device-name"></guest> 93 </interface> 94 <serial type="pty"> 95 <source path="/dev/pts/2"></source> 96 <target port="0"></target> 97 </serial> 98 <console type="pty" tty="/dev/pts/2"> 99 <source path="/dev/pts/2"></source> 100 <target port="0"></target> 101 </console> 102 </devices> 103 </domain>`[1:] 104 105 func (domainXMLSuite) TestNewDomain(c *gc.C) { 106 table := []struct { 107 arch, want string 108 }{ 109 {"amd64", amd64DomainStr}, 110 {"arm64", arm64DomainStr}, 111 } 112 for i, test := range table { 113 c.Logf("TestNewDomain: test #%d for %s", i+1, test.arch) 114 ifaces := []InterfaceInfo{ 115 dummyInterface{ 116 mac: "00:00:00:00:00:00", 117 parent: "parent-dev", 118 name: "device-name"}} 119 disks := []DiskInfo{ 120 dummyDisk{driver: "qcow2", source: "/some/path"}, 121 dummyDisk{driver: "raw", source: "/another/path"}, 122 } 123 params := dummyParams{ifaceInfo: ifaces, diskInfo: disks, memory: 1024, cpuCores: 2, hostname: "juju-someid", arch: test.arch} 124 125 if test.arch == "arm64" { 126 params.loader = "/shared/readonly.fd" 127 } 128 129 d, err := NewDomain(params) 130 c.Check(err, jc.ErrorIsNil) 131 ml, err := xml.MarshalIndent(&d, "", " ") 132 c.Check(err, jc.ErrorIsNil) 133 c.Assert(string(ml), jc.DeepEquals, test.want) 134 } 135 } 136 137 func (domainXMLSuite) TestNewDomainError(c *gc.C) { 138 d, err := NewDomain(dummyParams{err: errors.Errorf("boom")}) 139 c.Check(d, jc.DeepEquals, Domain{}) 140 c.Check(err, gc.ErrorMatches, "boom") 141 } 142 143 type dummyParams struct { 144 err error 145 arch string 146 cpuCores uint64 147 diskInfo []DiskInfo 148 hostname string 149 ifaceInfo []InterfaceInfo 150 loader string 151 memory uint64 152 nvram string 153 } 154 155 func (p dummyParams) Arch() string { return p.arch } 156 func (p dummyParams) CPUs() uint64 { return p.cpuCores } 157 func (p dummyParams) DiskInfo() []DiskInfo { return p.diskInfo } 158 func (p dummyParams) Host() string { return p.hostname } 159 func (p dummyParams) Loader() string { return p.loader } 160 func (p dummyParams) NVRAM() string { return p.nvram } 161 func (p dummyParams) NetworkInfo() []InterfaceInfo { return p.ifaceInfo } 162 func (p dummyParams) RAM() uint64 { return p.memory } 163 func (p dummyParams) ValidateDomainParams() error { return p.err } 164 165 type dummyDisk struct { 166 source string 167 driver string 168 } 169 170 func (d dummyDisk) Driver() string { return d.driver } 171 func (d dummyDisk) Source() string { return d.source } 172 173 type dummyInterface struct { 174 mac, parent, name string 175 } 176 177 func (i dummyInterface) InterfaceName() string { return i.name } 178 func (i dummyInterface) MACAddress() string { return i.mac } 179 func (i dummyInterface) ParentInterfaceName() string { return i.parent }