def test_checksum_mismatch(self, _hom, hashlib_mock, open_mock, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.image1] md5 = mock.MagicMock() md5.hexdigest.return_value = '1234' hashlib_mock.md5.return_value = md5 os_mock.path.exists.side_effect = [True, False, False] os_mock.path.isfile.return_value = True os_mock.path.getmtime.return_value = 0 resources = Resources('foo', self.config) open_mock.reset_mock() with self.assertRaisesRegex(RuntimeError, 'Checksum failure. File: '): _ = resources.provider_image glance_mock.images.data.assert_called_with('1') self.assertEqual([ mock.call('/foo/bar.part', 'wb'), mock.call('/foo/bar.part', 'rb') ], [ call for call in open_mock.call_args_list if call == mock.call('/foo/bar.part', 'wb') or call == mock.call('/foo/bar.part', 'rb') ]) os_mock.remove.assert_called_once_with('/foo/bar.part')
def test_checksum_mismatch(self, hashlib_mock, open_mock, temp_mock, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.image1] md5 = mock.MagicMock() md5.hexdigest.return_value = '1234' hashlib_mock.md5.return_value = md5 os_mock.path.exists.return_value = True os_mock.path.isfile.return_value = True os_mock.path.isdir.return_value = False os_mock.path.getmtime.return_value = 0 temp_mock.mkstemp.return_value = (0, 'tempfile') resources = Resources('foo', self.config) expected_image_path = self.config['disk']['image'][ 'provider_configuration']['target_path'] open_mock.reset_mock() with self.assertRaisesRegexp(RuntimeError, 'Checksum failure. File: '): assert resources.provider_image == expected_image_path glance_mock.images.data.assert_called_with('1') self.assertEqual( [mock.call('tempfile', 'wb'), mock.call('tempfile', 'rb')], [ call for call in open_mock.call_args_list if call == mock.call('tempfile', 'wb') or call == mock.call('tempfile', 'rb') ]) os_mock.remove.assert_called_once_with('tempfile')
def test_image_does_not_exist(self, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.wrongimage] os_mock.path.exists.return_value = False resources = Resources('foo', self.config) with self.assertRaises(FileNotFoundError): _ = resources.provider_image
def test_image_unavailable_target_does_not_exist(self, _hom, hashlib_mock, open_mock, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.image2] md5 = mock.MagicMock() md5.hexdigest.return_value = '2222' hashlib_mock.md5.return_value = md5 os_mock.path.join = os.path.join os_mock.path.dirname = os.path.dirname os_mock.path.exists.return_value = False os_mock.path.isfile.return_value = False os_mock.path.getmtime.return_value = 0 resources = Resources('foo', self.config) expected_image_path = self.config['disk']['image'][ 'provider_configuration']['path'] + '/2' assert resources.provider_image == expected_image_path glance_mock.images.data.assert_called_with('2') self.assertEqual([ mock.call('/foo/bar/2.part', 'wb'), mock.call('/foo/bar/2.part', 'rb') ], open_mock.call_args_list) os_mock.remove.assert_not_called()
def test_image_not_in_pool(self, os_mock, libvirt_mock): volume = mock.MagicMock() volume.path.return_value = '/nowhere/foo/bar' pool = mock.MagicMock() pool.storageVolLookupByName.return_value = volume hypervisor = mock.MagicMock() hypervisor.storagePoolDefineXML.return_value = pool import see hypervisor.storageVolLookupByPath.side_effect = libvirt.libvirtError('BOOM') libvirt_mock.return_value = hypervisor os_mock.exists.return_value = True resources = Resources('foo', self.config) expected_image_path = '%s%s' % ( self.config['disk']['image'][ 'provider_configuration']['storage_pool_path'], self.config['disk']['image']['name']) assert expected_image_path == resources.provider_image libvirt_mock.assert_called_with('baz') pool.assert_has_calls([ mock.call.setAutostart(True), mock.call.create(), mock.call.refresh(), mock.call.storageVolLookupByName(self.config['disk']['image']['name']) ])
def test_image_unavailable_target_is_file(self, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.wrongimage, self.image3] os_mock.path.exists.return_value = True os_mock.path.isfile.return_value = True resources = Resources('foo', self.config) expected_image_path = self.config['disk']['image']['provider_configuration']['target_path'] assert resources.provider_image == expected_image_path
def test_image_unavailable_target_is_dir(self, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.image3] os_mock.path.join = os.path.join os_mock.path.dirname = os.path.dirname os_mock.path.exists.side_effect = [True, False, True] os_mock.path.isfile.return_value = False resources = Resources('foo', self.config) expected_image_path = self.config['disk']['image'][ 'provider_configuration']['path'] + '/3' assert resources.provider_image == expected_image_path
def test_fresh_image_exists(self, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.image1] os_mock.path.exists.return_value = True os_mock.path.isfile.return_value = True os_mock.path.getmtime.return_value = 32503680000.0 # Jan 1st, 3000 resources = Resources('foo', self.config) expected_image_path = self.config['disk']['image']['provider_configuration']['target_path'] assert resources.provider_image == expected_image_path glance_mock.images.data.assert_not_called()
def test_image_unavailable_target_is_dir_no_cached(self, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.wrongimage, self.image3] os_mock.path.exists.side_effect = lambda x: {'/foo/bar/1': False, '/foo/bar/3': False, '/foo/bar': True}[x] os_mock.path.isfile.return_value = False os_mock.path.join = os.path.join resources = Resources('foo', self.config) with self.assertRaises(FileNotFoundError): _ = resources.provider_image
def test_image_property(self): resources = Resources( 'foo', { 'disk': { 'image': { 'name': 'bar', 'provider': 'see.image_providers.DummyProvider', 'provider_configuration': { 'path': '/foo' } } } }) assert resources.provider_image == '/foo/bar'
def test_image_in_pool(self, os_mock, libvirt_mock): volume = mock.MagicMock() volume.path.return_value = '/nowhere/foo/bar' hypervisor = mock.MagicMock() hypervisor.storageVolLookupByPath.return_value = volume libvirt_mock.return_value = hypervisor os_mock.exists.return_value = True resources = Resources('foo', self.config) expected_image_path = '%s%s' % ( self.config['disk']['image']['provider_configuration'] ['storage_pool_path'], self.config['disk']['image']['uri']) assert expected_image_path == resources.provider_image libvirt_mock.assert_called_with('baz') hypervisor.storageVolLookupByPath.assert_called_with( expected_image_path) hypervisor.storagePoolDefineXML.assert_not_called()
def test_same_name_image_is_downloading_older_does_not_exist( self, hashlib_mock, open_mock, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.image1, self.image2] md5 = mock.MagicMock() md5.hexdigest.return_value = '2222' hashlib_mock.md5.return_value = md5 os_mock.path.join = os.path.join os_mock.path.exists.side_effect = [True, True, False, False] os_mock.path.isfile.return_value = False os_mock.path.dirname.return_value = '/foo/bar' resources = Resources('foo', self.config) with self.assertRaises(FileNotFoundError): _ = resources.provider_image glance_mock.images.data.assert_not_called() open_mock.assert_not_called() os_mock.remove.assert_not_called()
def test_same_name_images_exist(self, hashlib_mock, open_mock, temp_mock, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.image1, self.image2] md5 = mock.MagicMock() md5.hexdigest.return_value = '2222' hashlib_mock.md5.return_value = md5 os_mock.path.exists.return_value = True os_mock.path.isfile.return_value = False os_mock.path.isdir.return_value = True temp_mock.mkstemp.return_value = (0, 'tempfile') resources = Resources('foo', self.config) expected_image_path = self.config['disk']['image']['provider_configuration']['target_path'] + '/2' assert resources.provider_image == expected_image_path glance_mock.images.data.assert_called_with('2') self.assertEqual([mock.call('tempfile', 'wb'), mock.call('tempfile', 'rb')], open_mock.call_args_list) os_mock.remove.assert_not_called()
def test_same_name_image_is_downloading_older_exists( self, hashlib_mock, open_mock, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.image1, self.image2] md5 = mock.MagicMock() md5.hexdigest.return_value = '2222' hashlib_mock.md5.return_value = md5 os_mock.path.join = os.path.join os_mock.path.exists.side_effect = [True, True, False, True] os_mock.path.isfile.return_value = False os_mock.path.dirname.return_value = '/foo/bar' resources = Resources('foo', self.config) expected_image_path = self.config['disk']['image'][ 'provider_configuration']['path'] + '/1' assert resources.provider_image == expected_image_path glance_mock.images.data.assert_not_called() open_mock.assert_not_called() os_mock.remove.assert_not_called()
def test_stale_image_exists(self, _hom, hashlib_mock, open_mock, glance_mock, os_mock, _): glance_mock.images.list.return_value = [self.image1] md5 = mock.MagicMock() md5.hexdigest.return_value = '1111' hashlib_mock.md5.return_value = md5 os_mock.path.join = os.path.join os_mock.path.exists.side_effect = [True, False, False] os_mock.path.isfile.return_value = True os_mock.path.getmtime.return_value = 0 resources = Resources('foo', self.config) expected_image_path = self.config['disk']['image'][ 'provider_configuration']['path'] assert resources.provider_image == expected_image_path glance_mock.images.data.assert_called_with('1') self.assertEqual([ mock.call('/foo/bar.part', 'wb'), mock.call('/foo/bar.part', 'rb') ], open_mock.call_args_list) os_mock.remove.assert_not_called()
def test_nonexistent_image(self, os_mock, _): os_mock.exists.return_value = False resources = Resources('foo', self.config) with self.assertRaises(FileNotFoundError): _ = resources.provider_image
def test_image_backwards_compatibility(self): image_path = '/foo/bar' resources = Resources('foo', {'disk': {'image': image_path}}) assert image_path == resources.provider_image