blob: 5793af6479796eb8e13cf2b65973fcb1e31cb7f5 [file] [log] [blame]
Laurent Chavey47eeccc2020-10-14 10:09:41 +09001#!/usr/bin/env python3
Laurent Chavey47eeccc2020-10-14 10:09:41 +09002# Copyright 2020 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for blocked_terms.txt and unblocked_terms.txt.
7
Mike Frysinger1c05e5f2021-03-23 17:14:44 -04008Implements unit tests for the blocked terms and unblocked terms
9regex processed by pre-upload.py.
Laurent Chavey47eeccc2020-10-14 10:09:41 +090010"""
11
Laurent Chavey47eeccc2020-10-14 10:09:41 +090012import os
13import sys
14
Laurent Chavey48b7ef32020-11-10 20:01:10 +090015# We access private members of the pre_upload module.
Mike Frysinger1c05e5f2021-03-23 17:14:44 -040016# pylint: disable=protected-access
Laurent Chavey47eeccc2020-10-14 10:09:41 +090017
18# Make sure we can find the chromite paths.
19sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
20 '..', '..'))
21
22# The sys.path monkey patching confuses the linter.
23# pylint: disable=wrong-import-position
24from chromite.lib import cros_test_lib
Laurent Chavey47eeccc2020-10-14 10:09:41 +090025
26
27assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
28
29
30pre_upload = __import__('pre-upload')
31
32
33class CheckFilesTest(cros_test_lib.MockTestCase, cros_test_lib.TempDirTestCase):
34 """Tests for blocked and unblocked files."""
35
36 DIFF = 'diff'
37 MATCH = 'match'
Laurent Chavey48b7ef32020-11-10 20:01:10 +090038 common_file_terms = None
39 unblocked_file_terms = None
40 hooks_dir = None
Laurent Chavey47eeccc2020-10-14 10:09:41 +090041
Laurent Chavey48b7ef32020-11-10 20:01:10 +090042 @classmethod
43 def setUpClass(cls):
Laurent Chavey625fbeb2020-11-11 16:17:56 +090044 """Initialize the class instance mocks."""
45 cls.hooks_dir = pre_upload._get_hooks_dir()
46 cls.common_file_terms = pre_upload._read_terms_file(
47 os.path.join(cls.hooks_dir, pre_upload.BLOCKED_TERMS_FILE))
48 cls.unblocked_file_terms = pre_upload._read_terms_file(
49 os.path.join(cls.hooks_dir, pre_upload.UNBLOCKED_TERMS_FILE))
Laurent Chavey47eeccc2020-10-14 10:09:41 +090050
Laurent Chavey48b7ef32020-11-10 20:01:10 +090051 def setUp(self):
52 """Initialize the test instance mockers."""
53 pre_upload.CACHE.clear()
Laurent Chavey47eeccc2020-10-14 10:09:41 +090054 self.PatchObject(pre_upload, '_get_affected_files',
55 return_value=['x.ebuild'])
56 self.PatchObject(pre_upload, '_filter_files', return_value=['x.ebuild'])
Laurent Chavey48b7ef32020-11-10 20:01:10 +090057
58 self.rf_mock = self.PatchObject(pre_upload, '_read_terms_file')
Laurent Chavey47eeccc2020-10-14 10:09:41 +090059 self.diff_mock = self.PatchObject(pre_upload, '_get_file_diff')
60 self.desc_mock = self.PatchObject(pre_upload, '_get_commit_desc')
61 self.project = pre_upload.Project(name='PROJECT', dir='./', remote=None)
Laurent Chavey48b7ef32020-11-10 20:01:10 +090062 self.hooks_dir_mock = self.PatchObject(pre_upload, '_get_hooks_dir')
Laurent Chavey625fbeb2020-11-11 16:17:56 +090063 self.hooks_dir_mock.return_value = self.hooks_dir
Laurent Chavey47eeccc2020-10-14 10:09:41 +090064
65 def CheckKeyword(self, test):
Laurent Chaveybb3debd2020-11-04 02:14:50 +090066 """Test a particular keyword.
67
68 Args:
69 test: { DIFF: [(int, 'line to test keyword against'), ],
70 MATCH: number of matched terms or None, }
71 """
Laurent Chavey48b7ef32020-11-10 20:01:10 +090072 def _check_keyword(unblocked):
Laurent Chavey47eeccc2020-10-14 10:09:41 +090073 self.desc_mock.return_value = 'Commit message'
74 self.diff_mock.return_value = test[self.DIFF]
75 failures = pre_upload._check_keywords(self.project, 'COMMIT', ())
76 if test[self.MATCH] and not unblocked:
77 self.assertNotEqual(failures, [])
Laurent Chavey48b7ef32020-11-10 20:01:10 +090078 self.assertEqual(test[self.MATCH], len(failures[0].items),
79 msg=failures[0].items)
Laurent Chavey47eeccc2020-10-14 10:09:41 +090080 else:
81 self.assertEqual(failures, [])
82
Laurent Chaveybb3debd2020-11-04 02:14:50 +090083 # Check blocked terms.
Daisuke Nojiri062a0a92021-02-14 15:38:16 -080084 self.rf_mock.side_effect = [self.common_file_terms, set(), set()]
Laurent Chavey48b7ef32020-11-10 20:01:10 +090085 _check_keyword(unblocked=False)
Laurent Chavey47eeccc2020-10-14 10:09:41 +090086
Laurent Chaveybb3debd2020-11-04 02:14:50 +090087 # Check unblocked terms.
Laurent Chavey625fbeb2020-11-11 16:17:56 +090088 self.rf_mock.side_effect = [self.common_file_terms,
Daisuke Nojiri062a0a92021-02-14 15:38:16 -080089 self.unblocked_file_terms,
Laurent Chavey625fbeb2020-11-11 16:17:56 +090090 self.unblocked_file_terms]
Laurent Chavey48b7ef32020-11-10 20:01:10 +090091 _check_keyword(unblocked=True)
92
93 def check_lines(self, blocked_lines, unblocked_lines):
94 """Check each lines for blocked or unblocked terms."""
95 self.CheckKeyword({self.DIFF: blocked_lines,
96 self.MATCH: len(blocked_lines)})
97 self.CheckKeyword({self.DIFF: unblocked_lines, self.MATCH: 0})
Laurent Chavey47eeccc2020-10-14 10:09:41 +090098
99 def test_mitm_keyword(self):
Laurent Chavey48b7ef32020-11-10 20:01:10 +0900100 """Test mitm term."""
101 self.check_lines([
102 # blocked
103 (1, 'blocked mitm '),
104 (2, 'blocked (mitm)'),
105 (3, 'blocked .mitm'),
106 (4, 'blocked MITM'),
107 (5, 'blocked mitm1'),
108 ], [
109 # unblocked
110 (11, 'unblocked (commitment)'),
111 (12, 'unblocked DailyLimitMins'),
112 ])
Laurent Chaveybb3debd2020-11-04 02:14:50 +0900113
114 def test_sane_keyword(self):
Laurent Chavey48b7ef32020-11-10 20:01:10 +0900115 """Test sane term."""
116 self.check_lines([
117 # blocked
118 (1, 'blocked sane '),
119 (2, 'blocked (sane)'),
120 (3, 'blocked .sane'),
121 (4, 'blocked SANE'),
122 (5, 'blocked sane1'),
123 (6, 'blocked insane'),
124 (7, 'blocked .insane'),
125 ], [
126 # unblocked
127 (11, 'unblocked asanEnabled'),
128 ])
Laurent Chavey47eeccc2020-10-14 10:09:41 +0900129
Bernie Thompson495976a2020-11-05 17:24:14 -0800130 def test_whitelist_keyword(self):
Laurent Chavey48b7ef32020-11-10 20:01:10 +0900131 """Test whitelist term."""
132 self.check_lines([
133 # blocked
134 (1, 'blocked white list '),
135 (2, 'blocked white-list'),
136 (3, 'blocked whitelist'),
137 (4, 'blocked _whitelist'),
138 (5, 'blocked whitelist1'),
139 (6, 'blocked whitelisted'),
140 ], [
141 # unblocked
142 (11, 'unblocked white dog list'),
143 ])
Bernie Thompson495976a2020-11-05 17:24:14 -0800144
Laurent Chavey48b7ef32020-11-10 20:01:10 +0900145 def test_sanity_keyword(self):
146 """Test sanity term."""
147 self.check_lines([
148 # blocked
149 (1, 'blocked sanity '),
150 (2, 'blocked (sanity)'),
151 (3, 'blocked struct.sanity'),
152 (4, 'blocked SANITY'),
153 (5, 'blocked AndroidSanityCheck'),
154 ], [
155 # unblocked
156 ])
Bernie Thompson495976a2020-11-05 17:24:14 -0800157
Laurent Chavey47eeccc2020-10-14 10:09:41 +0900158
159if __name__ == '__main__':
Laurent Chavey625fbeb2020-11-11 16:17:56 +0900160 cros_test_lib.main(module=__name__)