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 |
| 12 | |
| 13 | |
David James | 81f6917 | 2013-04-11 10:42:43 -0700 | [diff] [blame] | 14 | sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__))) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_build_lib_unittest |
| 16 | from chromite.lib import cros_test_lib |
| 17 | from chromite.scripts import autotest_quickmerge |
| 18 | |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 19 | |
| 20 | RSYNC_TEST_OUTPUT = """.d..t...... ./ |
| 21 | >f..t...... touched file with spaces |
| 22 | >f..t...... touched_file |
| 23 | >f.st...... modified_contents_file |
| 24 | .f...p..... modified_permissions_file |
| 25 | .f....o.... modified_owner_file |
| 26 | >f+++++++++ new_file |
| 27 | cL+++++++++ new_symlink -> directory_a/new_file_in_directory |
| 28 | .d..t...... directory_a/ |
| 29 | >f+++++++++ directory_a/new_file_in_directory |
| 30 | >f..t...... directory_a/touched_file_in_directory |
| 31 | cd+++++++++ new_empty_directory/ |
| 32 | .d..t...... touched_empty_directory/""" |
| 33 | # The output format of rsync's itemized changes has a few unusual cases |
| 34 | # that are ambiguous. For instance, if the operation involved creating a |
| 35 | # symbolic link named "a -> b" to a file named "c", the rsync output would be: |
| 36 | # cL+++++++++ a -> b -> c |
| 37 | # which is indistinguishable from the output for creating a symbolic link named |
| 38 | # "a" to a file named "b -> c". |
| 39 | # Since there is no easy resolution to this ambiguity, and it seems like a case |
| 40 | # that would rarely or never be encountered in the wild, rsync quickmerge |
| 41 | # will exclude all files which contain the substring " -> " in their name. |
| 42 | |
| 43 | class ItemizeChangesFromRsyncOutput(unittest.TestCase): |
| 44 | |
| 45 | def testItemizeChangesFromRsyncOutput(self): |
| 46 | """Test that rsync output parser returns correct FileMutations.""" |
| 47 | destination_path = '/foo/bar' |
| 48 | |
| 49 | expected_new = set( |
| 50 | [('>f+++++++++', '/foo/bar/new_file'), |
| 51 | ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'), |
| 52 | ('cL+++++++++', '/foo/bar/new_symlink')]) |
| 53 | |
| 54 | expected_mod = set( |
| 55 | [('>f..t......', '/foo/bar/touched file with spaces'), |
| 56 | ('>f..t......', '/foo/bar/touched_file'), |
| 57 | ('>f.st......', '/foo/bar/modified_contents_file'), |
| 58 | ('.f...p.....', '/foo/bar/modified_permissions_file'), |
| 59 | ('.f....o....', '/foo/bar/modified_owner_file'), |
| 60 | ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')]) |
| 61 | |
| 62 | expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')]) |
| 63 | |
| 64 | report = autotest_quickmerge.ItemizeChangesFromRsyncOutput( |
| 65 | RSYNC_TEST_OUTPUT, destination_path) |
| 66 | |
| 67 | self.assertEqual(expected_new, set(report.new_files)) |
| 68 | self.assertEqual(expected_mod, set(report.modified_files)) |
| 69 | self.assertEqual(expected_dir, set(report.new_directories)) |
| 70 | |
| 71 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 72 | class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase): |
| 73 | |
| 74 | def testRsyncQuickmergeCommand(self): |
| 75 | """Test that RsyncQuickMerge makes correct call to SudoRunCommand""" |
| 76 | include_file_name = 'an_include_file_name' |
| 77 | source_path = 'a_source_path' |
| 78 | sysroot_path = 'a_sysroot_path' |
| 79 | |
| 80 | expected_command = ['rsync', '-a', '-n', '-u', '-i', |
| 81 | '--exclude=**.pyc', '--exclude=**.pyo', |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 82 | '--exclude=** -> *', |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 83 | '--include-from=%s' % include_file_name, |
| 84 | '--exclude=*', |
| 85 | source_path, |
| 86 | sysroot_path] |
| 87 | |
| 88 | autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path, |
| 89 | include_file_name, |
| 90 | pretend=True, |
Aviv Keshet | 60968ec | 2013-04-11 18:44:14 -0700 | [diff] [blame^] | 91 | overwrite=False) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 92 | |
| 93 | self.assertCommandContains(expected_command) |
| 94 | |
| 95 | |
| 96 | if __name__ == '__main__': |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 97 | cros_test_lib.main() |