Пример #1
0
    def test_creds_are_pulled_from_secret_at_runtime(self, monkeypatch):
        task = GCSCopy(
            source_bucket="s",
            source_blob="s",
            dest_bucket="d",
            dest_blob="d",
            credentials_secret="GOOGLE_APPLICATION_CREDENTIALS",
        )

        creds_loader = MagicMock()
        monkeypatch.setattr("prefect.utilities.gcp.Credentials", creds_loader)
        monkeypatch.setattr("prefect.utilities.gcp.storage.Client",
                            MagicMock())

        with prefect.context(secrets=dict(GOOGLE_APPLICATION_CREDENTIALS=42)):
            task.run()

        assert creds_loader.from_service_account_info.call_args[0][0] == 42
Пример #2
0
    def test_project_is_pulled_from_creds_and_can_be_overriden_at_anytime(
            self, monkeypatch):
        task = GCSCopy(
            source_bucket="s",
            source_blob="s",
            dest_bucket="d",
            dest_blob="d",
            credentials_secret="GOOGLE_APPLICATION_CREDENTIALS",
        )
        task_proj = GCSCopy(
            source_bucket="s",
            source_blob="s",
            dest_bucket="d",
            dest_blob="d",
            project="test-init",
            credentials_secret="GOOGLE_APPLICATION_CREDENTIALS",
        )

        client = MagicMock()
        service_account_info = MagicMock(return_value=MagicMock(
            project_id="default"))
        monkeypatch.setattr(
            "prefect.utilities.gcp.Credentials",
            MagicMock(from_service_account_info=service_account_info),
        )
        monkeypatch.setattr("prefect.utilities.gcp.storage",
                            MagicMock(Client=client))

        with prefect.context(secrets=dict(GOOGLE_APPLICATION_CREDENTIALS={})):
            task.run()
            task_proj.run()
            task_proj.run(**{"project": "run-time"})

        x, y, z = client.call_args_list

        assert x[1]["project"] == "default"  ## pulled from credentials
        assert y[1]["project"] == "test-init"  ## pulled from init
        assert z[1]["project"] == "run-time"  ## pulled from run kwarg