def testGetResultAnalysisStatusNotFoundUntriaged(self):
        dummy_result = {
            "failures": [
                {"step_name": "a", "first_failure": 98, "last_pass": None, "suspected_cls": []},
                {"step_name": "b", "first_failure": 98, "last_pass": None, "suspected_cls": []},
            ]
        }

        self.assertEqual(
            wf_analysis_result_status.NOT_FOUND_UNTRIAGED,
            identify_culprit_pipeline._GetResultAnalysisStatus(dummy_result),
        )
  def testGetResultAnalysisStatusFoundUntriaged(self):
    dummy_result = {
        'failures': [
            {
                'step_name': 'a',
                'first_failure': 98,
                'last_pass': None,
                'suspected_cls': [
                    {
                        'build_number': 99,
                        'repo_name': 'chromium',
                        'revision': 'r99_2',
                        'commit_position': None,
                        'url': None,
                        'score': 1,
                        'hints': {
                            'modified f99_2.cc (and it was in log)': 1,
                        },
                    }
                ],
            },
            {
                'step_name': 'b',
                'first_failure': 98,
                'last_pass': None,
                'suspected_cls': [
                    {
                        'build_number': 99,
                        'repo_name': 'chromium',
                        'revision': 'r99_1',
                        'commit_position': None,
                        'url': None,
                        'score': 5,
                        'hints': {
                            'added x/y/f99_1.cc (and it was in log)': 5,
                        },
                    }
                ],
            }
        ]
    }

    self.assertEqual(result_status.FOUND_UNTRIAGED,
                     identify_culprit_pipeline._GetResultAnalysisStatus(
                         dummy_result))
    def testGetResultAnalysisStatusFoundUntriaged(self):
        dummy_result = {
            "failures": [
                {
                    "step_name": "a",
                    "first_failure": 98,
                    "last_pass": None,
                    "suspected_cls": [
                        {
                            "build_number": 99,
                            "repo_name": "chromium",
                            "revision": "r99_2",
                            "commit_position": None,
                            "url": None,
                            "score": 1,
                            "hints": {"modified f99_2.cc (and it was in log)": 1},
                        }
                    ],
                },
                {
                    "step_name": "b",
                    "first_failure": 98,
                    "last_pass": None,
                    "suspected_cls": [
                        {
                            "build_number": 99,
                            "repo_name": "chromium",
                            "revision": "r99_1",
                            "commit_position": None,
                            "url": None,
                            "score": 5,
                            "hints": {"added x/y/f99_1.cc (and it was in log)": 5},
                        }
                    ],
                },
            ]
        }

        self.assertEqual(
            wf_analysis_result_status.FOUND_UNTRIAGED, identify_culprit_pipeline._GetResultAnalysisStatus(dummy_result)
        )
  def testGetResultAnalysisStatusNotFoundUntriaged(self):
    dummy_result = {
        'failures': [
            {
                'step_name': 'a',
                'first_failure': 98,
                'last_pass': None,
                'suspected_cls': [],
            },
            {
                'step_name': 'b',
                'first_failure': 98,
                'last_pass': None,
                'suspected_cls': [],
            }
        ]
    }

    self.assertEqual(result_status.NOT_FOUND_UNTRIAGED,
                     identify_culprit_pipeline._GetResultAnalysisStatus(
                         dummy_result))
    def _AddAnalysisResults(self):
        """Create and store dummy data."""
        analyses = []
        stored_dates = {}

        def StoreTestBuildDate(analysis_number, start_time):
            if datetime:  # pragma: no cover
                stored_dates[analysis_number] = start_time.strftime(
                    '%Y-%m-%d %H:%M:%S UTC')

        for i in range(0, 10):
            analyses.append(self._AddAnalysisResult('m', 'b', i))

        self._AddAnalysisResult('chromium.linux', 'Linux GN', 26120)
        analyses.append(WfAnalysis.Get('chromium.linux', 'Linux GN', 26120))

        analyses[1].status = analysis_status.COMPLETED
        analyses[2].status = analysis_status.COMPLETED
        analyses[3].status = analysis_status.COMPLETED
        analyses[4].status = analysis_status.ERROR
        analyses[7].status = analysis_status.COMPLETED
        analyses[9].status = analysis_status.COMPLETED
        analyses[10].status = analysis_status.COMPLETED

        analyses[2].build_start_time = datetime.datetime.utcnow()
        StoreTestBuildDate(2, analyses[2].build_start_time)
        analyses[7].build_start_time = (datetime.datetime.utcnow() -
                                        datetime.timedelta(6))
        StoreTestBuildDate(7, analyses[7].build_start_time)
        analyses[10].build_start_time = (datetime.datetime.utcnow() -
                                         datetime.timedelta(4))
        StoreTestBuildDate(10, analyses[10].build_start_time)

        analyses[1].result = {
            'failures': [{
                'step_name':
                'b',
                'first_failure':
                1,
                'last_pass':
                None,
                'supported':
                True,
                'suspected_cls': [{
                    'build_number': 1,
                    'repo_name': 'chromium',
                    'revision': 'r99_1',
                    'commit_position': None,
                    'url': None,
                    'score': 5,
                    'hints': {
                        'added x/y/f99_1.cc (and it was in log)': 5,
                    },
                }],
            }]
        }

        analyses[2].result = {
            'failures': [{
                'step_name': 'a',
                'first_failure': 2,
                'last_pass': None,
                'supported': True,
                'suspected_cls': [],
            }, {
                'step_name': 'b',
                'first_failure': 1,
                'last_pass': None,
                'supported': True,
                'suspected_cls': [],
            }]
        }

        analyses[3].result = {
            'failures': [{
                'step_name': 'a',
                'first_failure': 3,
                'last_pass': None,
                'supported': True,
                'suspected_cls': [],
            }, {
                'step_name': 'b',
                'first_failure': 2,
                'last_pass': None,
                'supported': True,
                'suspected_cls': [],
            }]
        }

        analyses[7].result = {
            'failures': [{
                'step_name':
                'a',
                'first_failure':
                7,
                'last_pass':
                None,
                'supported':
                True,
                'suspected_cls': [{
                    'build_number': 7,
                    'repo_name': 'chromium',
                    'revision': 'r99_2',
                    'commit_position': None,
                    'url': None,
                    'score': 1,
                    'hints': {
                        'modified f99_2.cc (and it was in log)': 1,
                    },
                }, {
                    'build_number': 7,
                    'repo_name': 'chromium',
                    'revision': 'r99_6',
                    'commit_position': None,
                    'url': None,
                    'score': 5,
                    'hints': {
                        'added x/y/f99_7.cc (and it was in log)': 5,
                    },
                }],
            }, {
                'step_name':
                'b',
                'first_failure':
                7,
                'last_pass':
                None,
                'supported':
                True,
                'suspected_cls': [{
                    'build_number':
                    7,
                    'repo_name':
                    'chromium',
                    'revision':
                    'r99_1',
                    'commit_position':
                    None,
                    'url':
                    'https://chromium.googlesource.com/chromium/'
                    'src/r99_1',
                    'score':
                    5,
                    'hints': {
                        'added x/y/f99_1.cc (and it was in log)': 5,
                    },
                }],
            }]
        }

        analyses[9].result = {
            'failures': [{
                'step_name': 'a',
                'first_failure': 9,
                'last_pass': None,
                'supported': True,
                'suspected_cls': [],
            }, {
                'step_name':
                'b',
                'first_failure':
                9,
                'last_pass':
                None,
                'supported':
                True,
                'suspected_cls': [{
                    'build_number': 9,
                    'repo_name': 'chromium',
                    'revision': 'r99_9',
                    'commit_position': None,
                    'url': None,
                    'score': 1,
                    'hints': {
                        'modified f99_9.cc (and it was in log)': 1,
                    },
                }],
            }]
        }

        analyses[10].result = {
            'failures': [{
                'step_name':
                'a',
                'first_failure':
                10,
                'last_pass':
                None,
                'supported':
                True,
                'suspected_cls': [{
                    'build_number': 10,
                    'repo_name': 'chromium',
                    'revision': 'r99_10',
                    'commit_position': None,
                    'url': None,
                    'score': 5,
                    'hints': {
                        'added x/f99_10.cc (and it was in log)': 5,
                    },
                }],
            }, {
                'step_name':
                'b',
                'first_failure':
                10,
                'last_pass':
                None,
                'supported':
                True,
                'suspected_cls': [{
                    'build_number': 10,
                    'repo_name': 'chromium',
                    'revision': 'r99_10',
                    'commit_position': None,
                    'url': None,
                    'score': 1,
                    'hints': {
                        'modified x/f99_9.cc (and it was in log)': 1,
                    },
                }],
            }]
        }

        for analysis in analyses:
            analysis.suspected_cls = self._GetSuspectedCLs(analysis.result)
            analysis.result_status = (
                identify_culprit_pipeline._GetResultAnalysisStatus(
                    analysis.result))
            analysis.put()

        analyses[1].result_status = result_status.FOUND_INCORRECT
        analyses[1].put()
        analyses[3].result_status = result_status.NOT_FOUND_INCORRECT
        analyses[3].put()
        analyses[10].result_status = result_status.FOUND_CORRECT
        analyses[10].put()

        return stored_dates