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