Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 1 | #!/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 James | 81f6917 | 2013-04-11 10:42:43 -0700 | [diff] [blame] | 9 | import os |
| 10 | import sys |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 11 | import unittest |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 12 | import mox |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 13 | |
| 14 | |
David James | 81f6917 | 2013-04-11 10:42:43 -0700 | [diff] [blame] | 15 | sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__))) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib_unittest |
| 17 | from chromite.lib import cros_test_lib |
| 18 | from chromite.scripts import autotest_quickmerge |
| 19 | |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 20 | |
| 21 | RSYNC_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 |
| 28 | cL+++++++++ 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 |
| 32 | cd+++++++++ 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 Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame^] | 44 | RSYNC_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/ |
| 56 | cd+++++++++ client/site_tests/power_Thermal/a/ |
| 57 | cd+++++++++ client/site_tests/power_Thermal/a/b/ |
| 58 | cd+++++++++ client/site_tests/power_Thermal/a/b/c/ |
| 59 | >f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py""" |
| 60 | |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 61 | RSYNC_TEST_DESTINATION_PATH = '/foo/bar/' |
| 62 | |
| 63 | TEST_PACKAGE_CP = 'a_cute/little_puppy' |
| 64 | TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159' |
| 65 | TEST_PACKAGE_C = 'a_cute' |
| 66 | TEST_PACKAGE_PV = 'little_puppy-3.14159' |
| 67 | TEST_PORTAGE_ROOT = '/bib/bob/' |
| 68 | TEST_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 Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 73 | class ItemizeChangesFromRsyncOutput(unittest.TestCase): |
| 74 | |
| 75 | def testItemizeChangesFromRsyncOutput(self): |
| 76 | """Test that rsync output parser returns correct FileMutations.""" |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 77 | expected_new = set( |
| 78 | [('>f+++++++++', '/foo/bar/new_file'), |
| 79 | ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'), |
| 80 | ('cL+++++++++', '/foo/bar/new_symlink')]) |
| 81 | |
| 82 | expected_mod = set( |
| 83 | [('>f..t......', '/foo/bar/touched file with spaces'), |
| 84 | ('>f..t......', '/foo/bar/touched_file'), |
| 85 | ('>f.st......', '/foo/bar/modified_contents_file'), |
| 86 | ('.f...p.....', '/foo/bar/modified_permissions_file'), |
| 87 | ('.f....o....', '/foo/bar/modified_owner_file'), |
| 88 | ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')]) |
| 89 | |
| 90 | expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')]) |
| 91 | |
| 92 | report = autotest_quickmerge.ItemizeChangesFromRsyncOutput( |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 93 | RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH) |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 94 | |
| 95 | self.assertEqual(expected_new, set(report.new_files)) |
| 96 | self.assertEqual(expected_mod, set(report.modified_files)) |
| 97 | self.assertEqual(expected_dir, set(report.new_directories)) |
| 98 | |
| 99 | |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame^] | 100 | class PackageNameParsingTest(unittest.TestCase): |
| 101 | |
| 102 | def testGetStalePackageNames(self): |
| 103 | autotest_sysroot = '/an/arbitrary/path/' |
| 104 | change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput( |
| 105 | RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot) |
| 106 | package_matches = autotest_quickmerge.GetStalePackageNames( |
| 107 | change_report.modified_files + change_report.new_files, |
| 108 | autotest_sysroot) |
| 109 | expected_set = set(['factory_Leds', 'login_UserPolicyKeys', |
| 110 | 'platform_Cryptohome', 'power_Thermal']) |
| 111 | self.assertEqual(set(package_matches), expected_set) |
| 112 | |
| 113 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 114 | class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase): |
| 115 | |
| 116 | def testRsyncQuickmergeCommand(self): |
| 117 | """Test that RsyncQuickMerge makes correct call to SudoRunCommand""" |
| 118 | include_file_name = 'an_include_file_name' |
| 119 | source_path = 'a_source_path' |
| 120 | sysroot_path = 'a_sysroot_path' |
| 121 | |
| 122 | expected_command = ['rsync', '-a', '-n', '-u', '-i', |
| 123 | '--exclude=**.pyc', '--exclude=**.pyo', |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 124 | '--exclude=** -> *', |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 125 | '--include-from=%s' % include_file_name, |
| 126 | '--exclude=*', |
| 127 | source_path, |
| 128 | sysroot_path] |
| 129 | |
| 130 | autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path, |
| 131 | include_file_name, |
| 132 | pretend=True, |
Aviv Keshet | 60968ec | 2013-04-11 18:44:14 -0700 | [diff] [blame] | 133 | overwrite=False) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 134 | |
| 135 | self.assertCommandContains(expected_command) |
| 136 | |
| 137 | |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 138 | class PortageManipulationsTest(mox.MoxTestBase): |
| 139 | def testUpdatePackageContents(self): |
| 140 | """Test that UpdatePackageContents makes the correct calls to portage.""" |
| 141 | autotest_quickmerge.portage = self.mox.CreateMockAnything('portage') |
| 142 | portage = autotest_quickmerge.portage |
| 143 | |
| 144 | portage.root = TEST_PORTAGE_ROOT |
| 145 | |
| 146 | mock_vartree = self.mox.CreateMockAnything('vartree') |
| 147 | mock_vartree.settings = {'an arbitrary' : 'dictionary'} |
| 148 | mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}} |
| 149 | portage.create_trees(TEST_PORTAGE_ROOT, |
| 150 | TEST_PORTAGE_ROOT).AndReturn(mock_tree) |
| 151 | |
| 152 | mock_vartree.dbapi = self.mox.CreateMockAnything('dbapi') |
| 153 | mock_vartree.dbapi.cp_list(TEST_PACKAGE_CP).AndReturn([TEST_PACKAGE_CPV]) |
| 154 | |
| 155 | mock_package = self.mox.CreateMockAnything('dblink') |
| 156 | portage.dblink(TEST_PACKAGE_C, TEST_PACKAGE_PV, #pylint: disable-msg=E1101 |
| 157 | settings=mock_vartree.settings, |
| 158 | vartree=mock_vartree).AndReturn(mock_package) |
| 159 | mock_package.getcontents().AndReturn(TEST_PACKAGE_OLDCONTENTS) |
| 160 | |
| 161 | EXPECTED_NEW_ENTRIES = { |
| 162 | '/foo/bar/new_empty_directory': (u'dir',), |
| 163 | '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'), |
| 164 | '/foo/bar/new_file': (u'obj', '0', '0'), |
| 165 | '/foo/bar/new_symlink': (u'obj', '0', '0') |
| 166 | } |
| 167 | RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy() |
| 168 | RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES) |
| 169 | |
| 170 | mock_vartree.dbapi.writeContentsToContentsFile(mock_package, |
| 171 | RESULT_DICIONARY) |
| 172 | |
| 173 | self.mox.ReplayAll() |
| 174 | |
| 175 | change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput( |
| 176 | RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH) |
| 177 | autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP, |
| 178 | TEST_PORTAGE_ROOT) |
| 179 | |
| 180 | self.mox.VerifyAll() |
| 181 | |
| 182 | |
| 183 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 184 | if __name__ == '__main__': |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 185 | cros_test_lib.main() |