Browse Source

Merge "Extract fake KVM guest fixture for reuse"

changes/10/680810/1
Zuul 2 weeks ago
parent
commit
b96f932fdf

+ 150
- 0
nova/tests/unit/virt/libvirt/fake_libvirt_data.py View File

@@ -10,7 +10,157 @@
10 10
 #    License for the specific language governing permissions and limitations
11 11
 #    under the License.
12 12
 
13
+from oslo_utils import units
14
+
13 15
 from nova.objects.fields import Architecture
16
+from nova.virt.libvirt import config
17
+
18
+
19
+def fake_kvm_guest():
20
+    obj = config.LibvirtConfigGuest()
21
+    obj.virt_type = "kvm"
22
+    obj.memory = 100 * units.Mi
23
+    obj.vcpus = 2
24
+    obj.cpuset = set([0, 1, 3, 4, 5])
25
+
26
+    obj.cputune = config.LibvirtConfigGuestCPUTune()
27
+    obj.cputune.shares = 100
28
+    obj.cputune.quota = 50000
29
+    obj.cputune.period = 25000
30
+
31
+    obj.membacking = config.LibvirtConfigGuestMemoryBacking()
32
+    page1 = config.LibvirtConfigGuestMemoryBackingPage()
33
+    page1.size_kb = 2048
34
+    page1.nodeset = [0, 1, 2, 3, 5]
35
+    page2 = config.LibvirtConfigGuestMemoryBackingPage()
36
+    page2.size_kb = 1048576
37
+    page2.nodeset = [4]
38
+    obj.membacking.hugepages.append(page1)
39
+    obj.membacking.hugepages.append(page2)
40
+
41
+    obj.memtune = config.LibvirtConfigGuestMemoryTune()
42
+    obj.memtune.hard_limit = 496
43
+    obj.memtune.soft_limit = 672
44
+    obj.memtune.swap_hard_limit = 1638
45
+    obj.memtune.min_guarantee = 2970
46
+
47
+    obj.numatune = config.LibvirtConfigGuestNUMATune()
48
+
49
+    numamemory = config.LibvirtConfigGuestNUMATuneMemory()
50
+    numamemory.mode = "preferred"
51
+    numamemory.nodeset = [0, 1, 2, 3, 8]
52
+
53
+    obj.numatune.memory = numamemory
54
+
55
+    numamemnode0 = config.LibvirtConfigGuestNUMATuneMemNode()
56
+    numamemnode0.cellid = 0
57
+    numamemnode0.mode = "preferred"
58
+    numamemnode0.nodeset = [0, 1]
59
+
60
+    numamemnode1 = config.LibvirtConfigGuestNUMATuneMemNode()
61
+    numamemnode1.cellid = 1
62
+    numamemnode1.mode = "preferred"
63
+    numamemnode1.nodeset = [2, 3]
64
+
65
+    numamemnode2 = config.LibvirtConfigGuestNUMATuneMemNode()
66
+    numamemnode2.cellid = 2
67
+    numamemnode2.mode = "preferred"
68
+    numamemnode2.nodeset = [8]
69
+
70
+    obj.numatune.memnodes.extend([numamemnode0,
71
+                                  numamemnode1,
72
+                                  numamemnode2])
73
+
74
+    obj.name = "demo"
75
+    obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
76
+    obj.os_type = "linux"
77
+    obj.os_boot_dev = ["hd", "cdrom", "fd"]
78
+    obj.os_smbios = config.LibvirtConfigGuestSMBIOS()
79
+    obj.features = [
80
+        config.LibvirtConfigGuestFeatureACPI(),
81
+        config.LibvirtConfigGuestFeatureAPIC(),
82
+        config.LibvirtConfigGuestFeaturePAE(),
83
+        config.LibvirtConfigGuestFeatureKvmHidden()
84
+    ]
85
+
86
+    obj.sysinfo = config.LibvirtConfigGuestSysinfo()
87
+    obj.sysinfo.bios_vendor = "Acme"
88
+    obj.sysinfo.system_version = "1.0.0"
89
+
90
+    disk = config.LibvirtConfigGuestDisk()
91
+    disk.source_type = "file"
92
+    disk.source_path = "/tmp/img"
93
+    disk.target_dev = "/dev/vda"
94
+    disk.target_bus = "virtio"
95
+    obj.add_device(disk)
96
+
97
+    return obj
98
+
99
+
100
+FAKE_KVM_GUEST = """
101
+  <domain type="kvm">
102
+    <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
103
+    <name>demo</name>
104
+    <memory>104857600</memory>
105
+    <memoryBacking>
106
+      <hugepages>
107
+        <page size="2048" unit="KiB" nodeset="0-3,5"/>
108
+        <page size="1048576" unit="KiB" nodeset="4"/>
109
+      </hugepages>
110
+    </memoryBacking>
111
+    <memtune>
112
+      <hard_limit unit="KiB">496</hard_limit>
113
+      <soft_limit unit="KiB">672</soft_limit>
114
+      <swap_hard_limit unit="KiB">1638</swap_hard_limit>
115
+      <min_guarantee unit="KiB">2970</min_guarantee>
116
+    </memtune>
117
+    <numatune>
118
+      <memory mode="preferred" nodeset="0-3,8"/>
119
+      <memnode cellid="0" mode="preferred" nodeset="0-1"/>
120
+      <memnode cellid="1" mode="preferred" nodeset="2-3"/>
121
+      <memnode cellid="2" mode="preferred" nodeset="8"/>
122
+    </numatune>
123
+    <vcpu cpuset="0-1,3-5">2</vcpu>
124
+    <sysinfo type='smbios'>
125
+       <bios>
126
+         <entry name="vendor">Acme</entry>
127
+       </bios>
128
+       <system>
129
+         <entry name="version">1.0.0</entry>
130
+       </system>
131
+    </sysinfo>
132
+    <os>
133
+      <type>linux</type>
134
+      <boot dev="hd"/>
135
+      <boot dev="cdrom"/>
136
+      <boot dev="fd"/>
137
+      <smbios mode="sysinfo"/>
138
+    </os>
139
+    <features>
140
+      <acpi/>
141
+      <apic/>
142
+      <pae/>
143
+      <kvm>
144
+        <hidden state='on'/>
145
+      </kvm>
146
+    </features>
147
+    <cputune>
148
+      <shares>100</shares>
149
+      <quota>50000</quota>
150
+      <period>25000</period>
151
+    </cputune>
152
+    <devices>
153
+      <disk type="file" device="disk">
154
+        <source file="/tmp/img"/>
155
+        <target bus="virtio" dev="/dev/vda"/>
156
+      </disk>
157
+    </devices>
158
+    <launchSecurity type="sev">
159
+      <policy>0x0033</policy>
160
+      <cbitpos>47</cbitpos>
161
+      <reducedPhysBits>1</reducedPhysBits>
162
+    </launchSecurity>
163
+  </domain>"""
14 164
 
