Beispiel #1
0
 def execute(self, context):
     self._check_input()
     hook = GKEClusterHook(gcp_conn_id=self.gcp_conn_id,
                           location=self.location)
     create_op = hook.create_cluster(cluster=self.body,
                                     project_id=self.project_id)
     return create_op
class GKEClusterHookCreateTest(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_create_cluster_proto(self, wait_mock, convert_mock):
        from google.cloud.container_v1.proto.cluster_service_pb2 import Cluster

        mock_cluster_proto = Cluster()
        mock_cluster_proto.name = CLUSTER_NAME

        retry_mock, timeout_mock = mock.Mock(), mock.Mock()

        client_create = self.gke_hook.client.create_cluster = mock.Mock()

        self.gke_hook.create_cluster(mock_cluster_proto, retry=retry_mock,
                                     timeout=timeout_mock)

        client_create.assert_called_with(project_id=TEST_PROJECT_ID, zone=ZONE,
                                         cluster=mock_cluster_proto,
                                         retry=retry_mock, timeout=timeout_mock)
        wait_mock.assert_called_with(client_create.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_dict(self, wait_mock, convert_mock):
        mock_cluster_dict = {'name': CLUSTER_NAME}
        retry_mock, timeout_mock = mock.Mock(), mock.Mock()

        client_create = self.gke_hook.client.create_cluster = mock.Mock()
        proto_mock = convert_mock.return_value = mock.Mock()

        self.gke_hook.create_cluster(mock_cluster_dict, retry=retry_mock,
                                     timeout=timeout_mock)

        client_create.assert_called_with(project_id=TEST_PROJECT_ID, zone=ZONE,
                                         cluster=proto_mock,
                                         retry=retry_mock, timeout=timeout_mock)
        wait_mock.assert_called_with(client_create.return_value)
        self.assertEqual(convert_mock.call_args[1]['py_dict'], mock_cluster_dict)

    @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_create_cluster_error(self, wait_mock, convert_mock):
        # to force an error
        mock_cluster_proto = None

        with self.assertRaises(AirflowException):
            self.gke_hook.create_cluster(mock_cluster_proto)
            wait_mock.assert_not_called()
            convert_mock.assert_not_called()
class GKEClusterHookCreateTest(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_create_cluster_proto(self, wait_mock, convert_mock):
        from google.cloud.container_v1.proto.cluster_service_pb2 import Cluster

        mock_cluster_proto = Cluster()
        mock_cluster_proto.name = CLUSTER_NAME

        retry_mock, timeout_mock = mock.Mock(), mock.Mock()

        client_create = self.gke_hook._client.create_cluster = mock.Mock()

        self.gke_hook.create_cluster(mock_cluster_proto,
                                     project_id=TEST_GCP_PROJECT_ID,
                                     retry=retry_mock,
                                     timeout=timeout_mock)

        client_create.assert_called_with(project_id=TEST_GCP_PROJECT_ID,
                                         zone=GKE_ZONE,
                                         cluster=mock_cluster_proto,
                                         retry=retry_mock, timeout=timeout_mock)
        wait_mock.assert_called_with(client_create.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_create_cluster_dict(self, wait_mock, convert_mock):
        mock_cluster_dict = {'name': CLUSTER_NAME}
        retry_mock, timeout_mock = mock.Mock(), mock.Mock()

        client_create = self.gke_hook._client.create_cluster = mock.Mock()
        proto_mock = convert_mock.return_value = mock.Mock()

        self.gke_hook.create_cluster(mock_cluster_dict,
                                     project_id=TEST_GCP_PROJECT_ID,
                                     retry=retry_mock,
                                     timeout=timeout_mock)

        client_create.assert_called_with(project_id=TEST_GCP_PROJECT_ID,
                                         zone=GKE_ZONE,
                                         cluster=proto_mock,
                                         retry=retry_mock, timeout=timeout_mock)
        wait_mock.assert_called_with(client_create.return_value)
        self.assertEqual(convert_mock.call_args[1]['py_dict'], mock_cluster_dict)

    @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_create_cluster_error(self, wait_mock, convert_mock):
        # to force an error
        mock_cluster_proto = None

        with self.assertRaises(AirflowException):
            self.gke_hook.create_cluster(mock_cluster_proto)
            wait_mock.assert_not_called()
            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_create_cluster_already_exists(self, wait_mock, convert_mock, log_mock):
        from google.api_core.exceptions import AlreadyExists
        # To force an error
        message = 'Already Exists'
        self.gke_hook._client.create_cluster.side_effect = AlreadyExists(message=message)

        self.gke_hook.create_cluster({})
        wait_mock.assert_not_called()
        self.assertEqual(convert_mock.call_count, 1)
        log_mock.info.assert_any_call("Assuming Success: %s", message)
 def execute(self, context):
     self._check_input()
     hook = GKEClusterHook(self.project_id, self.location)
     create_op = hook.create_cluster(cluster=self.body)
     return create_op
class GKEClusterHookCreateTest(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_create_cluster_proto(self, wait_mock, convert_mock):
        from google.cloud.container_v1.proto.cluster_service_pb2 import Cluster

        mock_cluster_proto = Cluster()
        mock_cluster_proto.name = CLUSTER_NAME

        retry_mock, timeout_mock = mock.Mock(), mock.Mock()

        client_create = self.gke_hook.client.create_cluster = mock.Mock()

        self.gke_hook.create_cluster(mock_cluster_proto,
                                     retry=retry_mock,
                                     timeout=timeout_mock)

        client_create.assert_called_with(project_id=TEST_PROJECT_ID,
                                         zone=ZONE,
                                         cluster=mock_cluster_proto,
                                         retry=retry_mock,
                                         timeout=timeout_mock)
        wait_mock.assert_called_with(client_create.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_dict(self, wait_mock, convert_mock):
        mock_cluster_dict = {'name': CLUSTER_NAME}
        retry_mock, timeout_mock = mock.Mock(), mock.Mock()

        client_create = self.gke_hook.client.create_cluster = mock.Mock()
        proto_mock = convert_mock.return_value = mock.Mock()

        self.gke_hook.create_cluster(mock_cluster_dict,
                                     retry=retry_mock,
                                     timeout=timeout_mock)

        client_create.assert_called_with(project_id=TEST_PROJECT_ID,
                                         zone=ZONE,
                                         cluster=proto_mock,
                                         retry=retry_mock,
                                         timeout=timeout_mock)
        wait_mock.assert_called_with(client_create.return_value)
        self.assertEqual(convert_mock.call_args[1]['py_dict'],
                         mock_cluster_dict)

    @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_create_cluster_error(self, wait_mock, convert_mock):
        # to force an error
        mock_cluster_proto = None

        with self.assertRaises(AirflowException):
            self.gke_hook.create_cluster(mock_cluster_proto)
            wait_mock.assert_not_called()
            convert_mock.assert_not_called()
class GKEClusterHookCreateTest(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_create_cluster_proto(self, wait_mock, convert_mock):
        from google.cloud.container_v1.proto.cluster_service_pb2 import Cluster

        mock_cluster_proto = Cluster()
        mock_cluster_proto.name = CLUSTER_NAME

        retry_mock, timeout_mock = mock.Mock(), mock.Mock()

        client_create = self.gke_hook._client.create_cluster = mock.Mock()

        self.gke_hook.create_cluster(mock_cluster_proto, project_id=TEST_PROJECT_ID, retry=retry_mock,
                                     timeout=timeout_mock)

        client_create.assert_called_with(project_id=TEST_PROJECT_ID, zone=ZONE,
                                         cluster=mock_cluster_proto,
                                         retry=retry_mock, timeout=timeout_mock)
        wait_mock.assert_called_with(client_create.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_create_cluster_dict(self, wait_mock, convert_mock):
        mock_cluster_dict = {'name': CLUSTER_NAME}
        retry_mock, timeout_mock = mock.Mock(), mock.Mock()

        client_create = self.gke_hook._client.create_cluster = mock.Mock()
        proto_mock = convert_mock.return_value = mock.Mock()

        self.gke_hook.create_cluster(mock_cluster_dict, project_id=TEST_PROJECT_ID, retry=retry_mock,
                                     timeout=timeout_mock)

        client_create.assert_called_with(project_id=TEST_PROJECT_ID, zone=ZONE,
                                         cluster=proto_mock,
                                         retry=retry_mock, timeout=timeout_mock)
        wait_mock.assert_called_with(client_create.return_value)
        self.assertEqual(convert_mock.call_args[1]['py_dict'], mock_cluster_dict)

    @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_create_cluster_error(self, wait_mock, convert_mock):
        # to force an error
        mock_cluster_proto = None

        with self.assertRaises(AirflowException):
            self.gke_hook.create_cluster(mock_cluster_proto)
            wait_mock.assert_not_called()
            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_create_cluster_already_exists(self, wait_mock, convert_mock, log_mock):
        from google.api_core.exceptions import AlreadyExists
        # To force an error
        message = 'Already Exists'
        self.gke_hook._client.create_cluster.side_effect = AlreadyExists(message=message)

        self.gke_hook.create_cluster({})
        wait_mock.assert_not_called()
        self.assertEquals(convert_mock.call_count, 1)
        log_mock.info.assert_any_call("Assuming Success: " + message)