Example #1
0
 def execute(self, context):
     hook = GKEHook(gcp_conn_id=self.gcp_conn_id, location=self.location)
     delete_result = hook.delete_cluster(name=self.name,
                                         project_id=self.project_id)
     return delete_result
Example #2
0
class TestGKEHookDelete(unittest.TestCase):
    def setUp(self):
        self.gke_hook = GKEHook(location=GKE_ZONE)
        self.gke_hook._client = mock.Mock()

    @mock.patch('airflow.gcp.hooks.base.CloudBaseHook.project_id',
                new_callable=PropertyMock,
                return_value=None)
    @mock.patch("airflow.gcp.hooks.kubernetes_engine.ParseDict")
    @mock.patch(
        "airflow.gcp.hooks.kubernetes_engine.GKEHook.wait_for_operation")
    def test_delete_cluster(self, wait_mock, convert_mock, mock_project_id):
        retry_mock, timeout_mock = mock.Mock(), mock.Mock()

        client_delete = self.gke_hook._client.delete_cluster = mock.Mock()

        self.gke_hook.delete_cluster(name=CLUSTER_NAME,
                                     project_id=TEST_GCP_PROJECT_ID,
                                     retry=retry_mock,
                                     timeout=timeout_mock)

        client_delete.assert_called_once_with(project_id=TEST_GCP_PROJECT_ID,
                                              zone=GKE_ZONE,
                                              cluster_id=CLUSTER_NAME,
                                              retry=retry_mock,
                                              timeout=timeout_mock)
        wait_mock.assert_called_once_with(client_delete.return_value)
        convert_mock.assert_not_called()

    @mock.patch('airflow.gcp.hooks.base.CloudBaseHook.project_id',
                new_callable=PropertyMock,
                return_value=None)
    @mock.patch("airflow.gcp.hooks.kubernetes_engine.GKEHook.log")
    @mock.patch("airflow.gcp.hooks.kubernetes_engine.ParseDict")
    @mock.patch(
        "airflow.gcp.hooks.kubernetes_engine.GKEHook.wait_for_operation")
    def test_delete_cluster_not_found(self, wait_mock, convert_mock, log_mock,
                                      mock_project_id):
        from google.api_core.exceptions import NotFound
        # To force an error
        message = 'Not Found'
        self.gke_hook._client.delete_cluster.side_effect = NotFound(
            message=message)

        self.gke_hook.delete_cluster(name='not-existing',
                                     project_id=TEST_GCP_PROJECT_ID)
        wait_mock.assert_not_called()
        convert_mock.assert_not_called()
        log_mock.info.assert_any_call("Assuming Success: %s", message)

    @mock.patch('airflow.gcp.hooks.base.CloudBaseHook.project_id',
                new_callable=PropertyMock,
                return_value=None)
    @mock.patch("airflow.gcp.hooks.kubernetes_engine.ParseDict")
    @mock.patch(
        "airflow.gcp.hooks.kubernetes_engine.GKEHook.wait_for_operation")
    def test_delete_cluster_error(self, wait_mock, convert_mock,
                                  mock_project_id):
        # To force an error
        self.gke_hook._client.delete_cluster.side_effect = AirflowException(
            '400')

        with self.assertRaises(AirflowException):
            self.gke_hook.delete_cluster(name='a-cluster')
            wait_mock.assert_not_called()
            convert_mock.assert_not_called()