blob: be0fd8bd816527f4954b065fa8b45a456dccb807 [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
Aviv Keshet940c17f2013-04-11 18:41:42 -070012import mox
Aviv Keshet787ffcd2013-04-08 15:14:56 -070013
14
David James81f69172013-04-11 10:42:43 -070015sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__)))
Aviv Keshetb1238c32013-04-01 11:42:13 -070016from chromite.lib import cros_build_lib_unittest
17from chromite.lib import cros_test_lib
18from chromite.scripts import autotest_quickmerge
19
Aviv Keshet787ffcd2013-04-08 15:14:56 -070020
21RSYNC_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
28cL+++++++++ 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
32cd+++++++++ 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 Keshet940c17f2013-04-11 18:41:42 -070044RSYNC_TEST_DESTINATION_PATH = '/foo/bar/'
45
46TEST_PACKAGE_CP = 'a_cute/little_puppy'
47TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159'
48TEST_PACKAGE_C = 'a_cute'
49TEST_PACKAGE_PV = 'little_puppy-3.14159'
50TEST_PORTAGE_ROOT = '/bib/bob/'
51TEST_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 Keshet787ffcd2013-04-08 15:14:56 -070056class ItemizeChangesFromRsyncOutput(unittest.TestCase):
57
58 def testItemizeChangesFromRsyncOutput(self):
59 """Test that rsync output parser returns correct FileMutations."""
Aviv Keshet787ffcd2013-04-08 15:14:56 -070060 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 Keshet940c17f2013-04-11 18:41:42 -070076 RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH)
Aviv Keshet787ffcd2013-04-08 15:14:56 -070077
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 Keshetb1238c32013-04-01 11:42:13 -070083class 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 Keshet787ffcd2013-04-08 15:14:56 -070093 '--exclude=** -> *',
Aviv Keshetb1238c32013-04-01 11:42:13 -070094 '--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 Keshet60968ec2013-04-11 18:44:14 -0700102 overwrite=False)
Aviv Keshetb1238c32013-04-01 11:42:13 -0700103
104 self.assertCommandContains(expected_command)
105
106
Aviv Keshet940c17f2013-04-11 18:41:42 -0700107class 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 Keshetb1238c32013-04-01 11:42:13 -0700153if __name__ == '__main__':
Aviv Keshet787ffcd2013-04-08 15:14:56 -0700154 cros_test_lib.main()