VirtualBox

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

Last change on this file since 30635 was 30635, checked in by vboxsync, 14 years ago

tstOVF.cpp: two warnings about HRESULT (unsigned) vs. signed values (-1).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/* $Id: tstOVF.cpp 30635 2010-07-05 19:45:12Z 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
36using namespace com;
37
38// main
39///////////////////////////////////////////////////////////////////////////////
40
41/**
42 * Quick hack exception structure.
43 *
44 */
45struct MyError
46{
47 MyError(HRESULT rc,
48 const char *pcsz,
49 IProgress *pProgress = NULL)
50 : m_rc(rc)
51 {
52 m_str = pcsz;
53
54 if (pProgress)
55 {
56 com::ProgressErrorInfo info(pProgress);
57 com::GluePrintErrorInfo(info);
58 }
59 else
60 {
61 com::ErrorInfo info;
62 if (!info.isFullAvailable() && !info.isBasicAvailable())
63 {
64 com::GluePrintRCMessage(rc);
65 m_str.append("Most likely, the VirtualBox COM server is not running or failed to start.");
66 }
67 else
68 com::GluePrintErrorInfo(info);
69 }
70 }
71
72 Utf8Str m_str;
73 HRESULT m_rc;
74};
75
76/**
77 * Imports the given OVF file, with all bells and whistles.
78 * Throws MyError on errors.
79 * @param pVirtualBox VirtualBox instance.
80 * @param pcszOVF File to import.
81 */
82void importOVF(ComPtr<IVirtualBox> &pVirtualBox,
83 const char *pcszOVF0)
84{
85 char szAbsOVF[RTPATH_MAX];
86 RTPathAbs(pcszOVF0, szAbsOVF, sizeof(szAbsOVF));
87
88 RTPrintf("Reading appliance \"%s\"...\n", szAbsOVF);
89 ComPtr<IAppliance> pAppl;
90 HRESULT rc = pVirtualBox->CreateAppliance(pAppl.asOutParam());
91 if (FAILED(rc))
92 throw MyError(rc, "failed to create appliace");
93
94 ComPtr<IProgress> pProgress;
95 rc = pAppl->Read(Bstr(szAbsOVF), pProgress.asOutParam());
96 if (FAILED(rc))
97 throw MyError(rc, "Appliance::Read() failed");
98 rc = pProgress->WaitForCompletion(-1);
99 if (FAILED(rc))
100 throw MyError(rc, "Progress::WaitForCompletion() failed");
101 LONG rc2;
102 pProgress->COMGETTER(ResultCode)(&rc2);
103 if (FAILED(rc2))
104 throw MyError(rc2, "Appliance::Read() failed", pProgress);
105
106 RTPrintf("Interpreting appliance \"%s\"...\n", szAbsOVF);
107 rc = pAppl->Interpret();
108 if (FAILED(rc))
109 throw MyError(rc, "Appliance::Interpret() failed");
110
111 com::SafeIfaceArray<IVirtualSystemDescription> aDescriptions;
112 rc = pAppl->COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aDescriptions));
113 for (uint32_t u = 0;
114 u < aDescriptions.size();
115 ++u)
116 {
117 ComPtr<IVirtualSystemDescription> pVSys = aDescriptions[u];
118
119 com::SafeArray<VirtualSystemDescriptionType_T> aTypes;
120 com::SafeArray<BSTR> aRefs;
121 com::SafeArray<BSTR> aOvfValues;
122 com::SafeArray<BSTR> aVboxValues;
123 com::SafeArray<BSTR> aExtraConfigValues;
124 rc = pVSys->GetDescription(ComSafeArrayAsOutParam(aTypes),
125 ComSafeArrayAsOutParam(aRefs),
126 ComSafeArrayAsOutParam(aOvfValues),
127 ComSafeArrayAsOutParam(aVboxValues),
128 ComSafeArrayAsOutParam(aExtraConfigValues));
129 for (uint32_t u2 = 0;
130 u2 < aTypes.size();
131 ++u2)
132 {
133 const char *pcszType;
134
135 VirtualSystemDescriptionType_T t = aTypes[u2];
136 switch (t)
137 {
138 case VirtualSystemDescriptionType_OS:
139 pcszType = "ostype";
140 break;
141
142 case VirtualSystemDescriptionType_Name:
143 pcszType = "name";
144 break;
145
146 case VirtualSystemDescriptionType_Product:
147 pcszType = "product";
148 break;
149
150 case VirtualSystemDescriptionType_ProductUrl:
151 pcszType = "producturl";
152 break;
153
154 case VirtualSystemDescriptionType_Vendor:
155 pcszType = "vendor";
156 break;
157
158 case VirtualSystemDescriptionType_VendorUrl:
159 pcszType = "vendorurl";
160 break;
161
162 case VirtualSystemDescriptionType_Version:
163 pcszType = "version";
164 break;
165
166 case VirtualSystemDescriptionType_Description:
167 pcszType = "description";
168 break;
169
170 case VirtualSystemDescriptionType_License:
171 pcszType = "license";
172 break;
173
174 case VirtualSystemDescriptionType_CPU:
175 pcszType = "cpu";
176 break;
177
178 case VirtualSystemDescriptionType_Memory:
179 pcszType = "memory";
180 break;
181
182 case VirtualSystemDescriptionType_HardDiskControllerIDE:
183 pcszType = "ide";
184 break;
185
186 case VirtualSystemDescriptionType_HardDiskControllerSATA:
187 pcszType = "sata";
188 break;
189
190 case VirtualSystemDescriptionType_HardDiskControllerSAS:
191 pcszType = "sas";
192 break;
193
194 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
195 pcszType = "scsi";
196 break;
197
198 case VirtualSystemDescriptionType_HardDiskImage:
199 pcszType = "hd";
200 break;
201
202 case VirtualSystemDescriptionType_CDROM:
203 pcszType = "cdrom";
204 break;
205
206 case VirtualSystemDescriptionType_Floppy:
207 pcszType = "floppy";
208 break;
209
210 case VirtualSystemDescriptionType_NetworkAdapter:
211 pcszType = "net";
212 break;
213
214 case VirtualSystemDescriptionType_USBController:
215 pcszType = "usb";
216 break;
217
218 case VirtualSystemDescriptionType_SoundCard:
219 pcszType = "sound";
220 break;
221
222 default:
223 throw MyError(E_UNEXPECTED, "Invalid VirtualSystemDescriptionType");
224 break;
225 }
226
227 RTPrintf(" vsys %2u item %2u: type %2d (%s), ovf: \"%ls\", vbox: \"%ls\", extra: \"%ls\"\n",
228 u, u2, t, pcszType,
229 aOvfValues[u2],
230 aVboxValues[u2],
231 aExtraConfigValues[u2] );
232 }
233 }
234
235 RTPrintf("Importing machines...\n");
236 rc = pAppl->ImportMachines(pProgress.asOutParam());
237 if (FAILED(rc))
238 throw MyError(rc, "Appliance::ImportMachines() failed");
239 rc = pProgress->WaitForCompletion(-1);
240 if (FAILED(rc))
241 throw MyError(rc, "Progress::WaitForCompletion() failed");
242 pProgress->COMGETTER(ResultCode)(&rc2);
243 if (FAILED(rc2))
244 throw MyError(rc2, "Appliance::ImportMachines() failed", pProgress);
245}
246
247/**
248 * Copies ovf-testcases/ovf-dummy.vmdk to the given target and appends that
249 * target as a string to the given list so that the caller can delete it
250 * again later.
251 * @param llFiles2Delete List of strings to append the target file path to.
252 * @param pcszDest Target for dummy VMDK.
253 */
254void copyDummyDiskImage(std::list<Utf8Str> &llFiles2Delete, const char *pcszDest)
255{
256 int vrc = RTFileCopy("ovf-testcases/ovf-dummy.vmdk", pcszDest);
257 if (RT_FAILURE(vrc))
258 throw MyError(E_UNEXPECTED, "Cannot copy ovf-dummy.vmdk");
259 llFiles2Delete.push_back(pcszDest);
260}
261
262/**
263 *
264 * @param argc
265 * @param argv[]
266 * @return
267 */
268int main(int argc, char *argv[])
269{
270 RTR3Init();
271
272 HRESULT rc = S_OK;
273
274 std::list<Utf8Str> llFiles2Delete;
275
276 try
277 {
278 RTPrintf("Initializing COM...\n");
279 rc = com::Initialize();
280 if (FAILED(rc))
281 throw MyError(rc, "failed to initialize COM!");
282
283 ComPtr<IVirtualBox> pVirtualBox;
284 ComPtr<ISession> pSession;
285
286 RTPrintf("Creating VirtualBox object...\n");
287 rc = pVirtualBox.createLocalObject(CLSID_VirtualBox);
288 if (FAILED(rc))
289 throw MyError(rc, "failed to create the VirtualBox object!");
290
291 rc = pSession.createInprocObject(CLSID_Session);
292 if (FAILED(rc))
293 throw MyError(rc, "failed to create a session object!");
294
295 // create the event queue
296 // (here it is necessary only to process remaining XPCOM/IPC events after the session is closed)
297 EventQueue eventQ;
298
299 // testcase 1: import ovf-joomla-0.9/joomla-1.1.4-ovf.ovf
300 copyDummyDiskImage(llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-0.vmdk");
301 copyDummyDiskImage(llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-1.vmdk");
302 importOVF(pVirtualBox, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf.ovf");
303
304 RTPrintf ("tstOVF all done, no errors!.\n");
305
306 // todo: cleanup created machines, created disk images
307 }
308 catch (MyError &e)
309 {
310 rc = e.m_rc;
311 RTPrintf("%s\n", e.m_str.c_str());
312 }
313
314 // clean up
315 for (std::list<Utf8Str>::const_iterator it = llFiles2Delete.begin();
316 it != llFiles2Delete.end();
317 ++it)
318 {
319 const Utf8Str &strFile = *it;
320 RTFileDelete(strFile.c_str());
321 }
322
323 RTPrintf("Shutting down COM...\n");
324 com::Shutdown();
325
326 return rc;
327}
328
Note: See TracBrowser for help on using the repository browser.

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