def test_successful_copy_template_with_updated_nested_fields( self, mock_hook): mock_hook.return_value.get_instance_template.side_effect = [ HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT), GCE_INSTANCE_TEMPLATE_BODY_GET, GCE_INSTANCE_TEMPLATE_BODY_GET_NEW ] op = ComputeEngineCopyInstanceTemplateOperator( project_id=GCP_PROJECT_ID, resource_id=GCE_INSTANCE_TEMPLATE_NAME, task_id='id', body_patch={ "name": GCE_INSTANCE_TEMPLATE_NEW_NAME, "properties": { "machineType": "n1-standard-2", } }) result = op.execute(None) mock_hook.assert_called_once_with(api_version='v1', gcp_conn_id='google_cloud_default') body_insert = deepcopy(GCE_INSTANCE_TEMPLATE_BODY_INSERT) body_insert["properties"]["machineType"] = "n1-standard-2" mock_hook.return_value.insert_instance_template.assert_called_once_with( project_id=GCP_PROJECT_ID, body=body_insert, request_id=None) self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
def test_missing_name(self, mock_hook): mock_hook.return_value.get_instance_template.side_effect = [ HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT), GCE_INSTANCE_TEMPLATE_BODY_GET, GCE_INSTANCE_TEMPLATE_BODY_GET_NEW ] with self.assertRaises(AirflowException) as cm: op = ComputeEngineCopyInstanceTemplateOperator( project_id=GCP_PROJECT_ID, resource_id=GCE_INSTANCE_TEMPLATE_NAME, request_id=GCE_INSTANCE_TEMPLATE_REQUEST_ID, task_id='id', body_patch={"description": "New description"}) op.execute(None) err = cm.exception self.assertIn( "should contain at least name for the new operator " "in the 'name' field", str(err)) mock_hook.assert_not_called()
def test_successful_copy_template_with_bigger_array_fields( self, mock_hook): mock_hook.return_value.get_instance_template.side_effect = [ HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT), GCE_INSTANCE_TEMPLATE_BODY_GET, GCE_INSTANCE_TEMPLATE_BODY_GET_NEW ] op = ComputeEngineCopyInstanceTemplateOperator( project_id=GCP_PROJECT_ID, resource_id=GCE_INSTANCE_TEMPLATE_NAME, task_id='id', body_patch={ "name": GCE_INSTANCE_TEMPLATE_NEW_NAME, "properties": { "disks": [{ "kind": "compute#attachedDisk", "type": "SCRATCH", "licenses": [ "Updated String", ] }, { "kind": "compute#attachedDisk", "type": "PERSISTENT", "licenses": [ "Another String", ] }], } }) result = op.execute(None) mock_hook.assert_called_once_with(api_version='v1', gcp_conn_id='google_cloud_default') body_insert = deepcopy(GCE_INSTANCE_TEMPLATE_BODY_INSERT) body_insert["properties"]["disks"] = [{ "kind": "compute#attachedDisk", "type": "SCRATCH", "licenses": [ "Updated String", ] }, { "kind": "compute#attachedDisk", "type": "PERSISTENT", "licenses": [ "Another String", ] }] mock_hook.return_value.insert_instance_template.assert_called_once_with( project_id=GCP_PROJECT_ID, body=body_insert, request_id=None, ) self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
def test_idempotent_copy_template_when_already_copied(self, mock_hook): mock_hook.return_value.get_instance_template.side_effect = [ GCE_INSTANCE_TEMPLATE_BODY_GET_NEW ] op = ComputeEngineCopyInstanceTemplateOperator( project_id=GCP_PROJECT_ID, resource_id=GCE_INSTANCE_TEMPLATE_NAME, task_id='id', body_patch={"name": GCE_INSTANCE_TEMPLATE_NEW_NAME}) result = op.execute(None) mock_hook.assert_called_once_with(api_version='v1', gcp_conn_id='google_cloud_default') mock_hook.return_value.insert_instance_template.assert_not_called() self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
def test_successful_copy_template_missing_project_id(self, mock_hook): mock_hook.return_value.get_instance_template.side_effect = [ HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT), GCE_INSTANCE_TEMPLATE_BODY_GET, GCE_INSTANCE_TEMPLATE_BODY_GET_NEW ] op = ComputeEngineCopyInstanceTemplateOperator( resource_id=GCE_INSTANCE_TEMPLATE_NAME, task_id='id', body_patch={"name": GCE_INSTANCE_TEMPLATE_NEW_NAME}) result = op.execute(None) mock_hook.assert_called_once_with(api_version='v1', gcp_conn_id='google_cloud_default') mock_hook.return_value.insert_instance_template.assert_called_once_with( project_id=None, body=GCE_INSTANCE_TEMPLATE_BODY_INSERT, request_id=None) self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)
def test_successful_copy_template_with_smaller_array_fields( self, mock_hook): mock_hook.return_value.get_instance_template.side_effect = [ HttpError(resp=httplib2.Response({'status': 404}), content=EMPTY_CONTENT), GCE_INSTANCE_TEMPLATE_BODY_GET, GCE_INSTANCE_TEMPLATE_BODY_GET_NEW ] op = ComputeEngineCopyInstanceTemplateOperator( project_id=GCP_PROJECT_ID, resource_id=GCE_INSTANCE_TEMPLATE_NAME, task_id='id', body_patch={ "name": GCE_INSTANCE_TEMPLATE_NEW_NAME, "properties": { "machineType": "n1-standard-1", "networkInterfaces": [{ "network": "https://www.googleapis.com/compute/v1/" "projects/project/global/networks/default", "accessConfigs": [{ "type": "ONE_TO_ONE_NAT", "natIP": "8.8.8.8" }] }] } }) result = op.execute(None) mock_hook.assert_called_once_with(api_version='v1', gcp_conn_id='google_cloud_default') body_insert = deepcopy(GCE_INSTANCE_TEMPLATE_BODY_INSERT) body_insert["properties"]["networkInterfaces"] = [{ "network": "https://www.googleapis.com/compute/v1/" "projects/project/global/networks/default", "accessConfigs": [{ "type": "ONE_TO_ONE_NAT", "natIP": "8.8.8.8" }] }] mock_hook.return_value.insert_instance_template.assert_called_once_with( project_id=GCP_PROJECT_ID, body=body_insert, request_id=None) self.assertEqual(GCE_INSTANCE_TEMPLATE_BODY_GET_NEW, result)