def test_init(self): res = backend.PyPIResource('name', {'pypi': 'jujuresources>=0.2'}, 'od') self.assertEqual(res.spec, 'jujuresources>=0.2') self.assertEqual(res.package_name, 'jujuresources') self.assertEqual(res.destination_dir, 'od/jujuresources') self.assertEqual(res.filename, '') self.assertEqual(res.destination, '') self.assertEqual(res.hash, '') self.assertEqual(res.hash_type, '') res = backend.PyPIResource( 'name', {'pypi': 'http://example.com/foo#egg=jujuresources'}, 'od') self.assertEqual(res.spec, 'http://example.com/foo#egg=jujuresources') self.assertEqual(res.package_name, 'jujuresources') self.assertEqual(res.filename, 'foo') self.assertEqual(res.destination, 'od/foo') res = backend.PyPIResource('name', {'pypi': 'http://example.com/foo'}, 'od') self.assertEqual(res.package_name, '') self.assertEqual(res.filename, 'foo') self.assertEqual(res.destination, 'od/foo') res = backend.PyPIResource('name', { 'pypi': 'foo', 'hash': 'h', 'hash_type': 'ht' }, 'od') self.assertEqual(res.hash, 'h') self.assertEqual(res.hash_type, 'ht')
def test_install_group(self, mcall): resources = [ backend.PyPIResource('name', {'pypi': 'foo>=0.1'}, 'od'), backend.PyPIResource('name', {'pypi': 'bar>=0.1'}, 'od'), ] resources[0].verify = mock.Mock(return_value=False) resources[1].verify = mock.Mock(return_value=True) resources[1].destination = 'od/bar' backend.PyPIResource.install_group(resources) mcall.assert_called_with(['pip', 'install', 'foo>=0.1', 'od/bar']) backend.PyPIResource.install_group(resources, mirror_url='mirror') mcall.assert_called_with( ['pip', 'install', 'foo>=0.1', 'od/bar', '-i', 'mirror'])
def test_get_local_hash(self): res = backend.PyPIResource('name', {'pypi': 'jujuresources>=0.2'}, self.test_data) res.filename = 'jujuresources-0.2.tar.gz' res.get_local_hash() self.assertEqual(res.hash, '4f08575d804517cea2265a7d43022771') self.assertEqual(res.hash_type, 'md5')
def test_fetch_mirror(self, mexists, mrmtree, mmakedirs, msubprocess, mlistdir): mexists.return_value = True res = backend.PyPIResource('name', {'pypi': 'jujuresources>=0.2'}, 'od') res.get_remote_hash = mock.Mock() res._write_file = mock.Mock() res.process_dependency = mock.Mock() mlistdir.return_value = ['pyaml-3.0.tgz', 'jujuresources-0.2.tgz'] res.get_remote_hash.return_value = ('hash_type', 'hash') res.fetch('mirror') assert mrmtree.called assert mmakedirs.called msubprocess.check_output.assert_called_once_with( [ 'pip', 'install', 'jujuresources>=0.2', '--download', 'od/jujuresources', '-i', 'mirror' ], stderr=msubprocess.STDOUT) res.get_remote_hash.assert_called_once_with('jujuresources-0.2.tgz', 'mirror/') res._write_file.assert_called_once_with( 'od/jujuresources/jujuresources-0.2.tgz.hash_type', 'hash\n') self.assertEqual(res.filename, 'jujuresources-0.2.tgz') self.assertEqual(res.destination, 'od/jujuresources/jujuresources-0.2.tgz') self.assertEqual(res.hash, 'hash') self.assertEqual(res.hash_type, 'hash_type') res.process_dependency.assert_called_once_with('pyaml-3.0.tgz', 'mirror/')
def test_get_local_hash_missing(self, mlistdir): res = backend.PyPIResource('name', {'pypi': 'jujuresources>=0.2'}, self.test_data) res.filename = 'jujuresources-0.2.tar.gz' mlistdir.return_value = ['jujuresources-0.2.tar.gz.md5'] res.get_local_hash() self.assertEqual(res.hash, '') self.assertEqual(res.hash_type, '')
def test_verify(self): res = backend.PyPIResource('name', {'pypi': 'jujuresources>=0.2'}, self.test_data) res.filename = 'res-defaults.yaml' res.destination = os.path.join(self.test_data, res.filename) res.hash = '4f08575d804517cea2265a7d43022771' res.hash_type = 'md5' res.get_local_hash = mock.Mock() assert res.verify() assert res.get_local_hash.called
def test_install(self, mcall): mcall.return_value = 0 res = backend.PyPIResource('name', {'pypi': 'jujuresources>=0.1'}, 'od') res.destination = 'od/foo' res.verify = mock.Mock(return_value=False) assert not res.install() assert not mcall.called res.verify.return_value = True assert res.install() mcall.assert_called_with(['pip', 'install', 'od/foo']) mcall.return_value = 1 assert not res.install()
def test_get_remote_hash_no_match(self, murlopen): res = backend.PyPIResource('name', {'pypi': 'jujuresources>=0.2'}, 'od') murlopen.return_value.read.return_value = ( '<html>' '<a href="../../packages/source/j/jujuresources/' 'jujuresources-0.1.tar.gz#md5=4fdc461dcde13b1e919c17bac6e01464">' 'jujuresources-0.1.tar.gz' '</a>' '</html>') hash_type, hash = res.get_remote_hash('jujuresources-0.2.tar.gz', 'mirror') self.assertEqual(hash, '') self.assertEqual(hash_type, '')
def test_get_remote_hash(self, murlopen): res = backend.PyPIResource('name', {'pypi': 'jujuresources>=0.1'}, 'od') murlopen.return_value.__iter__.return_value = [ b'<html>', b'<a href="../../packages/source/j/jujuresources/' b'jujuresources-0.1.tar.gz#md5=4fdc461dcde13b1e919c17bac6e01464">' b'jujuresources-0.1.tar.gz' b'</a>', b'<a href="../../packages/source/j/jujuresources/' b'jujuresources-0.2.tar.gz#md5=deadbeef">' b'jujuresources-0.2.tar.gz' b'</a>', b'</html>' ] hash_type, hash = res.get_remote_hash('jujuresources-0.2.tar.gz', 'mirror') self.assertEqual(hash, 'deadbeef') self.assertEqual(hash_type, 'md5')
def test_fetch_urlspec(self, mexists, mmakedirs, murlfetch, mlistdir): mexists.return_value = True mmakedirs.side_effect = AssertionError('makedirs should not be called') mlistdir.return_value = ['bar-0.2.tgz'] res = backend.PyPIResource( 'name', { 'pypi': 'http://example.com/foo#egg=bar', 'hash': 'hash', 'hash_type': 'hash_type', }, 'od') res.get_remote_hash = mock.Mock( side_effect=AssertionError('get_remote_hash should not be called')) res.fetch() murlfetch.assert_called_once_with(None) self.assertEqual(res.hash, 'hash') self.assertEqual(res.hash_type, 'hash_type')
def test_fetch_fail(self, mexists, mrmtree, mmakedirs, mcheck_output): mexists.return_value = True res = backend.PyPIResource('name', {'pypi': 'jujuresources>=0.2'}, 'od') res.get_remote_hash = mock.Mock() mcheck_output.side_effect = subprocess.CalledProcessError( 1, ['cmd'], ) res.fetch() mcheck_output.assert_called_once_with([ 'pip', 'install', 'jujuresources>=0.2', '--download', 'od/jujuresources' ], stderr=subprocess.STDOUT) assert not res.get_remote_hash.called
def test_process_dependency(self, mexists, mmakedirs, mrename): mexists.return_value = False res = backend.PyPIResource('name', {'pypi': 'jujuresources>=0.2'}, 'od') res._package_name_from_filename = mock.Mock(return_value='new-package') res.get_remote_hash = mock.Mock(return_value=('hash_type', 'hash')) res._write_file = mock.Mock() res.process_dependency('new-package-1.0-python2.7.egg', 'mirror') mexists.assert_called_with('od/new-package') mmakedirs.assert_called_with('od/new-package') mrename.assert_called_with( 'od/jujuresources/new-package-1.0-python2.7.egg', 'od/new-package/new-package-1.0-python2.7.egg') res.get_remote_hash.assert_called_with('new-package-1.0-python2.7.egg', 'mirror') res._write_file.assert_called_with( 'od/new-package/new-package-1.0-python2.7.egg.hash_type', 'hash\n')