blob: d6dcbb867e6b16fec17b1668cc97313abebb76ae [file] [log] [blame]
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Tests for gerrit exonerating code.."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import contextlib
import functools
import json
import os
from chromite.lib import clactions
import mock
from exonerator import gerrit_cq
TEST_CASES = 'test_cases'
CQ_BIT_REMOVED_BY_HUMAN = 'CQ_bit_removed_by_human_676269.json'
CQ_BIT_REMOVED_BY_BOT = 'CQ_bit_removed_by_bot_710511.json'
READY_CASE = 'ready_for_exoneration_710511.json'
def _GetFakeHelper(change_details):
"""Returns a fake GerritHelper which only returns the given |change_details|
Args:
change_details: A change_details dict of the form returned by Gerrit's API
Returns:
A mock object with a .GetChangeDetail method which always returns
|change_details|.
"""
return mock.Mock(
GetChangeDetail=lambda *args, **kwargs: change_details)
def CasePath(case):
"""Returns the path to the testcase file |case|.
Args:
case: A file path.
"""
return os.path.join(os.path.dirname(__file__), TEST_CASES, case)
@contextlib.contextmanager
def FakeChangeDetails(module, case):
"""Mocks out the _GetHelper method to return the given test case.
Args:
module: The module to patch
case: The test case file to use.
"""
with open(CasePath(case)) as fh:
test_case = json.load(fh)
with mock.patch.object(module, 'GetHelper',
return_value=_GetFakeHelper(test_case)):
yield
_FakeChangeDetails = functools.partial(FakeChangeDetails, gerrit_cq)
def TestCanBeMarkedReady():
with _FakeChangeDetails(READY_CASE):
assert gerrit_cq.CanBeMarkedReady(
clactions.GerritPatchTuple(1, 5, False))
def TestDontExonerateSubmitted():
with _FakeChangeDetails(CQ_BIT_REMOVED_BY_BOT):
assert not gerrit_cq.CanBeMarkedReady(
clactions.GerritPatchTuple(1, 5, False))
# pylint: disable=protected-access
def TestCQBitWasRemovedByBot():
with open(CasePath(CQ_BIT_REMOVED_BY_BOT)) as fh:
details = json.load(fh)
assert gerrit_cq.CQApprovalWasRemovedByBot(details)
def TestCQBitWasRemovedByHuman():
with open(CasePath(CQ_BIT_REMOVED_BY_HUMAN)) as fh:
details = json.load(fh)
assert not gerrit_cq.CQApprovalWasRemovedByBot(details)