blob: b51977174374196e99f79502f629813d250a26c9 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Aviv Keshetb1238c32013-04-01 11:42:13 -07002# Copyright (c) 2013 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 autotest_quickmerge."""
7
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Mike Frysinger2c9d0612020-02-19 02:52:41 -050010import sys
11
Mike Frysinger6db648e2018-07-24 19:57:58 -040012import mock
13
Aviv Keshetb1238c32013-04-01 11:42:13 -070014from chromite.lib import cros_test_lib
15from chromite.scripts import autotest_quickmerge
16
Aviv Keshet787ffcd2013-04-08 15:14:56 -070017
Mike Frysinger2c9d0612020-02-19 02:52:41 -050018assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
19
20
Aviv Keshet787ffcd2013-04-08 15:14:56 -070021RSYNC_TEST_OUTPUT = """.d..t...... ./
22>f..t...... touched file with spaces
23>f..t...... touched_file
24>f.st...... modified_contents_file
25.f...p..... modified_permissions_file
26.f....o.... modified_owner_file
27>f+++++++++ new_file
28cL+++++++++ new_symlink -> directory_a/new_file_in_directory
29.d..t...... directory_a/
30>f+++++++++ directory_a/new_file_in_directory
31>f..t...... directory_a/touched_file_in_directory
32cd+++++++++ new_empty_directory/
33.d..t...... touched_empty_directory/"""
34# The output format of rsync's itemized changes has a few unusual cases
35# that are ambiguous. For instance, if the operation involved creating a
36# symbolic link named "a -> b" to a file named "c", the rsync output would be:
37# cL+++++++++ a -> b -> c
38# which is indistinguishable from the output for creating a symbolic link named
39# "a" to a file named "b -> c".
40# Since there is no easy resolution to this ambiguity, and it seems like a case
41# that would rarely or never be encountered in the wild, rsync quickmerge
42# will exclude all files which contain the substring " -> " in their name.
43
Aviv Keshet75d65962013-04-17 16:15:23 -070044RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE = \
45""">f..t...... client/ardvark.py
46.d..t...... client/site_tests/
47>f+++++++++ client/site_tests/nothing.py
48.d..t...... client/site_tests/factory_Leds/
49>f+++++++++ client/site_tests/factory_Leds/factory_Leds2.py
50>f..tpog... client/site_tests/login_UserPolicyKeys/control
51>f..tpog... client/site_tests/login_UserPolicyKeys/login_UserPolicyKeys.py
52>f..t...... client/site_tests/platform_Cryptohome/platform_Cryptohome.py
53>f..tpog... server/site_tests/security_DbusFuzzServer/control
54>f..t.og... utils/coverage_suite.py
55.d..t...... client/site_tests/power_Thermal/
56cd+++++++++ client/site_tests/power_Thermal/a/
57cd+++++++++ client/site_tests/power_Thermal/a/b/
58cd+++++++++ client/site_tests/power_Thermal/a/b/c/
59>f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py"""
60
Aviv Keshet940c17f2013-04-11 18:41:42 -070061RSYNC_TEST_DESTINATION_PATH = '/foo/bar/'
62
63TEST_PACKAGE_CP = 'a_cute/little_puppy'
64TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159'
65TEST_PACKAGE_C = 'a_cute'
66TEST_PACKAGE_PV = 'little_puppy-3.14159'
67TEST_PORTAGE_ROOT = '/bib/bob/'
68TEST_PACKAGE_OLDCONTENTS = {
Mike Frysingere65f3752014-12-08 00:46:39 -050069 u'/by/the/prickling/of/my/thumbs': (u'obj', '1234', '4321'),
70 u'/something/wicked/this/way/comes': (u'dir',)
Aviv Keshet940c17f2013-04-11 18:41:42 -070071}
72
Mike Frysingere65f3752014-12-08 00:46:39 -050073
Mike Frysingercc851fc2014-12-08 11:31:59 -050074class ItemizeChangesFromRsyncOutput(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070075 """Test autotest_quickmerge.ItemizeChangesFromRsyncOutput."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070076
77 def testItemizeChangesFromRsyncOutput(self):
78 """Test that rsync output parser returns correct FileMutations."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070079 expected_new = set(
80 [('>f+++++++++', '/foo/bar/new_file'),
81 ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'),
82 ('cL+++++++++', '/foo/bar/new_symlink')])
83
84 expected_mod = set(
85 [('>f..t......', '/foo/bar/touched file with spaces'),
86 ('>f..t......', '/foo/bar/touched_file'),
87 ('>f.st......', '/foo/bar/modified_contents_file'),
88 ('.f...p.....', '/foo/bar/modified_permissions_file'),
89 ('.f....o....', '/foo/bar/modified_owner_file'),
90 ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')])
91
92 expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')])
93
94 report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Aviv Keshet940c17f2013-04-11 18:41:42 -070095 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet787ffcd2013-04-08 15:14:56 -070096
97 self.assertEqual(expected_new, set(report.new_files))
98 self.assertEqual(expected_mod, set(report.modified_files))
99 self.assertEqual(expected_dir, set(report.new_directories))
100
101
Mike Frysingercc851fc2014-12-08 11:31:59 -0500102class PackageNameParsingTest(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700103 """Test autotest_quickmerge.GetStalePackageNames."""
Aviv Keshet75d65962013-04-17 16:15:23 -0700104
105 def testGetStalePackageNames(self):
106 autotest_sysroot = '/an/arbitrary/path/'
107 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
108 RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
109 package_matches = autotest_quickmerge.GetStalePackageNames(
110 change_report.modified_files + change_report.new_files,
111 autotest_sysroot)
112 expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
113 'platform_Cryptohome', 'power_Thermal'])
114 self.assertEqual(set(package_matches), expected_set)
115
116
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600117class RsyncCommandTest(cros_test_lib.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700118 """Test autotest_quickmerge.RsyncQuickmerge."""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700119
120 def testRsyncQuickmergeCommand(self):
Mike Frysinger45602c72019-09-22 02:15:11 -0400121 """Test that RsyncQuickMerge makes correct call to sudo_run"""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700122 include_file_name = 'an_include_file_name'
123 source_path = 'a_source_path'
124 sysroot_path = 'a_sysroot_path'
125
126 expected_command = ['rsync', '-a', '-n', '-u', '-i',
127 '--exclude=**.pyc', '--exclude=**.pyo',
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700128 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -0700129 '--include-from=%s' % include_file_name,
130 '--exclude=*',
131 source_path,
132 sysroot_path]
133
134 autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path,
135 include_file_name,
136 pretend=True,
Aviv Keshet60968ec2013-04-11 18:44:14 -0700137 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700138
139 self.assertCommandContains(expected_command)
140
141
Mike Frysingerc9785342014-12-08 00:47:08 -0500142class PortageManipulationsTest(cros_test_lib.MockTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700143 """Test usage of autotest_quickmerge.portage."""
144
Aviv Keshet940c17f2013-04-11 18:41:42 -0700145 def testUpdatePackageContents(self):
146 """Test that UpdatePackageContents makes the correct calls to portage."""
Mike Frysingerc9785342014-12-08 00:47:08 -0500147 autotest_quickmerge.portage = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700148 portage = autotest_quickmerge.portage
149
150 portage.root = TEST_PORTAGE_ROOT
151
Mike Frysingerc9785342014-12-08 00:47:08 -0500152 mock_vartree = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700153 mock_vartree.settings = {'an arbitrary' : 'dictionary'}
154 mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}}
Mike Frysingerc9785342014-12-08 00:47:08 -0500155 portage.create_trees.return_value = mock_tree
Aviv Keshet940c17f2013-04-11 18:41:42 -0700156
Mike Frysingerc9785342014-12-08 00:47:08 -0500157 mock_vartree.dbapi = mock.MagicMock()
158 mock_vartree.dbapi.cp_list.return_value = [TEST_PACKAGE_CPV]
Aviv Keshet940c17f2013-04-11 18:41:42 -0700159
Mike Frysingerc9785342014-12-08 00:47:08 -0500160 mock_package = mock.MagicMock()
161 portage.dblink.return_value = mock_package # pylint: disable=no-member
162 mock_package.getcontents.return_value = TEST_PACKAGE_OLDCONTENTS
Aviv Keshet940c17f2013-04-11 18:41:42 -0700163
164 EXPECTED_NEW_ENTRIES = {
165 '/foo/bar/new_empty_directory': (u'dir',),
166 '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'),
167 '/foo/bar/new_file': (u'obj', '0', '0'),
168 '/foo/bar/new_symlink': (u'obj', '0', '0')
169 }
170 RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy()
171 RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES)
172
173 mock_vartree.dbapi.writeContentsToContentsFile(mock_package,
Mike Frysingere65f3752014-12-08 00:46:39 -0500174 RESULT_DICIONARY)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700175
Aviv Keshet940c17f2013-04-11 18:41:42 -0700176 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Mike Frysingere65f3752014-12-08 00:46:39 -0500177 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700178 autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP,
Mike Frysingere65f3752014-12-08 00:46:39 -0500179 TEST_PORTAGE_ROOT)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700180
Aviv Keshet940c17f2013-04-11 18:41:42 -0700181
Mike Frysingercc851fc2014-12-08 11:31:59 -0500182class PortageAPITest(cros_test_lib.TestCase):
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700183 """Ensures that required portage API exists."""
Mike Frysingere65f3752014-12-08 00:46:39 -0500184
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700185 def runTest(self):
186 try:
187 import portage
188 except ImportError:
189 self.skipTest('Portage not available in test environment. Re-run test '
190 'in chroot.')
191 try:
Mike Frysingere65f3752014-12-08 00:46:39 -0500192 # pylint: disable=no-member
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700193 f = portage.vardbapi.writeContentsToContentsFile
194 except AttributeError:
195 self.fail('Required writeContentsToContentsFile function does '
196 'not exist.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700197
Mike Frysingerca28b0e2019-10-13 21:13:48 -0400198 self.assertTrue(
199 callable(f),
200 msg='Required writeContentsToContentsFile is not a function.')