blob: 16b687fad5a2ca5f01096e57b326ea9fbf152f93 [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 Frysinger1d4752b2014-11-08 04:00:18 -05008# pylint: disable=bad-continuation
9
Mike Frysinger383367e2014-09-16 15:06:17 -040010from __future__ import print_function
11
David James81f69172013-04-11 10:42:43 -070012import os
13import sys
Aviv Keshetb60fb3a2013-10-10 13:46:55 -070014import types
Aviv Keshet787ffcd2013-04-08 15:14:56 -070015
David James81f69172013-04-11 10:42:43 -070016sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__)))
Aviv Keshetb1238c32013-04-01 11:42:13 -070017from chromite.lib import cros_build_lib_unittest
18from chromite.lib import cros_test_lib
19from chromite.scripts import autotest_quickmerge
20
Mike Frysingerc9785342014-12-08 00:47:08 -050021import mock
22
Aviv Keshet787ffcd2013-04-08 15:14:56 -070023
24RSYNC_TEST_OUTPUT = """.d..t...... ./
25>f..t...... touched file with spaces
26>f..t...... touched_file
27>f.st...... modified_contents_file
28.f...p..... modified_permissions_file
29.f....o.... modified_owner_file
30>f+++++++++ new_file
31cL+++++++++ new_symlink -> directory_a/new_file_in_directory
32.d..t...... directory_a/
33>f+++++++++ directory_a/new_file_in_directory
34>f..t...... directory_a/touched_file_in_directory
35cd+++++++++ new_empty_directory/
36.d..t...... touched_empty_directory/"""
37# The output format of rsync's itemized changes has a few unusual cases
38# that are ambiguous. For instance, if the operation involved creating a
39# symbolic link named "a -> b" to a file named "c", the rsync output would be:
40# cL+++++++++ a -> b -> c
41# which is indistinguishable from the output for creating a symbolic link named
42# "a" to a file named "b -> c".
43# Since there is no easy resolution to this ambiguity, and it seems like a case
44# that would rarely or never be encountered in the wild, rsync quickmerge
45# will exclude all files which contain the substring " -> " in their name.
46
Aviv Keshet75d65962013-04-17 16:15:23 -070047RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE = \
48""">f..t...... client/ardvark.py
49.d..t...... client/site_tests/
50>f+++++++++ client/site_tests/nothing.py
51.d..t...... client/site_tests/factory_Leds/
52>f+++++++++ client/site_tests/factory_Leds/factory_Leds2.py
53>f..tpog... client/site_tests/login_UserPolicyKeys/control
54>f..tpog... client/site_tests/login_UserPolicyKeys/login_UserPolicyKeys.py
55>f..t...... client/site_tests/platform_Cryptohome/platform_Cryptohome.py
56>f..tpog... server/site_tests/security_DbusFuzzServer/control
57>f..t.og... utils/coverage_suite.py
58.d..t...... client/site_tests/power_Thermal/
59cd+++++++++ client/site_tests/power_Thermal/a/
60cd+++++++++ client/site_tests/power_Thermal/a/b/
61cd+++++++++ client/site_tests/power_Thermal/a/b/c/
62>f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py"""
63
Aviv Keshet940c17f2013-04-11 18:41:42 -070064RSYNC_TEST_DESTINATION_PATH = '/foo/bar/'
65
66TEST_PACKAGE_CP = 'a_cute/little_puppy'
67TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159'
68TEST_PACKAGE_C = 'a_cute'
69TEST_PACKAGE_PV = 'little_puppy-3.14159'
70TEST_PORTAGE_ROOT = '/bib/bob/'
71TEST_PACKAGE_OLDCONTENTS = {
72 u'/by/the/prickling/of/my/thumbs' : (u'obj', '1234', '4321'),
73 u'/something/wicked/this/way/comes' : (u'dir',)
74}
75
Mike Frysingercc851fc2014-12-08 11:31:59 -050076class ItemizeChangesFromRsyncOutput(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070077 """Test autotest_quickmerge.ItemizeChangesFromRsyncOutput."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070078
79 def testItemizeChangesFromRsyncOutput(self):
80 """Test that rsync output parser returns correct FileMutations."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070081 expected_new = set(
82 [('>f+++++++++', '/foo/bar/new_file'),
83 ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'),
84 ('cL+++++++++', '/foo/bar/new_symlink')])
85
86 expected_mod = set(
87 [('>f..t......', '/foo/bar/touched file with spaces'),
88 ('>f..t......', '/foo/bar/touched_file'),
89 ('>f.st......', '/foo/bar/modified_contents_file'),
90 ('.f...p.....', '/foo/bar/modified_permissions_file'),
91 ('.f....o....', '/foo/bar/modified_owner_file'),
92 ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')])
93
94 expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')])
95
96 report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Aviv Keshet940c17f2013-04-11 18:41:42 -070097 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet787ffcd2013-04-08 15:14:56 -070098
99 self.assertEqual(expected_new, set(report.new_files))
100 self.assertEqual(expected_mod, set(report.modified_files))
101 self.assertEqual(expected_dir, set(report.new_directories))
102
103
Mike Frysingercc851fc2014-12-08 11:31:59 -0500104class PackageNameParsingTest(cros_test_lib.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700105 """Test autotest_quickmerge.GetStalePackageNames."""
Aviv Keshet75d65962013-04-17 16:15:23 -0700106
107 def testGetStalePackageNames(self):
108 autotest_sysroot = '/an/arbitrary/path/'
109 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
110 RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
111 package_matches = autotest_quickmerge.GetStalePackageNames(
112 change_report.modified_files + change_report.new_files,
113 autotest_sysroot)
114 expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
115 'platform_Cryptohome', 'power_Thermal'])
116 self.assertEqual(set(package_matches), expected_set)
117
118
Aviv Keshetb1238c32013-04-01 11:42:13 -0700119class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700120 """Test autotest_quickmerge.RsyncQuickmerge."""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700121
122 def testRsyncQuickmergeCommand(self):
123 """Test that RsyncQuickMerge makes correct call to SudoRunCommand"""
124 include_file_name = 'an_include_file_name'
125 source_path = 'a_source_path'
126 sysroot_path = 'a_sysroot_path'
127
128 expected_command = ['rsync', '-a', '-n', '-u', '-i',
129 '--exclude=**.pyc', '--exclude=**.pyo',
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700130 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -0700131 '--include-from=%s' % include_file_name,
132 '--exclude=*',
133 source_path,
134 sysroot_path]
135
136 autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path,
137 include_file_name,
138 pretend=True,
Aviv Keshet60968ec2013-04-11 18:44:14 -0700139 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700140
141 self.assertCommandContains(expected_command)
142
143
Mike Frysingerc9785342014-12-08 00:47:08 -0500144class PortageManipulationsTest(cros_test_lib.MockTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700145 """Test usage of autotest_quickmerge.portage."""
146
Aviv Keshet940c17f2013-04-11 18:41:42 -0700147 def testUpdatePackageContents(self):
148 """Test that UpdatePackageContents makes the correct calls to portage."""
Mike Frysingerc9785342014-12-08 00:47:08 -0500149 autotest_quickmerge.portage = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700150 portage = autotest_quickmerge.portage
151
152 portage.root = TEST_PORTAGE_ROOT
153
Mike Frysingerc9785342014-12-08 00:47:08 -0500154 mock_vartree = mock.MagicMock()
Aviv Keshet940c17f2013-04-11 18:41:42 -0700155 mock_vartree.settings = {'an arbitrary' : 'dictionary'}
156 mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}}
Mike Frysingerc9785342014-12-08 00:47:08 -0500157 portage.create_trees.return_value = mock_tree
Aviv Keshet940c17f2013-04-11 18:41:42 -0700158
Mike Frysingerc9785342014-12-08 00:47:08 -0500159 mock_vartree.dbapi = mock.MagicMock()
160 mock_vartree.dbapi.cp_list.return_value = [TEST_PACKAGE_CPV]
Aviv Keshet940c17f2013-04-11 18:41:42 -0700161
Mike Frysingerc9785342014-12-08 00:47:08 -0500162 mock_package = mock.MagicMock()
163 portage.dblink.return_value = mock_package # pylint: disable=no-member
164 mock_package.getcontents.return_value = TEST_PACKAGE_OLDCONTENTS
Aviv Keshet940c17f2013-04-11 18:41:42 -0700165
166 EXPECTED_NEW_ENTRIES = {
167 '/foo/bar/new_empty_directory': (u'dir',),
168 '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'),
169 '/foo/bar/new_file': (u'obj', '0', '0'),
170 '/foo/bar/new_symlink': (u'obj', '0', '0')
171 }
172 RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy()
173 RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES)
174
175 mock_vartree.dbapi.writeContentsToContentsFile(mock_package,
176 RESULT_DICIONARY)
177
Aviv Keshet940c17f2013-04-11 18:41:42 -0700178 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
179 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
180 autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP,
181 TEST_PORTAGE_ROOT)
182
Aviv Keshet940c17f2013-04-11 18:41:42 -0700183
Mike Frysingercc851fc2014-12-08 11:31:59 -0500184class PortageAPITest(cros_test_lib.TestCase):
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700185 """Ensures that required portage API exists."""
186 def runTest(self):
187 try:
Don Garrett25f309a2014-03-19 14:02:12 -0700188 # pylint: disable=F0401
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700189 import portage
190 except ImportError:
191 self.skipTest('Portage not available in test environment. Re-run test '
192 'in chroot.')
193 try:
Mike Frysinger1132a702014-11-10 21:50:14 -0500194 # pylint: disable=E1101
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700195 f = portage.vardbapi.writeContentsToContentsFile
196 except AttributeError:
197 self.fail('Required writeContentsToContentsFile function does '
198 'not exist.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700199
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700200 self.assertIsInstance(f, types.UnboundMethodType,
201 'Required writeContentsToContentsFile is not '
202 'a function.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700203
Aviv Keshetb1238c32013-04-01 11:42:13 -0700204if __name__ == '__main__':
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700205 cros_test_lib.main()