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 = 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_copy_plain_resource_with_cacher(mocker, tmpdir): config.cfg['common']['cache_url'] = '#filename#,#algorithm#,#hash#' config.cfg['common']['work_dir'] = str(tmpdir) 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 = Resource({'name': 'foo', 'md5': '5b9164ad6f496d9dee12ec7634ce253f'}) substitute_cache_url_mock = mocker.patch.object( res, '_Resource__substitute_cache_url', return_value='http://cache/abc') res.copy(str(tmpdir)) substitute_cache_url_mock.assert_called_once_with(None) urlopen_class_mock.assert_called_with('http://cache/abc', context=ctx)
def test_git_clone(mocker): mock = mocker.patch('subprocess.check_output') mocker.patch('os.path.isdir', ret='True') res = Resource({'git': {'url': 'url', 'ref': 'ref'}}) res.copy('dir') mock.assert_called_with( ['git', 'clone', '--depth', '1', 'url', 'dir/url-ref', '-b', 'ref'], stderr=-2)
def test_fetching_with_ssl_verify(mocker): ctx = get_ctx(mocker) get_mock_ssl(mocker, ctx) mock_urlopen = get_mock_urlopen(mocker) res = Resource({'name': 'file', 'url': 'https:///dummy'}) try: res.copy() except: pass mock_urlopen.assert_called_with('https:///dummy', context=ctx) assert ctx.check_hostname is True assert ctx.verify_mode == 1
def test_fetching_file_exists_but_used_as_is(mocker): """ It should not download the file, because we didn't specify any hash algorithm, so integrity checking is implicitly disabled here. """ with open('file', 'w') as f: # noqa: F841 pass mock_urlopen = get_mock_urlopen(mocker) res = Resource({'name': 'file', 'url': 'http:///dummy', 'md5': 'd41d8cd98f00b204e9800998ecf8427e'}) res.copy() mock_urlopen.assert_not_called()
def test_fetching_disable_ssl_verify(mocker): config.cfg['common']['ssl_verify'] = False mock_urlopen = get_mock_urlopen(mocker) ctx = get_ctx(mocker) get_mock_ssl(mocker, ctx) res = Resource({'name': 'file', 'url': 'https:///dummy'}) try: res.copy() except: pass mock_urlopen.assert_called_with('https:///dummy', context=ctx) assert ctx.check_hostname is False assert ctx.verify_mode == 0
def test_fetching_file_exists_no_hash_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 = Resource({'name': 'file', 'url': 'http:///dummy'}) with pytest.raises(CekitError): # url is not valid so we get error, but we are not interested # in it. We just need to check that we attempted to downlad. res.copy() mock_urlopen.assert_called_with('http:///dummy', context=ctx)
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 = 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_fetching_bad_status_code(): res = Resource({'name': 'file', 'url': 'http:///dummy'}) with pytest.raises(CekitError): res.copy()
def test_repository_dir_is_constructed_properly(mocker): mocker.patch('subprocess.check_output') mocker.patch('os.path.isdir', ret='True') res = Resource({'git': {'url': 'url/repo', 'ref': 'ref'}}) assert res.copy('dir') == 'dir/repo-ref'