def get(self) -> str: if not os.path.exists(self.path): commit = self.provider_params.get('git_commit', None) branch = self.provider_params.get('git_branch', None) depth = self.provider_params.get('git_commit_depth', None) clone_git_repo(self.uri, target_dir=self.path, commit=commit, branch=branch, depth=depth) return self.path
def test_clone_git_repo_commit_failure(tmpdir, commit, branch, depth): tmpdir_path = str(tmpdir.realpath()) with pytest.raises(OsbsCommitNotFound) as exc: clone_git_repo(TEST_DOCKERFILE_GIT, tmpdir_path, commit=commit, retry_times=1, branch=branch, depth=depth) assert 'Commit {} is not reachable in branch {}'.format(commit, branch) in str(exc)
def test_calc_depth_git_repo(tmpdir): tmpdir_path = str(tmpdir.realpath()) repo_data = clone_git_repo(TEST_DOCKERFILE_GIT, tmpdir_path, commit='HEAD', branch='master', depth=None) assert repo_data.commit_id is not None assert tmpdir_path == repo_data.repo_path assert repo_data.commit_depth == 1
def build_image_from_git(self, url, image, git_path=None, git_commit=None, copy_dockerfile_to=None, use_cache=False, buildargs=None): """ build image from provided url and tag it this operation is asynchronous and you should consume returned generator in order to wait for build to finish :param url: str :param image: ImageName, name of the resulting image :param git_path: str, path to dockerfile within gitrepo :param copy_dockerfile_to: str, copy dockerfile to provided path :param use_cache: bool, True if you want to use cache :param buildargs: dict, build-time variables --build-arg :return: generator """ logger.info( "building image '%s' from git repo '%s' specified as URL '%s'", image, git_path, url) logger.info("will copy Dockerfile to '%s'", copy_dockerfile_to) temp_dir = tempfile.mkdtemp() response = None try: clone_git_repo(url, temp_dir, git_commit) build_file_path, build_file_dir = figure_out_build_file( temp_dir, git_path) if copy_dockerfile_to: # TODO: pre build plugin shutil.copyfile(build_file_path, copy_dockerfile_to) response = self.build_image_from_path(build_file_dir, image, use_cache=use_cache, buildargs=buildargs) finally: try: shutil.rmtree(temp_dir) except (IOError, OSError) as ex: # no idea why this is happening logger.warning("Failed to remove dir '%s': %s", temp_dir, ex) logger.info("build finished") return response
def test_clone_git_repo(tmpdir, commit, branch, depth): tmpdir_path = str(tmpdir.realpath()) repo_data = clone_git_repo(TEST_DOCKERFILE_GIT, tmpdir_path, commit=commit, branch=branch, depth=depth) assert repo_data.commit_id is not None assert tmpdir_path == repo_data.repo_path if commit: assert commit == repo_data.commit_id assert len(repo_data.commit_id) == 40 # current git hashes are this long assert os.path.isdir(os.path.join(tmpdir_path, '.git'))
def test_calc_depth_git_repo(tmpdir): tmpdir_path = str(tmpdir.realpath()) flexmock(time).should_receive('sleep').and_return(None) repo_data = clone_git_repo(TEST_DOCKERFILE_GIT, tmpdir_path, commit='HEAD', branch='master', depth=None) assert repo_data.commit_id is not None assert tmpdir_path == repo_data.repo_path assert repo_data.commit_depth == 1
def build_image_from_git(self, url, image, git_path=None, git_commit=None, copy_dockerfile_to=None, stream=False, use_cache=False): """ build image from provided url and tag it this operation is asynchronous and you should consume returned generator in order to wait for build to finish :param url: str :param image: ImageName, name of the resulting image :param git_path: str, path to dockerfile within gitrepo :param copy_dockerfile_to: str, copy dockerfile to provided path :param stream: bool, True returns generator, False returns str :param use_cache: bool, True if you want to use cache :return: generator """ logger.info("building image '%s' from git repo '%s' specified as URL '%s'", image, git_path, url) logger.info("will copy Dockerfile to '%s'", copy_dockerfile_to) temp_dir = tempfile.mkdtemp() response = None try: clone_git_repo(url, temp_dir, git_commit) build_file_path, build_file_dir = figure_out_build_file(temp_dir, git_path) if copy_dockerfile_to: # TODO: pre build plugin shutil.copyfile(build_file_path, copy_dockerfile_to) response = self.build_image_from_path(build_file_dir, image, use_cache=use_cache) finally: try: shutil.rmtree(temp_dir) except (IOError, OSError) as ex: # no idea why this is happening logger.warning("Failed to remove dir '%s': %r", temp_dir, ex) logger.info("build finished") return response
def test_clone_git_repo(tmpdir, commit, branch, depth, modified): tmpdir_path = str(tmpdir.realpath()) flexmock(time).should_receive('sleep').and_return(None) repo_data = clone_git_repo(TEST_DOCKERFILE_GIT, tmpdir_path, commit=commit, branch=branch, depth=depth) assert repo_data.commit_id is not None assert tmpdir_path == repo_data.repo_path if commit: assert commit == repo_data.commit_id assert len(repo_data.commit_id) == 40 # current git hashes are this long assert os.path.isdir(os.path.join(tmpdir_path, '.git')) if modified: os.mknod(os.path.join(tmpdir_path, 'test')) with pytest.raises(OsbsLocallyModified): reset_git_repo(tmpdir_path, commit)
def test_clone_git_repo_total_failure(tmpdir): tmpdir_path = str(tmpdir.realpath()) with pytest.raises(OsbsException) as exc: clone_git_repo(tmpdir_path + 'failure', tmpdir_path, retry_times=1) assert 'Unable to clone git repo' in exc.value.message
def test_clone_git_repo_commit_failure(tmpdir, commit, branch, depth): tmpdir_path = str(tmpdir.realpath()) with pytest.raises(OsbsException) as exc: clone_git_repo(TEST_DOCKERFILE_GIT, tmpdir_path, commit=commit, retry_times=1, branch=branch, depth=depth) assert 'cannot find commit' in exc.value.message
def test_clone_git_repo_total_failure(tmpdir): tmpdir_path = str(tmpdir.realpath()) flexmock(time).should_receive('sleep').and_return(None) with pytest.raises(OsbsException) as exc: clone_git_repo(tmpdir_path + 'failure', tmpdir_path, retry_times=1) assert 'Unable to clone git repo' in exc.value.message