blob: 8b8d4c33d652e219dce302e2762bbdcfd56afea0 [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
Aviv Keshetb60fb3a2013-10-10 13:46:55 -070010import types
Aviv Keshet787ffcd2013-04-08 15:14:56 -070011
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
18RSYNC_TEST_OUTPUT = """.d..t...... ./
19>f..t...... touched file with spaces
20>f..t...... touched_file
21>f.st...... modified_contents_file
22.f...p..... modified_permissions_file
23.f....o.... modified_owner_file
24>f+++++++++ new_file
25cL+++++++++ new_symlink -> directory_a/new_file_in_directory
26.d..t...... directory_a/
27>f+++++++++ directory_a/new_file_in_directory
28>f..t...... directory_a/touched_file_in_directory
29cd+++++++++ new_empty_directory/
30.d..t...... touched_empty_directory/"""
31# The output format of rsync's itemized changes has a few unusual cases
32# that are ambiguous. For instance, if the operation involved creating a
33# symbolic link named "a -> b" to a file named "c", the rsync output would be:
34# cL+++++++++ a -> b -> c
35# which is indistinguishable from the output for creating a symbolic link named
36# "a" to a file named "b -> c".
37# Since there is no easy resolution to this ambiguity, and it seems like a case
38# that would rarely or never be encountered in the wild, rsync quickmerge
39# will exclude all files which contain the substring " -> " in their name.
40
Aviv Keshet75d65962013-04-17 16:15:23 -070041RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE = \
42""">f..t...... client/ardvark.py
43.d..t...... client/site_tests/
44>f+++++++++ client/site_tests/nothing.py
45.d..t...... client/site_tests/factory_Leds/
46>f+++++++++ client/site_tests/factory_Leds/factory_Leds2.py
47>f..tpog... client/site_tests/login_UserPolicyKeys/control
48>f..tpog... client/site_tests/login_UserPolicyKeys/login_UserPolicyKeys.py
49>f..t...... client/site_tests/platform_Cryptohome/platform_Cryptohome.py
50>f..tpog... server/site_tests/security_DbusFuzzServer/control
51>f..t.og... utils/coverage_suite.py
52.d..t...... client/site_tests/power_Thermal/
53cd+++++++++ client/site_tests/power_Thermal/a/
54cd+++++++++ client/site_tests/power_Thermal/a/b/
55cd+++++++++ client/site_tests/power_Thermal/a/b/c/
56>f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py"""
57
Aviv Keshet940c17f2013-04-11 18:41:42 -070058RSYNC_TEST_DESTINATION_PATH = '/foo/bar/'
59
60TEST_PACKAGE_CP = 'a_cute/little_puppy'
61TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159'
62TEST_PACKAGE_C = 'a_cute'
63TEST_PACKAGE_PV = 'little_puppy-3.14159'
64TEST_PORTAGE_ROOT = '/bib/bob/'
65TEST_PACKAGE_OLDCONTENTS = {
Mike Frysingere65f3752014-12-08 00:46:39 -050066 u'/by/the/prickling/of/my/thumbs': (u'obj', '1234', '4321'),
67 u'/something/wicked/this/way/comes': (u'dir',)
Aviv Keshet940c17f2013-04-11 18:41:42 -070068}
69
Mike Frysingere65f3752014-12-08 00:46:39 -050070
Mike Frysingercc851fc2014-12-08 11:31:59 -050071class ItemizeChangesFromRsyncOutput(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070072 """Test autotest_quickmerge.ItemizeChangesFromRsyncOutput."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070073
74 def testItemizeChangesFromRsyncOutput(self):
75 """Test that rsync output parser returns correct FileMutations."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070076 expected_new = set(
77 [('>f+++++++++', '/foo/bar/new_file'),
78 ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'),
79 ('cL+++++++++', '/foo/bar/new_symlink')])
80
81 expected_mod = set(
82 [('>f..t......', '/foo/bar/touched file with spaces'),
83 ('>f..t......', '/foo/bar/touched_file'),
84 ('>f.st......', '/foo/bar/modified_contents_file'),
85 ('.f...p.....', '/foo/bar/modified_permissions_file'),
86 ('.f....o....', '/foo/bar/modified_owner_file'),
87 ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')])
88
89 expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')])
90
91 report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Aviv Keshet940c17f2013-04-11 18:41:42 -070092 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet787ffcd2013-04-08 15:14:56 -070093
94 self.assertEqual(expected_new, set(report.new_files))
95 self.assertEqual(expected_mod, set(report.modified_files))
96 self.assertEqual(expected_dir, set(report.new_directories))
97
98
Mike Frysingercc851fc2014-12-08 11:31:59 -050099class PackageNameParsingTest(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700100 """Test autotest_quickmerge.GetStalePackageNames."""
Aviv Keshet75d65962013-04-17 16:15:23 -0700101
102 def testGetStalePackageNames(self):
103 autotest_sysroot = '/an/arbitrary/path/'
104 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
105 RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
106 package_matches = autotest_quickmerge.GetStalePackageNames(
107 change_report.modified_files + change_report.new_files,
108 autotest_sysroot)
109 expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
110 'platform_Cryptohome', 'power_Thermal'])
111 self.assertEqual(set(package_matches), expected_set)
112
113
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600114class RsyncCommandTest(cros_test_lib.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700115 """Test autotest_quickmerge.RsyncQuickmerge."""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700116
117 def testRsyncQuickmergeCommand(self):
118 """Test that RsyncQuickMerge makes correct call to SudoRunCommand"""
119 include_file_name = 'an_include_file_name'
120 source_path = 'a_source_path'
121 sysroot_path = 'a_sysroot_path'
122
123 expected_command = ['rsync', '-a', '-n', '-u', '-i',
124 '--exclude=**.pyc', '--exclude=**.pyo',
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700125 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -0700126 '--include-from=%s' % include_file_name,
127 '--exclude=*',
128 source_path,
129 sysroot_path]
130
131 autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path,
132 include_file_name,
133 pretend=True,
Aviv Keshet60968ec2013-04-11 18:44:14 -0700134 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700135
136 self.assertCommandContains(expected_command)
137
138
Mike Frysingerc9785342014-12-08 00:47:08 -0500139class PortageManipulationsTest(cros_test_lib.MockTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700140 """Test usage of autotest_quickmerge.portage."""
141
Aviv Keshet940c17f2013-04-11 18:41:42 -0700142 def testUpdatePackageContents(self):
143 """Test that UpdatePackageContents makes the correct calls to portage."""
Mike Frysingerc9785342014-12-08 00:47:08 -0500144 autotest_quickmerge.portage = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700145 portage = autotest_quickmerge.portage
146
147 portage.root = TEST_PORTAGE_ROOT
148
Mike Frysingerc9785342014-12-08 00:47:08 -0500149 mock_vartree = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700150 mock_vartree.settings = {'an arbitrary' : 'dictionary'}
151 mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}}
Mike Frysingerc9785342014-12-08 00:47:08 -0500152 portage.create_trees.return_value = mock_tree
Aviv Keshet940c17f2013-04-11 18:41:42 -0700153
Mike Frysingerc9785342014-12-08 00:47:08 -0500154 mock_vartree.dbapi = mock.MagicMock()
155 mock_vartree.dbapi.cp_list.return_value = [TEST_PACKAGE_CPV]
Aviv Keshet940c17f2013-04-11 18:41:42 -0700156
Mike Frysingerc9785342014-12-08 00:47:08 -0500157 mock_package = mock.MagicMock()
158 portage.dblink.return_value = mock_package # pylint: disable=no-member
159 mock_package.getcontents.return_value = TEST_PACKAGE_OLDCONTENTS
Aviv Keshet940c17f2013-04-11 18:41:42 -0700160
161 EXPECTED_NEW_ENTRIES = {
162 '/foo/bar/new_empty_directory': (u'dir',),
163 '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'),
164 '/foo/bar/new_file': (u'obj', '0', '0'),
165 '/foo/bar/new_symlink': (u'obj', '0', '0')
166 }
167 RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy()
168 RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES)
169
170 mock_vartree.dbapi.writeContentsToContentsFile(mock_package,
Mike Frysingere65f3752014-12-08 00:46:39 -0500171 RESULT_DICIONARY)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700172
Aviv Keshet940c17f2013-04-11 18:41:42 -0700173 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Mike Frysingere65f3752014-12-08 00:46:39 -0500174 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700175 autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP,
Mike Frysingere65f3752014-12-08 00:46:39 -0500176 TEST_PORTAGE_ROOT)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700177
Aviv Keshet940c17f2013-04-11 18:41:42 -0700178
Mike Frysingercc851fc2014-12-08 11:31:59 -0500179class PortageAPITest(cros_test_lib.TestCase):
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700180 """Ensures that required portage API exists."""
Mike Frysingere65f3752014-12-08 00:46:39 -0500181
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700182 def runTest(self):
183 try:
184 import portage
185 except ImportError:
186 self.skipTest('Portage not available in test environment. Re-run test '
187 'in chroot.')
188 try:
Mike Frysingere65f3752014-12-08 00:46:39 -0500189 # pylint: disable=no-member
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700190 f = portage.vardbapi.writeContentsToContentsFile
191 except AttributeError:
192 self.fail('Required writeContentsToContentsFile function does '
193 'not exist.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700194
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700195 self.assertIsInstance(f, types.UnboundMethodType,
196 'Required writeContentsToContentsFile is not '
197 'a function.')