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