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 | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 44 | RSYNC_TEST_DESTINATION_PATH = '/foo/bar/' |
| 45 | |
| 46 | TEST_PACKAGE_CP = 'a_cute/little_puppy' |
| 47 | TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159' |
| 48 | TEST_PACKAGE_C = 'a_cute' |
| 49 | TEST_PACKAGE_PV = 'little_puppy-3.14159' |
| 50 | TEST_PORTAGE_ROOT = '/bib/bob/' |
| 51 | TEST_PACKAGE_OLDCONTENTS = { |
| 52 | u'/by/the/prickling/of/my/thumbs' : (u'obj', '1234', '4321'), |
| 53 | u'/something/wicked/this/way/comes' : (u'dir',) |
| 54 | } |
| 55 | |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 56 | class ItemizeChangesFromRsyncOutput(unittest.TestCase): |
| 57 | |
| 58 | def testItemizeChangesFromRsyncOutput(self): |
| 59 | """Test that rsync output parser returns correct FileMutations.""" |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 60 | expected_new = set( |
| 61 | [('>f+++++++++', '/foo/bar/new_file'), |
| 62 | ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'), |
| 63 | ('cL+++++++++', '/foo/bar/new_symlink')]) |
| 64 | |
| 65 | expected_mod = set( |
| 66 | [('>f..t......', '/foo/bar/touched file with spaces'), |
| 67 | ('>f..t......', '/foo/bar/touched_file'), |
| 68 | ('>f.st......', '/foo/bar/modified_contents_file'), |
| 69 | ('.f...p.....', '/foo/bar/modified_permissions_file'), |
| 70 | ('.f....o....', '/foo/bar/modified_owner_file'), |
| 71 | ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')]) |
| 72 | |
| 73 | expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')]) |
| 74 | |
| 75 | report = autotest_quickmerge.ItemizeChangesFromRsyncOutput( |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 76 | RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH) |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 77 | |
| 78 | self.assertEqual(expected_new, set(report.new_files)) |
| 79 | self.assertEqual(expected_mod, set(report.modified_files)) |
| 80 | self.assertEqual(expected_dir, set(report.new_directories)) |
| 81 | |
| 82 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 83 | class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase): |
| 84 | |
| 85 | def testRsyncQuickmergeCommand(self): |
| 86 | """Test that RsyncQuickMerge makes correct call to SudoRunCommand""" |
| 87 | include_file_name = 'an_include_file_name' |
| 88 | source_path = 'a_source_path' |
| 89 | sysroot_path = 'a_sysroot_path' |
| 90 | |
| 91 | expected_command = ['rsync', '-a', '-n', '-u', '-i', |
| 92 | '--exclude=**.pyc', '--exclude=**.pyo', |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 93 | '--exclude=** -> *', |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 94 | '--include-from=%s' % include_file_name, |
| 95 | '--exclude=*', |
| 96 | source_path, |
| 97 | sysroot_path] |
| 98 | |
| 99 | autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path, |
| 100 | include_file_name, |
| 101 | pretend=True, |
Aviv Keshet | 60968ec | 2013-04-11 18:44:14 -0700 | [diff] [blame] | 102 | overwrite=False) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 103 | |
| 104 | self.assertCommandContains(expected_command) |
| 105 | |
| 106 | |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 107 | class PortageManipulationsTest(mox.MoxTestBase): |
| 108 | def testUpdatePackageContents(self): |
| 109 | """Test that UpdatePackageContents makes the correct calls to portage.""" |
| 110 | autotest_quickmerge.portage = self.mox.CreateMockAnything('portage') |
| 111 | portage = autotest_quickmerge.portage |
| 112 | |
| 113 | portage.root = TEST_PORTAGE_ROOT |
| 114 | |
| 115 | mock_vartree = self.mox.CreateMockAnything('vartree') |
| 116 | mock_vartree.settings = {'an arbitrary' : 'dictionary'} |
| 117 | mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}} |
| 118 | portage.create_trees(TEST_PORTAGE_ROOT, |
| 119 | TEST_PORTAGE_ROOT).AndReturn(mock_tree) |
| 120 | |
| 121 | mock_vartree.dbapi = self.mox.CreateMockAnything('dbapi') |
| 122 | mock_vartree.dbapi.cp_list(TEST_PACKAGE_CP).AndReturn([TEST_PACKAGE_CPV]) |
| 123 | |
| 124 | mock_package = self.mox.CreateMockAnything('dblink') |
| 125 | portage.dblink(TEST_PACKAGE_C, TEST_PACKAGE_PV, #pylint: disable-msg=E1101 |
| 126 | settings=mock_vartree.settings, |
| 127 | vartree=mock_vartree).AndReturn(mock_package) |
| 128 | mock_package.getcontents().AndReturn(TEST_PACKAGE_OLDCONTENTS) |
| 129 | |
| 130 | EXPECTED_NEW_ENTRIES = { |
| 131 | '/foo/bar/new_empty_directory': (u'dir',), |
| 132 | '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'), |
| 133 | '/foo/bar/new_file': (u'obj', '0', '0'), |
| 134 | '/foo/bar/new_symlink': (u'obj', '0', '0') |
| 135 | } |
| 136 | RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy() |
| 137 | RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES) |
| 138 | |
| 139 | mock_vartree.dbapi.writeContentsToContentsFile(mock_package, |
| 140 | RESULT_DICIONARY) |
| 141 | |
| 142 | self.mox.ReplayAll() |
| 143 | |
| 144 | change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput( |
| 145 | RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH) |
| 146 | autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP, |
| 147 | TEST_PORTAGE_ROOT) |
| 148 | |
| 149 | self.mox.VerifyAll() |
| 150 | |
| 151 | |
| 152 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 153 | if __name__ == '__main__': |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 154 | cros_test_lib.main() |