/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.cloud.hypervisor.kvm.resource;

import junit.framework.TestCase;

public class LibvirtVMDefTest extends TestCase {

    public void testInterfaceEtehrnet() {
        LibvirtVMDef.InterfaceDef ifDef = new LibvirtVMDef.InterfaceDef();
        ifDef.defEthernet("targetDeviceName", "00:11:22:aa:bb:dd", LibvirtVMDef.InterfaceDef.nicModel.VIRTIO);

        String expected =
            "<interface type='ethernet'>\n" + "<target dev='targetDeviceName'/>\n" + "<mac address='00:11:22:aa:bb:dd'/>\n" + "<model type='virtio'/>\n"
                + "</interface>\n";

        assertEquals(expected, ifDef.toString());
    }

    public void testInterfaceDirectNet() {
        LibvirtVMDef.InterfaceDef ifDef = new LibvirtVMDef.InterfaceDef();
        ifDef.defDirectNet("targetDeviceName", null, "00:11:22:aa:bb:dd", LibvirtVMDef.InterfaceDef.nicModel.VIRTIO, "private");

        String expected =
            "<interface type='" + LibvirtVMDef.InterfaceDef.guestNetType.DIRECT + "'>\n" + "<source dev='targetDeviceName' mode='private'/>\n" +
                "<mac address='00:11:22:aa:bb:dd'/>\n" + "<model type='virtio'/>\n" + "</interface>\n";

        assertEquals(expected, ifDef.toString());
    }

    public void testCpuModeDef() {
        LibvirtVMDef.CpuModeDef cpuModeDef = new LibvirtVMDef.CpuModeDef();
        cpuModeDef.setMode("custom");
        cpuModeDef.setModel("Nehalem");

        String expected1 = "<cpu mode='custom' match='exact'><model fallback='allow'>Nehalem</model></cpu>";

        assertEquals(expected1, cpuModeDef.toString());

        cpuModeDef.setMode("host-model");
        String expected2 = "<cpu mode='host-model'><model fallback='allow'></model></cpu>";

        assertEquals(expected2, cpuModeDef.toString());

        cpuModeDef.setMode("host-passthrough");
        String expected3 = "<cpu mode='host-passthrough'></cpu>";
        assertEquals(expected3, cpuModeDef.toString());

    }

}

@@ -878,16 +878,79 @@
 estCase;
+%0Aimport com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef;
 %0A%0Apublic
@@ -2803,10 +2803,1089 @@
 %0A    %7D%0A%0A
+    public void testDiskDef() %7B%0A        String filePath = %22/var/lib/libvirt/images/disk.qcow2%22;%0A        String diskLabel = %22vda%22;%0A%0A        DiskDef disk = new DiskDef();%0A        DiskDef.diskBus bus = DiskDef.diskBus.VIRTIO;%0A        DiskDef.diskFmtType type = DiskDef.diskFmtType.QCOW2;%0A        DiskDef.diskCacheMode cacheMode = DiskDef.diskCacheMode.WRITEBACK;%0A%0A        disk.defFileBasedDisk(filePath, diskLabel, bus, type);%0A        disk.setCacheMode(cacheMode);%0A%0A        assertEquals(filePath, disk.getDiskPath());%0A        assertEquals(diskLabel, disk.getDiskLabel());%0A        assertEquals(bus, disk.getBusType());%0A        assertEquals(DiskDef.deviceType.DISK, disk.getDeviceType());%0A%0A        String xmlDef = disk.toString();%0A        String expectedXml = %22%3Cdisk  device='disk' type='file'%3E%5Cn%3Cdriver name='qemu' type='%22 + type.toString() + %22' cache='%22 + cacheMode.toString() + %22' /%3E%5Cn%22 +%0A                             %22%3Csource file='%22 + filePath + %22'/%3E%5Cn%3Ctarget dev='%22 + diskLabel + %22' bus='%22 + bus.toString() + %22'/%3E%5Cn%3C/disk%3E%5Cn%22;%0A%0A        assertEquals(xmlDef, expectedXml);%0A    %7D%0A%0A
 %7D%0A

Add Unit Test for LibvirtVMDef DiskDef