Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 2 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for autotest_quickmerge.""" |
| 7 | |
Mike Frysinger | 1d4752b | 2014-11-08 04:00:18 -0500 | [diff] [blame] | 8 | # pylint: disable=bad-continuation |
| 9 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 10 | from __future__ import print_function |
| 11 | |
David James | 81f6917 | 2013-04-11 10:42:43 -0700 | [diff] [blame] | 12 | import os |
| 13 | import sys |
Aviv Keshet | b60fb3a | 2013-10-10 13:46:55 -0700 | [diff] [blame] | 14 | import types |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 15 | import unittest |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 16 | import mox |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 17 | |
| 18 | |
David James | 81f6917 | 2013-04-11 10:42:43 -0700 | [diff] [blame] | 19 | sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__))) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 20 | from chromite.lib import cros_build_lib_unittest |
| 21 | from chromite.lib import cros_test_lib |
| 22 | from chromite.scripts import autotest_quickmerge |
| 23 | |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 24 | |
| 25 | RSYNC_TEST_OUTPUT = """.d..t...... ./ |
| 26 | >f..t...... touched file with spaces |
| 27 | >f..t...... touched_file |
| 28 | >f.st...... modified_contents_file |
| 29 | .f...p..... modified_permissions_file |
| 30 | .f....o.... modified_owner_file |
| 31 | >f+++++++++ new_file |
| 32 | cL+++++++++ new_symlink -> directory_a/new_file_in_directory |
| 33 | .d..t...... directory_a/ |
| 34 | >f+++++++++ directory_a/new_file_in_directory |
| 35 | >f..t...... directory_a/touched_file_in_directory |
| 36 | cd+++++++++ new_empty_directory/ |
| 37 | .d..t...... touched_empty_directory/""" |
| 38 | # The output format of rsync's itemized changes has a few unusual cases |
| 39 | # that are ambiguous. For instance, if the operation involved creating a |
| 40 | # symbolic link named "a -> b" to a file named "c", the rsync output would be: |
| 41 | # cL+++++++++ a -> b -> c |
| 42 | # which is indistinguishable from the output for creating a symbolic link named |
| 43 | # "a" to a file named "b -> c". |
| 44 | # Since there is no easy resolution to this ambiguity, and it seems like a case |
| 45 | # that would rarely or never be encountered in the wild, rsync quickmerge |
| 46 | # will exclude all files which contain the substring " -> " in their name. |
| 47 | |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 48 | RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE = \ |
| 49 | """>f..t...... client/ardvark.py |
| 50 | .d..t...... client/site_tests/ |
| 51 | >f+++++++++ client/site_tests/nothing.py |
| 52 | .d..t...... client/site_tests/factory_Leds/ |
| 53 | >f+++++++++ client/site_tests/factory_Leds/factory_Leds2.py |
| 54 | >f..tpog... client/site_tests/login_UserPolicyKeys/control |
| 55 | >f..tpog... client/site_tests/login_UserPolicyKeys/login_UserPolicyKeys.py |
| 56 | >f..t...... client/site_tests/platform_Cryptohome/platform_Cryptohome.py |
| 57 | >f..tpog... server/site_tests/security_DbusFuzzServer/control |
| 58 | >f..t.og... utils/coverage_suite.py |
| 59 | .d..t...... client/site_tests/power_Thermal/ |
| 60 | cd+++++++++ client/site_tests/power_Thermal/a/ |
| 61 | cd+++++++++ client/site_tests/power_Thermal/a/b/ |
| 62 | cd+++++++++ client/site_tests/power_Thermal/a/b/c/ |
| 63 | >f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py""" |
| 64 | |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 65 | RSYNC_TEST_DESTINATION_PATH = '/foo/bar/' |
| 66 | |
| 67 | TEST_PACKAGE_CP = 'a_cute/little_puppy' |
| 68 | TEST_PACKAGE_CPV = 'a_cute/little_puppy-3.14159' |
| 69 | TEST_PACKAGE_C = 'a_cute' |
| 70 | TEST_PACKAGE_PV = 'little_puppy-3.14159' |
| 71 | TEST_PORTAGE_ROOT = '/bib/bob/' |
| 72 | TEST_PACKAGE_OLDCONTENTS = { |
| 73 | u'/by/the/prickling/of/my/thumbs' : (u'obj', '1234', '4321'), |
| 74 | u'/something/wicked/this/way/comes' : (u'dir',) |
| 75 | } |
| 76 | |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 77 | class ItemizeChangesFromRsyncOutput(unittest.TestCase): |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 78 | """Test autotest_quickmerge.ItemizeChangesFromRsyncOutput.""" |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 79 | |
| 80 | def testItemizeChangesFromRsyncOutput(self): |
| 81 | """Test that rsync output parser returns correct FileMutations.""" |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 82 | expected_new = set( |
| 83 | [('>f+++++++++', '/foo/bar/new_file'), |
| 84 | ('>f+++++++++', '/foo/bar/directory_a/new_file_in_directory'), |
| 85 | ('cL+++++++++', '/foo/bar/new_symlink')]) |
| 86 | |
| 87 | expected_mod = set( |
| 88 | [('>f..t......', '/foo/bar/touched file with spaces'), |
| 89 | ('>f..t......', '/foo/bar/touched_file'), |
| 90 | ('>f.st......', '/foo/bar/modified_contents_file'), |
| 91 | ('.f...p.....', '/foo/bar/modified_permissions_file'), |
| 92 | ('.f....o....', '/foo/bar/modified_owner_file'), |
| 93 | ('>f..t......', '/foo/bar/directory_a/touched_file_in_directory')]) |
| 94 | |
| 95 | expected_dir = set([('cd+++++++++', '/foo/bar/new_empty_directory/')]) |
| 96 | |
| 97 | report = autotest_quickmerge.ItemizeChangesFromRsyncOutput( |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 98 | RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH) |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 99 | |
| 100 | self.assertEqual(expected_new, set(report.new_files)) |
| 101 | self.assertEqual(expected_mod, set(report.modified_files)) |
| 102 | self.assertEqual(expected_dir, set(report.new_directories)) |
| 103 | |
| 104 | |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 105 | class PackageNameParsingTest(unittest.TestCase): |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 106 | """Test autotest_quickmerge.GetStalePackageNames.""" |
Aviv Keshet | 75d6596 | 2013-04-17 16:15:23 -0700 | [diff] [blame] | 107 | |
| 108 | def testGetStalePackageNames(self): |
| 109 | autotest_sysroot = '/an/arbitrary/path/' |
| 110 | change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput( |
| 111 | RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot) |
| 112 | package_matches = autotest_quickmerge.GetStalePackageNames( |
| 113 | change_report.modified_files + change_report.new_files, |
| 114 | autotest_sysroot) |
| 115 | expected_set = set(['factory_Leds', 'login_UserPolicyKeys', |
| 116 | 'platform_Cryptohome', 'power_Thermal']) |
| 117 | self.assertEqual(set(package_matches), expected_set) |
| 118 | |
| 119 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 120 | class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase): |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 121 | """Test autotest_quickmerge.RsyncQuickmerge.""" |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 122 | |
| 123 | def testRsyncQuickmergeCommand(self): |
| 124 | """Test that RsyncQuickMerge makes correct call to SudoRunCommand""" |
| 125 | include_file_name = 'an_include_file_name' |
| 126 | source_path = 'a_source_path' |
| 127 | sysroot_path = 'a_sysroot_path' |
| 128 | |
| 129 | expected_command = ['rsync', '-a', '-n', '-u', '-i', |
| 130 | '--exclude=**.pyc', '--exclude=**.pyo', |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 131 | '--exclude=** -> *', |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 132 | '--include-from=%s' % include_file_name, |
| 133 | '--exclude=*', |
| 134 | source_path, |
| 135 | sysroot_path] |
| 136 | |
| 137 | autotest_quickmerge.RsyncQuickmerge(source_path, sysroot_path, |
| 138 | include_file_name, |
| 139 | pretend=True, |
Aviv Keshet | 60968ec | 2013-04-11 18:44:14 -0700 | [diff] [blame] | 140 | overwrite=False) |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 141 | |
| 142 | self.assertCommandContains(expected_command) |
| 143 | |
| 144 | |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 145 | class PortageManipulationsTest(mox.MoxTestBase): |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 146 | """Test usage of autotest_quickmerge.portage.""" |
| 147 | |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 148 | def testUpdatePackageContents(self): |
| 149 | """Test that UpdatePackageContents makes the correct calls to portage.""" |
| 150 | autotest_quickmerge.portage = self.mox.CreateMockAnything('portage') |
| 151 | portage = autotest_quickmerge.portage |
| 152 | |
| 153 | portage.root = TEST_PORTAGE_ROOT |
| 154 | |
| 155 | mock_vartree = self.mox.CreateMockAnything('vartree') |
| 156 | mock_vartree.settings = {'an arbitrary' : 'dictionary'} |
| 157 | mock_tree = {TEST_PORTAGE_ROOT : {'vartree' : mock_vartree}} |
| 158 | portage.create_trees(TEST_PORTAGE_ROOT, |
| 159 | TEST_PORTAGE_ROOT).AndReturn(mock_tree) |
| 160 | |
| 161 | mock_vartree.dbapi = self.mox.CreateMockAnything('dbapi') |
| 162 | mock_vartree.dbapi.cp_list(TEST_PACKAGE_CP).AndReturn([TEST_PACKAGE_CPV]) |
| 163 | |
| 164 | mock_package = self.mox.CreateMockAnything('dblink') |
Mike Frysinger | 1132a70 | 2014-11-10 21:50:14 -0500 | [diff] [blame] | 165 | portage.dblink(TEST_PACKAGE_C, TEST_PACKAGE_PV, # pylint: disable=E1101 |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 166 | settings=mock_vartree.settings, |
| 167 | vartree=mock_vartree).AndReturn(mock_package) |
| 168 | mock_package.getcontents().AndReturn(TEST_PACKAGE_OLDCONTENTS) |
| 169 | |
| 170 | EXPECTED_NEW_ENTRIES = { |
| 171 | '/foo/bar/new_empty_directory': (u'dir',), |
| 172 | '/foo/bar/directory_a/new_file_in_directory': (u'obj', '0', '0'), |
| 173 | '/foo/bar/new_file': (u'obj', '0', '0'), |
| 174 | '/foo/bar/new_symlink': (u'obj', '0', '0') |
| 175 | } |
| 176 | RESULT_DICIONARY = TEST_PACKAGE_OLDCONTENTS.copy() |
| 177 | RESULT_DICIONARY.update(EXPECTED_NEW_ENTRIES) |
| 178 | |
| 179 | mock_vartree.dbapi.writeContentsToContentsFile(mock_package, |
| 180 | RESULT_DICIONARY) |
| 181 | |
| 182 | self.mox.ReplayAll() |
| 183 | |
| 184 | change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput( |
| 185 | RSYNC_TEST_OUTPUT, RSYNC_TEST_DESTINATION_PATH) |
| 186 | autotest_quickmerge.UpdatePackageContents(change_report, TEST_PACKAGE_CP, |
| 187 | TEST_PORTAGE_ROOT) |
| 188 | |
| 189 | self.mox.VerifyAll() |
| 190 | |
Aviv Keshet | b60fb3a | 2013-10-10 13:46:55 -0700 | [diff] [blame] | 191 | class PortageAPITest(unittest.TestCase): |
| 192 | """Ensures that required portage API exists.""" |
| 193 | def runTest(self): |
| 194 | try: |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 195 | # pylint: disable=F0401 |
Aviv Keshet | b60fb3a | 2013-10-10 13:46:55 -0700 | [diff] [blame] | 196 | import portage |
| 197 | except ImportError: |
| 198 | self.skipTest('Portage not available in test environment. Re-run test ' |
| 199 | 'in chroot.') |
| 200 | try: |
Mike Frysinger | 1132a70 | 2014-11-10 21:50:14 -0500 | [diff] [blame] | 201 | # pylint: disable=E1101 |
Aviv Keshet | b60fb3a | 2013-10-10 13:46:55 -0700 | [diff] [blame] | 202 | f = portage.vardbapi.writeContentsToContentsFile |
| 203 | except AttributeError: |
| 204 | self.fail('Required writeContentsToContentsFile function does ' |
| 205 | 'not exist.') |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 206 | |
Aviv Keshet | b60fb3a | 2013-10-10 13:46:55 -0700 | [diff] [blame] | 207 | self.assertIsInstance(f, types.UnboundMethodType, |
| 208 | 'Required writeContentsToContentsFile is not ' |
| 209 | 'a function.') |
Aviv Keshet | 940c17f | 2013-04-11 18:41:42 -0700 | [diff] [blame] | 210 | |
Aviv Keshet | b1238c3 | 2013-04-01 11:42:13 -0700 | [diff] [blame] | 211 | if __name__ == '__main__': |
Aviv Keshet | 787ffcd | 2013-04-08 15:14:56 -0700 | [diff] [blame] | 212 | cros_test_lib.main() |