Beispiel #1
0
 def test_create_should_throw_ex_when_empty_instance(self, mock_hook):
     with self.assertRaises(AirflowException) as cm:
         op = CloudSqlInstanceCreateOperator(project_id=PROJECT_ID,
                                             body=CREATE_BODY,
                                             instance="",
                                             task_id="id")
         op.execute(None)
     err = cm.exception
     self.assertIn("The required parameter 'instance' is empty", str(err))
     mock_hook.assert_not_called()
Beispiel #2
0
 def test_create_should_validate_non_empty_fields(self, mock_hook):
     empty_tier_body = {
         "name": INSTANCE_NAME,
         "settings": {
             "tier":
             "",  # Field can't be empty (defined in CLOUD_SQL_VALIDATION).
             # Testing if the validation catches this.
         }
     }
     with self.assertRaises(AirflowException) as cm:
         op = CloudSqlInstanceCreateOperator(project_id=PROJECT_ID,
                                             body=empty_tier_body,
                                             instance=INSTANCE_NAME,
                                             task_id="id")
         op.execute(None)
     err = cm.exception
     self.assertIn(
         "The body field 'settings.tier' can't be empty. "
         "Please provide a value.", str(err))
     mock_hook.assert_called_once_with(api_version="v1beta4",
                                       gcp_conn_id="google_cloud_default")
Beispiel #3
0
 def test_create_should_validate_list_type(self, mock_hook):
     wrong_list_type_body = {
         "name": INSTANCE_NAME,
         "settings": {
             "tier": "db-n1-standard-1",
             "ipConfiguration": {
                 "authorizedNetworks": {}  # Should be a list, not a dict.
                 # Testing if the validation catches this.
             }
         }
     }
     with self.assertRaises(AirflowException) as cm:
         op = CloudSqlInstanceCreateOperator(project_id=PROJECT_ID,
                                             body=wrong_list_type_body,
                                             instance=INSTANCE_NAME,
                                             task_id="id")
         op.execute(None)
     err = cm.exception
     self.assertIn(
         "The field 'settings.ipConfiguration.authorizedNetworks' "
         "should be of list type according to the specification", str(err))
     mock_hook.assert_called_once_with(api_version="v1beta4",
                                       gcp_conn_id="google_cloud_default")
Beispiel #4
0
 def test_instance_create_idempotent(self, mock_hook,
                                     _check_if_instance_exists):
     _check_if_instance_exists.return_value = True
     mock_hook.return_value.create_instance.return_value = True
     op = CloudSqlInstanceCreateOperator(project_id=PROJECT_ID,
                                         instance=INSTANCE_NAME,
                                         body=CREATE_BODY,
                                         task_id="id")
     result = op.execute(
         context={  # pylint: disable=assignment-from-no-return
             'task_instance': mock.Mock()
         })
     mock_hook.assert_called_once_with(api_version="v1beta4",
                                       gcp_conn_id="google_cloud_default")
     mock_hook.return_value.create_instance.assert_not_called()
     self.assertIsNone(result)