Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 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 | |
| 6 | """API controller tests.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 5dffe5e | 2021-02-19 15:50:00 -0700 | [diff] [blame^] | 10 | import os |
| 11 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 12 | from chromite.api import api_config |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 13 | from chromite.api import router |
| 14 | from chromite.api.controller import api as api_controller |
| 15 | from chromite.api.gen.chromite.api import api_pb2 |
Alex Klein | 5dffe5e | 2021-02-19 15:50:00 -0700 | [diff] [blame^] | 16 | from chromite.lib import constants |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 17 | from chromite.lib import cros_test_lib |
| 18 | |
| 19 | |
Alex Klein | 5dffe5e | 2021-02-19 15:50:00 -0700 | [diff] [blame^] | 20 | class CompileProtoTest(cros_test_lib.RunCommandTestCase, |
| 21 | api_config.ApiConfigMixin): |
| 22 | """CompileProto tests.""" |
| 23 | |
| 24 | def setUp(self): |
| 25 | self.request = api_pb2.CompileProtoRequest() |
| 26 | self.response = api_pb2.CompileProtoResponse() |
| 27 | |
| 28 | def testCompileProto(self): |
| 29 | """Quick CompileProto functional check.""" |
| 30 | self.rc.SetDefaultCmdResult(stdout=' M foo/bar.py') |
| 31 | expected = [os.path.join(constants.CHROMITE_DIR, 'foo/bar.py')] |
| 32 | |
| 33 | api_controller.CompileProto(self.request, self.response, self.api_config) |
| 34 | returned = [f.path for f in self.response.modified_files] |
| 35 | |
| 36 | self.assertCountEqual(expected, returned) |
| 37 | self.assertTrue(all(f in returned for f in expected)) |
| 38 | self.assertTrue(all(f in expected for f in returned)) |
| 39 | |
| 40 | def testValidateOnly(self): |
| 41 | """Verify validate only calls do not execute logic.""" |
| 42 | api_controller.CompileProto(self.request, self.response, |
| 43 | self.validate_only_config) |
| 44 | |
| 45 | self.assertFalse(self.rc.call_count) |
| 46 | |
| 47 | def testMockSuccess(self): |
| 48 | """Verify mock success calls do not execute logic.""" |
| 49 | api_controller.CompileProto(self.request, self.response, |
| 50 | self.mock_call_config) |
| 51 | self.assertTrue(len(self.response.modified_files)) |
| 52 | self.assertFalse(self.rc.call_count) |
| 53 | |
| 54 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 55 | class GetMethodsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 56 | """GetMethods tests.""" |
| 57 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 58 | def setUp(self): |
| 59 | self.request = api_pb2.MethodGetRequest() |
| 60 | self.response = api_pb2.MethodGetResponse() |
| 61 | |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 62 | def testGetMethods(self): |
| 63 | """Simple GetMethods sanity check.""" |
| 64 | methods = ['foo', 'bar'] |
| 65 | self.PatchObject(router.Router, 'ListMethods', return_value=methods) |
| 66 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 67 | api_controller.GetMethods(self.request, self.response, self.api_config) |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 68 | |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 69 | self.assertCountEqual(methods, [m.method for m in self.response.methods]) |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 70 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 71 | def testValidateOnly(self): |
| 72 | """Sanity check validate only calls only validate.""" |
| 73 | patch = self.PatchObject(router.Router, 'ListMethods') |
| 74 | |
| 75 | api_controller.GetMethods(self.request, self.response, |
| 76 | self.validate_only_config) |
| 77 | |
| 78 | patch.assert_not_called() |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 79 | |
| 80 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 81 | class GetVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 82 | """GetVersion tests.""" |
| 83 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 84 | def setUp(self): |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 85 | self.PatchObject(api_controller, 'VERSION_MAJOR', new=1) |
| 86 | self.PatchObject(api_controller, 'VERSION_MINOR', new=2) |
| 87 | self.PatchObject(api_controller, 'VERSION_BUG', new=3) |
| 88 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 89 | self.request = api_pb2.VersionGetRequest() |
| 90 | self.response = api_pb2.VersionGetResponse() |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 91 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 92 | def testGetVersion(self): |
| 93 | """Simple GetVersion sanity check.""" |
| 94 | api_controller.GetVersion(self.request, self.response, self.api_config) |
Alex Klein | 104b067 | 2019-06-28 15:43:00 -0600 | [diff] [blame] | 95 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 96 | self.assertEqual(self.response.version.major, 1) |
| 97 | self.assertEqual(self.response.version.minor, 2) |
| 98 | self.assertEqual(self.response.version.bug, 3) |
| 99 | |
| 100 | def testValidateOnly(self): |
| 101 | """Sanity check validate only calls only validate.""" |
| 102 | api_controller.GetVersion(self.request, self.response, |
| 103 | self.validate_only_config) |
| 104 | |
| 105 | self.assertFalse(self.response.version.major) |
| 106 | self.assertFalse(self.response.version.minor) |
| 107 | self.assertFalse(self.response.version.bug) |