class GKEClusterHookDeleteTest(unittest.TestCase):
        def setUp(self):
            with mock.patch.object(GKEClusterHook,
                                   "__init__",
                                   return_value=None):
                self.gke_hook = GKEClusterHook(None, None, None)
                self.gke_hook.project_id = TEST_PROJECT_ID
                self.gke_hook.location = ZONE
                self.gke_hook.client = mock.Mock()

        @mock.patch(
            "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook._dict_to_proto"
        )
        @mock.patch(
            "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.wait_for_operation"
        )
        def test_delete_cluster(self, wait_mock, convert_mock):
            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,
                                         retry=retry_mock,
                                         timeout=timeout_mock)

            client_delete.assert_called_with(project_id=TEST_PROJECT_ID,
                                             zone=ZONE,
                                             cluster_id=CLUSTER_NAME,
                                             retry=retry_mock,
                                             timeout=timeout_mock)
            wait_mock.assert_called_with(client_delete.return_value)
            convert_mock.assert_not_called()

        @mock.patch(
            "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook._dict_to_proto"
        )
        @mock.patch(
            "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.wait_for_operation"
        )
        def test_delete_cluster_error(self, wait_mock, convert_mock):
            # To force an error
            self.gke_hook.client.delete_cluster.side_effect = AirflowException(
                '400')

            with self.assertRaises(AirflowException):
                self.gke_hook.delete_cluster(None)
                wait_mock.assert_not_called()
                convert_mock.assert_not_called()
Exemplo n.º 2
0
 def execute(self, context):
     self._check_input()
     hook = GKEClusterHook(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
class GKEClusterHookDeleteTest(unittest.TestCase):
    def setUp(self):
        with mock.patch.object(GKEClusterHook, "__init__", return_value=None):
            self.gke_hook = GKEClusterHook(None, None, None)
            self.gke_hook.project_id = TEST_PROJECT_ID
            self.gke_hook.location = ZONE
            self.gke_hook.client = mock.Mock()

    @mock.patch("airflow.contrib.hooks.gcp_container_hook.GKEClusterHook._dict_to_proto")
    @mock.patch(
        "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.wait_for_operation")
    def test_delete_cluster(self, wait_mock, convert_mock):
        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, retry=retry_mock,
                                     timeout=timeout_mock)

        client_delete.assert_called_with(project_id=TEST_PROJECT_ID, zone=ZONE,
                                         cluster_id=CLUSTER_NAME,
                                         retry=retry_mock, timeout=timeout_mock)
        wait_mock.assert_called_with(client_delete.return_value)
        convert_mock.assert_not_called()

    @mock.patch("airflow.contrib.hooks.gcp_container_hook.GKEClusterHook._dict_to_proto")
    @mock.patch(
        "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.wait_for_operation")
    def test_delete_cluster_error(self, wait_mock, convert_mock):
        # To force an error
        self.gke_hook.client.delete_cluster.side_effect = AirflowException('400')

        with self.assertRaises(AirflowException):
            self.gke_hook.delete_cluster(None)
            wait_mock.assert_not_called()
            convert_mock.assert_not_called()
class GKEClusterHookDeleteTest(unittest.TestCase):
    def setUp(self):
        self.gke_hook = GKEClusterHook(location=GKE_ZONE)
        self.gke_hook._client = mock.Mock()

    @mock.patch("airflow.contrib.hooks.gcp_container_hook.GKEClusterHook._dict_to_proto")
    @mock.patch(
        "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.wait_for_operation")
    def test_delete_cluster(self, wait_mock, convert_mock):
        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_with(project_id=TEST_GCP_PROJECT_ID,
                                         zone=GKE_ZONE,
                                         cluster_id=CLUSTER_NAME,
                                         retry=retry_mock,
                                         timeout=timeout_mock)
        wait_mock.assert_called_with(client_delete.return_value)
        convert_mock.assert_not_called()

    @mock.patch(
        "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.log")
    @mock.patch("airflow.contrib.hooks.gcp_container_hook.GKEClusterHook._dict_to_proto")
    @mock.patch(
        "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.wait_for_operation")
    def test_delete_cluster_not_found(self, wait_mock, convert_mock, log_mock):
        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('not-existing')
        wait_mock.assert_not_called()
        convert_mock.assert_not_called()
        log_mock.info.assert_any_call("Assuming Success: %s", message)

    @mock.patch("airflow.contrib.hooks.gcp_container_hook.GKEClusterHook._dict_to_proto")
    @mock.patch(
        "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.wait_for_operation")
    def test_delete_cluster_error(self, wait_mock, convert_mock):
        # To force an error
        self.gke_hook._client.delete_cluster.side_effect = AirflowException('400')

        with self.assertRaises(AirflowException):
            self.gke_hook.delete_cluster('a-cluster')
            wait_mock.assert_not_called()
            convert_mock.assert_not_called()
Exemplo n.º 5
0
class GKEClusterHookDeleteTest(unittest.TestCase):
    def setUp(self):
        self.gke_hook = GKEClusterHook(location=ZONE)
        self.gke_hook._client = mock.Mock()

    @mock.patch("airflow.contrib.hooks.gcp_container_hook.GKEClusterHook._dict_to_proto")
    @mock.patch(
        "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.wait_for_operation")
    def test_delete_cluster(self, wait_mock, convert_mock):
        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_PROJECT_ID, retry=retry_mock,
                                     timeout=timeout_mock)

        client_delete.assert_called_with(project_id=TEST_PROJECT_ID, zone=ZONE,
                                         cluster_id=CLUSTER_NAME,
                                         retry=retry_mock, timeout=timeout_mock)
        wait_mock.assert_called_with(client_delete.return_value)
        convert_mock.assert_not_called()

    @mock.patch(
        "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.log")
    @mock.patch("airflow.contrib.hooks.gcp_container_hook.GKEClusterHook._dict_to_proto")
    @mock.patch(
        "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.wait_for_operation")
    def test_delete_cluster_not_found(self, wait_mock, convert_mock, log_mock):
        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(None)
        wait_mock.assert_not_called()
        convert_mock.assert_not_called()
        log_mock.info.assert_any_call("Assuming Success: " + message)

    @mock.patch("airflow.contrib.hooks.gcp_container_hook.GKEClusterHook._dict_to_proto")
    @mock.patch(
        "airflow.contrib.hooks.gcp_container_hook.GKEClusterHook.wait_for_operation")
    def test_delete_cluster_error(self, wait_mock, convert_mock):
        # To force an error
        self.gke_hook._client.delete_cluster.side_effect = AirflowException('400')

        with self.assertRaises(AirflowException):
            self.gke_hook.delete_cluster(None)
            wait_mock.assert_not_called()
            convert_mock.assert_not_called()
 def execute(self, context):
     self._check_input()
     hook = GKEClusterHook(self.project_id, self.location)
     delete_result = hook.delete_cluster(name=self.name)
     return delete_result