blob: 8f4716987736dadc318d829f8d4b021734952883 [file] [log] [blame]
Derek Beckettbcc4a9e2021-07-29 14:40:38 -07001#!/usr/bin/python3
Scott Zawalskieadbf702013-03-14 09:23:06 -04002# 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
Derek Beckettbcc4a9e2021-07-29 14:40:38 -07006
Scott Zawalskieadbf702013-03-14 09:23:06 -04007import unittest
Derek Beckettbcc4a9e2021-07-29 14:40:38 -07008from unittest import mock
Scott Zawalskieadbf702013-03-14 09:23:06 -04009
10import common
David Haddock77b75c32020-05-14 01:56:32 -070011from autotest_lib.client.common_lib.cros import kernel_utils
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070012from autotest_lib.server.cros import provisioner
Richard Barnette5adb6d42018-06-28 15:52:32 -070013
14
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070015class _StubUpdateError(provisioner._AttributedUpdateError):
Richard Barnette5adb6d42018-06-28 15:52:32 -070016 STUB_MESSAGE = 'Stub message'
17 STUB_PATTERN = 'Stub pattern matched'
18 _SUMMARY = 'Stub summary'
19 _CLASSIFIERS = [
Jae Hoon Kim3f004992020-09-10 17:48:33 -070020 (STUB_MESSAGE, STUB_MESSAGE),
21 ('Stub .*', STUB_PATTERN),
Richard Barnette5adb6d42018-06-28 15:52:32 -070022 ]
23
24 def __init__(self, info, msg):
Jae Hoon Kim3f004992020-09-10 17:48:33 -070025 super(_StubUpdateError, self).__init__('Stub %s' % info, msg)
Richard Barnette5adb6d42018-06-28 15:52:32 -070026
27
28class TestErrorClassifications(unittest.TestCase):
29 """Test error message handling in `_AttributedUpdateError`."""
30
31 def test_exception_message(self):
32 """Test that the exception string includes its arguments."""
33 info = 'info marker'
34 msg = 'an error message'
35 stub = _StubUpdateError(info, msg)
36 self.assertIn(info, str(stub))
37 self.assertIn(msg, str(stub))
38
39 def test_classifier_message(self):
40 """Test that the exception classifier can match a simple string."""
41 info = 'info marker'
42 stub = _StubUpdateError(info, _StubUpdateError.STUB_MESSAGE)
43 self.assertNotIn(info, stub.failure_summary)
44 self.assertIn(_StubUpdateError._SUMMARY, stub.failure_summary)
45 self.assertIn(_StubUpdateError.STUB_MESSAGE, stub.failure_summary)
46
47 def test_classifier_pattern(self):
48 """Test that the exception classifier can match a regex."""
49 info = 'info marker'
50 stub = _StubUpdateError(info, 'Stub this is a test')
51 self.assertNotIn(info, stub.failure_summary)
52 self.assertIn(_StubUpdateError._SUMMARY, stub.failure_summary)
53 self.assertIn(_StubUpdateError.STUB_PATTERN, stub.failure_summary)
54
55 def test_classifier_unmatched(self):
56 """Test exception summary when no classifier matches."""
57 info = 'info marker'
58 stub = _StubUpdateError(info, 'This matches no pattern')
59 self.assertNotIn(info, stub.failure_summary)
60 self.assertIn(_StubUpdateError._SUMMARY, stub.failure_summary)
61
62 def test_host_update_error(self):
Derek Beckett4de221e2021-06-03 10:50:58 -070063 """Test the `HostUpdateError` classifier."""
Jae Hoon Kim3f004992020-09-10 17:48:33 -070064 exception = provisioner.HostUpdateError('chromeos6-row3-rack3-host19',
65 'Fake message')
Richard Barnette5adb6d42018-06-28 15:52:32 -070066 self.assertTrue(isinstance(exception.failure_summary, str))
67
Richard Barnette5adb6d42018-06-28 15:52:32 -070068 def test_image_install_error(self):
Derek Beckett4de221e2021-06-03 10:50:58 -070069 """Test the `ImageInstallError` classifier."""
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070070 exception = provisioner.ImageInstallError(
Jae Hoon Kim3f004992020-09-10 17:48:33 -070071 'chromeos6-row3-rack3-host19', 'chromeos4-devserver7.cros',
Richard Barnette5adb6d42018-06-28 15:52:32 -070072 'Fake message')
73 self.assertTrue(isinstance(exception.failure_summary, str))
74
75 def test_new_build_update_error(self):
Derek Beckett4de221e2021-06-03 10:50:58 -070076 """Test the `NewBuildUpdateError` classifier."""
Jae Hoon Kim3f004992020-09-10 17:48:33 -070077 exception = provisioner.NewBuildUpdateError('R68-10621.0.0',
78 'Fake message')
Richard Barnette5adb6d42018-06-28 15:52:32 -070079 self.assertTrue(isinstance(exception.failure_summary, str))
80
Scott Zawalskieadbf702013-03-14 09:23:06 -040081
Derek Beckettbcc4a9e2021-07-29 14:40:38 -070082class TestProvisioner(unittest.TestCase):
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070083 """Test provisioner module."""
Scott Zawalskieadbf702013-03-14 09:23:06 -040084
Scott Zawalskieadbf702013-03-14 09:23:06 -040085 def testParseBuildFromUpdateUrlwithUpdate(self):
86 """Test that we properly parse the build from an update_url."""
87 update_url = ('http://172.22.50.205:8082/update/lumpy-release/'
88 'R27-3837.0.0')
89 expected_value = 'lumpy-release/R27-3837.0.0'
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070090 self.assertEqual(provisioner.url_to_image_name(update_url),
Scott Zawalskieadbf702013-03-14 09:23:06 -040091 expected_value)
92
Richard Barnettef00a2ee2018-06-08 11:51:38 -070093 def testGetRemoteScript(self):
94 """Test _get_remote_script() behaviors."""
Gwendal Grignou3e96cc22017-06-07 16:22:51 -070095 update_url = ('http://172.22.50.205:8082/update/lumpy-chrome-perf/'
96 'R28-4444.0.0-b2996')
Richard Barnettef00a2ee2018-06-08 11:51:38 -070097 script_name = 'fubar'
98 local_script = '/usr/local/bin/%s' % script_name
Derek Beckettbcc4a9e2021-07-29 14:40:38 -070099 host = mock.MagicMock()
Jae Hoon Kim3f004992020-09-10 17:48:33 -0700100 cros_provisioner = provisioner.ChromiumOSProvisioner(update_url,
101 host=host)
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700102 host.path_exists(local_script).AndReturn(True)
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700103
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700104 # Simple case: file exists on DUT
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700105 self.assertEqual(cros_provisioner._get_remote_script(script_name),
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700106 local_script)
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700107
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700108 fake_shell = '/bin/ash'
Laurence Goodby06fb42c2020-02-29 17:14:42 -0800109 tmp_script = '/usr/local/tmp/%s' % script_name
Derek Beckettbcc4a9e2021-07-29 14:40:38 -0700110 fake_result = mock.MagicMock()
Dana Goyette353d1d92019-06-27 10:43:59 -0700111 fake_result.stdout = '#!%s\n' % fake_shell
Derek Beckettbcc4a9e2021-07-29 14:40:38 -0700112 host.path_exists.return_value = False
113 host.run.return_value = fake_result
114 host.path_exists.assert_called_with(local_script)
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700115
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700116 # Complicated case: script not on DUT, so try to download it.
Jae Hoon Kim3f004992020-09-10 17:48:33 -0700117 self.assertEqual(cros_provisioner._get_remote_script(script_name),
118 '%s %s' % (fake_shell, tmp_script))
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700119
Chris Sosac8617522014-06-09 23:22:26 +0000120
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700121class TestProvisioner2(unittest.TestCase):
122 """Another test for provisioner module that using mock."""
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700123
124 def testAlwaysRunQuickProvision(self):
125 """Tests that we call quick provsion for all kinds of builds."""
126 image = 'foo-whatever/R65-1234.5.6'
127 devserver = 'http://mock_devserver'
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700128 provisioner.dev_server = mock.MagicMock()
129 provisioner.metrics = mock.MagicMock()
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700130 host = mock.MagicMock()
131 update_url = '%s/update/%s' % (devserver, image)
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700132 cros_provisioner = provisioner.ChromiumOSProvisioner(update_url, host)
133 cros_provisioner.check_update_status = mock.MagicMock()
David Haddock77b75c32020-05-14 01:56:32 -0700134 kernel_utils.verify_kernel_state_after_update = mock.MagicMock()
135 kernel_utils.verify_kernel_state_after_update.return_value = 3
136 kernel_utils.verify_boot_expectations = mock.MagicMock()
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700137
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700138 cros_provisioner.run_provision()
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700139 host.run.assert_any_call(
Jae Hoon Kim3f004992020-09-10 17:48:33 -0700140 '/usr/local/bin/quick-provision --noreboot %s '
141 '%s/download/chromeos-image-archive' % (image, devserver))
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700142
143
Scott Zawalskieadbf702013-03-14 09:23:06 -0400144if __name__ == '__main__':
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700145 unittest.main()