1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | # $Id: tdMoveMedium1.py 71099 2018-02-22 10:07:34Z vboxsync $
|
---|
4 |
|
---|
5 | """
|
---|
6 | VirtualBox Validation Kit - Medium Move Test #1
|
---|
7 | """
|
---|
8 |
|
---|
9 | __copyright__ = \
|
---|
10 | """
|
---|
11 | Copyright (C) 2010-2017 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: 71099 $"
|
---|
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, 'move-medium', oTstDrv)
|
---|
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 setLocation(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 | try:
|
---|
77 | oProgress = vboxwrappers.ProgressWrapper(oMedium.setLocation(sLocation), self.oTstDrv.oVBoxMgr, self.oTstDrv,
|
---|
78 | 'move "%s"' % (oMedium.name,))
|
---|
79 | except:
|
---|
80 | return reporter.errorXcpt('Medium::setLocation("%s") for medium "%s" failed' % (sLocation, oMedium.name,))
|
---|
81 |
|
---|
82 | oProgress.wait()
|
---|
83 | if oProgress.logResult() is False:
|
---|
84 | return False
|
---|
85 | return True
|
---|
86 |
|
---|
87 | def checkLocation(self, sLocation, aoMediumAttachments, asFiles):
|
---|
88 | fRc = True
|
---|
89 | for oAttachment in aoMediumAttachments:
|
---|
90 | sFilePath = os.path.join(sLocation, asFiles[oAttachment.port])
|
---|
91 | sActualFilePath = oAttachment.medium.location
|
---|
92 | if os.path.abspath(sFilePath) != os.path.abspath(sActualFilePath):
|
---|
93 | reporter.log('medium location expected to be "%s" but is "%s"' % (sFilePath, sActualFilePath))
|
---|
94 | fRc = False;
|
---|
95 | if not os.path.exists(sFilePath):
|
---|
96 | reporter.log('medium file does not exist at "%s"' % (sFilePath,))
|
---|
97 | fRc = False;
|
---|
98 | return fRc
|
---|
99 |
|
---|
100 | def testMediumMove(self):
|
---|
101 | """
|
---|
102 | Test medium moving.
|
---|
103 | """
|
---|
104 | reporter.testStart('medium moving')
|
---|
105 |
|
---|
106 | try:
|
---|
107 | oVM = self.oTstDrv.createTestVM('test-medium-move', 1, None, 4)
|
---|
108 | assert oVM is not None
|
---|
109 |
|
---|
110 | # create hard disk images, one for each file-based backend, using the first applicable extension
|
---|
111 | fRc = True
|
---|
112 | oSession = self.oTstDrv.openSession(oVM)
|
---|
113 | aoDskFmts = self.oTstDrv.oVBoxMgr.getArray(self.oTstDrv.oVBox.systemProperties, 'mediumFormats')
|
---|
114 | asFiles = []
|
---|
115 | for oDskFmt in aoDskFmts:
|
---|
116 | aoDskFmtCaps = self.oTstDrv.oVBoxMgr.getArray(oDskFmt, 'capabilities')
|
---|
117 | if vboxcon.MediumFormatCapabilities_File not in aoDskFmtCaps \
|
---|
118 | or vboxcon.MediumFormatCapabilities_CreateDynamic not in aoDskFmtCaps:
|
---|
119 | continue
|
---|
120 | (asExts, aTypes) = oDskFmt.describeFileExtensions()
|
---|
121 | for i in range(0, len(asExts)):
|
---|
122 | if aTypes[i] is vboxcon.DeviceType_HardDisk:
|
---|
123 | sExt = '.' + asExts[i]
|
---|
124 | break
|
---|
125 | if sExt is None:
|
---|
126 | fRc = False
|
---|
127 | break
|
---|
128 | sFile = 'Test' + str(len(asFiles)) + sExt
|
---|
129 | sHddPath = os.path.join(self.oTstDrv.sScratchPath, sFile)
|
---|
130 | oHd = oSession.createBaseHd(sHddPath, sFmt=oDskFmt.id, cb=1024*1024)
|
---|
131 | if oHd is None:
|
---|
132 | fRc = False
|
---|
133 | break
|
---|
134 |
|
---|
135 | # attach HDD, IDE controller exists by default, but we use SATA just in case
|
---|
136 | sController='SATA Controller'
|
---|
137 | fRc = fRc and oSession.attachHd(sHddPath, sController, iPort = len(asFiles),
|
---|
138 | fImmutable=False, fForceResource=False)
|
---|
139 | if fRc:
|
---|
140 | asFiles.append(sFile)
|
---|
141 |
|
---|
142 | fRc = fRc and oSession.saveSettings()
|
---|
143 |
|
---|
144 | #create temporary subdirectory in the current working directory
|
---|
145 | sOrigLoc = self.oTstDrv.sScratchPath
|
---|
146 | sNewLoc = os.path.join(sOrigLoc, 'newLocation')
|
---|
147 | os.mkdir(sNewLoc, 0o775)
|
---|
148 |
|
---|
149 | aoMediumAttachments = oVM.getMediumAttachmentsOfController(sController)
|
---|
150 | #case 1. Only path without file name, with trailing separator
|
---|
151 | fRc = self.setLocation(sNewLoc + os.sep, aoMediumAttachments) and fRc
|
---|
152 | fRc = self.checkLocation(sNewLoc, aoMediumAttachments, asFiles) and fRc
|
---|
153 |
|
---|
154 | #case 2. Only path without file name, without trailing separator
|
---|
155 | fRc = self.setLocation(sOrigLoc, aoMediumAttachments) and fRc
|
---|
156 | fRc = self.checkLocation(sOrigLoc, aoMediumAttachments, asFiles) and fRc
|
---|
157 |
|
---|
158 | #case 3. Path with file name
|
---|
159 | #The case supposes that user has passed a destination path with a file name but hasn't added an extension/suffix
|
---|
160 | #to this destination file. User supposes that the extension would be added automatically and to be the same as
|
---|
161 | #for the original file. Difficult case, apparently this case should follow mv(1) logic
|
---|
162 | #and the file name is processed as folder name (aka mv(1) logic).
|
---|
163 | #Be discussed.
|
---|
164 | fRc = self.setLocation(os.path.join(sNewLoc, 'newName'), aoMediumAttachments) and fRc
|
---|
165 | asNewFiles = ['newName' + os.path.splitext(s)[1] for s in asFiles]
|
---|
166 | fRc = self.checkLocation(os.path.join(sNewLoc, 'newName'), aoMediumAttachments, asFiles) and fRc
|
---|
167 |
|
---|
168 | #after the case the destination path must be corrected
|
---|
169 | sNewLoc = os.path.join(sNewLoc, 'newName')
|
---|
170 |
|
---|
171 | #case 4. Only file name
|
---|
172 | fRc = self.setLocation('onlyMediumName', aoMediumAttachments) and fRc
|
---|
173 | asNewFiles = ['onlyMediumName' + os.path.splitext(s)[1] for s in asFiles]
|
---|
174 | fRc = self.checkLocation(sNewLoc, aoMediumAttachments, asNewFiles) and fRc
|
---|
175 |
|
---|
176 | #case 5. Move all files from a snapshot
|
---|
177 | fRc = fRc and oSession.takeSnapshot('Snapshot1')
|
---|
178 | if fRc:
|
---|
179 | aoMediumAttachments = oVM.getMediumAttachmentsOfController(sController)
|
---|
180 | asSnapFiles = [os.path.basename(o.medium.name) for o in aoMediumAttachments]
|
---|
181 | fRc = self.setLocation(sOrigLoc, aoMediumAttachments) and fRc
|
---|
182 | fRc = self.checkLocation(sOrigLoc, aoMediumAttachments, asSnapFiles) and fRc
|
---|
183 |
|
---|
184 | fRc = oSession.close() and fRc
|
---|
185 |
|
---|
186 | assert fRc is True
|
---|
187 | except:
|
---|
188 | reporter.errorXcpt()
|
---|
189 |
|
---|
190 | return reporter.testDone()[1] == 0
|
---|
191 |
|
---|
192 |
|
---|
193 | if __name__ == '__main__':
|
---|
194 | sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
---|
195 | from tdApi1 import tdApi1
|
---|
196 | sys.exit(tdApi1([SubTstDrvMoveMedium1]).main(sys.argv))
|
---|
197 |
|
---|