Skip to content

Commit

Permalink
Remove unused func and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
tnk-ysk committed Apr 7, 2023
1 parent bfd61e5 commit ee8eefb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 77 deletions.
46 changes: 0 additions & 46 deletions airflow/providers/google/cloud/hooks/cloud_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,52 +189,6 @@ def create_build_without_waiting_for_result(

return operation, id_

@GoogleBaseHook.fallback_to_default_project_id
def create_build(
self,
build: dict | Build,
project_id: str = PROVIDE_PROJECT_ID,
wait: bool = True,
retry: Retry | _MethodDefault = DEFAULT,
timeout: float | None = None,
metadata: Sequence[tuple[str, str]] = (),
) -> Build:
"""
Starts a build with the specified configuration.
:param build: The build resource to create. If a dict is provided, it must be of the same form
as the protobuf message `google.cloud.devtools.cloudbuild_v1.types.Build`
:param project_id: Optional, Google Cloud Project project_id where the function belongs.
If set to None or missing, the default project_id from the GCP connection is used.
:param wait: Optional, wait for operation to finish.
:param retry: Optional, a retry object used to retry requests. If `None` is specified, requests
will not be retried.
:param timeout: Optional, the amount of time, in seconds, to wait for the request to complete.
Note that if `retry` is specified, the timeout applies to each individual attempt.
:param metadata: Optional, additional metadata that is provided to the method.
"""
client = self.get_conn()

self.log.info("Start creating build...")

operation = client.create_build(
request={"project_id": project_id, "build": build},
retry=retry,
timeout=timeout,
metadata=metadata,
)

id_ = self._get_build_id_from_operation(operation)
self.log.info("Build has been created: %s.", id_)

if not wait:
return self.get_build(id_=id_, project_id=project_id)

self.wait_for_operation(operation, timeout)

return self.get_build(id_=id_, project_id=project_id)

@GoogleBaseHook.fallback_to_default_project_id
def create_build_trigger(
self,
Expand Down
40 changes: 9 additions & 31 deletions tests/providers/google/cloud/hooks/test_cloud_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,44 +90,22 @@ def test_cancel_build(self, get_conn):
@mock.patch(
"airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook._get_build_id_from_operation"
)
@mock.patch("airflow.providers.google.cloud.hooks.cloud_build.TIME_TO_SLEEP_IN_SECONDS")
@mock.patch("airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook.get_conn")
def test_create_build_with_wait(self, get_conn, wait_time, mock_get_id_from_operation):
def test_create_build_without_waiting_for_result(self, get_conn, mock_get_id_from_operation):
get_conn.return_value.run_build_trigger.return_value = mock.MagicMock()
mock_get_id_from_operation.return_value = BUILD_ID

wait_time.return_value = 0

self.hook.create_build(build=BUILD, project_id=PROJECT_ID)

get_conn.return_value.create_build.assert_called_once_with(
request={"project_id": PROJECT_ID, "build": BUILD}, retry=DEFAULT, timeout=None, metadata=()
)

get_conn.return_value.create_build.return_value.result.assert_called_once_with()

get_conn.return_value.get_build.assert_called_once_with(
request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=()
self.hook.create_build_without_waiting_for_result(
build=BUILD, project_id=PROJECT_ID, location=LOCATION
)

@mock.patch(
"airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook._get_build_id_from_operation"
)
@mock.patch("airflow.providers.google.cloud.hooks.cloud_build.CloudBuildHook.get_conn")
def test_create_build_without_wait(self, get_conn, mock_get_id_from_operation):
get_conn.return_value.run_build_trigger.return_value = mock.MagicMock()
mock_get_id_from_operation.return_value = BUILD_ID

self.hook.create_build(build=BUILD, project_id=PROJECT_ID, wait=False)

mock_operation = get_conn.return_value.create_build

mock_operation.assert_called_once_with(
request={"project_id": PROJECT_ID, "build": BUILD}, retry=DEFAULT, timeout=None, metadata=()
)

get_conn.return_value.get_build.assert_called_once_with(
request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=()
request={"parent": PARENT, "project_id": PROJECT_ID, "build": BUILD},
retry=DEFAULT,
timeout=None,
metadata=(),
)

mock_get_id_from_operation.assert_called_once_with(mock_operation())
Expand Down Expand Up @@ -220,7 +198,7 @@ def test_retry_build_with_wait(self, get_conn, wait_time, mock_get_id_from_opera
request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=()
)

get_conn.return_value.retry_build.return_value.result.assert_called_once_with()
get_conn.return_value.retry_build.return_value.result.assert_called_once_with(timeout=None)

get_conn.return_value.get_build.assert_called_once_with(
request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=()
Expand Down Expand Up @@ -274,7 +252,7 @@ def test_run_build_trigger_with_wait(self, get_conn, wait_time, mock_get_id_from
metadata=(),
)

get_conn.return_value.run_build_trigger.return_value.result.assert_called_once_with()
get_conn.return_value.run_build_trigger.return_value.result.assert_called_once_with(timeout=None)

get_conn.return_value.get_build.assert_called_once_with(
request={"project_id": PROJECT_ID, "id": BUILD_ID}, retry=DEFAULT, timeout=None, metadata=()
Expand Down

0 comments on commit ee8eefb

Please sign in to comment.