VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/tests/api/tdMoveMedium1.py@ 71099

Last change on this file since 71099 was 71099, checked in by vboxsync, 7 years ago

bugref:8344. Trailing whitespace was removed.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Id Revision
File size: 8.1 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# $Id: tdMoveMedium1.py 71099 2018-02-22 10:07:34Z vboxsync $
4
5"""
6VirtualBox Validation Kit - Medium Move Test #1
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2010-2017 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: 71099 $"
31
32
33# Standard Python imports.
34import os
35import sys
36
37# Only the main script needs to modify the path.
38try: __file__
39except: __file__ = sys.argv[0]
40g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
41sys.path.append(g_ksValidationKitDir)
42
43# Validation Kit imports.
44from testdriver import base
45from testdriver import reporter
46from testdriver import vboxcon
47from testdriver import vboxwrappers
48
49
50class 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
193if __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
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