blob: 7497e650a62e833394073448586ecab58f5f528c [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
David James81f69172013-04-11 10:42:43 -07008import os
9import sys
Aviv Keshetb60fb3a2013-10-10 13:46:55 -070010import types
Aviv Keshet787ffcd2013-04-08 15:14:56 -070011import unittest
Aviv Keshet940c17f2013-04-11 18:41:42 -070012import mox
Aviv Keshet787ffcd2013-04-08 15:14:56 -070013
14
David James81f69172013-04-11 10:42:43 -070015sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__)))
Aviv Keshetb1238c32013-04-01 11:42:13 -070016from chromite.lib import cros_build_lib_unittest
17from chromite.lib import cros_test_lib
18from chromite.scripts import autotest_quickmerge
19
Aviv Keshet787ffcd2013-04-08 15:14:56 -070020
21RSYNC_TEST_OUTPUT = """.d..t...... ./
22>f..t...... touched file with spaces
23>f..t...... touched_file
24>f.st...... modified_contents_file
25.f...p..... modified_permissions_file
26.f....o.... modified_owner_file
27>f+++++++++ new_file
28cL+++++++++ new_symlink -> directory_a/new_file_in_directory
29.d..t...... directory_a/
30>f+++++++++ directory_a/new_file_in_directory
31>f..t...... directory_a/touched_file_in_directory
32cd+++++++++ new_empty_directory/
33.d..t...... touched_empty_directory/"""
34# The output format of rsync's itemized changes has a few unusual cases
35# that are ambiguous. For instance, if the operation involved creating a
36# symbolic link named "a -> b" to a file named "c", the rsync output would be:
37# cL+++++++++ a -> b -> c
38# which is indistinguishable from the output for creating a symbolic link named
39# "a" to a file named "b -> c".
40# Since there is no easy resolution to this ambiguity, and it seems like a case
41# that would rarely or never be encountered in the wild, rsync quickmerge
42# will exclude all files which contain the substring " -> " in their name.
43
Aviv Keshet75d65962013-04-17 16:15:23 -070044RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE = \
45""">f..t...... client/ardvark.py
46.d..t...... client/site_tests/
47>f+++++++++ client/site_tests/nothing.py
48.d..t...... client/site_tests/factory_Leds/
49>f+++++++++ client/site_tests/factory_Leds/factory_Leds2.py
50>f..tpog... client/site_tests/login_UserPolicyKeys/control
51>f..tpog... client/site_tests/login_UserPolicyKeys/login_UserPolicyKeys.py
52>f..t...... client/site_tests/platform_Cryptohome/platform_Cryptohome.py
53>f..tpog... server/site_tests/security_DbusFuzzServer/control
54>f..t.og... utils/coverage_suite.py
55.d..t...... client/site_tests/power_Thermal/
56cd+++++++++ client/site_tests/power_Thermal/a/
57cd+++++++++ client/site_tests/power_Thermal/a/b/
58cd+++++++++ client/site_tests/power_Thermal/a/b/c/
59>f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py"""
60
Aviv Keshet940c17f2013-04-11 18:41:42 -070061RSYNC_TEST_DESTINATION_PATH = '/foo/bar/'
62
63TEST_PACKAGE_CP = 'a_cute/little_puppy'
64TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159'
65TEST_PACKAGE_C = 'a_cute'
66TEST_PACKAGE_PV = 'little_puppy-3.14159'
67TEST_PORTAGE_ROOT = '/bib/bob/'
68TEST_PACKAGE_OLDCONTENTS = {
69 u'/by/the/prickling/of/my/thumbs' : (u'obj', '1234', '4321'),
70 u'/something/wicked/this/way/comes' : (u'dir',)
71}
72
Aviv Keshet787ffcd2013-04-08 15:14:56 -070073class ItemizeChangesFromRsyncOutput(unittest.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070074 """Test autotest_quickmerge.ItemizeChangesFromRsyncOutput."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070075
76 def testItemizeChangesFromRsyncOutput(self):
77 """Test that rsync output parser returns correct FileMutations."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070078 expected_new = set(
79 [('>f+++++++++', '/foo/bar/new_file'),
80 ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'),
81 ('cL+++++++++', '/foo/bar/new_symlink')])
82
83 expected_mod = set(
84 [('>f..t......', '/foo/bar/touched file with spaces'),
85 ('>f..t......', '/foo/bar/touched_file'),
86 ('>f.st......', '/foo/bar/modified_contents_file'),
87 ('.f...p.....', '/foo/bar/modified_permissions_file'),
88 ('.f....o....', '/foo/bar/modified_owner_file'),
89 ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')])
90
91 expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')])
92
93 report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Aviv Keshet940c17f2013-04-11 18:41:42 -070094 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet787ffcd2013-04-08 15:14:56 -070095
96 self.assertEqual(expected_new, set(report.new_files))
97 self.assertEqual(expected_mod, set(report.modified_files))
98 self.assertEqual(expected_dir, set(report.new_directories))
99
100
Aviv Keshet75d65962013-04-17 16:15:23 -0700101class PackageNameParsingTest(unittest.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700102 """Test autotest_quickmerge.GetStalePackageNames."""
Aviv Keshet75d65962013-04-17 16:15:23 -0700103
104 def testGetStalePackageNames(self):
105 autotest_sysroot = '/an/arbitrary/path/'
106 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
107 RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
108 package_matches = autotest_quickmerge.GetStalePackageNames(
109 change_report.modified_files + change_report.new_files,
110 autotest_sysroot)
111 expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
112 'platform_Cryptohome', 'power_Thermal'])
113 self.assertEqual(set(package_matches), expected_set)
114
115
Aviv Keshetb1238c32013-04-01 11:42:13 -0700116class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700117 """Test autotest_quickmerge.RsyncQuickmerge."""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700118
119 def testRsyncQuickmergeCommand(self):
120 """Test that RsyncQuickMerge makes correct call to SudoRunCommand"""
121 include_file_name = 'an_include_file_name'
122 source_path = 'a_source_path'
123 sysroot_path = 'a_sysroot_path'
124
125 expected_command = ['rsync', '-a', '-n', '-u', '-i',
126 '--exclude=**.pyc', '--exclude=**.pyo',
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700127 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -0700128 '--include-from=%s' % include_file_name,
129 '--exclude=*',
130 source_path,
131 sysroot_path]
132
133 autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path,
134 include_file_name,
135 pretend=True,
Aviv Keshet60968ec2013-04-11 18:44:14 -0700136 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700137
138 self.assertCommandContains(expected_command)
139
140
Aviv Keshet940c17f2013-04-11 18:41:42 -0700141class PortageManipulationsTest(mox.MoxTestBase):
Don Garrett25f309a2014-03-19 14:02:12 -0700142 """Test usage of autotest_quickmerge.portage."""
143
Aviv Keshet940c17f2013-04-11 18:41:42 -0700144 def testUpdatePackageContents(self):
145 """Test that UpdatePackageContents makes the correct calls to portage."""
146 autotest_quickmerge.portage = self.mox.CreateMockAnything('portage')
147 portage = autotest_quickmerge.portage
148
149 portage.root = TEST_PORTAGE_ROOT
150
151 mock_vartree = self.mox.CreateMockAnything('vartree')
152 mock_vartree.settings = {'an arbitrary' : 'dictionary'}
153 mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}}
154 portage.create_trees(TEST_PORTAGE_ROOT,
155 TEST_PORTAGE_ROOT).AndReturn(mock_tree)
156
157 mock_vartree.dbapi = self.mox.CreateMockAnything('dbapi')
158 mock_vartree.dbapi.cp_list(TEST_PACKAGE_CP).AndReturn([TEST_PACKAGE_CPV])
159
160 mock_package = self.mox.CreateMockAnything('dblink')
161 portage.dblink(TEST_PACKAGE_C, TEST_PACKAGE_PV, #pylint: disable-msg=E1101
162 settings=mock_vartree.settings,
163 vartree=mock_vartree).AndReturn(mock_package)
164 mock_package.getcontents().AndReturn(TEST_PACKAGE_OLDCONTENTS)
165
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
178 self.mox.ReplayAll()
179
180 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
181 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
182 autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP,
183 TEST_PORTAGE_ROOT)
184
185 self.mox.VerifyAll()
186
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700187class PortageAPITest(unittest.TestCase):
188 """Ensures that required portage API exists."""
189 def runTest(self):
190 try:
Don Garrett25f309a2014-03-19 14:02:12 -0700191 # pylint: disable=F0401
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700192 import portage
193 except ImportError:
194 self.skipTest('Portage not available in test environment. Re-run test '
195 'in chroot.')
196 try:
197 # pylint: disable-msg=E1101
198 f = portage.vardbapi.writeContentsToContentsFile
199 except AttributeError:
200 self.fail('Required writeContentsToContentsFile function does '
201 'not exist.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700202
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700203 self.assertIsInstance(f, types.UnboundMethodType,
204 'Required writeContentsToContentsFile is not '
205 'a function.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700206
Aviv Keshetb1238c32013-04-01 11:42:13 -0700207if __name__ == '__main__':
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700208 cros_test_lib.main()