blob: c7dcd8bb211b6d7b1550703cc8697a0095a3388f [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 Keshet787ffcd2013-04-08 15:14:56 -070011import unittest
12
13
David James81f69172013-04-11 10:42:43 -070014sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__)))
Aviv Keshetb1238c32013-04-01 11:42:13 -070015from chromite.lib import cros_build_lib_unittest
16from chromite.lib import cros_test_lib
17from chromite.scripts import autotest_quickmerge
18
Aviv Keshet787ffcd2013-04-08 15:14:56 -070019
20RSYNC_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
27cL+++++++++ 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
31cd+++++++++ 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
43class 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 Keshetb1238c32013-04-01 11:42:13 -070072class 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 Keshet787ffcd2013-04-08 15:14:56 -070082 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -070083 '--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,
91 overwrite=False,
92 quiet=False)
93
94 self.assertCommandContains(expected_command)
95
96
97if __name__ == '__main__':
Aviv Keshet787ffcd2013-04-08 15:14:56 -070098 cros_test_lib.main()