blob: 47b9b39287fbaaf9c78705b64966a573a9c357ec [file] [log] [blame]
Aviv Keshetb1238c32013-04-01 11:42:13 -07001# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unit tests for autotest_quickmerge."""
6
Mike Frysinger383367e2014-09-16 15:06:17 -04007from __future__ import print_function
8
Mike Frysingerea838d12014-12-08 11:55:32 -05009import mock
Aviv Keshetb60fb3a2013-10-10 13:46:55 -070010import types
Aviv Keshet787ffcd2013-04-08 15:14:56 -070011
Aviv Keshetb1238c32013-04-01 11:42:13 -070012from chromite.lib import cros_build_lib_unittest
13from chromite.lib import cros_test_lib
14from chromite.scripts import autotest_quickmerge
15
Aviv Keshet787ffcd2013-04-08 15:14:56 -070016
17RSYNC_TEST_OUTPUT = """.d..t...... ./
18>f..t...... touched file with spaces
19>f..t...... touched_file
20>f.st...... modified_contents_file
21.f...p..... modified_permissions_file
22.f....o.... modified_owner_file
23>f+++++++++ new_file
24cL+++++++++ new_symlink -> directory_a/new_file_in_directory
25.d..t...... directory_a/
26>f+++++++++ directory_a/new_file_in_directory
27>f..t...... directory_a/touched_file_in_directory
28cd+++++++++ new_empty_directory/
29.d..t...... touched_empty_directory/"""
30# The output format of rsync's itemized changes has a few unusual cases
31# that are ambiguous. For instance, if the operation involved creating a
32# symbolic link named "a -> b" to a file named "c", the rsync output would be:
33# cL+++++++++ a -> b -> c
34# which is indistinguishable from the output for creating a symbolic link named
35# "a" to a file named "b -> c".
36# Since there is no easy resolution to this ambiguity, and it seems like a case
37# that would rarely or never be encountered in the wild, rsync quickmerge
38# will exclude all files which contain the substring " -> " in their name.
39
Aviv Keshet75d65962013-04-17 16:15:23 -070040RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE = \
41""">f..t...... client/ardvark.py
42.d..t...... client/site_tests/
43>f+++++++++ client/site_tests/nothing.py
44.d..t...... client/site_tests/factory_Leds/
45>f+++++++++ client/site_tests/factory_Leds/factory_Leds2.py
46>f..tpog... client/site_tests/login_UserPolicyKeys/control
47>f..tpog... client/site_tests/login_UserPolicyKeys/login_UserPolicyKeys.py
48>f..t...... client/site_tests/platform_Cryptohome/platform_Cryptohome.py
49>f..tpog... server/site_tests/security_DbusFuzzServer/control
50>f..t.og... utils/coverage_suite.py
51.d..t...... client/site_tests/power_Thermal/
52cd+++++++++ client/site_tests/power_Thermal/a/
53cd+++++++++ client/site_tests/power_Thermal/a/b/
54cd+++++++++ client/site_tests/power_Thermal/a/b/c/
55>f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py"""
56
Aviv Keshet940c17f2013-04-11 18:41:42 -070057RSYNC_TEST_DESTINATION_PATH = '/foo/bar/'
58
59TEST_PACKAGE_CP = 'a_cute/little_puppy'
60TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159'
61TEST_PACKAGE_C = 'a_cute'
62TEST_PACKAGE_PV = 'little_puppy-3.14159'
63TEST_PORTAGE_ROOT = '/bib/bob/'
64TEST_PACKAGE_OLDCONTENTS = {
Mike Frysingere65f3752014-12-08 00:46:39 -050065 u'/by/the/prickling/of/my/thumbs': (u'obj', '1234', '4321'),
66 u'/something/wicked/this/way/comes': (u'dir',)
Aviv Keshet940c17f2013-04-11 18:41:42 -070067}
68
Mike Frysingere65f3752014-12-08 00:46:39 -050069
Mike Frysingercc851fc2014-12-08 11:31:59 -050070class ItemizeChangesFromRsyncOutput(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070071 """Test autotest_quickmerge.ItemizeChangesFromRsyncOutput."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070072
73 def testItemizeChangesFromRsyncOutput(self):
74 """Test that rsync output parser returns correct FileMutations."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070075 expected_new = set(
76 [('>f+++++++++', '/foo/bar/new_file'),
77 ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'),
78 ('cL+++++++++', '/foo/bar/new_symlink')])
79
80 expected_mod = set(
81 [('>f..t......', '/foo/bar/touched file with spaces'),
82 ('>f..t......', '/foo/bar/touched_file'),
83 ('>f.st......', '/foo/bar/modified_contents_file'),
84 ('.f...p.....', '/foo/bar/modified_permissions_file'),
85 ('.f....o....', '/foo/bar/modified_owner_file'),
86 ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')])
87
88 expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')])
89
90 report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Aviv Keshet940c17f2013-04-11 18:41:42 -070091 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet787ffcd2013-04-08 15:14:56 -070092
93 self.assertEqual(expected_new, set(report.new_files))
94 self.assertEqual(expected_mod, set(report.modified_files))
95 self.assertEqual(expected_dir, set(report.new_directories))
96
97
Mike Frysingercc851fc2014-12-08 11:31:59 -050098class PackageNameParsingTest(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070099 """Test autotest_quickmerge.GetStalePackageNames."""
Aviv Keshet75d65962013-04-17 16:15:23 -0700100
101 def testGetStalePackageNames(self):
102 autotest_sysroot = '/an/arbitrary/path/'
103 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
104 RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
105 package_matches = autotest_quickmerge.GetStalePackageNames(
106 change_report.modified_files + change_report.new_files,
107 autotest_sysroot)
108 expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
109 'platform_Cryptohome', 'power_Thermal'])
110 self.assertEqual(set(package_matches), expected_set)
111
112
Aviv Keshetb1238c32013-04-01 11:42:13 -0700113class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700114 """Test autotest_quickmerge.RsyncQuickmerge."""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700115
116 def testRsyncQuickmergeCommand(self):
117 """Test that RsyncQuickMerge makes correct call to SudoRunCommand"""
118 include_file_name = 'an_include_file_name'
119 source_path = 'a_source_path'
120 sysroot_path = 'a_sysroot_path'
121
122 expected_command = ['rsync', '-a', '-n', '-u', '-i',
123 '--exclude=**.pyc', '--exclude=**.pyo',
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700124 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -0700125 '--include-from=%s' % include_file_name,
126 '--exclude=*',
127 source_path,
128 sysroot_path]
129
130 autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path,
131 include_file_name,
132 pretend=True,
Aviv Keshet60968ec2013-04-11 18:44:14 -0700133 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700134
135 self.assertCommandContains(expected_command)
136
137
Mike Frysingerc9785342014-12-08 00:47:08 -0500138class PortageManipulationsTest(cros_test_lib.MockTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700139 """Test usage of autotest_quickmerge.portage."""
140
Aviv Keshet940c17f2013-04-11 18:41:42 -0700141 def testUpdatePackageContents(self):
142 """Test that UpdatePackageContents makes the correct calls to portage."""
Mike Frysingerc9785342014-12-08 00:47:08 -0500143 autotest_quickmerge.portage = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700144 portage = autotest_quickmerge.portage
145
146 portage.root = TEST_PORTAGE_ROOT
147
Mike Frysingerc9785342014-12-08 00:47:08 -0500148 mock_vartree = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700149 mock_vartree.settings = {'an arbitrary' : 'dictionary'}
150 mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}}
Mike Frysingerc9785342014-12-08 00:47:08 -0500151 portage.create_trees.return_value = mock_tree
Aviv Keshet940c17f2013-04-11 18:41:42 -0700152
Mike Frysingerc9785342014-12-08 00:47:08 -0500153 mock_vartree.dbapi = mock.MagicMock()
154 mock_vartree.dbapi.cp_list.return_value = [TEST_PACKAGE_CPV]
Aviv Keshet940c17f2013-04-11 18:41:42 -0700155
Mike Frysingerc9785342014-12-08 00:47:08 -0500156 mock_package = mock.MagicMock()
157 portage.dblink.return_value = mock_package # pylint: disable=no-member
158 mock_package.getcontents.return_value = TEST_PACKAGE_OLDCONTENTS
Aviv Keshet940c17f2013-04-11 18:41:42 -0700159
160 EXPECTED_NEW_ENTRIES = {
161 '/foo/bar/new_empty_directory': (u'dir',),
162 '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'),
163 '/foo/bar/new_file': (u'obj', '0', '0'),
164 '/foo/bar/new_symlink': (u'obj', '0', '0')
165 }
166 RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy()
167 RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES)
168
169 mock_vartree.dbapi.writeContentsToContentsFile(mock_package,
Mike Frysingere65f3752014-12-08 00:46:39 -0500170 RESULT_DICIONARY)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700171
Aviv Keshet940c17f2013-04-11 18:41:42 -0700172 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Mike Frysingere65f3752014-12-08 00:46:39 -0500173 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700174 autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP,
Mike Frysingere65f3752014-12-08 00:46:39 -0500175 TEST_PORTAGE_ROOT)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700176
Aviv Keshet940c17f2013-04-11 18:41:42 -0700177
Mike Frysingercc851fc2014-12-08 11:31:59 -0500178class PortageAPITest(cros_test_lib.TestCase):
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700179 """Ensures that required portage API exists."""
Mike Frysingere65f3752014-12-08 00:46:39 -0500180
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700181 def runTest(self):
182 try:
183 import portage
184 except ImportError:
185 self.skipTest('Portage not available in test environment. Re-run test '
186 'in chroot.')
187 try:
Mike Frysingere65f3752014-12-08 00:46:39 -0500188 # pylint: disable=no-member
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700189 f = portage.vardbapi.writeContentsToContentsFile
190 except AttributeError:
191 self.fail('Required writeContentsToContentsFile function does '
192 'not exist.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700193
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700194 self.assertIsInstance(f, types.UnboundMethodType,
195 'Required writeContentsToContentsFile is not '
196 'a function.')