1 | /* $Id: tstOVF.cpp 33294 2010-10-21 10:45:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * tstOVF - testcases for OVF import and export
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2010 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include <VBox/com/VirtualBox.h>
|
---|
20 |
|
---|
21 | #include <VBox/com/com.h>
|
---|
22 | #include <VBox/com/array.h>
|
---|
23 | #include <VBox/com/string.h>
|
---|
24 | #include <VBox/com/ErrorInfo.h>
|
---|
25 | #include <VBox/com/errorprint.h>
|
---|
26 | #include <VBox/com/EventQueue.h>
|
---|
27 |
|
---|
28 | #include <iprt/initterm.h>
|
---|
29 | #include <iprt/stream.h>
|
---|
30 | #include <iprt/file.h>
|
---|
31 | #include <iprt/path.h>
|
---|
32 | #include <iprt/param.h>
|
---|
33 |
|
---|
34 | #include <list>
|
---|
35 |
|
---|
36 | using namespace com;
|
---|
37 |
|
---|
38 | // main
|
---|
39 | ///////////////////////////////////////////////////////////////////////////////
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Quick hack exception structure.
|
---|
43 | *
|
---|
44 | */
|
---|
45 | struct MyError
|
---|
46 | {
|
---|
47 | MyError(HRESULT rc,
|
---|
48 | const char *pcsz,
|
---|
49 | IProgress *pProgress = NULL)
|
---|
50 | : m_rc(rc)
|
---|
51 | {
|
---|
52 | m_str = "ERROR: ";
|
---|
53 | m_str += pcsz;
|
---|
54 |
|
---|
55 | if (pProgress)
|
---|
56 | {
|
---|
57 | com::ProgressErrorInfo info(pProgress);
|
---|
58 | com::GluePrintErrorInfo(info);
|
---|
59 | }
|
---|
60 | else if (rc)
|
---|
61 | {
|
---|
62 | com::ErrorInfo info;
|
---|
63 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
64 | com::GluePrintRCMessage(rc);
|
---|
65 | else
|
---|
66 | com::GluePrintErrorInfo(info);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | Utf8Str m_str;
|
---|
71 | HRESULT m_rc;
|
---|
72 | };
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Imports the given OVF file, with all bells and whistles.
|
---|
76 | * Throws MyError on errors.
|
---|
77 | * @param pcszPrefix Descriptive short prefix string for console output.
|
---|
78 | * @param pVirtualBox VirtualBox instance.
|
---|
79 | * @param pcszOVF0 File to import.
|
---|
80 | * @param llMachinesCreated out: UUIDs of machines that were created so that caller can clean up.
|
---|
81 | */
|
---|
82 | void importOVF(const char *pcszPrefix,
|
---|
83 | ComPtr<IVirtualBox> &pVirtualBox,
|
---|
84 | const char *pcszOVF0,
|
---|
85 | std::list<Guid> &llMachinesCreated)
|
---|
86 | {
|
---|
87 | char szAbsOVF[RTPATH_MAX];
|
---|
88 | RTPathAbs(pcszOVF0, szAbsOVF, sizeof(szAbsOVF));
|
---|
89 |
|
---|
90 | RTPrintf("%s: reading appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
|
---|
91 | ComPtr<IAppliance> pAppl;
|
---|
92 | HRESULT rc = pVirtualBox->CreateAppliance(pAppl.asOutParam());
|
---|
93 | if (FAILED(rc)) throw MyError(rc, "failed to create appliance\n");
|
---|
94 |
|
---|
95 | ComPtr<IProgress> pProgress;
|
---|
96 | rc = pAppl->Read(Bstr(szAbsOVF).raw(), pProgress.asOutParam());
|
---|
97 | if (FAILED(rc)) throw MyError(rc, "Appliance::Read() failed\n");
|
---|
98 | rc = pProgress->WaitForCompletion(-1);
|
---|
99 | if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
|
---|
100 | LONG rc2;
|
---|
101 | pProgress->COMGETTER(ResultCode)(&rc2);
|
---|
102 | if (FAILED(rc2)) throw MyError(rc2, "Appliance::Read() failed\n", pProgress);
|
---|
103 |
|
---|
104 | RTPrintf("%s: interpreting appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
|
---|
105 | rc = pAppl->Interpret();
|
---|
106 | if (FAILED(rc)) throw MyError(rc, "Appliance::Interpret() failed\n");
|
---|
107 |
|
---|
108 | com::SafeIfaceArray<IVirtualSystemDescription> aDescriptions;
|
---|
109 | rc = pAppl->COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aDescriptions));
|
---|
110 | for (uint32_t u = 0;
|
---|
111 | u < aDescriptions.size();
|
---|
112 | ++u)
|
---|
113 | {
|
---|
114 | ComPtr<IVirtualSystemDescription> pVSys = aDescriptions[u];
|
---|
115 |
|
---|
116 | com::SafeArray<VirtualSystemDescriptionType_T> aTypes;
|
---|
117 | com::SafeArray<BSTR> aRefs;
|
---|
118 | com::SafeArray<BSTR> aOvfValues;
|
---|
119 | com::SafeArray<BSTR> aVboxValues;
|
---|
120 | com::SafeArray<BSTR> aExtraConfigValues;
|
---|
121 | rc = pVSys->GetDescription(ComSafeArrayAsOutParam(aTypes),
|
---|
122 | ComSafeArrayAsOutParam(aRefs),
|
---|
123 | ComSafeArrayAsOutParam(aOvfValues),
|
---|
124 | ComSafeArrayAsOutParam(aVboxValues),
|
---|
125 | ComSafeArrayAsOutParam(aExtraConfigValues));
|
---|
126 | if (FAILED(rc)) throw MyError(rc, "VirtualSystemDescription::GetDescription() failed\n");
|
---|
127 |
|
---|
128 | for (uint32_t u2 = 0;
|
---|
129 | u2 < aTypes.size();
|
---|
130 | ++u2)
|
---|
131 | {
|
---|
132 | const char *pcszType;
|
---|
133 |
|
---|
134 | VirtualSystemDescriptionType_T t = aTypes[u2];
|
---|
135 | switch (t)
|
---|
136 | {
|
---|
137 | case VirtualSystemDescriptionType_OS:
|
---|
138 | pcszType = "ostype";
|
---|
139 | break;
|
---|
140 |
|
---|
141 | case VirtualSystemDescriptionType_Name:
|
---|
142 | pcszType = "name";
|
---|
143 | break;
|
---|
144 |
|
---|
145 | case VirtualSystemDescriptionType_Product:
|
---|
146 | pcszType = "product";
|
---|
147 | break;
|
---|
148 |
|
---|
149 | case VirtualSystemDescriptionType_ProductUrl:
|
---|
150 | pcszType = "producturl";
|
---|
151 | break;
|
---|
152 |
|
---|
153 | case VirtualSystemDescriptionType_Vendor:
|
---|
154 | pcszType = "vendor";
|
---|
155 | break;
|
---|
156 |
|
---|
157 | case VirtualSystemDescriptionType_VendorUrl:
|
---|
158 | pcszType = "vendorurl";
|
---|
159 | break;
|
---|
160 |
|
---|
161 | case VirtualSystemDescriptionType_Version:
|
---|
162 | pcszType = "version";
|
---|
163 | break;
|
---|
164 |
|
---|
165 | case VirtualSystemDescriptionType_Description:
|
---|
166 | pcszType = "description";
|
---|
167 | break;
|
---|
168 |
|
---|
169 | case VirtualSystemDescriptionType_License:
|
---|
170 | pcszType = "license";
|
---|
171 | break;
|
---|
172 |
|
---|
173 | case VirtualSystemDescriptionType_CPU:
|
---|
174 | pcszType = "cpu";
|
---|
175 | break;
|
---|
176 |
|
---|
177 | case VirtualSystemDescriptionType_Memory:
|
---|
178 | pcszType = "memory";
|
---|
179 | break;
|
---|
180 |
|
---|
181 | case VirtualSystemDescriptionType_HardDiskControllerIDE:
|
---|
182 | pcszType = "ide";
|
---|
183 | break;
|
---|
184 |
|
---|
185 | case VirtualSystemDescriptionType_HardDiskControllerSATA:
|
---|
186 | pcszType = "sata";
|
---|
187 | break;
|
---|
188 |
|
---|
189 | case VirtualSystemDescriptionType_HardDiskControllerSAS:
|
---|
190 | pcszType = "sas";
|
---|
191 | break;
|
---|
192 |
|
---|
193 | case VirtualSystemDescriptionType_HardDiskControllerSCSI:
|
---|
194 | pcszType = "scsi";
|
---|
195 | break;
|
---|
196 |
|
---|
197 | case VirtualSystemDescriptionType_HardDiskImage:
|
---|
198 | pcszType = "hd";
|
---|
199 | break;
|
---|
200 |
|
---|
201 | case VirtualSystemDescriptionType_CDROM:
|
---|
202 | pcszType = "cdrom";
|
---|
203 | break;
|
---|
204 |
|
---|
205 | case VirtualSystemDescriptionType_Floppy:
|
---|
206 | pcszType = "floppy";
|
---|
207 | break;
|
---|
208 |
|
---|
209 | case VirtualSystemDescriptionType_NetworkAdapter:
|
---|
210 | pcszType = "net";
|
---|
211 | break;
|
---|
212 |
|
---|
213 | case VirtualSystemDescriptionType_USBController:
|
---|
214 | pcszType = "usb";
|
---|
215 | break;
|
---|
216 |
|
---|
217 | case VirtualSystemDescriptionType_SoundCard:
|
---|
218 | pcszType = "sound";
|
---|
219 | break;
|
---|
220 |
|
---|
221 | default:
|
---|
222 | throw MyError(E_UNEXPECTED, "Invalid VirtualSystemDescriptionType\n");
|
---|
223 | break;
|
---|
224 | }
|
---|
225 |
|
---|
226 | RTPrintf(" vsys %2u item %2u: type %2d (%s), ovf: \"%ls\", vbox: \"%ls\", extra: \"%ls\"\n",
|
---|
227 | u, u2, t, pcszType,
|
---|
228 | aOvfValues[u2],
|
---|
229 | aVboxValues[u2],
|
---|
230 | aExtraConfigValues[u2]);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | RTPrintf("%s: importing %d machine(s)...\n", pcszPrefix, aDescriptions.size());
|
---|
235 | rc = pAppl->ImportMachines(pProgress.asOutParam());
|
---|
236 | if (FAILED(rc)) throw MyError(rc, "Appliance::ImportMachines() failed\n");
|
---|
237 | rc = pProgress->WaitForCompletion(-1);
|
---|
238 | if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
|
---|
239 | pProgress->COMGETTER(ResultCode)(&rc2);
|
---|
240 | if (FAILED(rc2)) throw MyError(rc2, "Appliance::ImportMachines() failed\n", pProgress);
|
---|
241 |
|
---|
242 | com::SafeArray<BSTR> aMachineUUIDs;
|
---|
243 | rc = pAppl->COMGETTER(Machines)(ComSafeArrayAsOutParam(aMachineUUIDs));
|
---|
244 | if (FAILED(rc)) throw MyError(rc, "Appliance::GetMachines() failed\n");
|
---|
245 |
|
---|
246 | for (size_t u = 0;
|
---|
247 | u < aMachineUUIDs.size();
|
---|
248 | ++u)
|
---|
249 | {
|
---|
250 | RTPrintf("%s: created machine %u: %ls\n", pcszPrefix, u, aMachineUUIDs[u]);
|
---|
251 | llMachinesCreated.push_back(Guid(Bstr(aMachineUUIDs[u])));
|
---|
252 | }
|
---|
253 |
|
---|
254 | RTPrintf("%s: success!\n", pcszPrefix);
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Copies ovf-testcases/ovf-dummy.vmdk to the given target and appends that
|
---|
259 | * target as a string to the given list so that the caller can delete it
|
---|
260 | * again later.
|
---|
261 | * @param llFiles2Delete List of strings to append the target file path to.
|
---|
262 | * @param pcszDest Target for dummy VMDK.
|
---|
263 | */
|
---|
264 | void copyDummyDiskImage(const char *pcszPrefix,
|
---|
265 | std::list<Utf8Str> &llFiles2Delete,
|
---|
266 | const char *pcszDest)
|
---|
267 | {
|
---|
268 | RTPrintf("%s: copying ovf-dummy.vmdk to \"%s\"...\n", pcszPrefix, pcszDest);
|
---|
269 |
|
---|
270 | int vrc = RTFileCopy("ovf-testcases/ovf-dummy.vmdk", pcszDest);
|
---|
271 | if (RT_FAILURE(vrc)) throw MyError(0, Utf8StrFmt("Cannot copy ovf-dummy.vmdk to %s: %Rra\n", pcszDest, vrc).c_str());
|
---|
272 | llFiles2Delete.push_back(pcszDest);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | *
|
---|
277 | * @param argc
|
---|
278 | * @param argv[]
|
---|
279 | * @return
|
---|
280 | */
|
---|
281 | int main(int argc, char *argv[])
|
---|
282 | {
|
---|
283 | RTR3Init();
|
---|
284 |
|
---|
285 | HRESULT rc = S_OK;
|
---|
286 |
|
---|
287 | std::list<Utf8Str> llFiles2Delete;
|
---|
288 | std::list<Guid> llMachinesCreated;
|
---|
289 |
|
---|
290 | ComPtr<IVirtualBox> pVirtualBox;
|
---|
291 |
|
---|
292 | try
|
---|
293 | {
|
---|
294 | RTPrintf("Initializing COM...\n");
|
---|
295 | rc = com::Initialize();
|
---|
296 | if (FAILED(rc)) throw MyError(rc, "failed to initialize COM!\n");
|
---|
297 |
|
---|
298 | ComPtr<ISession> pSession;
|
---|
299 |
|
---|
300 | RTPrintf("Creating VirtualBox object...\n");
|
---|
301 | rc = pVirtualBox.createLocalObject(CLSID_VirtualBox);
|
---|
302 | if (FAILED(rc)) throw MyError(rc, "failed to create the VirtualBox object!\n");
|
---|
303 |
|
---|
304 | rc = pSession.createInprocObject(CLSID_Session);
|
---|
305 | if (FAILED(rc)) throw MyError(rc, "failed to create a session object!\n");
|
---|
306 |
|
---|
307 | // create the event queue
|
---|
308 | // (here it is necessary only to process remaining XPCOM/IPC events after the session is closed)
|
---|
309 | EventQueue eventQ;
|
---|
310 |
|
---|
311 | // for each testcase, we will copy the dummy VMDK image to the subdirectory with the OVF testcase
|
---|
312 | // so that the import will find the disks it expects; this is just for testing the import since
|
---|
313 | // the imported machines will obviously not be usable.
|
---|
314 | // llFiles2Delete receives the paths of all the files that we need to clean up later.
|
---|
315 |
|
---|
316 | // testcase 1: import ovf-joomla-0.9/joomla-1.1.4-ovf.ovf
|
---|
317 | copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-0.vmdk");
|
---|
318 | copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-1.vmdk");
|
---|
319 | importOVF("joomla-0.9", pVirtualBox, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf.ovf", llMachinesCreated);
|
---|
320 |
|
---|
321 | // testcase 2: import ovf-winxp-vbox-sharedfolders/winxp.ovf
|
---|
322 | copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/Windows 5.1 XP 1 merged.vmdk");
|
---|
323 | copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/smallvdi.vmdk");
|
---|
324 | importOVF("winxp-vbox-sharedfolders", pVirtualBox, "ovf-testcases/ovf-winxp-vbox-sharedfolders/winxp.ovf", llMachinesCreated);
|
---|
325 |
|
---|
326 | // testcase 3: import ovf-winxp-vbox-sharedfolders/winxp.ovf
|
---|
327 | importOVF("winhost-audio-nodisks", pVirtualBox, "ovf-testcases/ovf-winhost-audio-nodisks/WinXP.ovf", llMachinesCreated);
|
---|
328 |
|
---|
329 | RTPrintf("Machine imports done, no errors. Cleaning up...\n");
|
---|
330 | }
|
---|
331 | catch (MyError &e)
|
---|
332 | {
|
---|
333 | rc = e.m_rc;
|
---|
334 | RTPrintf("%s", e.m_str.c_str());
|
---|
335 | }
|
---|
336 |
|
---|
337 | try
|
---|
338 | {
|
---|
339 | // clean up the machines created
|
---|
340 | for (std::list<Guid>::const_iterator it = llMachinesCreated.begin();
|
---|
341 | it != llMachinesCreated.end();
|
---|
342 | ++it)
|
---|
343 | {
|
---|
344 | const Guid &uuid = *it;
|
---|
345 | Bstr bstrUUID(uuid.toUtf16());
|
---|
346 | ComPtr<IMachine> pMachine;
|
---|
347 | rc = pVirtualBox->FindMachine(bstrUUID.raw(), pMachine.asOutParam());
|
---|
348 | if (FAILED(rc)) throw MyError(rc, "VirtualBox::FindMachine() failed\n");
|
---|
349 |
|
---|
350 | RTPrintf(" Deleting machine %ls...\n", bstrUUID.raw());
|
---|
351 | SafeIfaceArray<IMedium> sfaMedia;
|
---|
352 | rc = pMachine->Unregister(CleanupMode_DetachAllReturnHardDisksOnly,
|
---|
353 | ComSafeArrayAsOutParam(sfaMedia));
|
---|
354 | if (FAILED(rc)) throw MyError(rc, "Machine::Unregister() failed\n");
|
---|
355 |
|
---|
356 | ComPtr<IProgress> pProgress;
|
---|
357 | rc = pMachine->Delete(ComSafeArrayAsInParam(sfaMedia), pProgress.asOutParam());
|
---|
358 | if (FAILED(rc)) throw MyError(rc, "Machine::DeleteSettings() failed\n");
|
---|
359 | rc = pProgress->WaitForCompletion(-1);
|
---|
360 | if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
|
---|
361 | }
|
---|
362 | }
|
---|
363 | catch (MyError &e)
|
---|
364 | {
|
---|
365 | rc = e.m_rc;
|
---|
366 | RTPrintf("%s", e.m_str.c_str());
|
---|
367 | }
|
---|
368 |
|
---|
369 | // clean up the VMDK copies that we made in copyDummyDiskImage()
|
---|
370 | for (std::list<Utf8Str>::const_iterator it = llFiles2Delete.begin();
|
---|
371 | it != llFiles2Delete.end();
|
---|
372 | ++it)
|
---|
373 | {
|
---|
374 | const Utf8Str &strFile = *it;
|
---|
375 | RTPrintf("Deleting file %s...\n", strFile.c_str());
|
---|
376 | RTFileDelete(strFile.c_str());
|
---|
377 | }
|
---|
378 |
|
---|
379 | pVirtualBox.setNull();
|
---|
380 |
|
---|
381 | RTPrintf("Shutting down COM...\n");
|
---|
382 | com::Shutdown();
|
---|
383 | RTPrintf ("tstOVF all done!\n");
|
---|
384 |
|
---|
385 | return rc;
|
---|
386 | }
|
---|
387 |
|
---|