pci_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package guestman
  15. import (
  16. "fmt"
  17. "testing"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/hostman/guestman/arch"
  22. "yunion.io/x/onecloud/pkg/hostman/guestman/desc"
  23. )
  24. type sKVMGuestInstance struct {
  25. SKVMGuestInstance
  26. }
  27. func TestSKVMGuestInstance_initGuestDesc(t *testing.T) {
  28. descStr := `{
  29. "admin_security_rules": "in:allow any",
  30. "bios": "BIOS",
  31. "boot_order": "cdn",
  32. "cpu": 8,
  33. "disks": [
  34. {
  35. "aio_mode": "threads",
  36. "bps": 0,
  37. "cache_mode": "writeback",
  38. "disk_id": "44566ebf-b9c5-4e8a-8b95-d1a347e83cea",
  39. "driver": "scsi",
  40. "format": "qcow2",
  41. "image_path": "/opt/cloud/workspace/disks/image_cache/9e050dd4-4e6c-4b71-825f-10613a59326d",
  42. "index": 0,
  43. "iops": 0,
  44. "is_ssd": false,
  45. "merge_snapshot": false,
  46. "migrating": true,
  47. "num_queues": 0,
  48. "path": "/opt/cloud/workspace/disks/44566ebf-b9c5-4e8a-8b95-d1a347e83cea",
  49. "size": 30720,
  50. "storage_id": "003f26ee-ba71-4d62-8bd3-057d6249df64",
  51. "storage_type": "local",
  52. "target_storage_id": "1b5895d9-3517-4e45-84f0-59db7660d840",
  53. "template_id": "9e050dd4-4e6c-4b71-825f-10613a59326d"
  54. }
  55. ],
  56. "domain": "test.yunion.io",
  57. "domain_id": "default",
  58. "host_id": "6fc10297-eb20-4a96-86a8-4b65260d6016",
  59. "machine": "q35",
  60. "mem": 8192,
  61. "metadata": {
  62. "__cpu_mode": "host",
  63. "__os_profile__": "{\"disk_driver\":\"scsi\",\"fs_format\":\"ext4\",\"hypervisor\":\"kvm\",\"net_driver\":\"virtio\",\"os_type\":\"Linux\"}",
  64. "__qemu_version": "2.12.1",
  65. "__ssh_port": "0",
  66. "__vnc_password": "Q8TyN4Td",
  67. "__vnc_port": "3",
  68. "generate_name": "test-ubuntu18",
  69. "hot_remove_nic": "enable",
  70. "hotplug_cpu_mem": "enable",
  71. "os_arch": "x86_32",
  72. "os_distribution": "Ubuntu",
  73. "os_name": "Linux",
  74. "os_version": "18.04"
  75. },
  76. "name": "test-ubuntu18",
  77. "nics": [
  78. {
  79. "bridge": "br1",
  80. "bw": 1000,
  81. "dns": "114.114.114.114",
  82. "domain": "test.yunion.io",
  83. "driver": "virtio",
  84. "gateway": "10.127.190.1",
  85. "ifname": "GUESTNET1-194",
  86. "index": 0,
  87. "interface": "em2",
  88. "ip": "10.127.190.194",
  89. "link_up": false,
  90. "mac": "00:22:a8:be:a3:13",
  91. "masklen": 24,
  92. "mtu": 1500,
  93. "net": "GUEST-NET190",
  94. "net_id": "21fbe474-77aa-472d-82e7-6f0098e15b57",
  95. "num_queues": 2,
  96. "rate": 0,
  97. "virtual": false,
  98. "vlan": 1,
  99. "wire_id": "b0aaa839-2f33-464a-8454-1736d0707fe3"
  100. }
  101. ],
  102. "os_name": "Linux",
  103. "pending_deleted": false,
  104. "project_domain": "Default",
  105. "secgroups": [
  106. {
  107. "id": "default",
  108. "name": "Default"
  109. }
  110. ],
  111. "security_rules": "in:allow any",
  112. "src_ip_check": true,
  113. "src_mac_check": true,
  114. "tenant": "system",
  115. "tenant_id": "55bb511b62bf47dc86e82c731005ba10",
  116. "uuid": "79a14b71-e752-4fb1-868e-1362ff9ed5e5",
  117. "vdi": "vnc",
  118. "vga": "qxl",
  119. "zone": "Wangjing",
  120. "zone_id": "eb40b924-7b44-4490-8197-9d695c0bae4b"
  121. }
  122. `
  123. s := &sKVMGuestInstance{
  124. SKVMGuestInstance: SKVMGuestInstance{
  125. archMan: arch.NewArch(arch.Arch_x86_64),
  126. sBaseGuestInstance: newBaseGuestInstance("", nil, api.HYPERVISOR_KVM),
  127. },
  128. //manager:
  129. }
  130. Desc := new(desc.SGuestDesc)
  131. descJson, err := jsonutils.ParseString(descStr)
  132. if err != nil {
  133. t.Errorf("parse desc %s", err)
  134. }
  135. err = descJson.Unmarshal(Desc)
  136. if err != nil {
  137. t.Errorf("unmarshal desc %s", err)
  138. }
  139. s.Desc = Desc
  140. s.setPcieExtendBus()
  141. s.Desc.CpuDesc = &desc.SGuestCpu{
  142. Accel: "kvm",
  143. }
  144. //s.initCpuDesc()
  145. // s.initMemDesc()
  146. s.Desc.MemDesc = new(desc.SGuestMem)
  147. s.Desc.MemDesc.SizeMB = s.Desc.Mem
  148. memDesc := desc.NewMemDesc("memory-backend-memfd", "mem", nil, nil)
  149. s.Desc.MemDesc.Mem = desc.NewMemsDesc(*memDesc, nil)
  150. s.Desc.MemDesc.Mem.Options = map[string]string{
  151. "size": fmt.Sprintf("%dM", s.Desc.Mem),
  152. "share": "on", "prealloc": "on",
  153. }
  154. // s.initMachineDesc()
  155. pciRoot, _ := s.initGuestPciControllers(true)
  156. err = s.initGuestPciAddresses()
  157. if err != nil {
  158. t.Error(err)
  159. }
  160. if err := s.initMachineDefaultAddresses(); err != nil {
  161. t.Error(err)
  162. }
  163. // vdi device for spice
  164. s.Desc.VdiDevice = new(desc.SGuestVdi)
  165. if s.IsVdiSpice() {
  166. s.initSpiceDevices(pciRoot)
  167. }
  168. s.initVirtioSerial(pciRoot)
  169. //s.initGuestVga(pciRoot)
  170. s.initCdromDesc()
  171. log.Infof("guest desc %s", jsonutils.Marshal(s.Desc).PrettyString())
  172. //s.initGuestDisks(pciRoot, pciBridge)
  173. //if err = s.initGuestNetworks(pciRoot, pciBridge); err != nil {
  174. // t.Error(err)
  175. //}
  176. //s.initIsolatedDevices(pciRoot, pciBridge)
  177. s.initUsbController(pciRoot)
  178. s.initRandomDevice(pciRoot, true)
  179. //s.initQgaDesc()
  180. s.initPvpanicDesc()
  181. s.initIsaSerialDesc()
  182. if err = s.ensurePciAddresses(); err != nil {
  183. t.Error(err)
  184. }
  185. log.Infof("guest desc %s", jsonutils.Marshal(s.Desc).PrettyString())
  186. }