def testGetErrorForNoError(self):
     build_response = {
         'id':
         1,
         'url':
         'https://build.chromium.org/p/m/builders/b/builds/1234',
         'status':
         'COMPLETED',
         'completed_ts':
         '1454367574000000',
         'created_ts':
         '1454367570000000',
         'result_details_json':
         json.dumps({
             'properties': {
                 'report': {
                     'result': {
                         'rev1': 'passed',
                         'rev2': 'failed'
                     },
                     'metadata': {
                         'regression_range_size': 2
                     }
                 }
             }
         })
     }
     self.assertEqual(
         monitor_try_job_pipeline._GetError(build_response, None, False,
                                            False), (None, None))
     self.assertEqual(
         monitor_try_job_pipeline._GetError({}, None, False, False),
         (None, None))
    def testGetErrorUnexpectedBuildFailure(self):
        build_response = {
            'result':
            'FAILED',
            'failure_reason':
            'BUILD_FAILURE',
            'result_details_json':
            json.dumps({
                'properties': {
                    'report': {
                        'metadata': {
                            'infra_failure': True
                        }
                    }
                }
            })
        }

        expected_error_dict = {
            'message': 'Buildbucket reported a general error.',
            'reason': MonitorTryJobPipeline.UNKNOWN
        }

        self.assertEqual(
            monitor_try_job_pipeline._GetError(build_response, None, False,
                                               False),
            (expected_error_dict, try_job_error.INFRA_FAILURE))
    def testGetErrorInfraFailure(self):
        build_response = {
            'result':
            'FAILED',
            'failure_reason':
            'INFRA_FAILURE',
            'result_details_json':
            json.dumps({
                'properties': {
                    'report': {
                        'metadata': {
                            'infra_failure': True
                        }
                    }
                }
            })
        }

        expected_error_dict = {
            'message': 'Try job encountered an infra issue during execution.',
            'reason': MonitorTryJobPipeline.UNKNOWN
        }

        self.assertEqual(
            monitor_try_job_pipeline._GetError(build_response, None, False,
                                               False),
            (expected_error_dict, try_job_error.INFRA_FAILURE))
    def testGetErrorForTimeout(self):
        expected_error_dict = {
            'message':
            'Try job monitoring was abandoned.',
            'reason':
            ('Timeout after %s hours' %
             waterfall_config.GetTryJobSettings().get('job_timeout_hours'))
        }

        self.assertEqual(
            monitor_try_job_pipeline._GetError({}, None, True, False),
            (expected_error_dict, try_job_error.TIMEOUT))
    def testGetErrorReportMissing(self):
        build_response = {
            'result_details_json': json.dumps({'properties': {}})
        }

        expected_error_dict = {
            'message': 'No result report was found.',
            'reason': MonitorTryJobPipeline.UNKNOWN
        }

        self.assertEqual(
            monitor_try_job_pipeline._GetError(build_response, None, False,
                                               True),
            (expected_error_dict, try_job_error.UNKNOWN))
    def testGetErrorUnknown(self):
        build_response = {
            'result_details_json': json.dumps({'error': {
                'abc': 'abc'
            }})
        }

        expected_error_dict = {
            'message': 'Buildbucket reported an error.',
            'reason': MonitorTryJobPipeline.UNKNOWN
        }

        self.assertEqual(
            monitor_try_job_pipeline._GetError(build_response, None, False,
                                               False),
            (expected_error_dict, try_job_error.CI_REPORTED_ERROR))
    def testGetErrorForBuildbucketReportedError(self):
        build_response = {
            'result_details_json':
            json.dumps({'error': {
                'message': 'Builder b not found'
            }})
        }

        expected_error_dict = {
            'message': 'Buildbucket reported an error.',
            'reason': 'Builder b not found'
        }

        self.assertEqual(
            monitor_try_job_pipeline._GetError(build_response, None, False,
                                               False),
            (expected_error_dict, try_job_error.CI_REPORTED_ERROR))
    def testGetErrorUnknownBuildbucketFailure(self):
        build_response = {
            'result': 'FAILED',
            'failure_reason': 'SOME_FAILURE',
            'result_details_json': json.dumps({'properties': {
                'report': {}
            }})
        }

        expected_error_dict = {
            'message': 'SOME_FAILURE',
            'reason': MonitorTryJobPipeline.UNKNOWN
        }

        self.assertEqual(
            monitor_try_job_pipeline._GetError(build_response, None, False,
                                               False),
            (expected_error_dict, try_job_error.UNKNOWN))