PageRenderTime 2594ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/nova/tests/virt/libvirt/test_utils.py

https://github.com/AsherBond/nova
Python | 301 lines | 281 code | 6 blank | 14 comment | 0 complexity | 3a9ef2196f8241c5aab0813c77d2dd44 MD5 | raw file
  1. # Copyright 2012 NTT Data. All Rights Reserved.
  2. # Copyright 2012 Yahoo! Inc. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. import functools
  16. import os
  17. import mock
  18. from oslo.config import cfg
  19. from nova.openstack.common import processutils
  20. from nova import test
  21. from nova import utils
  22. from nova.virt import images
  23. from nova.virt.libvirt import utils as libvirt_utils
  24. CONF = cfg.CONF
  25. class LibvirtUtilsTestCase(test.NoDBTestCase):
  26. def test_get_disk_type(self):
  27. path = "disk.config"
  28. example_output = """image: disk.config
  29. file format: raw
  30. virtual size: 64M (67108864 bytes)
  31. cluster_size: 65536
  32. disk size: 96K
  33. blah BLAH: bb
  34. """
  35. self.mox.StubOutWithMock(os.path, 'exists')
  36. self.mox.StubOutWithMock(utils, 'execute')
  37. os.path.exists(path).AndReturn(True)
  38. utils.execute('env', 'LC_ALL=C', 'LANG=C',
  39. 'qemu-img', 'info', path).AndReturn((example_output, ''))
  40. self.mox.ReplayAll()
  41. disk_type = libvirt_utils.get_disk_type(path)
  42. self.assertEqual(disk_type, 'raw')
  43. @mock.patch('nova.utils.execute')
  44. def test_copy_image_local_cp(self, mock_execute):
  45. libvirt_utils.copy_image('src', 'dest')
  46. mock_execute.assert_called_once_with('cp', 'src', 'dest')
  47. _rsync_call = functools.partial(mock.call,
  48. 'rsync', '--sparse', '--compress')
  49. @mock.patch('nova.utils.execute')
  50. def test_copy_image_rsync(self, mock_execute):
  51. libvirt_utils.copy_image('src', 'dest', host='host')
  52. mock_execute.assert_has_calls([
  53. self._rsync_call('--dry-run', 'src', 'host:dest'),
  54. self._rsync_call('src', 'host:dest'),
  55. ])
  56. self.assertEqual(2, mock_execute.call_count)
  57. @mock.patch('nova.utils.execute')
  58. def test_copy_image_scp(self, mock_execute):
  59. mock_execute.side_effect = [
  60. processutils.ProcessExecutionError,
  61. mock.DEFAULT,
  62. ]
  63. libvirt_utils.copy_image('src', 'dest', host='host')
  64. mock_execute.assert_has_calls([
  65. self._rsync_call('--dry-run', 'src', 'host:dest'),
  66. mock.call('scp', 'src', 'host:dest'),
  67. ])
  68. self.assertEqual(2, mock_execute.call_count)
  69. class ImageUtilsTestCase(test.NoDBTestCase):
  70. def test_disk_type(self):
  71. # Seems like lvm detection
  72. # if its in /dev ??
  73. for p in ['/dev/b', '/dev/blah/blah']:
  74. d_type = libvirt_utils.get_disk_type(p)
  75. self.assertEqual('lvm', d_type)
  76. # Try rbd detection
  77. d_type = libvirt_utils.get_disk_type('rbd:pool/instance')
  78. self.assertEqual('rbd', d_type)
  79. # Try the other types
  80. template_output = """image: %(path)s
  81. file format: %(format)s
  82. virtual size: 64M (67108864 bytes)
  83. cluster_size: 65536
  84. disk size: 96K
  85. """
  86. path = '/myhome/disk.config'
  87. for f in ['raw', 'qcow2']:
  88. output = template_output % ({
  89. 'format': f,
  90. 'path': path,
  91. })
  92. self.mox.StubOutWithMock(os.path, 'exists')
  93. self.mox.StubOutWithMock(utils, 'execute')
  94. os.path.exists(path).AndReturn(True)
  95. utils.execute('env', 'LC_ALL=C', 'LANG=C',
  96. 'qemu-img', 'info', path).AndReturn((output, ''))
  97. self.mox.ReplayAll()
  98. d_type = libvirt_utils.get_disk_type(path)
  99. self.assertEqual(f, d_type)
  100. self.mox.UnsetStubs()
  101. def test_disk_backing(self):
  102. path = '/myhome/disk.config'
  103. template_output = """image: %(path)s
  104. file format: raw
  105. virtual size: 2K (2048 bytes)
  106. cluster_size: 65536
  107. disk size: 96K
  108. """
  109. output = template_output % ({
  110. 'path': path,
  111. })
  112. self.mox.StubOutWithMock(os.path, 'exists')
  113. self.mox.StubOutWithMock(utils, 'execute')
  114. os.path.exists(path).AndReturn(True)
  115. utils.execute('env', 'LC_ALL=C', 'LANG=C',
  116. 'qemu-img', 'info', path).AndReturn((output, ''))
  117. self.mox.ReplayAll()
  118. d_backing = libvirt_utils.get_disk_backing_file(path)
  119. self.assertIsNone(d_backing)
  120. def test_disk_size(self):
  121. path = '/myhome/disk.config'
  122. template_output = """image: %(path)s
  123. file format: raw
  124. virtual size: %(v_size)s (%(vsize_b)s bytes)
  125. cluster_size: 65536
  126. disk size: 96K
  127. """
  128. for i in range(0, 128):
  129. bytes = i * 65336
  130. kbytes = bytes / 1024
  131. mbytes = kbytes / 1024
  132. output = template_output % ({
  133. 'v_size': "%sM" % (mbytes),
  134. 'vsize_b': i,
  135. 'path': path,
  136. })
  137. self.mox.StubOutWithMock(os.path, 'exists')
  138. self.mox.StubOutWithMock(utils, 'execute')
  139. os.path.exists(path).AndReturn(True)
  140. utils.execute('env', 'LC_ALL=C', 'LANG=C',
  141. 'qemu-img', 'info', path).AndReturn((output, ''))
  142. self.mox.ReplayAll()
  143. d_size = libvirt_utils.get_disk_size(path)
  144. self.assertEqual(i, d_size)
  145. self.mox.UnsetStubs()
  146. output = template_output % ({
  147. 'v_size': "%sK" % (kbytes),
  148. 'vsize_b': i,
  149. 'path': path,
  150. })
  151. self.mox.StubOutWithMock(os.path, 'exists')
  152. self.mox.StubOutWithMock(utils, 'execute')
  153. os.path.exists(path).AndReturn(True)
  154. utils.execute('env', 'LC_ALL=C', 'LANG=C',
  155. 'qemu-img', 'info', path).AndReturn((output, ''))
  156. self.mox.ReplayAll()
  157. d_size = libvirt_utils.get_disk_size(path)
  158. self.assertEqual(i, d_size)
  159. self.mox.UnsetStubs()
  160. def test_qemu_info_canon(self):
  161. path = "disk.config"
  162. example_output = """image: disk.config
  163. file format: raw
  164. virtual size: 64M (67108864 bytes)
  165. cluster_size: 65536
  166. disk size: 96K
  167. blah BLAH: bb
  168. """
  169. self.mox.StubOutWithMock(os.path, 'exists')
  170. self.mox.StubOutWithMock(utils, 'execute')
  171. os.path.exists(path).AndReturn(True)
  172. utils.execute('env', 'LC_ALL=C', 'LANG=C',
  173. 'qemu-img', 'info', path).AndReturn((example_output, ''))
  174. self.mox.ReplayAll()
  175. image_info = images.qemu_img_info(path)
  176. self.assertEqual('disk.config', image_info.image)
  177. self.assertEqual('raw', image_info.file_format)
  178. self.assertEqual(67108864, image_info.virtual_size)
  179. self.assertEqual(98304, image_info.disk_size)
  180. self.assertEqual(65536, image_info.cluster_size)
  181. def test_qemu_info_canon2(self):
  182. path = "disk.config"
  183. example_output = """image: disk.config
  184. file format: QCOW2
  185. virtual size: 67108844
  186. cluster_size: 65536
  187. disk size: 963434
  188. backing file: /var/lib/nova/a328c7998805951a_2
  189. """
  190. self.mox.StubOutWithMock(os.path, 'exists')
  191. self.mox.StubOutWithMock(utils, 'execute')
  192. os.path.exists(path).AndReturn(True)
  193. utils.execute('env', 'LC_ALL=C', 'LANG=C',
  194. 'qemu-img', 'info', path).AndReturn((example_output, ''))
  195. self.mox.ReplayAll()
  196. image_info = images.qemu_img_info(path)
  197. self.assertEqual('disk.config', image_info.image)
  198. self.assertEqual('qcow2', image_info.file_format)
  199. self.assertEqual(67108844, image_info.virtual_size)
  200. self.assertEqual(963434, image_info.disk_size)
  201. self.assertEqual(65536, image_info.cluster_size)
  202. self.assertEqual('/var/lib/nova/a328c7998805951a_2',
  203. image_info.backing_file)
  204. def test_qemu_backing_file_actual(self):
  205. path = "disk.config"
  206. example_output = """image: disk.config
  207. file format: raw
  208. virtual size: 64M (67108864 bytes)
  209. cluster_size: 65536
  210. disk size: 96K
  211. Snapshot list:
  212. ID TAG VM SIZE DATE VM CLOCK
  213. 1 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
  214. backing file: /var/lib/nova/a328c7998805951a_2 (actual path: /b/3a988059e51a_2)
  215. """
  216. self.mox.StubOutWithMock(os.path, 'exists')
  217. self.mox.StubOutWithMock(utils, 'execute')
  218. os.path.exists(path).AndReturn(True)
  219. utils.execute('env', 'LC_ALL=C', 'LANG=C',
  220. 'qemu-img', 'info', path).AndReturn((example_output, ''))
  221. self.mox.ReplayAll()
  222. image_info = images.qemu_img_info(path)
  223. self.assertEqual('disk.config', image_info.image)
  224. self.assertEqual('raw', image_info.file_format)
  225. self.assertEqual(67108864, image_info.virtual_size)
  226. self.assertEqual(98304, image_info.disk_size)
  227. self.assertEqual(1, len(image_info.snapshots))
  228. self.assertEqual('/b/3a988059e51a_2',
  229. image_info.backing_file)
  230. def test_qemu_info_convert(self):
  231. path = "disk.config"
  232. example_output = """image: disk.config
  233. file format: raw
  234. virtual size: 64M
  235. disk size: 96K
  236. Snapshot list:
  237. ID TAG VM SIZE DATE VM CLOCK
  238. 1 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
  239. 3 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
  240. 4 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
  241. junk stuff: bbb
  242. """
  243. self.mox.StubOutWithMock(os.path, 'exists')
  244. self.mox.StubOutWithMock(utils, 'execute')
  245. os.path.exists(path).AndReturn(True)
  246. utils.execute('env', 'LC_ALL=C', 'LANG=C',
  247. 'qemu-img', 'info', path).AndReturn((example_output, ''))
  248. self.mox.ReplayAll()
  249. image_info = images.qemu_img_info(path)
  250. self.assertEqual('disk.config', image_info.image)
  251. self.assertEqual('raw', image_info.file_format)
  252. self.assertEqual(67108864, image_info.virtual_size)
  253. self.assertEqual(98304, image_info.disk_size)
  254. def test_qemu_info_snaps(self):
  255. path = "disk.config"
  256. example_output = """image: disk.config
  257. file format: raw
  258. virtual size: 64M (67108864 bytes)
  259. disk size: 96K
  260. Snapshot list:
  261. ID TAG VM SIZE DATE VM CLOCK
  262. 1 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
  263. 3 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
  264. 4 d9a9784a500742a7bb95627bb3aace38 0 2012-08-20 10:52:46 00:00:00.000
  265. """
  266. self.mox.StubOutWithMock(os.path, 'exists')
  267. self.mox.StubOutWithMock(utils, 'execute')
  268. os.path.exists(path).AndReturn(True)
  269. utils.execute('env', 'LC_ALL=C', 'LANG=C',
  270. 'qemu-img', 'info', path).AndReturn((example_output, ''))
  271. self.mox.ReplayAll()
  272. image_info = images.qemu_img_info(path)
  273. self.assertEqual('disk.config', image_info.image)
  274. self.assertEqual('raw', image_info.file_format)
  275. self.assertEqual(67108864, image_info.virtual_size)
  276. self.assertEqual(98304, image_info.disk_size)
  277. self.assertEqual(3, len(image_info.snapshots))