Ejemplo n.º 1
0
class TestCloudBuildHookWithoutProjectId(unittest.TestCase):
    hook = None

    def setUp(self):
        with mock.patch(
            "airflow.contrib.hooks.gcp_api_base_hook.GoogleCloudBaseHook.__init__",
            new=mock_base_gcp_hook_no_default_project_id,
        ):
            self.hook = CloudBuildHook(gcp_conn_id="test")

    @mock.patch("airflow.contrib.hooks.gcp_cloud_build_hook.CloudBuildHook.get_conn")
    def test_create_build(self, _):
        with self.assertRaises(AirflowException) as e:
            self.hook.create_build(body={})

        self.assertEqual(
            "The project id must be passed either as keyword project_id parameter or as project_id extra in "
            "GCP connection definition. Both are not set!",
            str(e.exception),
        )
Ejemplo n.º 2
0
 def setUp(self):
     with mock.patch(
             "airflow.contrib.hooks.gcp_api_base_hook.GoogleCloudBaseHook.__init__",
             new=mock_base_gcp_hook_default_project_id,
     ):
         self.hook = CloudBuildHook(gcp_conn_id="test")
Ejemplo n.º 3
0
class TestCloudBuildHookWithPassedProjectId(unittest.TestCase):
    hook = None

    def setUp(self):
        with mock.patch(
                "airflow.contrib.hooks.gcp_api_base_hook.GoogleCloudBaseHook.__init__",
                new=mock_base_gcp_hook_default_project_id,
        ):
            self.hook = CloudBuildHook(gcp_conn_id="test")

    @mock.patch(
        "airflow.contrib.hooks.gcp_cloud_build_hook.CloudBuildHook.get_conn")
    def test_build_immediately_complete(self, get_conn_mock):
        service_mock = get_conn_mock.return_value

        service_mock.projects.return_value\
            .builds.return_value\
            .create.return_value\
            .execute.return_value = TEST_BUILD

        service_mock.projects.return_value.\
            builds.return_value.\
            get.return_value.\
            execute.return_value = TEST_BUILD

        service_mock.operations.return_value.\
            get.return_value.\
            execute.return_value = TEST_DONE_OPERATION

        result = self.hook.create_build(body={}, project_id=TEST_PROJECT_ID)

        service_mock.projects.return_value.builds.return_value.create.assert_called_once_with(
            body={}, projectId=TEST_PROJECT_ID)

        self.assertEqual(result, TEST_BUILD)

    @mock.patch(
        "airflow.contrib.hooks.gcp_cloud_build_hook.CloudBuildHook.get_conn")
    @mock.patch("airflow.contrib.hooks.gcp_cloud_build_hook.time.sleep")
    def test_waiting_operation(self, _, get_conn_mock):
        service_mock = get_conn_mock.return_value

        service_mock.projects.return_value.builds.return_value.create.return_value.execute.return_value = (
            TEST_BUILD)

        service_mock.projects.return_value.builds.return_value.get.return_value.execute.return_value = (
            TEST_BUILD)

        execute_mock = mock.Mock(
            **{
                "side_effect": [
                    TEST_WAITING_OPERATION, TEST_DONE_OPERATION,
                    TEST_DONE_OPERATION
                ]
            })
        service_mock.operations.return_value.get.return_value.execute = execute_mock

        result = self.hook.create_build(body={}, project_id=TEST_PROJECT_ID)

        self.assertEqual(result, TEST_BUILD)

    @mock.patch(
        "airflow.contrib.hooks.gcp_cloud_build_hook.CloudBuildHook.get_conn")
    @mock.patch("airflow.contrib.hooks.gcp_cloud_build_hook.time.sleep")
    def test_error_operation(self, _, get_conn_mock):
        service_mock = get_conn_mock.return_value

        service_mock.projects.return_value.builds.return_value.create.return_value.execute.return_value = (
            TEST_BUILD)

        execute_mock = mock.Mock(
            **{"side_effect": [TEST_WAITING_OPERATION, TEST_ERROR_OPERATION]})
        service_mock.operations.return_value.get.return_value.execute = execute_mock
        with six.assertRaisesRegex(self, AirflowException, "error"):
            self.hook.create_build(body={})
 def execute(self, context):
     hook = CloudBuildHook(gcp_conn_id=self.gcp_conn_id,
                           api_version=self.api_version)
     body = BuildProcessor(body=self.body).process_body()
     return hook.create_build(body=body, project_id=self.project_id)