Пример #1
0
    def test_key_not_found(self):
        self.update_yaml_file_mock.side_effect = KeyError("dummy key error")

        args = DeployCommand.Args(
            file="test/file.yml",
            values={
                "a.b.c": "foo",
                "a.b.d": "bar"
            },
            username="******",
            password="******",
            git_user="******",
            git_email="GIT_EMAIL",
            create_pr=False,
            auto_merge=False,
            single_commit=False,
            organisation="ORGA",
            repository_name="REPO",
            git_provider=GitProvider.GITHUB,
            git_provider_url=None,
            commit_message=None,
        )
        with pytest.raises(GitOpsException) as ex:
            DeployCommand(args).execute()
        self.assertEqual(str(ex.value),
                         "Key 'a.b.c' not found in file: test/file.yml")

        assert self.mock_manager.method_calls == [
            call.GitRepoApiFactory.create(args, "ORGA", "REPO"),
            call.GitRepo(self.git_repo_api_mock),
            call.GitRepo.clone(),
            call.GitRepo.get_full_file_path("test/file.yml"),
            call.update_yaml_file("/tmp/created-tmp-dir/test/file.yml",
                                  "a.b.c", "foo"),
        ]
Пример #2
0
    def test_clone_error(self):
        clone_exception = GitOpsException("dummy clone error")
        self.git_repo_mock.clone.side_effect = clone_exception

        args = DeployCommand.Args(
            file="test/file.yml",
            values={
                "a.b.c": "foo",
                "a.b.d": "bar"
            },
            username="******",
            password="******",
            git_user="******",
            git_email="GIT_EMAIL",
            create_pr=False,
            auto_merge=False,
            single_commit=False,
            organisation="ORGA",
            repository_name="REPO",
            git_provider=GitProvider.GITHUB,
            git_provider_url=None,
            commit_message=None,
        )
        with pytest.raises(GitOpsException) as ex:
            DeployCommand(args).execute()
        self.assertEqual(ex.value, clone_exception)

        assert self.mock_manager.method_calls == [
            call.GitRepoApiFactory.create(args, "ORGA", "REPO"),
            call.GitRepo(self.git_repo_api_mock),
            call.GitRepo.clone(),
        ]
Пример #3
0
    def test_single_commit_single_value_change_happy_flow(self):
        args = DeployCommand.Args(
            file="test/file.yml",
            values={"a.b.c": "foo"},
            username="******",
            password="******",
            git_user="******",
            git_email="GIT_EMAIL",
            create_pr=False,
            auto_merge=False,
            single_commit=True,
            organisation="ORGA",
            repository_name="REPO",
            git_provider=GitProvider.GITHUB,
            git_provider_url=None,
            commit_message=None,
        )
        DeployCommand(args).execute()

        assert self.mock_manager.method_calls == [
            call.GitRepoApiFactory.create(args, "ORGA", "REPO"),
            call.GitRepo(self.git_repo_api_mock),
            call.GitRepo.clone(),
            call.GitRepo.get_full_file_path("test/file.yml"),
            call.update_yaml_file("/tmp/created-tmp-dir/test/file.yml",
                                  "a.b.c", "foo"),
            call.logging.info("Updated yaml property %s to %s", "a.b.c",
                              "foo"),
            call.GitRepo.commit("GIT_USER", "GIT_EMAIL",
                                "changed 'a.b.c' to 'foo' in test/file.yml"),
            call.GitRepo.push(),
        ]
Пример #4
0
    def test_nothing_to_update(self):
        self.update_yaml_file_mock.return_value = False

        args = DeployCommand.Args(
            file="test/file.yml",
            values={"a.b.c": "foo", "a.b.d": "bar"},
            username="******",
            password="******",
            git_user="******",
            git_email="GIT_EMAIL",
            create_pr=False,
            auto_merge=False,
            single_commit=False,
            organisation="ORGA",
            repository_name="REPO",
            git_provider=GitProvider.GITHUB,
            git_provider_url=None,
            commit_message=None,
        )
        DeployCommand(args).execute()

        assert self.mock_manager.method_calls == [
            call.GitRepoApiFactory.create(args, "ORGA", "REPO"),
            call.GitRepo(self.git_repo_api_mock),
            call.GitRepo.clone(),
            call.GitRepo.get_full_file_path("test/file.yml"),
            call.update_yaml_file("/tmp/created-tmp-dir/test/file.yml", "a.b.c", "foo"),
            call.logging.info("Yaml property %s already up-to-date", "a.b.c"),
            call.update_yaml_file("/tmp/created-tmp-dir/test/file.yml", "a.b.d", "bar"),
            call.logging.info("Yaml property %s already up-to-date", "a.b.d"),
            call.logging.info("All values already up-to-date. I'm done here."),
        ]
Пример #5
0
    def test_create_pr_and_merge_happy_flow(self):
        args = DeployCommand.Args(
            file="test/file.yml",
            values={
                "a.b.c": "foo",
                "a.b.d": "bar"
            },
            username="******",
            password="******",
            git_user="******",
            git_email="GIT_EMAIL",
            create_pr=True,
            auto_merge=True,
            single_commit=False,
            organisation="ORGA",
            repository_name="REPO",
            git_provider=GitProvider.GITHUB,
            git_provider_url=None,
            commit_message=None,
        )
        DeployCommand(args).execute()

        assert self.mock_manager.method_calls == [
            call.GitRepoApiFactory.create(args, "ORGA", "REPO"),
            call.GitRepo(self.git_repo_api_mock),
            call.GitRepo.clone(),
            call.uuid.uuid4(),
            call.GitRepo.new_branch("gitopscli-deploy-b973b5bb"),
            call.GitRepo.get_full_file_path("test/file.yml"),
            call.update_yaml_file("/tmp/created-tmp-dir/test/file.yml",
                                  "a.b.c", "foo"),
            call.logging.info("Updated yaml property %s to %s", "a.b.c",
                              "foo"),
            call.GitRepo.commit("GIT_USER", "GIT_EMAIL",
                                "changed 'a.b.c' to 'foo' in test/file.yml"),
            call.update_yaml_file("/tmp/created-tmp-dir/test/file.yml",
                                  "a.b.d", "bar"),
            call.logging.info("Updated yaml property %s to %s", "a.b.d",
                              "bar"),
            call.GitRepo.commit("GIT_USER", "GIT_EMAIL",
                                "changed 'a.b.d' to 'bar' in test/file.yml"),
            call.GitRepo.push(),
            call.GitRepoApi.create_pull_request_to_default_branch(
                "gitopscli-deploy-b973b5bb",
                "Updated values in test/file.yml",
                "Updated 2 values in `test/file.yml`:\n```yaml\na.b.c: foo\na.b.d: bar\n```\n",
            ),
            call.GitRepoApi.merge_pull_request(42, "merge"),
            call.GitRepoApi.delete_branch("gitopscli-deploy-b973b5bb"),
        ]