blob: 3c5a4d7458bc9b6e289e9863709a3fba22b91baf [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
Aviv Keshetb1238c32013-04-01 11:42:13 -070012from chromite.lib import cros_test_lib
13from chromite.scripts import autotest_quickmerge
Mike Frysinger40ffb532021-02-12 07:36:08 -050014from chromite.third_party import mock
Aviv Keshetb1238c32013-04-01 11:42:13 -070015
Aviv Keshet787ffcd2013-04-08 15:14:56 -070016
Mike Frysinger2c9d0612020-02-19 02:52:41 -050017assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
18
19
Aviv Keshet787ffcd2013-04-08 15:14:56 -070020RSYNC_TEST_OUTPUT = """.d..t...... ./
21>f..t...... touched file with spaces
22>f..t...... touched_file
23>f.st...... modified_contents_file
24.f...p..... modified_permissions_file
25.f....o.... modified_owner_file
26>f+++++++++ new_file
27cL+++++++++ new_symlink -> directory_a/new_file_in_directory
28.d..t...... directory_a/
29>f+++++++++ directory_a/new_file_in_directory
30>f..t...... directory_a/touched_file_in_directory
31cd+++++++++ new_empty_directory/
32.d..t...... touched_empty_directory/"""
33# The output format of rsync's itemized changes has a few unusual cases
34# that are ambiguous. For instance, if the operation involved creating a
35# symbolic link named "a -> b" to a file named "c", the rsync output would be:
36# cL+++++++++ a -> b -> c
37# which is indistinguishable from the output for creating a symbolic link named
38# "a" to a file named "b -> c".
39# Since there is no easy resolution to this ambiguity, and it seems like a case
40# that would rarely or never be encountered in the wild, rsync quickmerge
41# will exclude all files which contain the substring " -> " in their name.
42
Mike Frysingerfcca49e2021-03-17 01:09:20 -040043RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE = """\
44>f..t...... client/ardvark.py
Aviv Keshet75d65962013-04-17 16:15:23 -070045.d..t...... client/site_tests/
46>f+++++++++ client/site_tests/nothing.py
47.d..t...... client/site_tests/factory_Leds/
48>f+++++++++ client/site_tests/factory_Leds/factory_Leds2.py
49>f..tpog... client/site_tests/login_UserPolicyKeys/control
50>f..tpog... client/site_tests/login_UserPolicyKeys/login_UserPolicyKeys.py
51>f..t...... client/site_tests/platform_Cryptohome/platform_Cryptohome.py
52>f..tpog... server/site_tests/security_DbusFuzzServer/control
53>f..t.og... utils/coverage_suite.py
54.d..t...... client/site_tests/power_Thermal/
55cd+++++++++ client/site_tests/power_Thermal/a/
56cd+++++++++ client/site_tests/power_Thermal/a/b/
57cd+++++++++ client/site_tests/power_Thermal/a/b/c/
58>f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py"""
59
Aviv Keshet940c17f2013-04-11 18:41:42 -070060RSYNC_TEST_DESTINATION_PATH = '/foo/bar/'
61
62TEST_PACKAGE_CP = 'a_cute/little_puppy'
63TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159'
64TEST_PACKAGE_C = 'a_cute'
65TEST_PACKAGE_PV = 'little_puppy-3.14159'
66TEST_PORTAGE_ROOT = '/bib/bob/'
67TEST_PACKAGE_OLDCONTENTS = {
Mike Frysingere65f3752014-12-08 00:46:39 -050068 u'/by/the/prickling/of/my/thumbs': (u'obj', '1234', '4321'),
69 u'/something/wicked/this/way/comes': (u'dir',)
Aviv Keshet940c17f2013-04-11 18:41:42 -070070}
71
Mike Frysingere65f3752014-12-08 00:46:39 -050072
Mike Frysingercc851fc2014-12-08 11:31:59 -050073class ItemizeChangesFromRsyncOutput(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070074 """Test autotest_quickmerge.ItemizeChangesFromRsyncOutput."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070075
76 def testItemizeChangesFromRsyncOutput(self):
77 """Test that rsync output parser returns correct FileMutations."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070078 expected_new = set(
79 [('>f+++++++++', '/foo/bar/new_file'),
80 ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'),
81 ('cL+++++++++', '/foo/bar/new_symlink')])
82
83 expected_mod = set(
84 [('>f..t......', '/foo/bar/touched file with spaces'),
85 ('>f..t......', '/foo/bar/touched_file'),
86 ('>f.st......', '/foo/bar/modified_contents_file'),
87 ('.f...p.....', '/foo/bar/modified_permissions_file'),
88 ('.f....o....', '/foo/bar/modified_owner_file'),
89 ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')])
90
91 expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')])
92
93 report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Aviv Keshet940c17f2013-04-11 18:41:42 -070094 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet787ffcd2013-04-08 15:14:56 -070095
96 self.assertEqual(expected_new, set(report.new_files))
97 self.assertEqual(expected_mod, set(report.modified_files))
98 self.assertEqual(expected_dir, set(report.new_directories))
99
100
Mike Frysingercc851fc2014-12-08 11:31:59 -0500101class PackageNameParsingTest(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700102 """Test autotest_quickmerge.GetStalePackageNames."""
Aviv Keshet75d65962013-04-17 16:15:23 -0700103
104 def testGetStalePackageNames(self):
105 autotest_sysroot = '/an/arbitrary/path/'
106 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
107 RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
108 package_matches = autotest_quickmerge.GetStalePackageNames(
109 change_report.modified_files + change_report.new_files,
110 autotest_sysroot)
111 expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
112 'platform_Cryptohome', 'power_Thermal'])
113 self.assertEqual(set(package_matches), expected_set)
114
115
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600116class RsyncCommandTest(cros_test_lib.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700117 """Test autotest_quickmerge.RsyncQuickmerge."""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700118
119 def testRsyncQuickmergeCommand(self):
Mike Frysinger45602c72019-09-22 02:15:11 -0400120 """Test that RsyncQuickMerge makes correct call to sudo_run"""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700121 include_file_name = 'an_include_file_name'
122 source_path = 'a_source_path'
123 sysroot_path = 'a_sysroot_path'
124
125 expected_command = ['rsync', '-a', '-n', '-u', '-i',
126 '--exclude=**.pyc', '--exclude=**.pyo',
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700127 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -0700128 '--include-from=%s' % include_file_name,
129 '--exclude=*',
130 source_path,
131 sysroot_path]
132
133 autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path,
134 include_file_name,
135 pretend=True,
Aviv Keshet60968ec2013-04-11 18:44:14 -0700136 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700137
138 self.assertCommandContains(expected_command)
139
140
Mike Frysingerc9785342014-12-08 00:47:08 -0500141class PortageManipulationsTest(cros_test_lib.MockTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700142 """Test usage of autotest_quickmerge.portage."""
143
Aviv Keshet940c17f2013-04-11 18:41:42 -0700144 def testUpdatePackageContents(self):
145 """Test that UpdatePackageContents makes the correct calls to portage."""
Mike Frysingerc9785342014-12-08 00:47:08 -0500146 autotest_quickmerge.portage = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700147 portage = autotest_quickmerge.portage
148
149 portage.root = TEST_PORTAGE_ROOT
150
Mike Frysingerc9785342014-12-08 00:47:08 -0500151 mock_vartree = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700152 mock_vartree.settings = {'an arbitrary' : 'dictionary'}
153 mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}}
Mike Frysingerc9785342014-12-08 00:47:08 -0500154 portage.create_trees.return_value = mock_tree
Aviv Keshet940c17f2013-04-11 18:41:42 -0700155
Mike Frysingerc9785342014-12-08 00:47:08 -0500156 mock_vartree.dbapi = mock.MagicMock()
157 mock_vartree.dbapi.cp_list.return_value = [TEST_PACKAGE_CPV]
Aviv Keshet940c17f2013-04-11 18:41:42 -0700158
Mike Frysingerc9785342014-12-08 00:47:08 -0500159 mock_package = mock.MagicMock()
160 portage.dblink.return_value = mock_package # pylint: disable=no-member
161 mock_package.getcontents.return_value = TEST_PACKAGE_OLDCONTENTS
Aviv Keshet940c17f2013-04-11 18:41:42 -0700162
163 EXPECTED_NEW_ENTRIES = {
164 '/foo/bar/new_empty_directory': (u'dir',),
165 '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'),
166 '/foo/bar/new_file': (u'obj', '0', '0'),
167 '/foo/bar/new_symlink': (u'obj', '0', '0')
168 }
169 RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy()
170 RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES)
171
172 mock_vartree.dbapi.writeContentsToContentsFile(mock_package,
Mike Frysingere65f3752014-12-08 00:46:39 -0500173 RESULT_DICIONARY)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700174
Aviv Keshet940c17f2013-04-11 18:41:42 -0700175 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Mike Frysingere65f3752014-12-08 00:46:39 -0500176 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700177 autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP,
Mike Frysingere65f3752014-12-08 00:46:39 -0500178 TEST_PORTAGE_ROOT)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700179
Aviv Keshet940c17f2013-04-11 18:41:42 -0700180
Mike Frysingercc851fc2014-12-08 11:31:59 -0500181class PortageAPITest(cros_test_lib.TestCase):
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700182 """Ensures that required portage API exists."""
Mike Frysingere65f3752014-12-08 00:46:39 -0500183
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700184 def runTest(self):
185 try:
186 import portage
187 except ImportError:
188 self.skipTest('Portage not available in test environment. Re-run test '
189 'in chroot.')
190 try:
Mike Frysingere65f3752014-12-08 00:46:39 -0500191 # pylint: disable=no-member
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700192 f = portage.vardbapi.writeContentsToContentsFile
193 except AttributeError:
194 self.fail('Required writeContentsToContentsFile function does '
195 'not exist.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700196
Mike Frysingerca28b0e2019-10-13 21:13:48 -0400197 self.assertTrue(
198 callable(f),
199 msg='Required writeContentsToContentsFile is not a function.')