1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | # $Id: tdMoveMedium1.py 98651 2023-02-20 13:10:54Z vboxsync $
|
---|
4 |
|
---|
5 | """
|
---|
6 | VirtualBox Validation Kit - Medium Move Test #1
|
---|
7 | """
|
---|
8 |
|
---|
9 | __copyright__ = \
|
---|
10 | """
|
---|
11 | Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
12 |
|
---|
13 | This file is part of VirtualBox base platform packages, as
|
---|
14 | available from https://www.virtualbox.org.
|
---|
15 |
|
---|
16 | This program is free software; you can redistribute it and/or
|
---|
17 | modify it under the terms of the GNU General Public License
|
---|
18 | as published by the Free Software Foundation, in version 3 of the
|
---|
19 | License.
|
---|
20 |
|
---|
21 | This program is distributed in the hope that it will be useful, but
|
---|
22 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
24 | General Public License for more details.
|
---|
25 |
|
---|
26 | You should have received a copy of the GNU General Public License
|
---|
27 | along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
28 |
|
---|
29 | The contents of this file may alternatively be used under the terms
|
---|
30 | of the Common Development and Distribution License Version 1.0
|
---|
31 | (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
32 | in the VirtualBox distribution, in which case the provisions of the
|
---|
33 | CDDL are applicable instead of those of the GPL.
|
---|
34 |
|
---|
35 | You may elect to license modified versions of this file under the
|
---|
36 | terms and conditions of either the GPL or the CDDL or both.
|
---|
37 |
|
---|
38 | SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
39 | """
|
---|
40 | __version__ = "$Revision: 98651 $"
|
---|
41 |
|
---|
42 |
|
---|
43 | # Standard Python imports.
|
---|
44 | import os
|
---|
45 | import sys
|
---|
46 |
|
---|
47 | # Only the main script needs to modify the path.
|
---|
48 | try: __file__ # pylint: disable=used-before-assignment
|
---|
49 | except: __file__ = sys.argv[0]
|
---|
50 | g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
---|
51 | sys.path.append(g_ksValidationKitDir)
|
---|
52 |
|
---|
53 | # Validation Kit imports.
|
---|
54 | from testdriver import base
|
---|
55 | from testdriver import reporter
|
---|
56 | from testdriver import vboxcon
|
---|
57 | from testdriver import vboxwrappers
|
---|
58 |
|
---|
59 |
|
---|
60 | class SubTstDrvMoveMedium1(base.SubTestDriverBase):
|
---|
61 | """
|
---|
62 | Sub-test driver for Medium Move Test #1.
|
---|
63 | """
|
---|
64 |
|
---|
65 | def __init__(self, oTstDrv):
|
---|
66 | base.SubTestDriverBase.__init__(self, oTstDrv, 'move-medium', 'Move Medium');
|
---|
67 |
|
---|
68 | def testIt(self):
|
---|
69 | """
|
---|
70 | Execute the sub-testcase.
|
---|
71 | """
|
---|
72 | return self.testMediumMove()
|
---|
73 |
|
---|
74 | #
|
---|
75 | # Test execution helpers.
|
---|
76 | #
|
---|
77 |
|
---|
78 | def moveTo(self, sLocation, aoMediumAttachments):
|
---|
79 | for oAttachment in aoMediumAttachments:
|
---|
80 | try:
|
---|
81 | oMedium = oAttachment.medium
|
---|
82 | reporter.log('Move medium "%s" to "%s"' % (oMedium.name, sLocation,))
|
---|
83 | except:
|
---|
84 | reporter.errorXcpt('failed to get the medium from the IMediumAttachment "%s"' % (oAttachment))
|
---|
85 |
|
---|
86 | if self.oTstDrv.fpApiVer >= 5.3 and self.oTstDrv.uRevision > 124748:
|
---|
87 | try:
|
---|
88 | oProgress = vboxwrappers.ProgressWrapper(oMedium.moveTo(sLocation), self.oTstDrv.oVBoxMgr, self.oTstDrv,
|
---|
89 | 'move "%s"' % (oMedium.name,));
|
---|
90 | except:
|
---|
91 | return reporter.errorXcpt('Medium::moveTo("%s") for medium "%s" failed' % (sLocation, oMedium.name,));
|
---|
92 | else:
|
---|
93 | try:
|
---|
94 | oProgress = vboxwrappers.ProgressWrapper(oMedium.setLocation(sLocation), self.oTstDrv.oVBoxMgr, self.oTstDrv,
|
---|
95 | 'move "%s"' % (oMedium.name,));
|
---|
96 | except:
|
---|
97 | return reporter.errorXcpt('Medium::setLocation("%s") for medium "%s" failed' % (sLocation, oMedium.name,));
|
---|
98 |
|
---|
99 |
|
---|
100 | oProgress.wait()
|
---|
101 | if oProgress.logResult() is False:
|
---|
102 | return False
|
---|
103 | return True
|
---|
104 |
|
---|
105 | def checkLocation(self, sLocation, aoMediumAttachments, asFiles):
|
---|
106 | fRc = True
|
---|
107 | for oAttachment in aoMediumAttachments:
|
---|
108 | sFilePath = os.path.join(sLocation, asFiles[oAttachment.port])
|
---|
109 | sActualFilePath = oAttachment.medium.location
|
---|
110 | if os.path.abspath(sFilePath) != os.path.abspath(sActualFilePath):
|
---|
111 | reporter.log('medium location expected to be "%s" but is "%s"' % (sFilePath, sActualFilePath))
|
---|
112 | fRc = False;
|
---|
113 | if not os.path.exists(sFilePath):
|
---|
114 | reporter.log('medium file does not exist at "%s"' % (sFilePath,))
|
---|
115 | fRc = False;
|
---|
116 | return fRc
|
---|
117 |
|
---|
118 | def testMediumMove(self):
|
---|
119 | """
|
---|
120 | Test medium moving.
|
---|
121 | """
|
---|
122 | reporter.testStart('medium moving')
|
---|
123 |
|
---|
124 | try: ## @todo r=bird: Bad 'ing style.
|
---|
125 | oVM = self.oTstDrv.createTestVM('test-medium-move', 1, None, 4)
|
---|
126 | assert oVM is not None
|
---|
127 |
|
---|
128 | # create hard disk images, one for each file-based backend, using the first applicable extension
|
---|
129 | fRc = True
|
---|
130 | oSession = self.oTstDrv.openSession(oVM)
|
---|
131 | aoDskFmts = self.oTstDrv.oVBoxMgr.getArray(self.oTstDrv.oVBox.systemProperties, 'mediumFormats')
|
---|
132 | asFiles = []
|
---|
133 | for oDskFmt in aoDskFmts:
|
---|
134 | aoDskFmtCaps = self.oTstDrv.oVBoxMgr.getArray(oDskFmt, 'capabilities')
|
---|
135 | if vboxcon.MediumFormatCapabilities_File not in aoDskFmtCaps \
|
---|
136 | or vboxcon.MediumFormatCapabilities_CreateDynamic not in aoDskFmtCaps:
|
---|
137 | continue
|
---|
138 | (asExts, aTypes) = oDskFmt.describeFileExtensions()
|
---|
139 | for i in range(0, len(asExts)): #pylint: disable=consider-using-enumerate
|
---|
140 | if aTypes[i] is vboxcon.DeviceType_HardDisk:
|
---|
141 | sExt = '.' + asExts[i]
|
---|
142 | break
|
---|
143 | if sExt is None:
|
---|
144 | fRc = False
|
---|
145 | break
|
---|
146 | sFile = 'Test' + str(len(asFiles)) + sExt
|
---|
147 | sHddPath = os.path.join(self.oTstDrv.sScratchPath, sFile)
|
---|
148 | oHd = oSession.createBaseHd(sHddPath, sFmt=oDskFmt.id, cb=1024*1024)
|
---|
149 | if oHd is None:
|
---|
150 | fRc = False
|
---|
151 | break
|
---|
152 |
|
---|
153 | # attach HDD, IDE controller exists by default, but we use SATA just in case
|
---|
154 | sController='SATA Controller'
|
---|
155 | fRc = fRc and oSession.attachHd(sHddPath, sController, iPort = len(asFiles),
|
---|
156 | fImmutable=False, fForceResource=False)
|
---|
157 | if fRc:
|
---|
158 | asFiles.append(sFile)
|
---|
159 |
|
---|
160 | fRc = fRc and oSession.saveSettings()
|
---|
161 |
|
---|
162 | #create temporary subdirectory in the current working directory
|
---|
163 | sOrigLoc = self.oTstDrv.sScratchPath
|
---|
164 | sNewLoc = os.path.join(sOrigLoc, 'newLocation')
|
---|
165 | os.mkdir(sNewLoc, 0o775)
|
---|
166 |
|
---|
167 | aoMediumAttachments = oVM.getMediumAttachmentsOfController(sController)
|
---|
168 | #case 1. Only path without file name, with trailing separator
|
---|
169 | fRc = self.moveTo(sNewLoc + os.sep, aoMediumAttachments) and fRc
|
---|
170 | fRc = self.checkLocation(sNewLoc, aoMediumAttachments, asFiles) and fRc
|
---|
171 |
|
---|
172 | #case 2. Only path without file name, without trailing separator
|
---|
173 | fRc = self.moveTo(sOrigLoc, aoMediumAttachments) and fRc
|
---|
174 | fRc = self.checkLocation(sOrigLoc, aoMediumAttachments, asFiles) and fRc
|
---|
175 |
|
---|
176 | #case 3. Path with file name
|
---|
177 | #The case supposes that user has passed a destination path with a file name but hasn't added an extension/suffix
|
---|
178 | #to this destination file. User supposes that the extension would be added automatically and to be the same as
|
---|
179 | #for the original file. Difficult case, apparently this case should follow mv(1) logic
|
---|
180 | #and the file name is processed as folder name (aka mv(1) logic).
|
---|
181 | #Be discussed.
|
---|
182 | fRc = self.moveTo(os.path.join(sNewLoc, 'newName'), aoMediumAttachments) and fRc
|
---|
183 | asNewFiles = ['newName' + os.path.splitext(s)[1] for s in asFiles]
|
---|
184 | fRc = self.checkLocation(os.path.join(sNewLoc, 'newName'), aoMediumAttachments, asFiles) and fRc
|
---|
185 |
|
---|
186 | #after the case the destination path must be corrected
|
---|
187 | sNewLoc = os.path.join(sNewLoc, 'newName')
|
---|
188 |
|
---|
189 | #case 4. Only file name
|
---|
190 | fRc = self.moveTo('onlyMediumName', aoMediumAttachments) and fRc
|
---|
191 | asNewFiles = ['onlyMediumName' + os.path.splitext(s)[1] for s in asFiles]
|
---|
192 | if self.oTstDrv.fpApiVer >= 5.3:
|
---|
193 | fRc = self.checkLocation(sNewLoc, aoMediumAttachments, asNewFiles) and fRc
|
---|
194 | else:
|
---|
195 | fRc = self.checkLocation(sNewLoc, aoMediumAttachments,
|
---|
196 | [s.replace('.hdd', '.parallels') for s in asNewFiles]) and fRc
|
---|
197 |
|
---|
198 | #case 5. Move all files from a snapshot
|
---|
199 | fRc = fRc and oSession.takeSnapshot('Snapshot1')
|
---|
200 | if fRc:
|
---|
201 | aoMediumAttachments = oVM.getMediumAttachmentsOfController(sController)
|
---|
202 | asSnapFiles = [os.path.basename(o.medium.name) for o in aoMediumAttachments]
|
---|
203 | fRc = self.moveTo(sOrigLoc, aoMediumAttachments) and fRc
|
---|
204 | fRc = self.checkLocation(sOrigLoc, aoMediumAttachments, asSnapFiles) and fRc
|
---|
205 |
|
---|
206 | fRc = oSession.close() and fRc
|
---|
207 | except:
|
---|
208 | reporter.errorXcpt()
|
---|
209 |
|
---|
210 | return reporter.testDone()[1] == 0
|
---|
211 |
|
---|
212 |
|
---|
213 | if __name__ == '__main__':
|
---|
214 | sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
---|
215 | from tdApi1 import tdApi1; # pylint: disable=relative-import
|
---|
216 | sys.exit(tdApi1([SubTstDrvMoveMedium1]).main(sys.argv))
|
---|
217 |
|
---|