VirtualBox

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

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

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