blob: c8036fdec955a14f5dd9da844ea53b397acdb57d [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 = [
20 (STUB_MESSAGE, STUB_MESSAGE),
21 ('Stub .*', STUB_PATTERN),
22 ]
23
24 def __init__(self, info, msg):
25 super(_StubUpdateError, self).__init__(
26 'Stub %s' % info, msg)
27
28
29class TestErrorClassifications(unittest.TestCase):
30 """Test error message handling in `_AttributedUpdateError`."""
31
32 def test_exception_message(self):
33 """Test that the exception string includes its arguments."""
34 info = 'info marker'
35 msg = 'an error message'
36 stub = _StubUpdateError(info, msg)
37 self.assertIn(info, str(stub))
38 self.assertIn(msg, str(stub))
39
40 def test_classifier_message(self):
41 """Test that the exception classifier can match a simple string."""
42 info = 'info marker'
43 stub = _StubUpdateError(info, _StubUpdateError.STUB_MESSAGE)
44 self.assertNotIn(info, stub.failure_summary)
45 self.assertIn(_StubUpdateError._SUMMARY, stub.failure_summary)
46 self.assertIn(_StubUpdateError.STUB_MESSAGE, stub.failure_summary)
47
48 def test_classifier_pattern(self):
49 """Test that the exception classifier can match a regex."""
50 info = 'info marker'
51 stub = _StubUpdateError(info, 'Stub this is a test')
52 self.assertNotIn(info, stub.failure_summary)
53 self.assertIn(_StubUpdateError._SUMMARY, stub.failure_summary)
54 self.assertIn(_StubUpdateError.STUB_PATTERN, stub.failure_summary)
55
56 def test_classifier_unmatched(self):
57 """Test exception summary when no classifier matches."""
58 info = 'info marker'
59 stub = _StubUpdateError(info, 'This matches no pattern')
60 self.assertNotIn(info, stub.failure_summary)
61 self.assertIn(_StubUpdateError._SUMMARY, stub.failure_summary)
62
63 def test_host_update_error(self):
64 """Sanity test the `HostUpdateError` classifier."""
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070065 exception = provisioner.HostUpdateError(
Richard Barnette5adb6d42018-06-28 15:52:32 -070066 'chromeos6-row3-rack3-host19', 'Fake message')
67 self.assertTrue(isinstance(exception.failure_summary, str))
68
Richard Barnette5adb6d42018-06-28 15:52:32 -070069 def test_image_install_error(self):
70 """Sanity test the `ImageInstallError` classifier."""
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070071 exception = provisioner.ImageInstallError(
Richard Barnette5adb6d42018-06-28 15:52:32 -070072 'chromeos6-row3-rack3-host19',
73 'chromeos4-devserver7.cros',
74 'Fake message')
75 self.assertTrue(isinstance(exception.failure_summary, str))
76
77 def test_new_build_update_error(self):
78 """Sanity test the `NewBuildUpdateError` classifier."""
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070079 exception = provisioner.NewBuildUpdateError(
Richard Barnette5adb6d42018-06-28 15:52:32 -070080 'R68-10621.0.0', 'Fake message')
81 self.assertTrue(isinstance(exception.failure_summary, str))
82
Scott Zawalskieadbf702013-03-14 09:23:06 -040083
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070084class TestProvisioner(mox.MoxTestBase):
85 """Test provisioner module."""
Scott Zawalskieadbf702013-03-14 09:23:06 -040086
Scott Zawalskieadbf702013-03-14 09:23:06 -040087 def testParseBuildFromUpdateUrlwithUpdate(self):
88 """Test that we properly parse the build from an update_url."""
89 update_url = ('http://172.22.50.205:8082/update/lumpy-release/'
90 'R27-3837.0.0')
91 expected_value = 'lumpy-release/R27-3837.0.0'
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -070092 self.assertEqual(provisioner.url_to_image_name(update_url),
Scott Zawalskieadbf702013-03-14 09:23:06 -040093 expected_value)
94
Chris Sosa72312602013-04-16 15:01:56 -070095
Richard Barnettef00a2ee2018-06-08 11:51:38 -070096 def testGetRemoteScript(self):
97 """Test _get_remote_script() behaviors."""
Gwendal Grignou3e96cc22017-06-07 16:22:51 -070098 update_url = ('http://172.22.50.205:8082/update/lumpy-chrome-perf/'
99 'R28-4444.0.0-b2996')
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700100 script_name = 'fubar'
101 local_script = '/usr/local/bin/%s' % script_name
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700102 host = self.mox.CreateMockAnything()
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700103 cros_provisioner = provisioner.ChromiumOSProvisioner(update_url, host=host)
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700104 host.path_exists(local_script).AndReturn(True)
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700105
106 self.mox.ReplayAll()
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700107 # Simple case: file exists on DUT
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700108 self.assertEqual(cros_provisioner._get_remote_script(script_name),
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700109 local_script)
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700110 self.mox.VerifyAll()
111
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700112 self.mox.ResetAll()
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700113 fake_shell = '/bin/ash'
Laurence Goodby06fb42c2020-02-29 17:14:42 -0800114 tmp_script = '/usr/local/tmp/%s' % script_name
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700115 fake_result = self.mox.CreateMockAnything()
Dana Goyette353d1d92019-06-27 10:43:59 -0700116 fake_result.stdout = '#!%s\n' % fake_shell
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700117 host.path_exists(local_script).AndReturn(False)
Laurence Goodby06fb42c2020-02-29 17:14:42 -0800118 host.run(mox.IgnoreArg())
Dana Goyette353d1d92019-06-27 10:43:59 -0700119 host.run(mox.IgnoreArg()).AndReturn(fake_result)
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700120
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700121 self.mox.ReplayAll()
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700122 # Complicated case: script not on DUT, so try to download it.
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700123 self.assertEqual(
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700124 cros_provisioner._get_remote_script(script_name),
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700125 '%s %s' % (fake_shell, tmp_script))
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700126 self.mox.VerifyAll()
127
Chris Sosac8617522014-06-09 23:22:26 +0000128
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700129class TestProvisioner2(unittest.TestCase):
130 """Another test for provisioner module that using mock."""
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700131
132 def testAlwaysRunQuickProvision(self):
133 """Tests that we call quick provsion for all kinds of builds."""
134 image = 'foo-whatever/R65-1234.5.6'
135 devserver = 'http://mock_devserver'
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700136 provisioner.dev_server = mock.MagicMock()
137 provisioner.metrics = mock.MagicMock()
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700138 host = mock.MagicMock()
139 update_url = '%s/update/%s' % (devserver, image)
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700140 cros_provisioner = provisioner.ChromiumOSProvisioner(update_url, host)
141 cros_provisioner.check_update_status = mock.MagicMock()
David Haddock77b75c32020-05-14 01:56:32 -0700142 kernel_utils.verify_kernel_state_after_update = mock.MagicMock()
143 kernel_utils.verify_kernel_state_after_update.return_value = 3
144 kernel_utils.verify_boot_expectations = mock.MagicMock()
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700145
Jae Hoon Kim5f6ca6e2020-09-10 16:11:23 -0700146 cros_provisioner.run_provision()
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700147 host.run.assert_any_call(
Congbin Guo4a2a6642019-08-12 15:03:01 -0700148 '/usr/local/bin/quick-provision --noreboot %s '
149 '%s/download/chromeos-image-archive' % (image, devserver))
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700150
151
Scott Zawalskieadbf702013-03-14 09:23:06 -0400152if __name__ == '__main__':
Congbin Guo20ef0ce2019-05-15 12:08:13 -0700153 unittest.main()