def test_download_remote_sources(source, package_config, expected_url, tmp_path: Path): specfile_content = ("Name: rsync\n" "Version: 3.1.3\n" "Release: 1\n" f"Source0: {source}\n" "License: GPLv3+\n" "Summary: rsync\n" "%description\nrsync\n") spec_path = tmp_path / "rsync.spec" spec_path.write_text(specfile_content) specfile = Specfile(spec_path, sources_dir=tmp_path) base_git = PackitRepositoryBase(config=flexmock(), package_config=package_config) flexmock(base_git).should_receive("specfile").and_return(specfile) expected_path = tmp_path / "rsync-3.1.3.tar.gz" # sadly we can't mock os.path.is_file, b/c the function is defined in posixpath.py # and flexmock is not able to mock that def mocked_download_file(url, destination_path, blocksize=8192): assert url == expected_url Path(destination_path).write_text("1") flexmock(DownloadHelper, download_file=mocked_download_file) base_git.download_remote_sources() assert expected_path.exists()
def test_get_output_from_action_defined(echo_cmd, expected_output): packit_repository_base = PackitRepositoryBase( config=flexmock(Config()), package_config=flexmock(PackageConfig(actions={ActionName.pre_sync: echo_cmd})), ) packit_repository_base.local_project = flexmock(working_dir=".") result = packit_repository_base.get_output_from_action(ActionName.pre_sync) assert result == expected_output
def test_run_in_sandbox(): packit_repository_base = PackitRepositoryBase( config=Config(), package_config=PackageConfig(actions={ActionName.pre_sync: "ls -lha"}), ) packit_repository_base.config.actions_handler = "sandcastle" result = packit_repository_base.get_output_from_action(ActionName.pre_sync) assert "total 4.0K" in result assert "drwxr-xr-x. 1 root root" in result
def test_base_push_good(upstream_distgit_remote): _, distgit, _ = upstream_distgit_remote b = PackitRepositoryBase(config=Config(), package_config=PackageConfig()) b.local_project = LocalProject( working_dir=str(distgit), git_url="https://github.com/packit-service/lol") flexmock( LocalProject, push=lambda *args, **kwargs: [PushInfo(PushInfo.FAST_FORWARD, None, None, None, None)], ) b.push("master")
def test_download_remote_sources_via_spec(self): """ Use case: package_config.sources and Source0 are out of sync, make sure packit downloads correct archive specifiec in the spec file """ # we should use an actual git.centos.org url but we'd store the tarball in our history # which we don't want I'd say # "https://git.centos.org/sources/rsync/c8s/82e7829c0b3cefbd33c233005341e2073c425629" git_centos_org_url = "https://example.org/" package_config = PackageConfig( specfile_path="rsync.spec", sources=[ SourcesItem( path="rsync-3.1.2.tar.gz", url=git_centos_org_url, ), ], jobs=[], ) # same drill here, let's not store tarballs in our git-history # source = "https://download.samba.org/pub/rsync/src/rsync-3.1.3.tar.gz" source = "https://httpbin.org/anything/rsync-3.1.3.tar.gz" base_git = PackitRepositoryBase(config=flexmock(), package_config=package_config) specfile_content = ("Name: rsync\n" "Version: 3.1.3\n" "Release: 1\n" f"Source0: {source}\n" "License: GPLv3+\n" "Summary: rsync\n" "%description\nrsync\n") tmp = Path(self.static_tmp) spec_path = tmp / "rsync.spec" spec_path.write_text(specfile_content) specfile = Specfile(spec_path, sourcedir=tmp, autosave=True) flexmock(base_git).should_receive("specfile").and_return(specfile) def mocked_is_file(): import inspect # return False only if Path.is_file() is called directly from within # the download_remote_sources() method # this is necessary because specfile relies on Path.is_file() as well return inspect.stack()[3].function != "download_remote_sources" flexmock(Path).should_receive("is_file").replace_with(mocked_is_file) base_git.download_remote_sources() expected_path = tmp / "rsync-3.1.3.tar.gz" assert Path(expected_path).exists()
def packit_repository_base_with_sandcastle_object(tmp_path): c = Config() c.command_handler = RunCommandType.sandcastle b = PackitRepositoryBase( config=c, package_config=PackageConfig( actions={ ActionName.pre_sync: "command -a", ActionName.get_current_version: "command -b", }), ) b.local_project = LocalProject(working_dir=tmp_path) return b
def test_get_output_from_action_defined(): echo_cmd = "echo 'hello world'" packit_repository_base = PackitRepositoryBase( config=flexmock(), package_config=flexmock( PackageConfig(actions={ActionName.pre_sync: echo_cmd})), ) packit_repository_base.local_project = flexmock(working_dir=".") result = packit_repository_base.get_output_from_action(ActionName.pre_sync) assert result == "hello world\n"
def test_base_push_bad(upstream_distgit_remote): _, distgit, _ = upstream_distgit_remote b = PackitRepositoryBase(config=Config(), package_config=PackageConfig()) b.local_project = LocalProject( working_dir=str(distgit), git_url="https://github.com/packit-service/lol") flexmock( LocalProject, push=lambda *args, **kwargs: [PushInfo(PushInfo.REMOTE_REJECTED, None, None, None, None)], ) with pytest.raises(PackitException) as e: b.push("master") assert "unable to push" in str(e.value)
def test_get_output_from_action_defined_in_sandcastle(): echo_cmd = "hello world" flexmock(Sandcastle).should_receive("get_api_client") flexmock(Sandcastle).should_receive("is_pod_already_deployed").and_return(True) c = Config() c.command_handler = RunCommandType.sandcastle packit_repository_base = PackitRepositoryBase( config=c, package_config=PackageConfig(actions={ActionName.pre_sync: echo_cmd}) ) packit_repository_base.local_project = LocalProject() flexmock(Sandcastle).should_receive("run") flexmock(Sandcastle).should_receive("exec").and_return(echo_cmd) result = packit_repository_base.get_output_from_action(ActionName.pre_sync) assert result == echo_cmd
def packit_repository_base_more_actions(): return PackitRepositoryBase( config=Config(), package_config=PackageConfig( actions={ ActionName.pre_sync: ["command --a", "command --a"], ActionName.get_current_version: "command --b", }), )
def packit_repository_base(): return PackitRepositoryBase( config=flexmock(), package_config=flexmock( PackageConfig( actions={ ActionName.pre_sync: "command --a", ActionName.get_current_version: "command --b", })), )
def test_set_spec_content(tmp_path): distgit_spec_contents = ( "Name: bring-me-to-the-life\n" "Version: 1.0\n" "Release: 1\n" "Source0: foo.bar\n" "License: GPLv3+\n" "Summary: evanescence\n" "%description\n-\n\n" "%changelog\n" "* Mon Mar 04 2019 Foo Bor <*****@*****.**> - 1.0-1\n" "- Initial package.\n") distgit_spec_path = tmp_path / "life.spec" distgit_spec_path.write_text(distgit_spec_contents) upstream_spec_contents = ( "Name: bring-me-to-the-life\n" "Version: 1.0\n" "Release: 1\n" "Source0: foo.bor\n" "License: MIT\n" "Summary: evanescence, I was brought to life\n" "%description\n-\n" "%changelog\n" "* Mon Mar 04 2019 Foo Bor <*****@*****.**> - 1.0-1\n" "- Initial package.\n") upstream_spec_path = tmp_path / "e-life.spec" upstream_spec_path.write_text(upstream_spec_contents) upstream_specfile = Specfile(upstream_spec_path, sources_dir=tmp_path) dist_git = PackitRepositoryBase(config=flexmock(), package_config=flexmock()) dist_git._specfile_path = distgit_spec_path dist_git.set_specfile_content(upstream_specfile, None, None) assert [ "* Mon Mar 04 2019 Foo Bor <*****@*****.**> - 1.0-1", "- Initial package.", ] == dist_git.specfile.spec_content.section("%changelog") assert "1.0" == dist_git.specfile.get_version() assert "License: MIT" in dist_git.specfile.spec_content.section("%package") assert ("Summary: evanescence, I was brought to life" in dist_git.specfile.spec_content.section("%package")) new_log = [ "* Wed Jun 02 2021 John Fou <*****@*****.**> - 1.1-1", "- 1.1 upstream release", ] flexmock(SpecFile).should_receive("get_new_log").with_args( "1.1 upstream release").and_return(new_log) dist_git.set_specfile_content(upstream_specfile, "1.1", "1.1 upstream release") assert new_log + [ "* Mon Mar 04 2019 Foo Bor <*****@*****.**> - 1.0-1", "- Initial package.", ] == dist_git.specfile.spec_content.section("%changelog") assert "1.1" == dist_git.specfile.get_version()