tstOVF.cpp revision 438870735e3606c923d51fce19d9fc3b6ebffde1
637712559132d2b3617aea0103fb0385978da14evboxsync * tstOVF - testcases for OVF import and export
637712559132d2b3617aea0103fb0385978da14evboxsync * Copyright (C) 2010-2014 Oracle Corporation
637712559132d2b3617aea0103fb0385978da14evboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
637712559132d2b3617aea0103fb0385978da14evboxsync * available from http://www.virtualbox.org. This file is free software;
637712559132d2b3617aea0103fb0385978da14evboxsync * you can redistribute it and/or modify it under the terms of the GNU
637712559132d2b3617aea0103fb0385978da14evboxsync * General Public License (GPL) as published by the Free Software
637712559132d2b3617aea0103fb0385978da14evboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
637712559132d2b3617aea0103fb0385978da14evboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
637712559132d2b3617aea0103fb0385978da14evboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
637712559132d2b3617aea0103fb0385978da14evboxsyncusing namespace com;
5a80f559035e4c5600e6486ec9f3a7ebb28efc48vboxsync///////////////////////////////////////////////////////////////////////////////
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync * Quick hack exception structure.
1729795f504cc1975ec195bca8b493c2faa0f5ddvboxsync const char *pcsz,
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (!info.isFullAvailable() && !info.isBasicAvailable())
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync * Imports the given OVF file, with all bells and whistles.
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync * Throws MyError on errors.
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync * @param pcszPrefix Descriptive short prefix string for console output.
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync * @param pVirtualBox VirtualBox instance.
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync * @param pcszOVF0 File to import.
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync * @param llMachinesCreated out: UUIDs of machines that were created so that caller can clean up.
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync const char *pcszOVF0,
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync RTPrintf("%s: reading appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync HRESULT rc = pVirtualBox->CreateAppliance(pAppl.asOutParam());
637712559132d2b3617aea0103fb0385978da14evboxsync if (FAILED(rc)) throw MyError(rc, "failed to create appliance\n");
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync rc = pAppl->Read(Bstr(szAbsOVF).raw(), pProgress.asOutParam());
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync if (FAILED(rc)) throw MyError(rc, "Appliance::Read() failed\n");
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync if (FAILED(rc2)) throw MyError(rc2, "Appliance::Read() failed\n", pProgress);
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync RTPrintf("%s: interpreting appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync if (FAILED(rc)) throw MyError(rc, "Appliance::Interpret() failed\n");
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync com::SafeIfaceArray<IVirtualSystemDescription> aDescriptions;
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync rc = pAppl->COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aDescriptions));
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync ComPtr<IVirtualSystemDescription> pVSys = aDescriptions[u];
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync com::SafeArray<VirtualSystemDescriptionType_T> aTypes;
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync rc = pVSys->GetDescription(ComSafeArrayAsOutParam(aTypes),
b2e185d7c946ed90402d1dd86b24c6d82d8e591bvboxsync if (FAILED(rc)) throw MyError(rc, "VirtualSystemDescription::GetDescription() failed\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync const char *pcszType;
d142cd5e90598dd8d14744cb6bb7dd467cec446cvboxsync case VirtualSystemDescriptionType_HardDiskControllerIDE:
d142cd5e90598dd8d14744cb6bb7dd467cec446cvboxsync case VirtualSystemDescriptionType_HardDiskControllerSATA:
d142cd5e90598dd8d14744cb6bb7dd467cec446cvboxsync case VirtualSystemDescriptionType_HardDiskControllerSAS:
d142cd5e90598dd8d14744cb6bb7dd467cec446cvboxsync case VirtualSystemDescriptionType_HardDiskControllerSCSI:
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync throw MyError(E_UNEXPECTED, "Invalid VirtualSystemDescriptionType\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync RTPrintf(" vsys %2u item %2u: type %2d (%s), ovf: \"%ls\", vbox: \"%ls\", extra: \"%ls\"\n",
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync RTPrintf("%s: importing %d machine(s)...\n", pcszPrefix, aDescriptions.size());
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync rc = pAppl->ImportMachines(ComSafeArrayAsInParam(sfaOptions), pProgress.asOutParam());
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (FAILED(rc)) throw MyError(rc, "Appliance::ImportMachines() failed\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (FAILED(rc2)) throw MyError(rc2, "Appliance::ImportMachines() failed\n", pProgress);
33698994f509c2b3a92e81a828ee7a0e01b81de0vboxsync rc = pAppl->COMGETTER(Machines)(ComSafeArrayAsOutParam(aMachineUUIDs));
33698994f509c2b3a92e81a828ee7a0e01b81de0vboxsync if (FAILED(rc)) throw MyError(rc, "Appliance::GetMachines() failed\n");
eeee785f36554f67d8737506fb0fadbf683cf9dfvboxsync RTPrintf("%s: created machine %u: %ls\n", pcszPrefix, u, aMachineUUIDs[u]);
eeee785f36554f67d8737506fb0fadbf683cf9dfvboxsync llMachinesCreated.push_back(Guid(Bstr(aMachineUUIDs[u])));
eeee785f36554f67d8737506fb0fadbf683cf9dfvboxsync * Copies ovf-testcases/ovf-dummy.vmdk to the given target and appends that
21a146e63ee0fdd6658cf0fb9ee378dacd5e416bvboxsync * target as a string to the given list so that the caller can delete it
21a146e63ee0fdd6658cf0fb9ee378dacd5e416bvboxsync * again later.
21a146e63ee0fdd6658cf0fb9ee378dacd5e416bvboxsync * @param llFiles2Delete List of strings to append the target file path to.
eeee785f36554f67d8737506fb0fadbf683cf9dfvboxsync * @param pcszDest Target for dummy VMDK.
eeee785f36554f67d8737506fb0fadbf683cf9dfvboxsync const char *pcszDest)
eeee785f36554f67d8737506fb0fadbf683cf9dfvboxsync RTPrintf("%s: copying ovf-dummy.vmdk to \"%s\"...\n", pcszPrefix, pcszDest);
eeee785f36554f67d8737506fb0fadbf683cf9dfvboxsync int vrc = RTFileCopy("ovf-testcases/ovf-dummy.vmdk", pcszDest);
eeee785f36554f67d8737506fb0fadbf683cf9dfvboxsync if (RT_FAILURE(vrc)) throw MyError(0, Utf8StrFmt("Cannot copy ovf-dummy.vmdk to %s: %Rra\n", pcszDest, vrc).c_str());
eeee785f36554f67d8737506fb0fadbf683cf9dfvboxsync * @param argc
eeee785f36554f67d8737506fb0fadbf683cf9dfvboxsync * @param argv[]
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (FAILED(rc)) throw MyError(rc, "failed to initialize COM!\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync rc = pVirtualBox.createLocalObject(CLSID_VirtualBox);
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (FAILED(rc)) throw MyError(rc, "failed to create the VirtualBox object!\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (FAILED(rc)) throw MyError(rc, "failed to create a session object!\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync // for each testcase, we will copy the dummy VMDK image to the subdirectory with the OVF testcase
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync // so that the import will find the disks it expects; this is just for testing the import since
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync // the imported machines will obviously not be usable.
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync // llFiles2Delete receives the paths of all the files that we need to clean up later.
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync // testcase 1: import ovf-joomla-0.9/joomla-1.1.4-ovf.ovf
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-0.vmdk");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-1.vmdk");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync importOVF("joomla-0.9", pVirtualBox, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf.ovf", llMachinesCreated);
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync // testcase 2: import ovf-winxp-vbox-sharedfolders/winxp.ovf
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/Windows 5.1 XP 1 merged.vmdk");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/smallvdi.vmdk");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync importOVF("winxp-vbox-sharedfolders", pVirtualBox, "ovf-testcases/ovf-winxp-vbox-sharedfolders/winxp.ovf", llMachinesCreated);
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync // testcase 3: import ovf-winxp-vbox-sharedfolders/winxp.ovf
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync importOVF("winhost-audio-nodisks", pVirtualBox, "ovf-testcases/ovf-winhost-audio-nodisks/WinXP.ovf", llMachinesCreated);
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync RTPrintf("Machine imports done, no errors. Cleaning up...\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync // clean up the machines created
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync for (std::list<Guid>::const_iterator it = llMachinesCreated.begin();
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync rc = pVirtualBox->FindMachine(bstrUUID.raw(), pMachine.asOutParam());
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (FAILED(rc)) throw MyError(rc, "VirtualBox::FindMachine() failed\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync RTPrintf(" Deleting machine %ls...\n", bstrUUID.raw());
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync rc = pMachine->Unregister(CleanupMode_DetachAllReturnHardDisksOnly,
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (FAILED(rc)) throw MyError(rc, "Machine::Unregister() failed\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync rc = pMachine->DeleteConfig(ComSafeArrayAsInParam(sfaMedia), pProgress.asOutParam());
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (FAILED(rc)) throw MyError(rc, "Machine::DeleteSettings() failed\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync // clean up the VMDK copies that we made in copyDummyDiskImage()
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync for (std::list<Utf8Str>::const_iterator it = llFiles2Delete.begin();
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync RTPrintf("Deleting file %s...\n", strFile.c_str());