def test_set_machine_type_should_handle_and_trim_gce_error( self, get_conn, _execute_set_machine_type, _check_zone_operation_status ): get_conn.return_value = {} _execute_set_machine_type.return_value = {"name": "test-operation"} _check_zone_operation_status.return_value = ast.literal_eval(self.MOCK_OP_RESPONSE) with self.assertRaises(AirflowException) as cm: op = ComputeEngineSetMachineTypeOperator( project_id=GCP_PROJECT_ID, zone=GCE_ZONE, resource_id=RESOURCE_ID, body=SET_MACHINE_TYPE_BODY, task_id='id', ) op.execute(None) err = cm.exception _check_zone_operation_status.assert_called_once_with( {}, "test-operation", GCP_PROJECT_ID, GCE_ZONE, mock.ANY ) _execute_set_machine_type.assert_called_once_with( GCE_ZONE, RESOURCE_ID, SET_MACHINE_TYPE_BODY, GCP_PROJECT_ID ) # Checking the full message was sometimes failing due to different order # of keys in the serialized JSON self.assertIn("400 BAD REQUEST: {", str(err)) # checking the square bracket trim self.assertIn("UNSUPPORTED_OPERATION", str(err))
def test_set_machine_type_should_throw_ex_when_missing_machine_type(self, mock_hook): with self.assertRaises(AirflowException) as cm: op = ComputeEngineSetMachineTypeOperator( project_id=GCP_PROJECT_ID, zone=GCE_ZONE, resource_id=RESOURCE_ID, body={}, task_id='id' ) op.execute(None) err = cm.exception self.assertIn("The required body field 'machineType' is missing. Please add it.", str(err)) mock_hook.assert_called_once_with( api_version='v1', gcp_conn_id='google_cloud_default', impersonation_chain=None, )
def test_set_machine_type_should_not_throw_ex_when_project_id_none(self, mock_hook): op = ComputeEngineSetMachineTypeOperator( zone=GCE_ZONE, resource_id=RESOURCE_ID, body=SET_MACHINE_TYPE_BODY, task_id='id' ) op.execute(None) mock_hook.assert_called_once_with( api_version='v1', gcp_conn_id='google_cloud_default', impersonation_chain=None, ) mock_hook.return_value.set_machine_type.assert_called_once_with( zone=GCE_ZONE, resource_id=RESOURCE_ID, body=SET_MACHINE_TYPE_BODY, project_id=None )
def test_set_machine_type_should_throw_ex_when_missing_resource_id(self, mock_hook): with self.assertRaises(AirflowException) as cm: op = ComputeEngineSetMachineTypeOperator( project_id=GCP_PROJECT_ID, zone=GCE_ZONE, resource_id="", body=SET_MACHINE_TYPE_BODY, task_id='id', ) op.execute(None) err = cm.exception self.assertIn("The required parameter 'resource_id' is missing", str(err)) mock_hook.assert_not_called()
def test_set_machine_type_should_throw_ex_when_missing_zone( self, mock_hook): with pytest.raises(AirflowException) as ctx: op = ComputeEngineSetMachineTypeOperator( project_id=GCP_PROJECT_ID, zone="", resource_id=RESOURCE_ID, body=SET_MACHINE_TYPE_BODY, task_id='id', ) op.execute(None) err = ctx.value assert "The required parameter 'zone' is missing" in str(err) mock_hook.assert_not_called()
def test_set_machine_type(self, mock_hook): mock_hook.return_value.set_machine_type.return_value = True op = ComputeEngineSetMachineTypeOperator(project_id=GCP_PROJECT_ID, zone=GCE_ZONE, resource_id=RESOURCE_ID, body=SET_MACHINE_TYPE_BODY, task_id='id') op.execute(None) mock_hook.assert_called_once_with(api_version='v1', gcp_conn_id='google_cloud_default') mock_hook.return_value.set_machine_type.assert_called_once_with( zone=GCE_ZONE, resource_id=RESOURCE_ID, body=SET_MACHINE_TYPE_BODY, project_id=GCP_PROJECT_ID)