blob: f9ee8c3924a321b04da0f8957636e4f1eaa3e5c5 [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):
75
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):
102
103 def testGetStalePackageNames(self):
104 autotest_sysroot = '/an/arbitrary/path/'
105 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
106 RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
107 package_matches = autotest_quickmerge.GetStalePackageNames(
108 change_report.modified_files + change_report.new_files,
109 autotest_sysroot)
110 expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
111 'platform_Cryptohome', 'power_Thermal'])
112 self.assertEqual(set(package_matches), expected_set)
113
114
Aviv Keshetb1238c32013-04-01 11:42:13 -0700115class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase):
116
117 def testRsyncQuickmergeCommand(self):
118 """Test that RsyncQuickMerge makes correct call to SudoRunCommand"""
119 include_file_name = 'an_include_file_name'
120 source_path = 'a_source_path'
121 sysroot_path = 'a_sysroot_path'
122
123 expected_command = ['rsync', '-a', '-n', '-u', '-i',
124 '--exclude=**.pyc', '--exclude=**.pyo',
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700125 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -0700126 '--include-from=%s' % include_file_name,
127 '--exclude=*',
128 source_path,
129 sysroot_path]
130
131 autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path,
132 include_file_name,
133 pretend=True,
Aviv Keshet60968ec2013-04-11 18:44:14 -0700134 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700135
136 self.assertCommandContains(expected_command)
137
138
Aviv Keshet940c17f2013-04-11 18:41:42 -0700139class PortageManipulationsTest(mox.MoxTestBase):
140 def testUpdatePackageContents(self):
141 """Test that UpdatePackageContents makes the correct calls to portage."""
142 autotest_quickmerge.portage = self.mox.CreateMockAnything('portage')
143 portage = autotest_quickmerge.portage
144
145 portage.root = TEST_PORTAGE_ROOT
146
147 mock_vartree = self.mox.CreateMockAnything('vartree')
148 mock_vartree.settings = {'an arbitrary' : 'dictionary'}
149 mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}}
150 portage.create_trees(TEST_PORTAGE_ROOT,
151 TEST_PORTAGE_ROOT).AndReturn(mock_tree)
152
153 mock_vartree.dbapi = self.mox.CreateMockAnything('dbapi')
154 mock_vartree.dbapi.cp_list(TEST_PACKAGE_CP).AndReturn([TEST_PACKAGE_CPV])
155
156 mock_package = self.mox.CreateMockAnything('dblink')
157 portage.dblink(TEST_PACKAGE_C, TEST_PACKAGE_PV, #pylint: disable-msg=E1101
158 settings=mock_vartree.settings,
159 vartree=mock_vartree).AndReturn(mock_package)
160 mock_package.getcontents().AndReturn(TEST_PACKAGE_OLDCONTENTS)
161
162 EXPECTED_NEW_ENTRIES = {
163 '/foo/bar/new_empty_directory': (u'dir',),
164 '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'),
165 '/foo/bar/new_file': (u'obj', '0', '0'),
166 '/foo/bar/new_symlink': (u'obj', '0', '0')
167 }
168 RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy()
169 RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES)
170
171 mock_vartree.dbapi.writeContentsToContentsFile(mock_package,
172 RESULT_DICIONARY)
173
174 self.mox.ReplayAll()
175
176 change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
177 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
178 autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP,
179 TEST_PORTAGE_ROOT)
180
181 self.mox.VerifyAll()
182
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700183class PortageAPITest(unittest.TestCase):
184 """Ensures that required portage API exists."""
185 def runTest(self):
186 try:
187 import portage
188 except ImportError:
189 self.skipTest('Portage not available in test environment. Re-run test '
190 'in chroot.')
191 try:
192 # pylint: disable-msg=E1101
193 f = portage.vardbapi.writeContentsToContentsFile
194 except AttributeError:
195 self.fail('Required writeContentsToContentsFile function does '
196 'not exist.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700197
Aviv Keshetb60fb3a2013-10-10 13:46:55 -0700198 self.assertIsInstance(f, types.UnboundMethodType,
199 'Required writeContentsToContentsFile is not '
200 'a function.')
Aviv Keshet940c17f2013-04-11 18:41:42 -0700201
Aviv Keshetb1238c32013-04-01 11:42:13 -0700202if __name__ == '__main__':
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700203 cros_test_lib.main()