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)
def test_set_machine_type_with_templates(self, _): dag_id = 'test_dag_id' args = {'start_date': DEFAULT_DATE} self.dag = DAG(dag_id, default_args=args) # pylint: disable=attribute-defined-outside-init op = ComputeEngineSetMachineTypeOperator( project_id='{{ dag.dag_id }}', zone='{{ dag.dag_id }}', resource_id='{{ dag.dag_id }}', body={}, gcp_conn_id='{{ dag.dag_id }}', api_version='{{ dag.dag_id }}', task_id='id', dag=self.dag) ti = TaskInstance(op, DEFAULT_DATE) ti.render_templates() self.assertEqual(dag_id, getattr(op, 'project_id')) self.assertEqual(dag_id, getattr(op, 'zone')) self.assertEqual(dag_id, getattr(op, 'resource_id')) self.assertEqual(dag_id, getattr(op, 'gcp_conn_id')) self.assertEqual(dag_id, getattr(op, 'api_version'))
task_id='gcp_compute_stop_task') # [END howto_operator_gce_stop] # Duplicate stop for idempotence testing # [START howto_operator_gce_stop_no_project_id] gce_instance_stop2 = ComputeEngineStopInstanceOperator( zone=GCE_ZONE, resource_id=GCE_INSTANCE, task_id='gcp_compute_stop_task2') # [END howto_operator_gce_stop_no_project_id] # [START howto_operator_gce_set_machine_type] gce_set_machine_type = ComputeEngineSetMachineTypeOperator( project_id=GCP_PROJECT_ID, zone=GCE_ZONE, resource_id=GCE_INSTANCE, body={ 'machineType': 'zones/{}/machineTypes/{}'.format(GCE_ZONE, GCE_SHORT_MACHINE_TYPE_NAME) }, task_id='gcp_compute_set_machine_type', ) # [END howto_operator_gce_set_machine_type] # Duplicate set machine type for idempotence testing # [START howto_operator_gce_set_machine_type_no_project_id] gce_set_machine_type2 = ComputeEngineSetMachineTypeOperator( zone=GCE_ZONE, resource_id=GCE_INSTANCE, body={ 'machineType': 'zones/{}/machineTypes/{}'.format(GCE_ZONE, GCE_SHORT_MACHINE_TYPE_NAME)
project_id=GCP_PROJECT_ID, zone=GCE_ZONE, resource_id=GCE_INSTANCE, task_id='gcp_compute_stop_task') # [END howto_operator_gce_stop] # Duplicate stop for idempotence testing # [START howto_operator_gce_stop_no_project_id] gce_instance_stop2 = ComputeEngineStopInstanceOperator( zone=GCE_ZONE, resource_id=GCE_INSTANCE, task_id='gcp_compute_stop_task2') # [END howto_operator_gce_stop_no_project_id] # [START howto_operator_gce_set_machine_type] gce_set_machine_type = ComputeEngineSetMachineTypeOperator( project_id=GCP_PROJECT_ID, zone=GCE_ZONE, resource_id=GCE_INSTANCE, body=SET_MACHINE_TYPE_BODY, task_id='gcp_compute_set_machine_type') # [END howto_operator_gce_set_machine_type] # Duplicate set machine type for idempotence testing # [START howto_operator_gce_set_machine_type_no_project_id] gce_set_machine_type2 = ComputeEngineSetMachineTypeOperator( zone=GCE_ZONE, resource_id=GCE_INSTANCE, body=SET_MACHINE_TYPE_BODY, task_id='gcp_compute_set_machine_type2') # [END howto_operator_gce_set_machine_type_no_project_id] gce_instance_start >> gce_instance_start2 >> gce_instance_stop >> \ gce_instance_stop2 >> gce_set_machine_type >> gce_set_machine_type2