VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/tests/api/tdCloneMedium1.py@ 97864

Last change on this file since 97864 was 97864, checked in by vboxsync, 2 years ago

Main: Added API function Medium::resizeAndCloneTo. Modified Medium::resize. bugref:10090

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# $Id: tdCloneMedium1.py 97864 2022-12-23 21:25:48Z vboxsync $
4
5"""
6VirtualBox Validation Kit - Clone Medium Test #1
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2010-2022 Oracle Corporation
12
13This file is part of VirtualBox Open Source Edition (OSE), as
14available from http://www.virtualbox.org. This file is free software;
15you can redistribute it and/or modify it under the terms of the GNU
16General Public License (GPL) as published by the Free Software
17Foundation, in version 2 as it comes in the "COPYING" file of the
18VirtualBox OSE distribution. VirtualBox OSE is distributed in the
19hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
20
21The contents of this file may alternatively be used under the terms
22of the Common Development and Distribution License Version 1.0
23(CDDL) only, as it comes in the "COPYING.CDDL" file of the
24VirtualBox OSE distribution, in which case the provisions of the
25CDDL are applicable instead of those of the GPL.
26
27You may elect to license modified versions of this file under the
28terms and conditions of either the GPL or the CDDL or both.
29"""
30__version__ = "$Revision: 97864 $"
31
32
33# Standard Python imports.
34import os
35from subprocess import call
36import sys
37
38# Only the main script needs to modify the path.
39try: __file__
40except: __file__ = sys.argv[0]
41g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
42sys.path.append(g_ksValidationKitDir)
43
44# Validation Kit imports.
45from testdriver import base
46from testdriver import reporter
47from testdriver import vboxcon
48from testdriver import vboxwrappers
49
50
51class SubTstDrvCloneMedium1(base.SubTestDriverBase):
52 """
53 Sub-test driver for Clone Medium Test #1.
54 """
55
56 def __init__(self, oTstDrv):
57 base.SubTestDriverBase.__init__(self, oTstDrv, 'clone-medium', 'Move Medium');
58
59 def testIt(self):
60 """
61 Execute the sub-testcase.
62 """
63
64 return self.testAll()
65
66 #
67 # Test execution helpers.
68 #
69
70 def createTestMedium(self, oVM, sPathSuffix, sFmt = 'VDI', cbSize = 1024*1024, data = []):
71 assert oVM is not None
72
73 oSession = self.oTstDrv.openSession(oVM)
74
75 if oSession is None:
76 return False
77
78 #
79 # Create Medium Object
80 #
81
82 sBaseHdd1Path = os.path.join(self.oTstDrv.sScratchPath, sPathSuffix)
83 sBaseHdd1Fmt = sFmt
84 cbBaseHdd1Size = cbSize
85
86 try:
87 oBaseHdd1 = oSession.createBaseHd(sBaseHdd1Path, sBaseHdd1Fmt, cbBaseHdd1Size)
88 except:
89 return reporter.errorXcpt('createBaseHd failed')
90
91 oMediumIOBaseHdd1 = oBaseHdd1.openForIO(True, "")
92
93 if(len(data) > 0):
94 cbWritten = oMediumIOBaseHdd1.write(0, data)
95
96 if cbWritten != 1:
97 return reporter.error("Failed writing to test hdd.")
98
99 oMediumIOBaseHdd1.close()
100
101 return oBaseHdd1
102
103 def cloneMedium(self, oSrcHd, oTgtHd):
104 """
105 Clones medium into target medium.
106 """
107 try:
108 oProgressCom = oSrcHd.cloneTo(oTgtHd, (vboxcon.MediumVariant_Standard, ), None);
109 except:
110 reporter.errorXcpt('failed to clone medium %s to %s' % (oSrcHd.name, oTgtHd.name));
111 return False;
112 oProgress = vboxwrappers.ProgressWrapper(oProgressCom, self.oTstDrv.oVBoxMgr, self.oTstDrv,
113 'clone base disk %s to %s' % (oSrcHd.name, oTgtHd.name));
114 oProgress.wait(cMsTimeout = 15*60*1000); # 15 min
115 oProgress.logResult();
116 return True;
117
118 def resizeAndCloneMedium(self, oSrcHd, oTgtHd, cbTgtSize):
119 """
120 Clones medium into target medium.
121 """
122
123 try:
124 oProgressCom = oSrcHd.resizeAndCloneTo(oTgtHd, cbTgtSize, (vboxcon.MediumVariant_Standard, ), None);
125 except:
126 reporter.errorXcpt('failed to resize and clone medium %s to %s and to size %d' % (oSrcHd.name, oTgtHd.name, cbTgtSize));
127 return False;
128 oProgress = vboxwrappers.ProgressWrapper(oProgressCom, self.oTstDrv.oVBoxMgr, self.oTstDrv,
129 'resize and clone base disk %s to %s and to size %d' % (oSrcHd.name, oTgtHd.name, cbTgtSize));
130 oProgress.wait(cMsTimeout = 15*60*1000); # 15 min
131 oProgress.logResult();
132 return True;
133
134 def deleteVM(self, oVM):
135 try:
136 oVM.unregister(vboxcon.CleanupMode_DetachAllReturnNone);
137 except:
138 reporter.logXcpt();
139
140 try:
141 oProgressCom = oVM.deleteConfig([]);
142 except:
143 reporter.logXcpt();
144 else:
145 oProgress = vboxwrappers.ProgressWrapper(oProgressCom, self.oTstDrv.oVBoxMgr, self.oTstDrv,
146 'Delete VM %s' % (oVM.name));
147 oProgress.wait(cMsTimeout = 15*60*1000); # 15 min
148 oProgress.logResult();
149
150 return None;
151
152 #
153 # Tests
154 #
155
156 def testCloneOnly(self):
157 """
158 Tests cloning mediums only. No resize.
159 """
160
161 reporter.testStart("testCloneOnly")
162
163 oVM = self.oTstDrv.createTestVM('test-medium-clone-only', 1, None, 4)
164
165 hd1 = self.createTestMedium(oVM, "hd1-cloneonly", data=[0xdeadbeef])
166 hd2 = self.createTestMedium(oVM, "hd2-cloneonly")
167
168 if not self.cloneMedium(hd1, hd2):
169 return False
170
171 oMediumIOhd1 = hd1.openForIO(True, "")
172 dataHd1 = oMediumIOhd1.read(0, 4)
173 oMediumIOhd1.close()
174
175 oMediumIOhd2 = hd2.openForIO(True, "")
176 dataHd2 = oMediumIOhd2.read(0, 4)
177 oMediumIOhd2.close()
178
179 if dataHd1 != dataHd2:
180 reporter.testFailure("Data read is unexpected.")
181
182 self.deleteVM(oVM)
183
184 reporter.testDone()
185 return True
186
187 def testResizeAndClone(self):
188 """
189 Tests resizing and cloning mediums only.
190 """
191
192 reporter.testStart("testResizeAndClone")
193
194 oVM = self.oTstDrv.createTestVM('test-medium-clone-only', 1, None, 4)
195
196 hd1 = self.createTestMedium(oVM, "hd1-resizeandclone", data=[0xdeadbeef])
197 hd2 = self.createTestMedium(oVM, "hd2-resizeandclone")
198
199 if not (hasattr(hd1, "resizeAndCloneTo") and callable(getattr(hd1, "resizeAndCloneTo"))):
200 self.deleteVM(oVM)
201 reporter.testDone()
202 return True
203
204 if not self.resizeAndCloneMedium(hd1, hd2, 1024*1024*2):
205 return False
206
207 oMediumIOhd1 = hd1.openForIO(True, "")
208 dataHd1 = oMediumIOhd1.read(0, 4)
209 oMediumIOhd1.close()
210
211 oMediumIOhd2 = hd2.openForIO(True, "")
212 dataHd2 = oMediumIOhd2.read(0, 4)
213 oMediumIOhd2.close()
214
215 if dataHd1 != dataHd2:
216 reporter.testFailure("Data read is unexpected.")
217
218 if hd1.logicalSize != hd2.logicalSize and hd2.logicalSize != 1024*1024*2:
219 reporter.testFailure("Target medium did not resize.")
220
221 self.deleteVM(oVM)
222
223 reporter.testDone()
224 return True
225
226 def testAll(self):
227 return (self.testCloneOnly() & self.testResizeAndClone())
228
229if __name__ == '__main__':
230 sys.path.append(os.path.dirname(os.path.abspath(__file__)))
231 from tdApi1 import tdApi1; # pylint: disable=relative-import
232 sys.exit(tdApi1([SubTstDrvCloneMedium1]).main(sys.argv))
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