|
21 | 21 | import static org.junit.Assert.assertEquals; |
22 | 22 | import static org.junit.Assert.assertTrue; |
23 | 23 |
|
| 24 | +import java.io.InputStream; |
24 | 25 | import java.util.ArrayList; |
25 | 26 | import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.HashMap; |
26 | 29 |
|
| 30 | +import org.apache.commons.io.IOUtils; |
27 | 31 | import org.junit.Assert; |
28 | 32 | import org.junit.Test; |
29 | 33 | import org.junit.runner.RunWith; |
|
43 | 47 | import com.cloud.hypervisor.kvm.resource.LibvirtConnection; |
44 | 48 | import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef; |
45 | 49 | import com.cloud.utils.exception.CloudRuntimeException; |
| 50 | +import org.w3c.dom.Document; |
| 51 | + |
| 52 | +import javax.xml.parsers.DocumentBuilder; |
| 53 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 54 | +import javax.xml.xpath.XPathExpressionException; |
| 55 | +import javax.xml.xpath.XPathFactory; |
46 | 56 |
|
47 | 57 | @RunWith(PowerMockRunner.class) |
48 | 58 | @PrepareForTest({LibvirtConnection.class, LibvirtMigrateCommandWrapper.class}) |
@@ -96,6 +106,11 @@ public class LibvirtMigrateCommandWrapperTest { |
96 | 106 | " <backingStore/>\n" + |
97 | 107 | " </backingStore>\n" + |
98 | 108 | " <target dev='vda' bus='virtio'/>\n" + |
| 109 | +" <iotune>\n" + |
| 110 | +" <write_iops_sec>500</write_iops_sec>\n" + |
| 111 | +" <write_iops_sec_max>5000</write_iops_sec_max>\n" + |
| 112 | +" <write_iops_sec_max_length>60</write_iops_sec_max_length>\n" + |
| 113 | +" </iotune>\n" + |
99 | 114 | " <serial>4650a2f7fce548e2beaa</serial>\n" + |
100 | 115 | " <alias name='virtio-disk0'/>\n" + |
101 | 116 | " <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>\n" + |
@@ -208,6 +223,11 @@ public class LibvirtMigrateCommandWrapperTest { |
208 | 223 | " <backingStore/>\n" + |
209 | 224 | " </backingStore>\n" + |
210 | 225 | " <target dev='vda' bus='virtio'/>\n" + |
| 226 | +" <iotune>\n" + |
| 227 | +" <write_iops_sec>500</write_iops_sec>\n" + |
| 228 | +" <write_iops_sec_max>5000</write_iops_sec_max>\n" + |
| 229 | +" <write_iops_sec_max_length>60</write_iops_sec_max_length>\n" + |
| 230 | +" </iotune>\n" + |
211 | 231 | " <serial>4650a2f7fce548e2beaa</serial>\n" + |
212 | 232 | " <alias name='virtio-disk0'/>\n" + |
213 | 233 | " <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>\n" + |
@@ -435,4 +455,32 @@ private void inOrderVerifyDeleteOrDisconnect(InOrder inOrder, LibvirtMigrateComm |
435 | 455 | inOrder.verify(virtResource, Mockito.times(timesCleanup)).cleanupDisk(disk); |
436 | 456 | } |
437 | 457 |
|
| 458 | + static void assertXpath(final Document doc, final String xPathExpr, |
| 459 | + final String expected) { |
| 460 | + try { |
| 461 | + Assert.assertEquals(expected, XPathFactory.newInstance().newXPath() |
| 462 | + .evaluate(xPathExpr, doc)); |
| 463 | + } catch (final XPathExpressionException e) { |
| 464 | + Assert.fail("Could not evaluate xpath" + xPathExpr + ":" |
| 465 | + + e.getMessage()); |
| 466 | + } |
| 467 | + } |
| 468 | + |
| 469 | + @Test |
| 470 | + public void testReplaceStorage() throws Exception { |
| 471 | + Map<String, MigrateDiskInfo> mapMigrateStorage = new HashMap<String, MigrateDiskInfo>(); |
| 472 | + |
| 473 | + MigrateDiskInfo diskInfo = new MigrateDiskInfo("123456", DiskType.BLOCK, DriverType.RAW, Source.FILE, "sourctest"); |
| 474 | + mapMigrateStorage.put("/mnt/812ea6a3-7ad0-30f4-9cab-01e3f2985b98/4650a2f7-fce5-48e2-beaa-bcdf063194e6", diskInfo); |
| 475 | + final String result = libvirtMigrateCmdWrapper.replaceStorage(fullfile, mapMigrateStorage); |
| 476 | + |
| 477 | + InputStream in = IOUtils.toInputStream(result); |
| 478 | + DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
| 479 | + DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); |
| 480 | + Document doc = docBuilder.parse(in); |
| 481 | + assertXpath(doc, "/domain/devices/disk/iotune/write_iops_sec", "500"); |
| 482 | + assertXpath(doc, "/domain/devices/disk/@type", "block"); |
| 483 | + assertXpath(doc, "/domain/devices/disk/driver/@type", "raw"); |
| 484 | + } |
| 485 | + |
438 | 486 | } |
0 commit comments