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