blob: 4eb8c87733241e5bba7afd00f9abc9125e505da4 [file] [log] [blame]
Aviv Keshetb1238c32013-04-01 11:42:13 -07001#!/usr/bin/python
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
David James81f69172013-04-11 10:42:43 -070010import os
11import sys
Aviv Keshetb60fb3a2013-10-10 13:46:55 -070012import types
Aviv Keshet787ffcd2013-04-08 15:14:56 -070013
David James81f69172013-04-11 10:42:43 -070014sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__)))
Aviv Keshetb1238c32013-04-01 11:42:13 -070015from chromite.lib import cros_build_lib_unittest
16from chromite.lib import cros_test_lib
17from chromite.scripts import autotest_quickmerge
18
Mike Frysingerc9785342014-12-08 00:47:08 -050019import mock
20
Aviv Keshet787ffcd2013-04-08 15:14:56 -070021
22RSYNC_TEST_OUTPUT = """.d..t...... ./
23>f..t...... touched file with spaces
24>f..t...... touched_file
25>f.st...... modified_contents_file
26.f...p..... modified_permissions_file
27.f....o.... modified_owner_file
28>f+++++++++ new_file
29cL+++++++++ new_symlink -> directory_a/new_file_in_directory
30.d..t...... directory_a/
31>f+++++++++ directory_a/new_file_in_directory
32>f..t...... directory_a/touched_file_in_directory
33cd+++++++++ new_empty_directory/
34.d..t...... touched_empty_directory/"""
35# The output format of rsync's itemized changes has a few unusual cases
36# that are ambiguous. For instance, if the operation involved creating a
37# symbolic link named "a -> b" to a file named "c", the rsync output would be:
38# cL+++++++++ a -> b -> c
39# which is indistinguishable from the output for creating a symbolic link named
40# "a" to a file named "b -> c".
41# Since there is no easy resolution to this ambiguity, and it seems like a case
42# that would rarely or never be encountered in the wild, rsync quickmerge
43# will exclude all files which contain the substring " -> " in their name.
44
Aviv Keshet75d65962013-04-17 16:15:23 -070045RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE = \
46""">f..t...... client/ardvark.py
47.d..t...... client/site_tests/
48>f+++++++++ client/site_tests/nothing.py
49.d..t...... client/site_tests/factory_Leds/
50>f+++++++++ client/site_tests/factory_Leds/factory_Leds2.py
51>f..tpog... client/site_tests/login_UserPolicyKeys/control
52>f..tpog... client/site_tests/login_UserPolicyKeys/login_UserPolicyKeys.py
53>f..t...... client/site_tests/platform_Cryptohome/platform_Cryptohome.py
54>f..tpog... server/site_tests/security_DbusFuzzServer/control
55>f..t.og... utils/coverage_suite.py
56.d..t...... client/site_tests/power_Thermal/
57cd+++++++++ client/site_tests/power_Thermal/a/
58cd+++++++++ client/site_tests/power_Thermal/a/b/
59cd+++++++++ client/site_tests/power_Thermal/a/b/c/
60>f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py"""
61
Aviv Keshet940c17f2013-04-11 18:41:42 -070062RSYNC_TEST_DESTINATION_PATH = '/foo/bar/'
63
64TEST_PACKAGE_CP = 'a_cute/little_puppy'
65TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159'
66TEST_PACKAGE_C = 'a_cute'
67TEST_PACKAGE_PV = 'little_puppy-3.14159'
68TEST_PORTAGE_ROOT = '/bib/bob/'
69TEST_PACKAGE_OLDCONTENTS = {
Mike Frysingere65f3752014-12-08 00:46:39 -050070 u'/by/the/prickling/of/my/thumbs': (u'obj', '1234', '4321'),
71 u'/something/wicked/this/way/comes': (u'dir',)
Aviv Keshet940c17f2013-04-11 18:41:42 -070072}
73
Mike Frysingere65f3752014-12-08 00:46:39 -050074
Mike Frysingercc851fc2014-12-08 11:31:59 -050075class ItemizeChangesFromRsyncOutput(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070076 """Test autotest_quickmerge.ItemizeChangesFromRsyncOutput."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070077
78 def testItemizeChangesFromRsyncOutput(self):
79 """Test that rsync output parser returns correct FileMutations."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070080 expected_new = set(
81 [('>f+++++++++', '/foo/bar/new_file'),
82 ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'),
83 ('cL+++++++++', '/foo/bar/new_symlink')])
84
85 expected_mod = set(
86 [('>f..t......', '/foo/bar/touched file with spaces'),
87 ('>f..t......', '/foo/bar/touched_file'),
88 ('>f.st......', '/foo/bar/modified_contents_file'),
89 ('.f...p.....', '/foo/bar/modified_permissions_file'),
90 ('.f....o....', '/foo/bar/modified_owner_file'),
91 ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')])
92
93 expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')])
94
95 report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Aviv Keshet940c17f2013-04-11 18:41:42 -070096 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet787ffcd2013-04-08 15:14:56 -070097
98 self.assertEqual(expected_new, set(report.new_files))
99 self.assertEqual(expected_mod, set(report.modified_files))
100 self.assertEqual(expected_dir, set(report.new_directories))
101
102
Mike Frysingercc851fc2014-12-08 11:31:59 -0500103class PackageNameParsingTest(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700104 """Test autotest_quickmerge.GetStalePackageNames."""
Aviv Keshet75d65962013-04-17 16:15:23 -0700105
106 def testGetStalePackageNames(self):
107 autotest_sysroot = '/an/arbitrary/path/'
108 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
109 RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
110 package_matches = autotest_quickmerge.GetStalePackageNames(
111 change_report.modified_files + change_report.new_files,
112 autotest_sysroot)
113 expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
114 'platform_Cryptohome', 'power_Thermal'])
115 self.assertEqual(set(package_matches), expected_set)
116
117
Aviv Keshetb1238c32013-04-01 11:42:13 -0700118class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700119 """Test autotest_quickmerge.RsyncQuickmerge."""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700120
121 def testRsyncQuickmergeCommand(self):
122 """Test that RsyncQuickMerge makes correct call to SudoRunCommand"""
123 include_file_name = 'an_include_file_name'
124 source_path = 'a_source_path'
125 sysroot_path = 'a_sysroot_path'
126
127 expected_command = ['rsync', '-a', '-n', '-u', '-i',
128 '--exclude=**.pyc', '--exclude=**.pyo',
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700129 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -0700130 '--include-from=%s' % include_file_name,
131 '--exclude=*',
132 source_path,
133 sysroot_path]
134
135 autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path,
136 include_file_name,
137 pretend=True,
Aviv Keshet60968ec2013-04-11 18:44:14 -0700138 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700139
140 self.assertCommandContains(expected_command)
141
142
Mike Frysingerc9785342014-12-08 00:47:08 -0500143class PortageManipulationsTest(cros_test_lib.MockTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700144 """Test usage of autotest_quickmerge.portage."""
145
Aviv Keshet940c17f2013-04-11 18:41:42 -0700146 def testUpdatePackageContents(self):
147 """Test that UpdatePackageContents makes the correct calls to portage."""
Mike Frysingerc9785342014-12-08 00:47:08 -0500148 autotest_quickmerge.portage = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700149 portage = autotest_quickmerge.portage
150
151 portage.root = TEST_PORTAGE_ROOT
152
Mike Frysingerc9785342014-12-08 00:47:08 -0500153 mock_vartree = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700154 mock_vartree.settings = {'an arbitrary' : 'dictionary'}
155 mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}}
Mike Frysingerc9785342014-12-08 00:47:08 -0500156 portage.create_trees.return_value = mock_tree
Aviv Keshet940c17f2013-04-11 18:41:42 -0700157
Mike Frysingerc9785342014-12-08 00:47:08 -0500158 mock_vartree.dbapi = mock.MagicMock()
159 mock_vartree.dbapi.cp_list.return_value = [TEST_PACKAGE_CPV]
Aviv Keshet940c17f2013-04-11 18:41:42 -0700160
Mike Frysingerc9785342014-12-08 00:47:08 -0500161 mock_package = mock.MagicMock()
162 portage.dblink.return_value = mock_package # pylint: disable=no-member
163 mock_package.getcontents.return_value = TEST_PACKAGE_OLDCONTENTS
Aviv Keshet940c17f2013-04-11 18:41:42 -0700164
165 EXPECTED_NEW_ENTRIES = {
166 '/foo/bar/new_empty_directory': (u'dir',),
167 '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'),
168 '/foo/bar/new_file': (u'obj', '0', '0'),
169 '/foo/bar/new_symlink': (u'obj', '0', '0')
170 }
171 RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy()
172 RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES)
173
174 mock_vartree.dbapi.writeContentsToContentsFile(mock_package,
Mike Frysingere65f3752014-12-08 00:46:39 -0500175 RESULT_DICIONARY)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700176
Aviv Keshet940c17f2013-04-11 18:41:42 -0700177 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Mike Frysingere65f3752014-12-08 00:46:39 -0500178 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700179 autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP,
Mike Frysingere65f3752014-12-08 00:46:39 -0500180 TEST_PORTAGE_ROOT)
Aviv Keshet940c17f2013-04-11 18:41:42 -0700181
Aviv Keshet940c17f2013-04-11 18:41:42 -0700182
Mike Frysingercc851fc2014-12-08 11:31:59 -0500183class PortageAPITest(cros_test_lib.TestCase):
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700184 """Ensures that required portage API exists."""
Mike Frysingere65f3752014-12-08 00:46:39 -0500185
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700186 def runTest(self):
187 try:
188 import portage
189 except ImportError:
190 self.skipTest('Portage not available in test environment. Re-run test '
191 'in chroot.')
192 try:
Mike Frysingere65f3752014-12-08 00:46:39 -0500193 # pylint: disable=no-member
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700194 f = portage.vardbapi.writeContentsToContentsFile
195 except AttributeError:
196 self.fail('Required writeContentsToContentsFile function does '
197 'not exist.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700198
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700199 self.assertIsInstance(f, types.UnboundMethodType,
200 'Required writeContentsToContentsFile is not '
201 'a function.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700202
Mike Frysingere65f3752014-12-08 00:46:39 -0500203
Aviv Keshetb1238c32013-04-01 11:42:13 -0700204if __name__ == '__main__':
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700205 cros_test_lib.main()