def setup_method(self): self.betka = Betka(task_name="task.betka.master_sync") self.config_json = config_json() self.betka.set_config() self.tmpdir = TemporaryDirectory() self.upstream_repo = Path(self.tmpdir.name) / "upstream" self.downstream_repo = Path(self.tmpdir.name) / "downstream" assert self._run_cmd( f"./setup_upstream_downstream.sh " f"{self.upstream_repo} {self.downstream_repo}", Path(__file__).parent / "src/", )
class TestBetkaCore(object): def setup_method(self): self.betka = Betka() @pytest.mark.parametrize("config_json", [ config_json_missing_pagure_api_token(), config_json_missing_github_api_token(), config_json_missing_new_api_version(), ]) def test_betka_config_keyerror(self, config_json): self.betka.config_json = config_json with pytest.raises(KeyError): self.betka.set_config()
def setup_method(self): self.github = "aklsdjfh19p3845yrp" self.pagure_user = "******" os.environ["GITHUB_API_TOKEN"] = self.github os.environ["PAGURE_API_TOKEN"] = "testing" os.environ["PAGURE_USER"] = self.pagure_user self.betka = Betka(task_name="task.betka.pr_sync") self.betka.set_config() self.tmpdir = TemporaryDirectory() self.upstream_repo = Path(self.tmpdir.name) / "upstream" self.downstream_repo = Path(self.tmpdir.name) / "downstream" assert self._run_cmd( f"./setup_upstream_downstream.sh " f"{self.upstream_repo} {self.downstream_repo}", Path(__file__).parent / "src/", )
def setup_method(self): self.betka = Betka()
class TestBetkaPrSync(object): def _run_cmd(self, cmd, work_directory): shell = subprocess.run( shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False, cwd=work_directory, universal_newlines=True, ) if shell.returncode == 0: return True return False def setup_method(self): self.github = "aklsdjfh19p3845yrp" self.pagure_user = "******" os.environ["GITHUB_API_TOKEN"] = self.github os.environ["PAGURE_API_TOKEN"] = "testing" os.environ["PAGURE_USER"] = self.pagure_user self.betka = Betka(task_name="task.betka.pr_sync") self.betka.set_config() self.tmpdir = TemporaryDirectory() self.upstream_repo = Path(self.tmpdir.name) / "upstream" self.downstream_repo = Path(self.tmpdir.name) / "downstream" assert self._run_cmd( f"./setup_upstream_downstream.sh " f"{self.upstream_repo} {self.downstream_repo}", Path(__file__).parent / "src/", ) def fake_pagure_fork(self): self.betka.pagure_api.clone_url = str(self.downstream_repo / ".git") return True def fake_git_clone(self, clone_url, tempdir): repodir = Git.strip_dot_git(clone_url.split("/")[-2]) Git.call_git_cmd( "clone --recurse-submodules {u} {d}".format(u=clone_url, d=os.path.join( tempdir, repodir)), msg=clone_url, ) return os.path.join(tempdir, repodir) @pytest.fixture() def mock_deploy(self): flexmock(self.betka, deploy_image=True) @pytest.fixture() def mock_get_pagure_fork(self): (flexmock(self.betka.pagure_api).should_receive( "get_pagure_fork").replace_with(self.fake_pagure_fork)) @pytest.fixture() def mock_prepare_upstream(self): self.betka.upstream_cloned_dir = clone_git_repo( str(self.upstream_repo), str(self.betka.betka_tmp_dir.name)) (flexmock( self.betka).should_receive("prepare_upstream_git").and_return(True) ) @pytest.fixture() def mock_prepare_downstream(self): self.betka.downstream_dir = clone_git_repo( str(self.downstream_repo), str(self.betka.betka_tmp_dir.name)) (flexmock(self.betka).should_receive( "prepare_downstream_git").and_return(True)) @pytest.fixture() def mock_get_bot_cfg_yaml(self): (flexmock(Betka).should_receive("_get_bot_cfg").with_args( branch="fc30").and_return(True)) @pytest.fixture() def mock_git_clone(self): (flexmock(Git).should_receive("clone_repo").replace_with( self.fake_git_clone)) @pytest.fixture() def init_betka_real_json(self, mock_whois, betka_config, real_json, mock_has_ssh_access): assert self.betka.get_pr_fedmsg_info(real_json) assert self.betka.prepare() def test_betka_wrong_url(self, betka_config, foo_bar_json): """Tests if betka doesn't include repos it's not supposed to include""" self.betka.betka_config["dist_git_repos"] = {} assert self.betka.get_pr_fedmsg_info(foo_bar_json) self.betka.set_config() assert self.betka.betka_config.get("github_api_token") == self.github assert self.betka.betka_config.get("pagure_user") == self.pagure_user assert not self.betka.get_synced_images() def test_betka_valid_branches( self, init_betka_real_json, mock_prepare_downstream, mock_get_pagure_fork, mock_get_branches, ): synced_image = self.betka.get_synced_images()[0] assert synced_image self.betka.set_config() assert self.betka.betka_config["pagure_user"] assert self.betka.pagure_api.get_pagure_fork() self.betka.clone_url = self.betka.pagure_api.get_clone_url() self.betka.pagure_api.betka_config = self.betka.betka_config assert self.betka.clone_url self.betka.prepare_downstream_git() assert self.betka.downstream_dir flexmock(self.betka.pagure_api).should_receive( "check_config_in_branch").with_args( downstream_dir=self.betka.downstream_dir, branch="fc31").and_return(True) flexmock(self.betka.pagure_api).should_receive( "check_config_in_branch").with_args( downstream_dir=self.betka.downstream_dir, branch="fc30").and_return(False) os.chdir(str(self.betka.downstream_dir)) branch_list = self.betka.pagure_api.get_valid_branches( Path(self.betka.downstream_dir), branch_list=["fc30", "fc31"]) # only 'fc31' branch has the bot-cfg.yml file assert branch_list == ["fc31"] def test_betka_run_pr_closed( self, init_betka_real_json, mock_get_pagure_fork, mock_prepare_downstream, mock_prepare_upstream, mock_git_clone, mock_get_valid_branches, mock_get_bot_cfg_yaml, mock_get_branches, mock_send_email, mock_rmtree, mock_check_upstream_pr_closed, ): self.betka.betka_config["dist_git_repos"].pop("s2i-core") self.betka.config = bot_cfg_yaml_pr_checker() flexmock(UMBSender).should_receive( "send_umb_message_in_progress").and_return().once() flexmock(UMBSender).should_receive("send_umb_message_error").never() self.betka.run_sync() def test_betka_run_pr_sync( self, init_betka_real_json, mock_get_pagure_fork, mock_prepare_downstream, mock_prepare_upstream, mock_git_clone, mock_get_valid_branches, mock_get_bot_cfg_yaml, mock_deploy, mock_fetch_pr_origin, mock_check_upstream_pr_opened, mock_check_prs, mock_send_email, mock_rmtree, ): self.betka.set_config() assert self.betka.betka_config["pagure_user"] self.betka.betka_config["dist_git_repos"].pop("s2i-core") self.betka.pagure_api.config = self.betka.betka_config self.betka.config = bot_cfg_yaml_pr_checker() flexmock(UMBSender).should_receive( "send_umb_message_in_progress").and_return().once() flexmock(UMBSender).should_receive("send_umb_message_error").never() self.betka.run_sync() assert self.betka.downstream_dir
def master_sync(message): betka = Betka(task_name="task.betka.master_sync") if betka.get_master_fedmsg_info(message) and betka.prepare(): betka.run_sync()
class TestBetkaMasterSync(object): def _run_cmd(self, cmd, work_directory): shell = subprocess.run( shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False, cwd=work_directory, universal_newlines=True, ) if shell.returncode == 0: return True return False def setup_method(self): self.betka = Betka(task_name="task.betka.master_sync") self.config_json = config_json() self.betka.set_config() self.tmpdir = TemporaryDirectory() self.upstream_repo = Path(self.tmpdir.name) / "upstream" self.downstream_repo = Path(self.tmpdir.name) / "downstream" assert self._run_cmd( f"./setup_upstream_downstream.sh " f"{self.upstream_repo} {self.downstream_repo}", Path(__file__).parent / "src/", ) def fake_git_clone(self, clone_url, tempdir): repodir = Git.strip_dot_git(clone_url.split("/")[-2]) Git.call_git_cmd( "clone --recurse-submodules {u} {d}".format(u=clone_url, d=os.path.join( tempdir, repodir)), msg=clone_url, ) return os.path.join(tempdir, repodir) def fake_pagure_fork(self): self.betka.pagure_api.clone_url = str(self.downstream_repo / ".git") return True @pytest.fixture() def mock_git_clone(self): (flexmock(Git).should_receive("clone_repo").replace_with( self.fake_git_clone)) @pytest.fixture() def mock_deploy(self): flexmock(self.betka, deploy_image=True) @pytest.fixture() def mock_get_pagure_fork(self): (flexmock(self.betka.pagure_api).should_receive( "get_pagure_fork").and_return(self.fake_pagure_fork())) @pytest.fixture() def mock_prepare_upstream(self): self.betka.upstream_cloned_dir = clone_git_repo( str(self.upstream_repo), str(self.betka.betka_tmp_dir.name)) (flexmock( self.betka).should_receive("prepare_upstream_git").and_return(True) ) @pytest.fixture() def mock_prepare_downstream(self): self.betka.downstream_dir = clone_git_repo( str(self.downstream_repo), str(self.betka.betka_tmp_dir.name)) (flexmock(self.betka).should_receive( "prepare_downstream_git").and_return(True)) @pytest.fixture() def mock_pagure_bot_cfg_yaml(self): (flexmock(self.betka.pagure_api).should_receive( "get_bot_cfg_yaml").with_args(branch="fc30").and_return( bot_cfg_yaml_master_checker())) @pytest.fixture() def init_betka_real_json(self, mock_whois, betka_config, real_json, mock_has_ssh_access): assert self.betka.get_master_fedmsg_info(real_json) assert self.betka.prepare() def test_betka_wrong_url(self, betka_config, foo_bar_json): """Tests if betka doesn't include repos it's not supposed to include""" self.betka.betka_config["dist_git_repos"] = {} assert self.betka.get_master_fedmsg_info(foo_bar_json) assert self.betka.betka_config.get( "github_api_token") == "aklsdjfh19p3845yrp" assert self.betka.betka_config.get("pagure_user") == "testymctestface" assert not self.betka.get_synced_images() def test_betka_non_master_push(self, wrong_branch): self.betka.betka_config["dist_git_repos"] = {} assert not self.betka.get_master_fedmsg_info(wrong_branch) def test_betka_master_sync( self, init_betka_real_json, mock_prepare_downstream, mock_prepare_upstream, mock_get_pagure_fork, mock_get_branches, ): synced_image = self.betka.get_synced_images()[0] assert synced_image self.betka.betka_config = betka_yaml() self.betka.pagure_api.config = betka_yaml() self.betka.pagure_api.set_image(synced_image) assert self.betka.pagure_api.get_pagure_fork() self.betka.clone_url = self.betka.pagure_api.get_clone_url() assert self.betka.clone_url assert self.betka.downstream_dir flexmock(self.betka.pagure_api).should_receive( "check_config_in_branch").with_args( downstream_dir=self.betka.downstream_dir, branch="fc31").and_return(True) flexmock(self.betka.pagure_api).should_receive( "check_config_in_branch").with_args( downstream_dir=self.betka.downstream_dir, branch="fc30").and_return(False) os.chdir(str(self.betka.downstream_dir)) branch_list = self.betka.pagure_api.get_valid_branches( self.betka.downstream_dir, branch_list=["fc31", "fc30"]) # only 'fc30' branch has the bot-cfg.yml file assert branch_list == ["fc31"] def test_betka_run_master_sync( self, init_betka_real_json, mock_get_pagure_fork, mock_prepare_downstream, mock_prepare_upstream, mock_git_clone, mock_get_branches, mock_check_prs, mock_deploy, mock_send_email, mock_rmtree, ): self.betka.betka_config["dist_git_repos"].pop("s2i-core") flexmock(UMBSender).should_receive( "send_umb_message_in_progress").and_return().once() flexmock(UMBSender).should_receive("send_umb_message_error").never() self.betka.run_sync() # check if readme was updated (compare betka downstream vs test upstream) assert self.betka.downstream_dir os.chdir(str(self.betka.downstream_dir)) Git.call_git_cmd("checkout fc31") upstream_readme = (self.upstream_repo / "README.md").read_text() downstream_readme = (self.betka.downstream_dir / "README.md").read_text() assert upstream_readme == downstream_readme # check git log latest_commit = Git.call_git_cmd("log -n 1 --format=medium") latest_commit = [ x.strip() for x in latest_commit.split("\n") if x != "" ] assert latest_commit assert latest_commit[3] == "Add bot-cfg.yml" # check the other branch - readme should be without the update, because the branch wasn't # configured with bot-cfg.yml Git.call_git_cmd("checkout fc30") # latest commit should be Init branch last_commit = Git.call_git_cmd("log -n 1 --format=medium") assert last_commit commit_fields = [ x.strip() for x in last_commit.split("\n") if x.strip() != "" ] assert commit_fields assert commit_fields[3] == "Init branch" assert commit_fields[4] == "For betka test" assert commit_fields[5] == "in fc30 branch"
def setup_method(self): os.environ["GITHUB_API_TOKEN"] = "aklsdjfh19p3845yrp" os.environ["PAGURE_API_TOKEN"] = "testing" os.environ["PAGURE_USER"] = "******" self.betka = Betka() self.betka.config_json = config_json()
class TestBetkaCore(object): def setup_method(self): os.environ["GITHUB_API_TOKEN"] = "aklsdjfh19p3845yrp" os.environ["PAGURE_API_TOKEN"] = "testing" os.environ["PAGURE_USER"] = "******" self.betka = Betka() self.betka.config_json = config_json() @pytest.fixture() def json_init(self): return { "msg": { "repository": { "html_url": "https://github.com/sclorg/s2i-base-container" }, "comment": "[citest]", "issue": { "number": 75, "title": "Update 10.2 fedora dockerfile for f28, and run rpm-file-permissions", }, } } @pytest.fixture() def json_missing_repository(self): return {"msg": {"issue": {"number": 75}}} @pytest.fixture() def json_missing_pullrequest(self): return { "msg": { "repository": { "html_url": "https://github.com/sclorg/s2i-base-container" }, "issue": { "number": 75 }, } } @pytest.fixture() def json_missing_issue(self): return { "msg": { "repository": { "html_url": "https://github.com/sclorg/s2i-base-container" } } } @pytest.fixture() def json_empty_head_commit(self): return {"msg": {}} @pytest.fixture() def json_missing_head_commit(self): return {"msg": {"head_commit": None}} def test_update_config(self): self.betka.set_config() assert self.betka.betka_config.get( "github_api_token") == "aklsdjfh19p3845yrp" assert self.betka.betka_config.get("pagure_user") == "testymctestface" assert self.betka.betka_config.get("pagure_api_token") == "testing" assert self.betka.betka_config.get("new_api_version") def test_new_api_version_not_supported(self): self.betka.config_json = config_json_api_not_supported() self.betka.set_config() assert self.betka.betka_config.get( "github_api_token") == "aklsdjfh19p3845yrp" assert self.betka.betka_config.get("pagure_user") == "testymctestface" assert self.betka.betka_config.get("pagure_api_token") == "testing" assert not self.betka.betka_config.get("new_api_version") def test_wrong_fedmsg_info(self, json_init): json_init["topic"] = "org.fedoraproject.prod.github.testing" assert not self.betka.get_master_fedmsg_info(json_init) def test_miss_repo_fedmsg_info(self, json_missing_repository): assert not self.betka.get_master_fedmsg_info(json_missing_repository) def test_pullrequest_fedmsg_info(self, json_missing_pullrequest): assert not self.betka.get_master_fedmsg_info(json_missing_pullrequest) def test_missing_issue_fedmsg_info(self, json_missing_issue): with pytest.raises(AttributeError): self.betka.get_pr_fedmsg_info(json_missing_issue) def test_empty_head_commit_fedmsg_info(self, json_empty_head_commit): assert not self.betka.get_master_fedmsg_info(json_empty_head_commit) def test_miss_head_commit_fedmsg_info(self, json_missing_head_commit): assert not self.betka.get_master_fedmsg_info(json_missing_head_commit)