Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright (c) 2014 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 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 6 | """Unittests for deploy_server_local.py.""" |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import mock |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 11 | import subprocess |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 12 | import unittest |
| 13 | |
Richard Barnette | 2e0a423 | 2018-03-07 17:13:16 -0800 | [diff] [blame] | 14 | import common |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 15 | import deploy_server_local as dsl |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 16 | |
| 17 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 18 | class TestDeployServerLocal(unittest.TestCase): |
| 19 | """Test deploy_server_local with commands mocked out.""" |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 20 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 21 | orig_timer = dsl.SERVICE_STABILITY_TIMER |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 22 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 23 | PROD_STATUS = ('\x1b[1mproject autotest/ ' |
| 24 | ' \x1b[m\x1b[1mbranch prod\x1b[m\n') |
| 25 | |
| 26 | PROD_VERSIONS = '''\x1b[1mproject autotest/\x1b[m |
| 27 | /usr/local/autotest |
| 28 | ebb2182 |
| 29 | |
| 30 | \x1b[1mproject autotest/site_utils/autotest_private/\x1b[m |
| 31 | /usr/local/autotest/site_utils/autotest_private |
| 32 | 78b9626 |
| 33 | |
| 34 | \x1b[1mproject autotest/site_utils/autotest_tools/\x1b[m |
| 35 | /usr/local/autotest/site_utils/autotest_tools |
| 36 | a1598f7 |
| 37 | ''' |
| 38 | |
| 39 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 40 | def setUp(self): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 41 | dsl.SERVICE_STABILITY_TIMER = 0.01 |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 42 | |
| 43 | def tearDown(self): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 44 | dsl.SERVICE_STABILITY_TIMER = self.orig_timer |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 45 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 46 | def test_strip_terminal_codes(self): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 47 | """Test deploy_server_local.strip_terminal_codes.""" |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 48 | # Leave format free lines alone. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 49 | result = dsl.strip_terminal_codes('') |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 50 | self.assertEqual(result, '') |
| 51 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 52 | result = dsl.strip_terminal_codes('This is normal text.') |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 53 | self.assertEqual(result, 'This is normal text.') |
| 54 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 55 | result = dsl.strip_terminal_codes('Line1\nLine2\n') |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 56 | self.assertEqual(result, 'Line1\nLine2\n') |
| 57 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 58 | result = dsl.strip_terminal_codes('Line1\nLine2\n') |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 59 | self.assertEqual(result, 'Line1\nLine2\n') |
| 60 | |
| 61 | # Test cleaning lines with formatting. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 62 | result = dsl.strip_terminal_codes('\x1b[1m') |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 63 | self.assertEqual(result, '') |
| 64 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 65 | result = dsl.strip_terminal_codes('\x1b[m') |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 66 | self.assertEqual(result, '') |
| 67 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 68 | result = dsl.strip_terminal_codes('\x1b[1mm') |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 69 | self.assertEqual(result, 'm') |
| 70 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 71 | result = dsl.strip_terminal_codes(self.PROD_STATUS) |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 72 | self.assertEqual(result, |
| 73 | 'project autotest/ branch prod\n') |
| 74 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 75 | result = dsl.strip_terminal_codes(self.PROD_VERSIONS) |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 76 | self.assertEqual(result, '''project autotest/ |
| 77 | /usr/local/autotest |
| 78 | ebb2182 |
| 79 | |
| 80 | project autotest/site_utils/autotest_private/ |
| 81 | /usr/local/autotest/site_utils/autotest_private |
| 82 | 78b9626 |
| 83 | |
| 84 | project autotest/site_utils/autotest_tools/ |
| 85 | /usr/local/autotest/site_utils/autotest_tools |
| 86 | a1598f7 |
| 87 | ''') |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 88 | |
| 89 | @mock.patch('subprocess.check_output', autospec=True) |
| 90 | def test_verify_repo_clean(self, run_cmd): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 91 | """Test deploy_server_local.verify_repo_clean. |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 92 | |
| 93 | @param run_cmd: Mock of subprocess call used. |
| 94 | """ |
| 95 | # If repo returns what we expect, exit cleanly. |
Don Garrett | 699b4b3 | 2014-12-11 13:10:15 -0800 | [diff] [blame] | 96 | run_cmd.return_value = 'nothing to commit (working directory clean)\n' |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 97 | dsl.verify_repo_clean() |
Don Garrett | 699b4b3 | 2014-12-11 13:10:15 -0800 | [diff] [blame] | 98 | |
Prathmesh Prabhu | da28699 | 2015-04-07 13:20:08 -0700 | [diff] [blame] | 99 | # If repo contains any branches (even clean ones), raise. |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 100 | run_cmd.return_value = self.PROD_STATUS |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 101 | with self.assertRaises(dsl.DirtyTreeException): |
| 102 | dsl.verify_repo_clean() |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 103 | |
Prathmesh Prabhu | da28699 | 2015-04-07 13:20:08 -0700 | [diff] [blame] | 104 | # If repo doesn't return what we expect, raise. |
| 105 | run_cmd.return_value = "That's a very dirty repo you've got." |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 106 | with self.assertRaises(dsl.DirtyTreeException): |
| 107 | dsl.verify_repo_clean() |
Don Garrett | 699b4b3 | 2014-12-11 13:10:15 -0800 | [diff] [blame] | 108 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 109 | @mock.patch('subprocess.check_output', autospec=True) |
| 110 | def test_repo_versions(self, run_cmd): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 111 | """Test deploy_server_local.repo_versions. |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 112 | |
| 113 | @param run_cmd: Mock of subprocess call used. |
| 114 | """ |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 115 | expected = { |
| 116 | 'autotest': |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 117 | ('/usr/local/autotest', 'ebb2182'), |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 118 | 'autotest/site_utils/autotest_private': |
| 119 | ('/usr/local/autotest/site_utils/autotest_private', '78b9626'), |
| 120 | 'autotest/site_utils/autotest_tools': |
| 121 | ('/usr/local/autotest/site_utils/autotest_tools', 'a1598f7'), |
| 122 | } |
| 123 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 124 | run_cmd.return_value = self.PROD_VERSIONS |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 125 | result = dsl.repo_versions() |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 126 | self.assertEquals(result, expected) |
| 127 | |
| 128 | run_cmd.assert_called_with( |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 129 | ['repo', 'forall', '-p', '-c', |
| 130 | 'pwd && git log -1 --format=%h']) |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 131 | |
| 132 | @mock.patch('subprocess.check_output', autospec=True) |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 133 | def test_repo_sync_not_for_push_servers(self, run_cmd): |
| 134 | """Test deploy_server_local.repo_sync. |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 135 | |
| 136 | @param run_cmd: Mock of subprocess call used. |
| 137 | """ |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 138 | dsl.repo_sync() |
Richard Barnette | 488ac8b | 2017-09-01 09:28:55 -0700 | [diff] [blame] | 139 | expect_cmds = [mock.call(['git', 'checkout', 'cros/prod'], stderr=-2)] |
Shuqian Zhao | ec26bd7 | 2017-01-31 10:17:25 -0800 | [diff] [blame] | 140 | run_cmd.assert_has_calls(expect_cmds) |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 141 | |
| 142 | @mock.patch('subprocess.check_output', autospec=True) |
| 143 | def test_repo_sync_for_push_servers(self, run_cmd): |
| 144 | """Test deploy_server_local.repo_sync. |
| 145 | |
| 146 | @param run_cmd: Mock of subprocess call used. |
| 147 | """ |
| 148 | dsl.repo_sync(update_push_servers=True) |
Richard Barnette | 488ac8b | 2017-09-01 09:28:55 -0700 | [diff] [blame] | 149 | expect_cmds = [mock.call(['git', 'checkout', 'cros/master'], stderr=-2)] |
Shuqian Zhao | ec26bd7 | 2017-01-31 10:17:25 -0800 | [diff] [blame] | 150 | run_cmd.assert_has_calls(expect_cmds) |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 151 | |
Allen Li | a2749cd | 2017-10-31 18:03:19 -0700 | [diff] [blame] | 152 | def test_discover_commands(self): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 153 | """Test deploy_server_local.discover_update_commands and |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 154 | discover_restart_services.""" |
| 155 | # It should always be a list, and should always be callable in |
| 156 | # any local environment, though the result will vary. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 157 | result = dsl.discover_update_commands() |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 158 | self.assertIsInstance(result, list) |
| 159 | |
Don Garrett | 4769c90 | 2015-01-05 15:58:56 -0800 | [diff] [blame] | 160 | @mock.patch('subprocess.check_output', autospec=True) |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 161 | def test_update_command(self, run_cmd): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 162 | """Test deploy_server_local.update_command. |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 163 | |
| 164 | @param run_cmd: Mock of subprocess call used. |
| 165 | """ |
| 166 | # Call with a bad command name. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 167 | with self.assertRaises(dsl.UnknownCommandException): |
| 168 | dsl.update_command('Unknown Command') |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 169 | self.assertFalse(run_cmd.called) |
| 170 | |
Richard Barnette | 2e0a423 | 2018-03-07 17:13:16 -0800 | [diff] [blame] | 171 | # Call with a couple valid command names. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 172 | dsl.update_command('apache') |
Don Garrett | 4769c90 | 2015-01-05 15:58:56 -0800 | [diff] [blame] | 173 | run_cmd.assert_called_with('sudo service apache2 reload', shell=True, |
Richard Barnette | 2e0a423 | 2018-03-07 17:13:16 -0800 | [diff] [blame] | 174 | cwd=common.autotest_dir, |
Don Garrett | 4769c90 | 2015-01-05 15:58:56 -0800 | [diff] [blame] | 175 | stderr=subprocess.STDOUT) |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 176 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 177 | dsl.update_command('build_externals') |
Richard Barnette | 2e0a423 | 2018-03-07 17:13:16 -0800 | [diff] [blame] | 178 | expected_cmd = './utils/build_externals.py' |
| 179 | run_cmd.assert_called_with(expected_cmd, shell=True, |
| 180 | cwd=common.autotest_dir, |
Don Garrett | 4769c90 | 2015-01-05 15:58:56 -0800 | [diff] [blame] | 181 | stderr=subprocess.STDOUT) |
| 182 | |
| 183 | # Test a failed command. |
Richard Barnette | 2e0a423 | 2018-03-07 17:13:16 -0800 | [diff] [blame] | 184 | failure = subprocess.CalledProcessError(10, expected_cmd, 'output') |
Don Garrett | 4769c90 | 2015-01-05 15:58:56 -0800 | [diff] [blame] | 185 | |
| 186 | run_cmd.side_effect = failure |
| 187 | with self.assertRaises(subprocess.CalledProcessError) as unstable: |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 188 | dsl.update_command('build_externals') |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 189 | |
| 190 | @mock.patch('subprocess.check_call', autospec=True) |
| 191 | def test_restart_service(self, run_cmd): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 192 | """Test deploy_server_local.restart_service. |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 193 | |
| 194 | @param run_cmd: Mock of subprocess call used. |
| 195 | """ |
| 196 | # Standard call. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 197 | dsl.restart_service('foobar') |
Shuqian Zhao | 9febd45 | 2017-01-31 15:36:40 -0800 | [diff] [blame] | 198 | run_cmd.assert_called_with(['sudo', 'service', 'foobar', 'restart'], |
| 199 | stderr=-2) |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 200 | |
| 201 | @mock.patch('subprocess.check_output', autospec=True) |
| 202 | def test_restart_status(self, run_cmd): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 203 | """Test deploy_server_local.service_status. |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 204 | |
| 205 | @param run_cmd: Mock of subprocess call used. |
| 206 | """ |
| 207 | # Standard call. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 208 | dsl.service_status('foobar') |
Shuqian Zhao | dcaa616 | 2017-11-09 10:58:45 -0800 | [diff] [blame] | 209 | run_cmd.assert_called_with(['sudo', 'service', 'foobar', 'status']) |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 210 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 211 | @mock.patch.object(dsl, 'restart_service', autospec=True) |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 212 | def _test_restart_services(self, service_results, _restart): |
| 213 | """Helper for testing restart_services. |
| 214 | |
| 215 | @param service_results: {'service_name': ['status_1', 'status_2']} |
| 216 | """ |
| 217 | # each call to service_status should return the next status value for |
| 218 | # that service. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 219 | with mock.patch.object(dsl, 'service_status', autospec=True, |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 220 | side_effect=lambda n: service_results[n].pop(0)): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 221 | dsl.restart_services(service_results.keys()) |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 222 | |
| 223 | def test_restart_services(self): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 224 | """Test deploy_server_local.restart_services.""" |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 225 | single_stable = {'foo': ['status_ok', 'status_ok']} |
| 226 | double_stable = {'foo': ['status_a', 'status_a'], |
| 227 | 'bar': ['status_b', 'status_b']} |
| 228 | |
| 229 | # Verify we can handle stable services. |
| 230 | self._test_restart_services(single_stable) |
| 231 | self._test_restart_services(double_stable) |
| 232 | |
| 233 | single_unstable = {'foo': ['status_ok', 'status_not_ok']} |
| 234 | triple_unstable = {'foo': ['status_a', 'status_a'], |
| 235 | 'bar': ['status_b', 'status_b_not_ok'], |
| 236 | 'joe': ['status_c', 'status_c_not_ok']} |
| 237 | |
| 238 | # Verify we can handle unstable services and report the right failures. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 239 | with self.assertRaises(dsl.UnstableServices) as unstable: |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 240 | self._test_restart_services(single_unstable) |
| 241 | self.assertEqual(unstable.exception.args[0], ['foo']) |
| 242 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 243 | with self.assertRaises(dsl.UnstableServices) as unstable: |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 244 | self._test_restart_services(triple_unstable) |
| 245 | self.assertEqual(unstable.exception.args[0], ['bar', 'joe']) |
| 246 | |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 247 | @mock.patch('subprocess.check_output', autospec=True) |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 248 | def test_report_changes_no_update(self, run_cmd): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 249 | """Test deploy_server_local.report_changes. |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 250 | |
| 251 | @param run_cmd: Mock of subprocess call used. |
| 252 | """ |
| 253 | |
| 254 | before = { |
| 255 | 'autotest': ('/usr/local/autotest', 'auto_before'), |
| 256 | 'autotest_private': ('/dir/autotest_private', '78b9626'), |
| 257 | 'other': ('/fake/unchanged', 'constant_hash'), |
| 258 | } |
| 259 | |
| 260 | run_cmd.return_value = 'hash1 Fix change.\nhash2 Bad change.\n' |
| 261 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 262 | result = dsl.report_changes(before, None) |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 263 | |
| 264 | self.assertEqual(result, """autotest: auto_before |
| 265 | autotest_private: 78b9626 |
| 266 | other: constant_hash |
| 267 | """) |
| 268 | |
| 269 | self.assertFalse(run_cmd.called) |
| 270 | |
| 271 | @mock.patch('subprocess.check_output', autospec=True) |
| 272 | def test_report_changes_diff(self, run_cmd): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 273 | """Test deploy_server_local.report_changes. |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 274 | |
| 275 | @param run_cmd: Mock of subprocess call used. |
| 276 | """ |
| 277 | |
| 278 | before = { |
| 279 | 'autotest': ('/usr/local/autotest', 'auto_before'), |
| 280 | 'autotest_private': ('/dir/autotest_private', '78b9626'), |
| 281 | 'other': ('/fake/unchanged', 'constant_hash'), |
| 282 | } |
| 283 | |
| 284 | after = { |
| 285 | 'autotest': ('/usr/local/autotest', 'auto_after'), |
| 286 | 'autotest_tools': ('/dir/autotest_tools', 'a1598f7'), |
| 287 | 'other': ('/fake/unchanged', 'constant_hash'), |
| 288 | } |
| 289 | |
| 290 | run_cmd.return_value = 'hash1 Fix change.\nhash2 Bad change.\n' |
| 291 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 292 | result = dsl.report_changes(before, after) |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 293 | |
| 294 | self.assertEqual(result, """autotest: |
| 295 | hash1 Fix change. |
| 296 | hash2 Bad change. |
| 297 | |
| 298 | autotest_private: |
| 299 | Removed. |
| 300 | |
| 301 | autotest_tools: |
| 302 | Added. |
| 303 | |
| 304 | other: |
| 305 | No Change. |
| 306 | """) |
| 307 | |
| 308 | run_cmd.assert_called_with( |
| 309 | ['git', 'log', 'auto_before..auto_after', '--oneline'], |
| 310 | cwd='/usr/local/autotest', stderr=subprocess.STDOUT) |
| 311 | |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 312 | def test_parse_arguments(self): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 313 | """Test deploy_server_local.parse_arguments.""" |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 314 | # No arguments. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 315 | results = dsl.parse_arguments([]) |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 316 | self.assertDictContainsSubset( |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 317 | {'verify': True, 'update': True, 'actions': True, |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 318 | 'report': True, 'dryrun': False, 'update_push_servers': False}, |
| 319 | vars(results)) |
| 320 | |
| 321 | # Update test_push servers. |
| 322 | results = dsl.parse_arguments(['--update_push_servers']) |
| 323 | self.assertDictContainsSubset( |
| 324 | {'verify': True, 'update': True, 'actions': True, |
| 325 | 'report': True, 'dryrun': False, 'update_push_servers': True}, |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 326 | vars(results)) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 327 | |
| 328 | # Dryrun. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 329 | results = dsl.parse_arguments(['--dryrun']) |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 330 | self.assertDictContainsSubset( |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 331 | {'verify': False, 'update': False, 'actions': True, |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 332 | 'report': True, 'dryrun': True, 'update_push_servers': False}, |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 333 | vars(results)) |
| 334 | |
| 335 | # Restart only. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 336 | results = dsl.parse_arguments(['--actions-only']) |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 337 | self.assertDictContainsSubset( |
| 338 | {'verify': False, 'update': False, 'actions': True, |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 339 | 'report': False, 'dryrun': False, |
| 340 | 'update_push_servers': False}, |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 341 | vars(results)) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 342 | |
| 343 | # All skip arguments. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 344 | results = dsl.parse_arguments(['--skip-verify', '--skip-update', |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 345 | '--skip-actions', '--skip-report']) |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 346 | self.assertDictContainsSubset( |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 347 | {'verify': False, 'update': False, 'actions': False, |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 348 | 'report': False, 'dryrun': False, |
| 349 | 'update_push_servers': False}, |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 350 | vars(results)) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 351 | |
| 352 | # All arguments. |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 353 | results = dsl.parse_arguments(['--skip-verify', '--skip-update', |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 354 | '--skip-actions', '--skip-report', |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 355 | '--actions-only', '--dryrun', |
| 356 | '--update_push_servers']) |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 357 | self.assertDictContainsSubset( |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 358 | {'verify': False, 'update': False, 'actions': False, |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 359 | 'report': False, 'dryrun': True, 'update_push_servers': True}, |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 360 | vars(results)) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 361 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 362 | |
| 363 | if __name__ == '__main__': |
| 364 | unittest.main() |