1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import os
19 import stat
20 import tempfile
21 import unittest
22 import testtools
23
24 import VMBuilder
25 from VMBuilder.disk import detect_size, parse_size, index_to_devname, devname_to_index, Disk
26 from VMBuilder.exception import VMBuilderException, VMBuilderUserError
27 from VMBuilder.util import run_cmd
28
29 TestSkipped = testtools.testcase.TestSkipped
30 TestCase = testtools.TestCase
33 (fd, tmpfile) = tempfile.mkstemp()
34 os.close(fd)
35 return tmpfile
36
40
53
56 "Suffixes in size strings are case-insensitive"
57
58 for letter in ['K', 'M', 'G']:
59 self.assertEqual(parse_size('1%s' % letter), parse_size('1%s' % letter.lower()))
60
62 "Suffix-less size string are counted as megabytes"
63 self.assertEqual(parse_size(10), 10)
64 self.assertEqual(parse_size('10'), 10)
65
67 "Sizes with M suffix are counted as megabytes"
68 self.assertEqual(parse_size('10M'), 10)
69
71 "1G is counted as 1024 megabytes"
72 self.assertEqual(parse_size('1G'), 1024)
73
75 "1024K is counted as 1 megabyte"
76 self.assertEqual(parse_size('1024K'), 1)
77
79 "parse_size rounds to nearest MB"
80 self.assertEqual(parse_size('1025K'), 1)
81 self.assertEqual(parse_size('10250K'), 10)
82
101
108
111
112 @testtools.skipIf(os.geteuid() != 0, 'Needs root to run')
114 self.imgdev = run_cmd('losetup', '-f', '--show', self.tmpfile).strip()
115 self.assertTrue(detect_size(self.imgdev), 5*1024)
116
121
123 TestCase.tearDown(self)
124 run_cmd('udevadm', 'settle')
125 if self.imgdev:
126 run_cmd('losetup', '-d', self.imgdev)
127 os.unlink(self.tmpfile)
128
138
140
141 K = 1024
142 M = K*1024
143 G = M*1024
144 sizes = [('10G', 10*G),
145 ('400M', 400*M),
146 ('345', 345*M),
147 ('10240k', 10*M),
148 ('10250k', 10*M),
149 ('10230k', 9*M)]
150
151 for (sizestr, size) in sizes:
152 tmpfile = get_temp_filename()
153 os.unlink(tmpfile)
154
155 disk = Disk(MockHypervisor(), filename=tmpfile, size=sizestr)
156 disk.create()
157 actual_size = os.stat(tmpfile)[stat.ST_SIZE]
158 self.assertEqual(size, actual_size, 'Asked for %s, expected %d, got %d' % (sizestr, size, actual_size))
159 os.unlink(tmpfile)
160
166
171
173 tmpfile = get_temp_filename()
174
175 fp = open(tmpfile, 'w')
176 fp.write('canary')
177 fp.close()
178
179 disk = Disk(MockHypervisor(), tmpfile)
180 disk.create()
181 fp = open(tmpfile, 'r')
182 self.assertEqual(fp.read(), 'canary')
183 fp.close()
184 os.unlink(tmpfile)
185
195
205
209
213
216
218 from VMBuilder.util import run_cmd
219
220 file_output = run_cmd('file', self.tmpfile)
221 self.assertEqual('%s: data' % self.tmpfile, file_output.strip())
222 self.disk.partition()
223 file_output = run_cmd('file', self.tmpfile)
224 self.assertEqual('%s: x86 boot sector, code offset 0xb8' % self.tmpfile, file_output.strip())
225
226 file_output = run_cmd('parted', '--script', self.tmpfile, 'print')
227 self.assertEqual('''Model: (file)
228 Disk %s: 1074MB
229 Sector size (logical/physical): 512B/512B
230 Partition Table: msdos
231
232 Number Start End Size Type File system Flags''' % self.tmpfile, file_output.strip())
233
235 from VMBuilder.util import run_cmd
236
237 self.disk.add_part(1, 1023, 'ext3', '/')
238 self.disk.partition()
239 file_output = run_cmd('parted', '--script', self.tmpfile, 'print')
240 self.assertEqual('''Model: (file)
241 Disk %s: 1074MB
242 Sector size (logical/physical): 512B/512B
243 Partition Table: msdos
244
245 Number Start End Size Type File system Flags
246 1 1049kB 1023MB 1022MB primary''' % self.tmpfile, file_output.strip())
247
248 @testtools.skipIf(os.geteuid() != 0, 'Needs root to run')
260
261 @testtools.skipIf(os.geteuid() != 0, 'Needs root to run')
272
281
290