Lee-W commented on code in PR #34071:
URL: https://github.com/apache/airflow/pull/34071#discussion_r1329899970


##########
tests/providers/databricks/hooks/test_databricks.py:
##########
@@ -616,6 +647,58 @@ def test_start_cluster(self, mock_requests):
             timeout=self.hook.timeout_seconds,
         )
 
+    @mock.patch("airflow.providers.databricks.hooks.databricks_base.requests")
+    def test_activate_cluster_with_running_state(self, mock_requests):
+        running_states = ["RUNNING", "RESIZING"]
+        json = {"cluster_id": CLUSTER_ID}
+        for state in running_states:
+            mock_requests.codes.ok = 200
+            mock_requests.get.return_value.json.return_value = {"state": 
state, "state_message": ""}
+            self.hook.activate_cluster(json=json, polling=5, timeout=60)
+            mock_requests.get.assert_called_once_with(
+                get_cluster_endpoint(HOST),
+                json=None,
+                params={"cluster_id": CLUSTER_ID},
+                auth=HTTPBasicAuth(LOGIN, PASSWORD),
+                headers=self.hook.user_agent_header,
+                timeout=self.hook.timeout_seconds,
+            )
+            mock_requests.get.reset_mock()

Review Comment:
   ```suggestion
       @pytest.mark.parametrize("state", ("RUNNING", "RESIZING"))
       
@mock.patch("airflow.providers.databricks.hooks.databricks_base.requests")
       def test_activate_cluster_with_running_state(self, mock_requests, state):
           running_states = ["RUNNING", "RESIZING"]
           json = {"cluster_id": CLUSTER_ID}
           
           mock_requests.codes.ok = 200
           mock_requests.get.return_value.json.return_value = {"state": state, 
"state_message": ""}
           self.hook.activate_cluster(json=json, polling=5, timeout=60)
           mock_requests.get.assert_called_once_with(
               get_cluster_endpoint(HOST),
               json=None,
               params={"cluster_id": CLUSTER_ID},
               auth=HTTPBasicAuth(LOGIN, PASSWORD),
               headers=self.hook.user_agent_header,
               timeout=self.hook.timeout_seconds,
           )
           mock_requests.get.reset_mock()
   ```



##########
airflow/providers/databricks/hooks/databricks.py:
##########
@@ -490,6 +567,41 @@ def start_cluster(self, json: dict) -> None:
         """
         self._do_api_call(START_CLUSTER_ENDPOINT, json)
 
+    def activate_cluster(self, json: dict, polling: int, timeout: int | None = 
None) -> None:
+        """
+        Start the cluster, and wait for it to be ready.
+
+        :param json: json dictionary containing cluster specification.
+        :param polling: polling interval in seconds.
+        :param timeout: timeout in seconds.
+        """
+        cluster_id = json["cluster_id"]
+
+        api_called = False
+        time_start = time.time()
+
+        while True:

Review Comment:
   Do we want to add an option for users to trigger the API simply but not wait 
for it?



##########
tests/providers/databricks/hooks/test_databricks.py:
##########
@@ -616,6 +647,58 @@ def test_start_cluster(self, mock_requests):
             timeout=self.hook.timeout_seconds,
         )
 
+    @mock.patch("airflow.providers.databricks.hooks.databricks_base.requests")
+    def test_activate_cluster_with_running_state(self, mock_requests):
+        running_states = ["RUNNING", "RESIZING"]
+        json = {"cluster_id": CLUSTER_ID}
+        for state in running_states:
+            mock_requests.codes.ok = 200
+            mock_requests.get.return_value.json.return_value = {"state": 
state, "state_message": ""}
+            self.hook.activate_cluster(json=json, polling=5, timeout=60)
+            mock_requests.get.assert_called_once_with(
+                get_cluster_endpoint(HOST),
+                json=None,
+                params={"cluster_id": CLUSTER_ID},
+                auth=HTTPBasicAuth(LOGIN, PASSWORD),
+                headers=self.hook.user_agent_header,
+                timeout=self.hook.timeout_seconds,
+            )
+            mock_requests.get.reset_mock()

Review Comment:
   I think this can be applied to other tests as well



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to