class TestCloudBuildHookWithoutProjectId(unittest.TestCase): hook = None # type: Optional[CloudBuildHook] 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.gcp.hooks.cloud_build.CloudBuildHook._authorize") @mock.patch("airflow.gcp.hooks.cloud_build.build") def test_cloud_build_client_creation(self, mock_build, mock_authorize): result = self.hook.get_conn() mock_build.assert_called_once_with( 'cloudbuild', 'v1', http=mock_authorize.return_value, cache_discovery=False ) self.assertEqual(mock_build.return_value, result) self.assertEqual(self.hook._conn, result) @mock.patch( 'airflow.contrib.hooks.gcp_api_base_hook.GoogleCloudBaseHook.project_id', new_callable=PropertyMock, return_value=None ) @mock.patch("airflow.gcp.hooks.cloud_build.CloudBuildHook.get_conn") def test_create_build(self, mock_get_conn, mock_project_id): 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), )
class TestCloudBuildHookWithPassedProjectId(unittest.TestCase): hook = None # type: Optional[CloudBuildHook] 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.gcp.hooks.cloud_build.CloudBuildHook._authorize") @mock.patch("airflow.gcp.hooks.cloud_build.build") def test_cloud_build_client_creation(self, mock_build, mock_authorize): result = self.hook.get_conn() mock_build.assert_called_once_with( 'cloudbuild', 'v1', http=mock_authorize.return_value, cache_discovery=False ) self.assertEqual(mock_build.return_value, result) self.assertEqual(self.hook._conn, result) @mock.patch("airflow.gcp.hooks.cloud_build.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.gcp.hooks.cloud_build.CloudBuildHook.get_conn") @mock.patch("airflow.gcp.hooks.cloud_build.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_api_base_hook.GoogleCloudBaseHook.project_id', new_callable=PropertyMock, return_value=GCP_PROJECT_ID_HOOK_UNIT_TEST ) @mock.patch("airflow.gcp.hooks.cloud_build.CloudBuildHook.get_conn") @mock.patch("airflow.gcp.hooks.cloud_build.time.sleep") def test_error_operation(self, _, get_conn_mock, mock_project_id): 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 self.assertRaisesRegex(AirflowException, "error"): self.hook.create_build(body={})