def testGetOneViolation(self): err = http_error.MakeDetailedHttpError( 400, url='https://mock.googleapis.com/v1/projects/your-stuff/junk/mine', content={ 'error': { 'code': 400, 'message': 'The request has violated one or more Org Policies. Please ' 'refer to the respective violations for more information.', 'status': 'FAILED_PRECONDITION', 'details': [{ '@type': 'type.googleapis.com/google.rpc.PreconditionFailure', 'violations': [{ 'subject': 'orgpolicy:projects/you-rusff', 'description': 'Constraint constraints/gcp.resourceLocations ' 'violated for projects/your-stuff attempting to ' 'create a function with location set to us-east4. ' 'See https://cloud.google.com/resource-manager/docs' '/organization-policy/org-policy-constraints for ' 'more information.', }]}]}}, details=http_error.ExampleErrorDetails()) self.assertEqual( self._GetViolations(err), {'orgpolicy:projects/you-rusff': 'Constraint constraints/gcp.resourceLocations ' 'violated for projects/your-stuff attempting to ' 'create a function with location set to us-east4. ' 'See https://cloud.google.com/resource-manager/docs' '/organization-policy/org-policy-constraints for ' 'more information.'})
def testGetSeveralFieldViolationsAndViolations(self): err = http_error.MakeDetailedHttpError( 400, url='https://mock.googleapis.com/v1/projects/your-stuff/junk/mine', content={ 'error': { 'code': 400, 'message': 'The request has errors.', 'status': 'INVALID_ARGUMENT', 'details': [ { '@type': 'type.googleapis.com/google.rpc.violations', 'violations': [ { 'type': 'type.googleapis.com/google.rpc.lien', 'subject': 'liens/123-456-abc', 'description': 'This house has a lien.' }]}, { '@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [ {'field': 'blog.blug', 'description': 'hahaha'}, {'field': 'doop.doop', 'description': 'lol'}, ] } ]}}, details=http_error.ExampleErrorDetails()) self.assertEqual( self._GetFieldViolations(err), {'blog.blug': 'hahaha', 'doop.doop': 'lol'}) self.assertEqual( self._GetViolations(err), {'liens/123-456-abc': 'This house has a lien.'})
def testGetSeveralViolations(self): err = http_error.MakeDetailedHttpError( 400, url='https://mock.googleapis.com/v1/projects/your-stuff/junk/mine', content={ 'error': { 'code': 400, 'message': 'The request has errors.', 'status': 'FAILED_PRECONDITION', 'details': [{ '@type': 'type.googleapis.com/google.rpc.PreconditionFailure', 'violations': [ { 'subject': 'blog', 'description': 'hahaha' }, { 'subject': 'doop', 'description': 'lol' }, ] }] } }, details=http_error.ExampleErrorDetails()) self.assertEqual(self._GetViolations(err), { 'blog': 'hahaha', 'doop': 'lol' })
def testGetOneFieldViolation(self): err = http_error.MakeDetailedHttpError( 400, url='https://mock.googleapis.com/v1/projects/your-stuff/junk/mine', content={ 'error': { 'code': 400, 'message': 'The request has errors.', 'status': 'INVALID_ARGUMENT', 'details': [{ '@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{ 'field': 'spec.revisionTemplate.spec.container.image', 'description': 'Invalid image provided in the revision' ' template. Expected [region.]gcr.io/repo-path[:tag' ' or @digest], obtained really/bad/image', }]}]}}, details=http_error.ExampleErrorDetails()) self.assertEqual( self._GetFieldViolations(err), {'spec.revisionTemplate.spec.container.image': 'Invalid image provided in the revision' ' template. Expected [region.]gcr.io/repo-path[:tag' ' or @digest], obtained really/bad/image'})
def testHttpExceptionErrorFormatV2ContentPrinterFormat(self): err = http_error.MakeDetailedHttpError( 400, url='https://mock.googleapis.com/v1/projects/your-stuff/junk/mine', content={ 'details': [ { '@type': 'type.googleapis.com/google.rpc.Quote', 'detail': "We're in a tight spot.", }, { '@type': 'type.googleapis.com/google.rpc.Quip', 'detail': "You're gonna need a bigger boat.", }, ], }, details=http_error.ExampleErrorDetails(),) exc = exceptions.HttpException( err, 'Error [{status_code}] {status_message}\n' '{.:value(details.detail.list(separator="\n"))}') self.assertEqual("""\ Error [400] Invalid request. We're in a tight spot. You're gonna need a bigger boat.""", exc.message)
def testGetFieldViolationsBadJson(self): err = http_error.MakeDetailedHttpError( 400, url='https://mock.googleapis.com/v1/projects/your-stuff/junk/mine', content={}, details=http_error.ExampleErrorDetails()) # Override content, since the factory doesn't let us make it invalid. err.content = 'this is bad json yo . it is . like. so bad' self.assertEqual(self._GetFieldViolations(err), {})
def testGetNoViolations(self): err = http_error.MakeDetailedHttpError( 400, url='https://mock.googleapis.com/v1/projects/your-stuff/junk/mine', content={ 'error': { 'code': 400, 'message': 'The request has errors.', 'status': 'INVALID_ARGUMENT', 'details': []}}, details=http_error.ExampleErrorDetails()) self.assertEqual(self._GetViolations(err), {})
def testDescribe_NoApp(self): """Test `gcloud app describe` raises informative error if no app found.""" properties.VALUES.core.project.Set(self.Project()) self.ExpectGetApplicationRequest( self.Project(), exception=http_error.MakeDetailedHttpError( code=404, details=http_error.ExampleErrorDetails())) missing_app_regex = (r'The current Google Cloud project \[{}\] ' 'does not contain an App Engine application. Use ' '`gcloud app create` to initialize an App Engine ' 'application within the project.'.format( self.Project())) with self.assertRaisesRegex(app_exceptions.MissingApplicationError, missing_app_regex): self.Run('app describe')
def ExpectSetDefault(self, project, service, version, num_tries=1, success=True): """Adds expected set-default call and response to mock client. Args: project: str, the project ID service: str, the name of the service being updated. version: str, the ID of the version being created. num_tries: int, number of times that setting default will be attempted. success: bool, whether operation is expected to succeed. """ op_name = 'apps/{0}/services/{1}/versions/{2}'.format( project, service, version) traffic_split = encoding.PyValueToMessage(self.messages.TrafficSplit, { 'allocations': { version: 1.0 }, 'shardBy': 'UNSPECIFIED' }) patch_call = self.messages.AppengineAppsServicesPatchRequest( name='apps/{0}/services/{1}'.format(project, service), updateMask='split', migrateTraffic=False, service=self.messages.Service(split=traffic_split)) err = http_error.MakeDetailedHttpError( message='Service does not exist', details=http_error.ExampleErrorDetails()) if success: final_response = self.messages.Operation( name=op_name, done=True, response=encoding.JsonToMessage( self.messages.Operation.ResponseValue, encoding.MessageToJson(patch_call))) else: final_response = err retry.ExpectWithRetries(method=self.mock_client.apps_services.Patch, request=patch_call, response=err, final_response=final_response, num_retries=num_tries - 1)
def testHttpExceptionErrorFormatV2Details(self): err = http_error.MakeDetailedHttpError( 400, url='https://mock.googleapis.com/v1/projects/your-stuff/junk/mine', details=http_error.ExampleErrorDetails(), ) exc = exceptions.HttpException(err) self.assertEqual("""\ Invalid request API reason: Invalid request. - '@type': type.googleapis.com/google.rpc.BadRequest fieldViolations: - description: Description of the violation. field: version.deployment.container.image - '@type': type.googleapis.com/google.rpc.DebugInfo detail: '[ORIGINAL ERROR] error_type::error: Error details.\\n\ And then more details.'""", exc.message)
def testBrowse_NoApp(self): """Test app browse command raises error when no app exists.""" project = 'example.com:appid' properties.VALUES.core.project.Set(project) self.ExpectGetApplicationRequest( project, hostname='appid.exampleplex.com', exception=http_error.MakeDetailedHttpError( code=404, details=http_error.ExampleErrorDetails())) missing_app_regex = ( r'The current Google Cloud project ' r'\[example.com:appid\] does not contain an App ' r'Engine application. Use `gcloud app create` to ' r'initialize an App Engine application within the ' r'project.') with self.assertRaisesRegex(app_exceptions.MissingApplicationError, missing_app_regex): self.Run('app browse')
def testHttpExceptionErrorFormatV2ContentVsPayload(self): err = http_error.MakeDetailedHttpError( 400, url='https://mock.googleapis.com/v1/projects/your-stuff/junk/mine', content={ 'details': [ { '@type': 'type.googleapis.com/google.rpc.Quote', 'detail': "We're in a tight spot.", }, { '@type': 'type.googleapis.com/google.rpc.Quip', 'detail': "You're gonna need a bigger boat.", }, ], }, details=http_error.ExampleErrorDetails(), ) exc = exceptions.HttpException( err, 'Error [{status_code}] {status_message}' '{error.details.detail?\nerror.details.detail:\n{?}}' '{.details.detail?\n.details.detail:\n{?}}' '{details.detail?\ndetails.detail:\n{?}}' ) self.assertEqual("""\ Error [400] Invalid request. error.details.detail: - '[ORIGINAL ERROR] error_type::error: Error details.\\n\ And then more details.' .details.detail: - We're in a tight spot. - You're gonna need a bigger boat. details.detail: - '[ORIGINAL ERROR] error_type::error: Error details.\\n\ And then more details.'""", exc.message)
def testHttpExceptionErrorFormatV2AggregateWithMissingDescription(self): err = http_error.MakeDetailedHttpError( 400, url='https://mock.googleapis.com/v1/projects/your-stuff/junk/mine', content={ 'error': { 'code': 400, 'message': 'Precondition check failed.', 'status': 'FAILED_PRECONDITION', 'details': [ { '@type': 'type.googleapis.com/google.rpc.violations', 'violations': [ { 'type': 'type.googleapis.com/google.rpc.lien', 'subject': 'liens/123-456-abc', }, { 'type': 'type.googleapis.com/google.rpc.lien', 'subject': 'liens/123-456-abc', 'description': 'Remove the lien [1.2].', }, ], }, { '@type': 'type.googleapis.com/google.rpc.violations', 'violations': [ { 'type': 'type.googleapis.com/google.rpc.lien', 'subject': 'liens/123-456-xyz', 'description': 'Remove the lien [2.1].', }, { 'type': 'type.googleapis.com/google.rpc.lien', 'subject': 'liens/123-456-abc', }, ], }, ], }, }, details=http_error.ExampleErrorDetails(), ) exc = exceptions.HttpException( err, 'Error [{status_code}] {status_message}' '{details.violations.description?\n{?}}' ) self.assertEqual("""\ Error [400] Precondition check failed. - - Remove the lien [1.2]. - - Remove the lien [2.1].""", exc.message) exc = exceptions.HttpException(err) self.assertEqual("""\ Invalid request API reason: Precondition check failed. - '@type': type.googleapis.com/google.rpc.violations violations: - subject: liens/123-456-abc type: type.googleapis.com/google.rpc.lien - description: Remove the lien [1.2]. subject: liens/123-456-abc type: type.googleapis.com/google.rpc.lien - '@type': type.googleapis.com/google.rpc.violations violations: - description: Remove the lien [2.1]. subject: liens/123-456-xyz type: type.googleapis.com/google.rpc.lien - subject: liens/123-456-abc type: type.googleapis.com/google.rpc.lien""", exc.message)