blob: 73697fa57dcf62a4eb2a1c6b7c1f6eaa8397e703 [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 Frysinger6db648e2018-07-24 19:57:58 -040010import mock
11
Aviv Keshetb1238c32013-04-01 11:42:13 -070012from chromite.lib import cros_test_lib
13from chromite.scripts import autotest_quickmerge
14
Aviv Keshet787ffcd2013-04-08 15:14:56 -070015
16RSYNC_TEST_OUTPUT = """.d..t...... ./
17>f..t...... touched file with spaces
18>f..t...... touched_file
19>f.st...... modified_contents_file
20.f...p..... modified_permissions_file
21.f....o.... modified_owner_file
22>f+++++++++ new_file
23cL+++++++++ new_symlink -> directory_a/new_file_in_directory
24.d..t...... directory_a/
25>f+++++++++ directory_a/new_file_in_directory
26>f..t...... directory_a/touched_file_in_directory
27cd+++++++++ new_empty_directory/
28.d..t...... touched_empty_directory/"""
29# The output format of rsync's itemized changes has a few unusual cases
30# that are ambiguous. For instance, if the operation involved creating a
31# symbolic link named "a -> b" to a file named "c", the rsync output would be:
32# cL+++++++++ a -> b -> c
33# which is indistinguishable from the output for creating a symbolic link named
34# "a" to a file named "b -> c".
35# Since there is no easy resolution to this ambiguity, and it seems like a case
36# that would rarely or never be encountered in the wild, rsync quickmerge
37# will exclude all files which contain the substring " -> " in their name.
38
Aviv Keshet75d65962013-04-17 16:15:23 -070039RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE = \
40""">f..t...... client/ardvark.py
41.d..t...... client/site_tests/
42>f+++++++++ client/site_tests/nothing.py
43.d..t...... client/site_tests/factory_Leds/
44>f+++++++++ client/site_tests/factory_Leds/factory_Leds2.py
45>f..tpog... client/site_tests/login_UserPolicyKeys/control
46>f..tpog... client/site_tests/login_UserPolicyKeys/login_UserPolicyKeys.py
47>f..t...... client/site_tests/platform_Cryptohome/platform_Cryptohome.py
48>f..tpog... server/site_tests/security_DbusFuzzServer/control
49>f..t.og... utils/coverage_suite.py
50.d..t...... client/site_tests/power_Thermal/
51cd+++++++++ client/site_tests/power_Thermal/a/
52cd+++++++++ client/site_tests/power_Thermal/a/b/
53cd+++++++++ client/site_tests/power_Thermal/a/b/c/
54>f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py"""
55
Aviv Keshet940c17f2013-04-11 18:41:42 -070056RSYNC_TEST_DESTINATION_PATH = '/foo/bar/'
57
58TEST_PACKAGE_CP = 'a_cute/little_puppy'
59TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159'
60TEST_PACKAGE_C = 'a_cute'
61TEST_PACKAGE_PV = 'little_puppy-3.14159'
62TEST_PORTAGE_ROOT = '/bib/bob/'
63TEST_PACKAGE_OLDCONTENTS = {
Mike Frysingere65f3752014-12-08 00:46:39 -050064 u'/by/the/prickling/of/my/thumbs': (u'obj', '1234', '4321'),
65 u'/something/wicked/this/way/comes': (u'dir',)
Aviv Keshet940c17f2013-04-11 18:41:42 -070066}
67
Mike Frysingere65f3752014-12-08 00:46:39 -050068
Mike Frysingercc851fc2014-12-08 11:31:59 -050069class ItemizeChangesFromRsyncOutput(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070070 """Test autotest_quickmerge.ItemizeChangesFromRsyncOutput."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070071
72 def testItemizeChangesFromRsyncOutput(self):
73 """Test that rsync output parser returns correct FileMutations."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070074 expected_new = set(
75 [('>f+++++++++', '/foo/bar/new_file'),
76 ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'),
77 ('cL+++++++++', '/foo/bar/new_symlink')])
78
79 expected_mod = set(
80 [('>f..t......', '/foo/bar/touched file with spaces'),
81 ('>f..t......', '/foo/bar/touched_file'),
82 ('>f.st......', '/foo/bar/modified_contents_file'),
83 ('.f...p.....', '/foo/bar/modified_permissions_file'),
84 ('.f....o....', '/foo/bar/modified_owner_file'),
85 ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')])
86
87 expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')])
88
89 report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Aviv Keshet940c17f2013-04-11 18:41:42 -070090 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet787ffcd2013-04-08 15:14:56 -070091
92 self.assertEqual(expected_new, set(report.new_files))
93 self.assertEqual(expected_mod, set(report.modified_files))
94 self.assertEqual(expected_dir, set(report.new_directories))
95
96
Mike Frysingercc851fc2014-12-08 11:31:59 -050097class PackageNameParsingTest(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070098 """Test autotest_quickmerge.GetStalePackageNames."""
Aviv Keshet75d65962013-04-17 16:15:23 -070099
100 def testGetStalePackageNames(self):
101 autotest_sysroot = '/an/arbitrary/path/'
102 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
103 RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
104 package_matches = autotest_quickmerge.GetStalePackageNames(
105 change_report.modified_files + change_report.new_files,
106 autotest_sysroot)
107 expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
108 'platform_Cryptohome', 'power_Thermal'])
109 self.assertEqual(set(package_matches), expected_set)
110
111
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600112class RsyncCommandTest(cros_test_lib.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700113 """Test autotest_quickmerge.RsyncQuickmerge."""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700114
115 def testRsyncQuickmergeCommand(self):
Mike Frysinger45602c72019-09-22 02:15:11 -0400116 """Test that RsyncQuickMerge makes correct call to sudo_run"""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700117 include_file_name = 'an_include_file_name'
118 source_path = 'a_source_path'
119 sysroot_path = 'a_sysroot_path'
120
121 expected_command = ['rsync', '-a', '-n', '-u', '-i',
122 '--exclude=**.pyc', '--exclude=**.pyo',
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700123 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -0700124 '--include-from=%s' % include_file_name,
125 '--exclude=*',
126 source_path,
127 sysroot_path]
128
129 autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path,
130 include_file_name,
131 pretend=True,
Aviv Keshet60968ec2013-04-11 18:44:14 -0700132 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700133
134 self.assertCommandContains(expected_command)
135
136
Mike Frysingerc9785342014-12-08 00:47:08 -0500137class PortageManipulationsTest(cros_test_lib.MockTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700138 """Test usage of autotest_quickmerge.portage."""
139
Aviv Keshet940c17f2013-04-11 18:41:42 -0700140 def testUpdatePackageContents(self):
141 """Test that UpdatePackageContents makes the correct calls to portage."""
Mike Frysingerc9785342014-12-08 00:47:08 -0500142 autotest_quickmerge.portage = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700143 portage = autotest_quickmerge.portage
144
145 portage.root = TEST_PORTAGE_ROOT
146
Mike Frysingerc9785342014-12-08 00:47:08 -0500147 mock_vartree = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700148 mock_vartree.settings = {'an arbitrary' : 'dictionary'}
149 mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}}
Mike Frysingerc9785342014-12-08 00:47:08 -0500150 portage.create_trees.return_value = mock_tree
Aviv Keshet940c17f2013-04-11 18:41:42 -0700151
Mike Frysingerc9785342014-12-08 00:47:08 -0500152 mock_vartree.dbapi = mock.MagicMock()
153 mock_vartree.dbapi.cp_list.return_value = [TEST_PACKAGE_CPV]
Aviv Keshet940c17f2013-04-11 18:41:42 -0700154
Mike Frysingerc9785342014-12-08 00:47:08 -0500155 mock_package = mock.MagicMock()
156 portage.dblink.return_value = mock_package # pylint: disable=no-member
157 mock_package.getcontents.return_value = TEST_PACKAGE_OLDCONTENTS
Aviv Keshet940c17f2013-04-11 18:41:42 -0700158
159 EXPECTED_NEW_ENTRIES = {
160 '/foo/bar/new_empty_directory': (u'dir',),
161 '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'),
162 '/foo/bar/new_file': (u'obj', '0', '0'),
163 '/foo/bar/new_symlink': (u'obj', '0', '0')
164 }
165 RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy()
166 RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES)
167
168 mock_vartree.dbapi.writeContentsToContentsFile(mock_package,
Mike Frysingere65f3752014-12-08 00:46:39 -0500169 RESULT_DICIONARY)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700170
Aviv Keshet940c17f2013-04-11 18:41:42 -0700171 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Mike Frysingere65f3752014-12-08 00:46:39 -0500172 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700173 autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP,
Mike Frysingere65f3752014-12-08 00:46:39 -0500174 TEST_PORTAGE_ROOT)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700175
Aviv Keshet940c17f2013-04-11 18:41:42 -0700176
Mike Frysingercc851fc2014-12-08 11:31:59 -0500177class PortageAPITest(cros_test_lib.TestCase):
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700178 """Ensures that required portage API exists."""
Mike Frysingere65f3752014-12-08 00:46:39 -0500179
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700180 def runTest(self):
181 try:
182 import portage
183 except ImportError:
184 self.skipTest('Portage not available in test environment. Re-run test '
185 'in chroot.')
186 try:
Mike Frysingere65f3752014-12-08 00:46:39 -0500187 # pylint: disable=no-member
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700188 f = portage.vardbapi.writeContentsToContentsFile
189 except AttributeError:
190 self.fail('Required writeContentsToContentsFile function does '
191 'not exist.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700192
Mike Frysingerca28b0e2019-10-13 21:13:48 -0400193 self.assertTrue(
194 callable(f),
195 msg='Required writeContentsToContentsFile is not a function.')