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