blob: 87dc2483af02bab49ade498bcc5a695d03d7a00e [file] [log] [blame]
Aviv Keshetb1238c32013-04-01 11:42:13 -07001#!/usr/bin/python
2
3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Unit tests for autotest_quickmerge."""
8
David James81f69172013-04-11 10:42:43 -07009import os
10import sys
Aviv Keshetb60fb3a2013-10-10 13:46:55 -070011import types
Aviv Keshet787ffcd2013-04-08 15:14:56 -070012import unittest
Aviv Keshet940c17f2013-04-11 18:41:42 -070013import mox
Aviv Keshet787ffcd2013-04-08 15:14:56 -070014
15
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
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 = {
70 u'/by/the/prickling/of/my/thumbs' : (u'obj', '1234', '4321'),
71 u'/something/wicked/this/way/comes' : (u'dir',)
72}
73
Aviv Keshet787ffcd2013-04-08 15:14:56 -070074class ItemizeChangesFromRsyncOutput(unittest.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070075 """Test autotest_quickmerge.ItemizeChangesFromRsyncOutput."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070076
77 def testItemizeChangesFromRsyncOutput(self):
78 """Test that rsync output parser returns correct FileMutations."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070079 expected_new = set(
80 [('>f+++++++++', '/foo/bar/new_file'),
81 ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'),
82 ('cL+++++++++', '/foo/bar/new_symlink')])
83
84 expected_mod = set(
85 [('>f..t......', '/foo/bar/touched file with spaces'),
86 ('>f..t......', '/foo/bar/touched_file'),
87 ('>f.st......', '/foo/bar/modified_contents_file'),
88 ('.f...p.....', '/foo/bar/modified_permissions_file'),
89 ('.f....o....', '/foo/bar/modified_owner_file'),
90 ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')])
91
92 expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')])
93
94 report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
Aviv Keshet940c17f2013-04-11 18:41:42 -070095 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet787ffcd2013-04-08 15:14:56 -070096
97 self.assertEqual(expected_new, set(report.new_files))
98 self.assertEqual(expected_mod, set(report.modified_files))
99 self.assertEqual(expected_dir, set(report.new_directories))
100
101
Aviv Keshet75d65962013-04-17 16:15:23 -0700102class PackageNameParsingTest(unittest.TestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700103 """Test autotest_quickmerge.GetStalePackageNames."""
Aviv Keshet75d65962013-04-17 16:15:23 -0700104
105 def testGetStalePackageNames(self):
106 autotest_sysroot = '/an/arbitrary/path/'
107 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
108 RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
109 package_matches = autotest_quickmerge.GetStalePackageNames(
110 change_report.modified_files + change_report.new_files,
111 autotest_sysroot)
112 expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
113 'platform_Cryptohome', 'power_Thermal'])
114 self.assertEqual(set(package_matches), expected_set)
115
116
Aviv Keshetb1238c32013-04-01 11:42:13 -0700117class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700118 """Test autotest_quickmerge.RsyncQuickmerge."""
Aviv Keshetb1238c32013-04-01 11:42:13 -0700119
120 def testRsyncQuickmergeCommand(self):
121 """Test that RsyncQuickMerge makes correct call to SudoRunCommand"""
122 include_file_name = 'an_include_file_name'
123 source_path = 'a_source_path'
124 sysroot_path = 'a_sysroot_path'
125
126 expected_command = ['rsync', '-a', '-n', '-u', '-i',
127 '--exclude=**.pyc', '--exclude=**.pyo',
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700128 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -0700129 '--include-from=%s' % include_file_name,
130 '--exclude=*',
131 source_path,
132 sysroot_path]
133
134 autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path,
135 include_file_name,
136 pretend=True,
Aviv Keshet60968ec2013-04-11 18:44:14 -0700137 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700138
139 self.assertCommandContains(expected_command)
140
141
Aviv Keshet940c17f2013-04-11 18:41:42 -0700142class PortageManipulationsTest(mox.MoxTestBase):
Don Garrett25f309a2014-03-19 14:02:12 -0700143 """Test usage of autotest_quickmerge.portage."""
144
Aviv Keshet940c17f2013-04-11 18:41:42 -0700145 def testUpdatePackageContents(self):
146 """Test that UpdatePackageContents makes the correct calls to portage."""
147 autotest_quickmerge.portage = self.mox.CreateMockAnything('portage')
148 portage = autotest_quickmerge.portage
149
150 portage.root = TEST_PORTAGE_ROOT
151
152 mock_vartree = self.mox.CreateMockAnything('vartree')
153 mock_vartree.settings = {'an arbitrary' : 'dictionary'}
154 mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}}
155 portage.create_trees(TEST_PORTAGE_ROOT,
156 TEST_PORTAGE_ROOT).AndReturn(mock_tree)
157
158 mock_vartree.dbapi = self.mox.CreateMockAnything('dbapi')
159 mock_vartree.dbapi.cp_list(TEST_PACKAGE_CP).AndReturn([TEST_PACKAGE_CPV])
160
161 mock_package = self.mox.CreateMockAnything('dblink')
162 portage.dblink(TEST_PACKAGE_C, TEST_PACKAGE_PV, #pylint: disable-msg=E1101
163 settings=mock_vartree.settings,
164 vartree=mock_vartree).AndReturn(mock_package)
165 mock_package.getcontents().AndReturn(TEST_PACKAGE_OLDCONTENTS)
166
167 EXPECTED_NEW_ENTRIES = {
168 '/foo/bar/new_empty_directory': (u'dir',),
169 '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'),
170 '/foo/bar/new_file': (u'obj', '0', '0'),
171 '/foo/bar/new_symlink': (u'obj', '0', '0')
172 }
173 RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy()
174 RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES)
175
176 mock_vartree.dbapi.writeContentsToContentsFile(mock_package,
177 RESULT_DICIONARY)
178
179 self.mox.ReplayAll()
180
181 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
182 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
183 autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP,
184 TEST_PORTAGE_ROOT)
185
186 self.mox.VerifyAll()
187
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700188class PortageAPITest(unittest.TestCase):
189 """Ensures that required portage API exists."""
190 def runTest(self):
191 try:
Don Garrett25f309a2014-03-19 14:02:12 -0700192 # pylint: disable=F0401
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700193 import portage
194 except ImportError:
195 self.skipTest('Portage not available in test environment. Re-run test '
196 'in chroot.')
197 try:
198 # pylint: disable-msg=E1101
199 f = portage.vardbapi.writeContentsToContentsFile
200 except AttributeError:
201 self.fail('Required writeContentsToContentsFile function does '
202 'not exist.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700203
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700204 self.assertIsInstance(f, types.UnboundMethodType,
205 'Required writeContentsToContentsFile is not '
206 'a function.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700207
Aviv Keshetb1238c32013-04-01 11:42:13 -0700208if __name__ == '__main__':
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700209 cros_test_lib.main()