Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_copy_plain_resource_from_brew(mocker, tmpdir):
config.cfg['common']['work_dir'] = str(tmpdir)
config.cfg['common']['redhat'] = True
urlopen_class_mock = mocker.patch('cekit.descriptor.resource.urlopen')
mock_urlopen = urlopen_class_mock.return_value
mock_urlopen.getcode.return_value = 200
mock_urlopen.read.side_effect = [b"one", b"two", None]
ctx = get_ctx(mocker)
get_mock_ssl(mocker, ctx)
with open('file', 'w') as f: # noqa: F841
pass
res = create_resource({'name': 'foo',
'md5': '5b9164ad6f496d9dee12ec7634ce253f'})
mocker.spy(res, '_Resource__substitute_cache_url')
mock_get_brew_url = mocker.patch(
'cekit.descriptor.resource.get_brew_url', return_value='http://cache/abc')
res.copy(str(tmpdir))
mock_get_brew_url.assert_called_once_with('5b9164ad6f496d9dee12ec7634ce253f')
res._Resource__substitute_cache_url.call_count == 0
urlopen_class_mock.assert_called_with('http://cache/abc', context=ctx)
def test_url_resource_download_cleanup_after_failure(mocker, tmpdir, caplog):
caplog.set_level(logging.DEBUG, logger="cekit")
mocker.patch('os.path.exists', return_value=False)
mocker.patch('os.makedirs')
os_remove_mock = mocker.patch('os.remove')
urlopen_class_mock = mocker.patch('cekit.descriptor.resource.urlopen')
urlopen_mock = urlopen_class_mock.return_value
urlopen_mock.getcode.return_value = 200
urlopen_mock.read.side_effect = Exception
res = create_resource({'url': 'http://server.org/dummy',
'sha256': 'justamocksum'})
targetfile = os.path.join(str(tmpdir), 'targetfile')
with pytest.raises(CekitError) as excinfo:
res.guarded_copy(targetfile)
assert "Error copying resource: 'dummy'. See logs for more info" in str(excinfo.value)
assert "Removing incompletely downloaded '{}' file".format(targetfile) in caplog.text
urlopen_class_mock.assert_called_with('http://server.org/dummy', context=mocker.ANY)
os_remove_mock.assert_called_with(targetfile)
def test_generated_url_with_cacher():
config.cfg['common']['cache_url'] = '#filename#,#algorithm#,#hash#'
res = create_resource({'url': 'dummy',
'sha256': 'justamocksum'})
res.name = 'file'
assert res._Resource__substitute_cache_url('file') == 'file,sha256,justamocksum'
def test_path_resource_relative():
res = create_resource({'name': 'foo',
'path': 'bar'}, directory='/foo')
assert res.path == '/foo/bar'
def test_fetching_bad_status_code():
res = create_resource(
{'name': 'file', 'url': 'http:///dummy'})
with pytest.raises(CekitError):
res.copy()
def test_repository_dir_uses_name_if_defined(mocker):
mocker.patch('subprocess.check_output')
mocker.patch('os.path.isdir', ret='True')
res = create_resource(
{'name': 'some-id', 'git': {'url': 'http://host.com/url/repo.git', 'ref': 'ref'}})
assert res.copy('dir') == 'dir/some-id'
def test_fetching_file_exists_fetched_again(mocker):
"""
It should download the file again, because available
file locally doesn't match checksum.
"""
mock_urlopen = get_mock_urlopen(mocker)
ctx = get_ctx(mocker)
get_mock_ssl(mocker, ctx)
with open('file', 'w') as f: # noqa: F841
pass
res = create_resource({'name': 'file', 'url': 'http:///dummy', 'md5': '123456'})
with pytest.raises(CekitError):
# Checksum will fail, because the "downloaded" file
# will not have md5 equal to 123456. We need investigate
# mocking of requests get calls to do it properly
res.copy()
mock_urlopen.assert_called_with('http:///dummy', context=ctx)
def test_path_local_non_existing_resource_with_cacher_use(mocker):
config.cfg['common']['cache_url'] = '#filename#,#algorithm#,#hash#'
mocker.patch('os.path.exists', return_value=False)
mocker.patch('os.makedirs')
res = create_resource({'name': 'foo',
'path': 'bar'}, directory='/foo')
mocker.spy(res, '_download_file')
download_file_mock = mocker.patch.object(res, '_download_file')
res.guarded_copy('target')
download_file_mock.assert_called_with('/foo/bar', 'target')
def test_git_clone(mocker):
mock = mocker.patch('subprocess.check_output')
mocker.patch('os.path.isdir', ret='True')
res = create_resource({'git': {'url': 'http://host.com/url/path.git', 'ref': 'ref'}})
res.copy('dir')
mock.assert_called_with(['git',
'clone',
'--depth',
'1',
'http://host.com/url/path.git',
'dir/path',
'-b',
'ref'],
stderr=-2)
def _prepare(self):
self._descriptor['labels'] = [Label(x) for x in self._descriptor.get('labels', [])]
self._descriptor['envs'] = [Env(x) for x in self._descriptor.get('envs', [])]
self._descriptor['ports'] = [Port(x) for x in self._descriptor.get('ports', [])]
if 'run' in self._descriptor:
self._descriptor['run'] = Run(self._descriptor['run'])
self._descriptor['artifacts'] = [create_resource(a, directory=self._artifact_dir)
for a in self._descriptor.get('artifacts', [])]
self._descriptor['modules'] = Modules(self._descriptor.get('modules', {}), self.path)
self._descriptor['packages'] = Packages(self._descriptor.get('packages', {}), self.path)
self._descriptor['osbs'] = Osbs(self._descriptor.get('osbs', {}), self.path)
self._descriptor['volumes'] = [Volume(x) for x in self._descriptor.get('volumes', [])]
# make sure image declarations override any module definitions
self._image_overrides = {'artifacts': Image._to_dict(
self.artifacts), 'modules': Image._to_dict(self.modules.install)}
self._all_artifacts = Image._to_dict(self.artifacts)