VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstOVF.cpp@ 50117

最後變更 在這個檔案從50117是 50117,由 vboxsync 提交於 11 年 前

Main+Frontends: clear out some cruft code, outdated EventQueue stuff and whitespace cleanup

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 13.2 KB
 
1/* $Id: tstOVF.cpp 50117 2014-01-20 14:05:02Z vboxsync $ */
2/** @file
3 *
4 * tstOVF - testcases for OVF import and export
5 */
6
7/*
8 * Copyright (C) 2010-2014 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.alldomusa.eu.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
27#include <iprt/initterm.h>
28#include <iprt/stream.h>
29#include <iprt/file.h>
30#include <iprt/path.h>
31#include <iprt/param.h>
32
33#include <list>
34
35using namespace com;
36
37// main
38///////////////////////////////////////////////////////////////////////////////
39
40/**
41 * Quick hack exception structure.
42 *
43 */
44struct MyError
45{
46 MyError(HRESULT rc,
47 const char *pcsz,
48 IProgress *pProgress = NULL)
49 : m_rc(rc)
50 {
51 m_str = "ERROR: ";
52 m_str += pcsz;
53
54 if (pProgress)
55 {
56 com::ProgressErrorInfo info(pProgress);
57 com::GluePrintErrorInfo(info);
58 }
59 else if (rc)
60 {
61 com::ErrorInfo info;
62 if (!info.isFullAvailable() && !info.isBasicAvailable())
63 com::GluePrintRCMessage(rc);
64 else
65 com::GluePrintErrorInfo(info);
66 }
67 }
68
69 Utf8Str m_str;
70 HRESULT m_rc;
71};
72
73/**
74 * Imports the given OVF file, with all bells and whistles.
75 * Throws MyError on errors.
76 * @param pcszPrefix Descriptive short prefix string for console output.
77 * @param pVirtualBox VirtualBox instance.
78 * @param pcszOVF0 File to import.
79 * @param llMachinesCreated out: UUIDs of machines that were created so that caller can clean up.
80 */
81void importOVF(const char *pcszPrefix,
82 ComPtr<IVirtualBox> &pVirtualBox,
83 const char *pcszOVF0,
84 std::list<Guid> &llMachinesCreated)
85{
86 char szAbsOVF[RTPATH_MAX];
87 RTPathAbs(pcszOVF0, szAbsOVF, sizeof(szAbsOVF));
88
89 RTPrintf("%s: reading appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
90 ComPtr<IAppliance> pAppl;
91 HRESULT rc = pVirtualBox->CreateAppliance(pAppl.asOutParam());
92 if (FAILED(rc)) throw MyError(rc, "failed to create appliance\n");
93
94 ComPtr<IProgress> pProgress;
95 rc = pAppl->Read(Bstr(szAbsOVF).raw(), pProgress.asOutParam());
96 if (FAILED(rc)) throw MyError(rc, "Appliance::Read() failed\n");
97 rc = pProgress->WaitForCompletion(-1);
98 if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
99 LONG rc2;
100 pProgress->COMGETTER(ResultCode)(&rc2);
101 if (FAILED(rc2)) throw MyError(rc2, "Appliance::Read() failed\n", pProgress);
102
103 RTPrintf("%s: interpreting appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
104 rc = pAppl->Interpret();
105 if (FAILED(rc)) throw MyError(rc, "Appliance::Interpret() failed\n");
106
107 com::SafeIfaceArray<IVirtualSystemDescription> aDescriptions;
108 rc = pAppl->COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aDescriptions));
109 for (uint32_t u = 0;
110 u < aDescriptions.size();
111 ++u)
112 {
113 ComPtr<IVirtualSystemDescription> pVSys = aDescriptions[u];
114
115 com::SafeArray<VirtualSystemDescriptionType_T> aTypes;
116 com::SafeArray<BSTR> aRefs;
117 com::SafeArray<BSTR> aOvfValues;
118 com::SafeArray<BSTR> aVboxValues;
119 com::SafeArray<BSTR> aExtraConfigValues;
120 rc = pVSys->GetDescription(ComSafeArrayAsOutParam(aTypes),
121 ComSafeArrayAsOutParam(aRefs),
122 ComSafeArrayAsOutParam(aOvfValues),
123 ComSafeArrayAsOutParam(aVboxValues),
124 ComSafeArrayAsOutParam(aExtraConfigValues));
125 if (FAILED(rc)) throw MyError(rc, "VirtualSystemDescription::GetDescription() failed\n");
126
127 for (uint32_t u2 = 0;
128 u2 < aTypes.size();
129 ++u2)
130 {
131 const char *pcszType;
132
133 VirtualSystemDescriptionType_T t = aTypes[u2];
134 switch (t)
135 {
136 case VirtualSystemDescriptionType_OS:
137 pcszType = "ostype";
138 break;
139
140 case VirtualSystemDescriptionType_Name:
141 pcszType = "name";
142 break;
143
144 case VirtualSystemDescriptionType_Product:
145 pcszType = "product";
146 break;
147
148 case VirtualSystemDescriptionType_ProductUrl:
149 pcszType = "producturl";
150 break;
151
152 case VirtualSystemDescriptionType_Vendor:
153 pcszType = "vendor";
154 break;
155
156 case VirtualSystemDescriptionType_VendorUrl:
157 pcszType = "vendorurl";
158 break;
159
160 case VirtualSystemDescriptionType_Version:
161 pcszType = "version";
162 break;
163
164 case VirtualSystemDescriptionType_Description:
165 pcszType = "description";
166 break;
167
168 case VirtualSystemDescriptionType_License:
169 pcszType = "license";
170 break;
171
172 case VirtualSystemDescriptionType_CPU:
173 pcszType = "cpu";
174 break;
175
176 case VirtualSystemDescriptionType_Memory:
177 pcszType = "memory";
178 break;
179
180 case VirtualSystemDescriptionType_HardDiskControllerIDE:
181 pcszType = "ide";
182 break;
183
184 case VirtualSystemDescriptionType_HardDiskControllerSATA:
185 pcszType = "sata";
186 break;
187
188 case VirtualSystemDescriptionType_HardDiskControllerSAS:
189 pcszType = "sas";
190 break;
191
192 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
193 pcszType = "scsi";
194 break;
195
196 case VirtualSystemDescriptionType_HardDiskImage:
197 pcszType = "hd";
198 break;
199
200 case VirtualSystemDescriptionType_CDROM:
201 pcszType = "cdrom";
202 break;
203
204 case VirtualSystemDescriptionType_Floppy:
205 pcszType = "floppy";
206 break;
207
208 case VirtualSystemDescriptionType_NetworkAdapter:
209 pcszType = "net";
210 break;
211
212 case VirtualSystemDescriptionType_USBController:
213 pcszType = "usb";
214 break;
215
216 case VirtualSystemDescriptionType_SoundCard:
217 pcszType = "sound";
218 break;
219
220 default:
221 throw MyError(E_UNEXPECTED, "Invalid VirtualSystemDescriptionType\n");
222 break;
223 }
224
225 RTPrintf(" vsys %2u item %2u: type %2d (%s), ovf: \"%ls\", vbox: \"%ls\", extra: \"%ls\"\n",
226 u, u2, t, pcszType,
227 aOvfValues[u2],
228 aVboxValues[u2],
229 aExtraConfigValues[u2]);
230 }
231 }
232
233 RTPrintf("%s: importing %d machine(s)...\n", pcszPrefix, aDescriptions.size());
234 SafeArray<ImportOptions_T> sfaOptions;
235 rc = pAppl->ImportMachines(ComSafeArrayAsInParam(sfaOptions), 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 */
264void 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 */
281int main(int argc, char *argv[])
282{
283 RTR3InitExe(argc, &argv, 0);
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 // for each testcase, we will copy the dummy VMDK image to the subdirectory with the OVF testcase
308 // so that the import will find the disks it expects; this is just for testing the import since
309 // the imported machines will obviously not be usable.
310 // llFiles2Delete receives the paths of all the files that we need to clean up later.
311
312 // testcase 1: import ovf-joomla-0.9/joomla-1.1.4-ovf.ovf
313 copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-0.vmdk");
314 copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-1.vmdk");
315 importOVF("joomla-0.9", pVirtualBox, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf.ovf", llMachinesCreated);
316
317 // testcase 2: import ovf-winxp-vbox-sharedfolders/winxp.ovf
318 copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/Windows 5.1 XP 1 merged.vmdk");
319 copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/smallvdi.vmdk");
320 importOVF("winxp-vbox-sharedfolders", pVirtualBox, "ovf-testcases/ovf-winxp-vbox-sharedfolders/winxp.ovf", llMachinesCreated);
321
322 // testcase 3: import ovf-winxp-vbox-sharedfolders/winxp.ovf
323 importOVF("winhost-audio-nodisks", pVirtualBox, "ovf-testcases/ovf-winhost-audio-nodisks/WinXP.ovf", llMachinesCreated);
324
325 RTPrintf("Machine imports done, no errors. Cleaning up...\n");
326 }
327 catch (MyError &e)
328 {
329 rc = e.m_rc;
330 RTPrintf("%s", e.m_str.c_str());
331 }
332
333 try
334 {
335 // clean up the machines created
336 for (std::list<Guid>::const_iterator it = llMachinesCreated.begin();
337 it != llMachinesCreated.end();
338 ++it)
339 {
340 const Guid &uuid = *it;
341 Bstr bstrUUID(uuid.toUtf16());
342 ComPtr<IMachine> pMachine;
343 rc = pVirtualBox->FindMachine(bstrUUID.raw(), pMachine.asOutParam());
344 if (FAILED(rc)) throw MyError(rc, "VirtualBox::FindMachine() failed\n");
345
346 RTPrintf(" Deleting machine %ls...\n", bstrUUID.raw());
347 SafeIfaceArray<IMedium> sfaMedia;
348 rc = pMachine->Unregister(CleanupMode_DetachAllReturnHardDisksOnly,
349 ComSafeArrayAsOutParam(sfaMedia));
350 if (FAILED(rc)) throw MyError(rc, "Machine::Unregister() failed\n");
351
352 ComPtr<IProgress> pProgress;
353 rc = pMachine->DeleteConfig(ComSafeArrayAsInParam(sfaMedia), pProgress.asOutParam());
354 if (FAILED(rc)) throw MyError(rc, "Machine::DeleteSettings() failed\n");
355 rc = pProgress->WaitForCompletion(-1);
356 if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
357 }
358 }
359 catch (MyError &e)
360 {
361 rc = e.m_rc;
362 RTPrintf("%s", e.m_str.c_str());
363 }
364
365 // clean up the VMDK copies that we made in copyDummyDiskImage()
366 for (std::list<Utf8Str>::const_iterator it = llFiles2Delete.begin();
367 it != llFiles2Delete.end();
368 ++it)
369 {
370 const Utf8Str &strFile = *it;
371 RTPrintf("Deleting file %s...\n", strFile.c_str());
372 RTFileDelete(strFile.c_str());
373 }
374
375 pVirtualBox.setNull();
376
377 RTPrintf("Shutting down COM...\n");
378 com::Shutdown();
379 RTPrintf ("tstOVF all done!\n");
380
381 return rc;
382}
383
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette