blob: 222626292b3fc46210a170aac227db57b93238c5 [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/python2
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
Congbin Guo20ef0ce2019-05-15 12:08:13 -07006import mock
Dan Shib95bb862013-03-22 16:29:28 -07007import mox
Scott Zawalskieadbf702013-03-14 09:23:06 -04008import unittest
9
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
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070082class TestProvisioner(mox.MoxTestBase):
83 """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
Gwendal Grignou3e96cc22017-06-07 16:22:51 -070099 host = self.mox.CreateMockAnything()
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
104 self.mox.ReplayAll()
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700105 # Simple case: file exists on DUT
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700106 self.assertEqual(cros_provisioner._get_remote_script(script_name),
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700107 local_script)
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700108 self.mox.VerifyAll()
109
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700110 self.mox.ResetAll()
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700111 fake_shell = '/bin/ash'
Laurence Goodby06fb42c2020-02-29 17:14:42 -0800112 tmp_script = '/usr/local/tmp/%s' % script_name
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700113 fake_result = self.mox.CreateMockAnything()
Dana Goyette353d1d92019-06-27 10:43:59 -0700114 fake_result.stdout = '#!%s\n' % fake_shell
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700115 host.path_exists(local_script).AndReturn(False)
Laurence Goodby06fb42c2020-02-29 17:14:42 -0800116 host.run(mox.IgnoreArg())
Dana Goyette353d1d92019-06-27 10:43:59 -0700117 host.run(mox.IgnoreArg()).AndReturn(fake_result)
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700118
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700119 self.mox.ReplayAll()
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700120 # Complicated case: script not on DUT, so try to download it.
Jae Hoon Kim3f004992020-09-10 17:48:33 -0700121 self.assertEqual(cros_provisioner._get_remote_script(script_name),
122 '%s %s' % (fake_shell, tmp_script))
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700123 self.mox.VerifyAll()
124
Chris Sosac8617522014-06-09 23:22:26 +0000125
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700126class TestProvisioner2(unittest.TestCase):
127 """Another test for provisioner module that using mock."""
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700128
129 def testAlwaysRunQuickProvision(self):
130 """Tests that we call quick provsion for all kinds of builds."""
131 image = 'foo-whatever/R65-1234.5.6'
132 devserver = 'http://mock_devserver'
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700133 provisioner.dev_server = mock.MagicMock()
134 provisioner.metrics = mock.MagicMock()
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700135 host = mock.MagicMock()
136 update_url = '%s/update/%s' % (devserver, image)
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700137 cros_provisioner = provisioner.ChromiumOSProvisioner(update_url, host)
138 cros_provisioner.check_update_status = mock.MagicMock()
David Haddock77b75c32020-05-14 01:56:32 -0700139 kernel_utils.verify_kernel_state_after_update = mock.MagicMock()
140 kernel_utils.verify_kernel_state_after_update.return_value = 3
141 kernel_utils.verify_boot_expectations = mock.MagicMock()
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700142
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700143 cros_provisioner.run_provision()
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700144 host.run.assert_any_call(
Jae Hoon Kim3f004992020-09-10 17:48:33 -0700145 '/usr/local/bin/quick-provision --noreboot %s '
146 '%s/download/chromeos-image-archive' % (image, devserver))
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700147
148
Scott Zawalskieadbf702013-03-14 09:23:06 -0400149if __name__ == '__main__':
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700150 unittest.main()