15 165
 
16 166
 CAPABILITIES_HOST_TEMPLATE = '''

+ 3
- 141
nova/tests/unit/virt/libvirt/test_config.py View File

@@ -18,6 +18,7 @@ from oslo_utils import units
18 18
 
19 19
 from nova.objects import fields as obj_fields
20 20
 from nova import test
21
+from nova.tests.unit.virt.libvirt import fake_libvirt_data
21 22
 from nova.virt.libvirt import config
22 23
 
23 24
 
@@ -2509,83 +2510,7 @@ class LibvirtConfigGuestTest(LibvirtConfigBaseTest):
2509 2510
             </domain>""")
2510 2511
 
2511 2512
     def test_config_kvm(self):
2512
-        obj = config.LibvirtConfigGuest()
2513
-        obj.virt_type = "kvm"
2514
-        obj.memory = 100 * units.Mi
2515
-        obj.vcpus = 2
2516
-        obj.cpuset = set([0, 1, 3, 4, 5])
2517
-
2518
-        obj.cputune = config.LibvirtConfigGuestCPUTune()
2519
-        obj.cputune.shares = 100
2520
-        obj.cputune.quota = 50000
2521
-        obj.cputune.period = 25000
2522
-
2523
-        obj.membacking = config.LibvirtConfigGuestMemoryBacking()
2524
-        page1 = config.LibvirtConfigGuestMemoryBackingPage()
2525
-        page1.size_kb = 2048
2526
-        page1.nodeset = [0, 1, 2, 3, 5]
2527
-        page2 = config.LibvirtConfigGuestMemoryBackingPage()
2528
-        page2.size_kb = 1048576
2529
-        page2.nodeset = [4]
2530
-        obj.membacking.hugepages.append(page1)
2531
-        obj.membacking.hugepages.append(page2)
2532
-
2533
-        obj.memtune = config.LibvirtConfigGuestMemoryTune()
2534
-        obj.memtune.hard_limit = 496
2535
-        obj.memtune.soft_limit = 672
2536
-        obj.memtune.swap_hard_limit = 1638
2537
-        obj.memtune.min_guarantee = 2970
2538
-
2539
-        obj.numatune = config.LibvirtConfigGuestNUMATune()
2540
-
2541
-        numamemory = config.LibvirtConfigGuestNUMATuneMemory()
2542
-        numamemory.mode = "preferred"
2543
-        numamemory.nodeset = [0, 1, 2, 3, 8]
2544
-
2545
-        obj.numatune.memory = numamemory
2546
-
2547
-        numamemnode0 = config.LibvirtConfigGuestNUMATuneMemNode()
2548
-        numamemnode0.cellid = 0
2549
-        numamemnode0.mode = "preferred"
2550
-        numamemnode0.nodeset = [0, 1]
2551
-
2552
-        numamemnode1 = config.LibvirtConfigGuestNUMATuneMemNode()
2553
-        numamemnode1.cellid = 1
2554
-        numamemnode1.mode = "preferred"
2555
-        numamemnode1.nodeset = [2, 3]
2556
-
2557
-        numamemnode2 = config.LibvirtConfigGuestNUMATuneMemNode()
2558
-        numamemnode2.cellid = 2
2559
-        numamemnode2.mode = "preferred"
2560
-        numamemnode2.nodeset = [8]
2561
-
2562
-        obj.numatune.memnodes.extend([numamemnode0,
2563
-                                      numamemnode1,
2564
-                                      numamemnode2])
2565
-
2566
-        obj.name = "demo"
2567
-        obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
2568
-        obj.os_type = "linux"
2569
-        obj.os_boot_dev = ["hd", "cdrom", "fd"]
2570
-        obj.os_smbios = config.LibvirtConfigGuestSMBIOS()
2571
-        obj.features = [
2572
-            config.LibvirtConfigGuestFeatureACPI(),
2573
-            config.LibvirtConfigGuestFeatureAPIC(),
2574
-            config.LibvirtConfigGuestFeaturePAE(),
2575
-            config.LibvirtConfigGuestFeatureKvmHidden()
2576
-        ]
2577
-
2578
-        obj.sysinfo = config.LibvirtConfigGuestSysinfo()
2579
-        obj.sysinfo.bios_vendor = "Acme"
2580
-        obj.sysinfo.system_version = "1.0.0"
2581
-
2582
-        disk = config.LibvirtConfigGuestDisk()
2583
-        disk.source_type = "file"
2584
-        disk.source_path = "/tmp/img"
2585
-        disk.target_dev = "/dev/vda"
2586
-        disk.target_bus = "virtio"
2587
-
2588
-        obj.add_device(disk)
2513
+        obj = fake_libvirt_data.fake_kvm_guest()
2589 2514
 
2590 2515
         launch_security = config.LibvirtConfigGuestSEVLaunchSecurity()
2591 2516
         launch_security.cbitpos = 47
@@ -2593,70 +2518,7 @@ class LibvirtConfigGuestTest(LibvirtConfigBaseTest):
2593 2518
         obj.launch_security = launch_security
2594 2519
 
2595 2520
         xml = obj.to_xml()
2596
-        self.assertXmlEqual(xml, """
2597
-            <domain type="kvm">
2598
-              <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
2599
-              <name>demo</name>
2600
-              <memory>104857600</memory>
2601
-              <memoryBacking>
2602
-                <hugepages>
2603
-                  <page size="2048" unit="KiB" nodeset="0-3,5"/>
2604
-                  <page size="1048576" unit="KiB" nodeset="4"/>
2605
-                </hugepages>
2606
-              </memoryBacking>
2607
-              <memtune>
2608
-                <hard_limit unit="KiB">496</hard_limit>
2609
-                <soft_limit unit="KiB">672</soft_limit>
2610
-                <swap_hard_limit unit="KiB">1638</swap_hard_limit>
2611
-                <min_guarantee unit="KiB">2970</min_guarantee>
2612
-              </memtune>
2613
-              <numatune>
2614
-                <memory mode="preferred" nodeset="0-3,8"/>
2615
-                <memnode cellid="0" mode="preferred" nodeset="0-1"/>
2616
-                <memnode cellid="1" mode="preferred" nodeset="2-3"/>
2617
-                <memnode cellid="2" mode="preferred" nodeset="8"/>
2618
-              </numatune>
2619
-              <vcpu cpuset="0-1,3-5">2</vcpu>
2620
-              <sysinfo type='smbios'>
2621
-                 <bios>
2622
-                   <entry name="vendor">Acme</entry>
2623
-                 </bios>
2624
-                 <system>
2625
-                   <entry name="version">1.0.0</entry>
2626
-                 </system>
2627
-              </sysinfo>
2628
-              <os>
2629
-                <type>linux</type>
2630
-                <boot dev="hd"/>
2631
-                <boot dev="cdrom"/>
2632
-                <boot dev="fd"/>
2633
-                <smbios mode="sysinfo"/>
2634
-              </os>
2635
-              <features>
2636
-                <acpi/>
2637
-                <apic/>
2638
-                <pae/>
2639
-                <kvm>
2640
-                  <hidden state='on'/>
2641
-                </kvm>
2642
-              </features>
2643
-              <cputune>
2644
-                <shares>100</shares>
2645
-                <quota>50000</quota>
2646
-                <period>25000</period>
2647
-              </cputune>
2648
-              <devices>
2649
-                <disk type="file" device="disk">
2650
-                  <source file="/tmp/img"/>
2651
-                  <target bus="virtio" dev="/dev/vda"/>
2652
-                </disk>
2653
-              </devices>
2654
-              <launchSecurity type="sev">
2655
-                <policy>0x0033</policy>
2656
-                <cbitpos>47</cbitpos>
2657
-                <reducedPhysBits>1</reducedPhysBits>
2658
-              </launchSecurity>
2659
-            </domain>""")
2521
+        self.assertXmlEqual(fake_libvirt_data.FAKE_KVM_GUEST, xml)
2660 2522
 
2661 2523
     def test_config_uefi(self):
2662 2524
         obj = config.LibvirtConfigGuest()

Loading…
Cancel
Save