blob: ebf03e5fc61f470d8add4dbb17abf16b46815c19 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Klein104b0672019-06-28 15:43:00 -06002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""API controller tests."""
6
Alex Klein5dffe5e2021-02-19 15:50:00 -07007import os
8
Alex Klein231d2da2019-07-22 16:44:45 -06009from chromite.api import api_config
Alex Klein104b0672019-06-28 15:43:00 -060010from chromite.api import router
11from chromite.api.controller import api as api_controller
12from chromite.api.gen.chromite.api import api_pb2
Alex Klein5dffe5e2021-02-19 15:50:00 -070013from chromite.lib import constants
Alex Klein104b0672019-06-28 15:43:00 -060014from chromite.lib import cros_test_lib
15
16
Alex Klein1699fab2022-09-08 08:46:06 -060017class CompileProtoTest(
18 cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin
19):
20 """CompileProto tests."""
Alex Klein5dffe5e2021-02-19 15:50:00 -070021
Alex Klein1699fab2022-09-08 08:46:06 -060022 def setUp(self):
23 self.request = api_pb2.CompileProtoRequest()
24 self.response = api_pb2.CompileProtoResponse()
Alex Klein5dffe5e2021-02-19 15:50:00 -070025
Alex Klein1699fab2022-09-08 08:46:06 -060026 def testCompileProto(self):
27 """Quick CompileProto functional check."""
28 self.rc.SetDefaultCmdResult(stdout=" M foo/bar.py")
29 expected = [os.path.join(constants.CHROMITE_DIR, "foo/bar.py")]
Alex Klein5dffe5e2021-02-19 15:50:00 -070030
Alex Klein1699fab2022-09-08 08:46:06 -060031 api_controller.CompileProto(
32 self.request, self.response, self.api_config
33 )
34 returned = [f.path for f in self.response.modified_files]
Alex Klein5dffe5e2021-02-19 15:50:00 -070035
Alex Klein1699fab2022-09-08 08:46:06 -060036 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))
Alex Klein5dffe5e2021-02-19 15:50:00 -070039
Alex Klein1699fab2022-09-08 08:46:06 -060040 def testValidateOnly(self):
41 """Verify validate only calls do not execute logic."""
42 api_controller.CompileProto(
43 self.request, self.response, self.validate_only_config
44 )
Alex Klein5dffe5e2021-02-19 15:50:00 -070045
Alex Klein1699fab2022-09-08 08:46:06 -060046 self.assertFalse(self.rc.call_count)
Alex Klein5dffe5e2021-02-19 15:50:00 -070047
Alex Klein1699fab2022-09-08 08:46:06 -060048 def testMockSuccess(self):
49 """Verify mock success calls do not execute logic."""
50 api_controller.CompileProto(
51 self.request, self.response, self.mock_call_config
52 )
53 self.assertTrue(len(self.response.modified_files))
54 self.assertFalse(self.rc.call_count)
Alex Klein5dffe5e2021-02-19 15:50:00 -070055
56
Alex Klein231d2da2019-07-22 16:44:45 -060057class GetMethodsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -060058 """GetMethods tests."""
Alex Klein104b0672019-06-28 15:43:00 -060059
Alex Klein1699fab2022-09-08 08:46:06 -060060 def setUp(self):
61 self.request = api_pb2.MethodGetRequest()
62 self.response = api_pb2.MethodGetResponse()
Alex Klein231d2da2019-07-22 16:44:45 -060063
Alex Klein1699fab2022-09-08 08:46:06 -060064 def testGetMethods(self):
65 """Simple GetMethods check."""
66 methods = ["foo", "bar"]
67 self.PatchObject(router.Router, "ListMethods", return_value=methods)
Alex Klein104b0672019-06-28 15:43:00 -060068
Alex Klein1699fab2022-09-08 08:46:06 -060069 api_controller.GetMethods(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060070
Alex Klein1699fab2022-09-08 08:46:06 -060071 self.assertCountEqual(
72 methods, [m.method for m in self.response.methods]
73 )
Alex Klein104b0672019-06-28 15:43:00 -060074
Alex Klein1699fab2022-09-08 08:46:06 -060075 def testValidateOnly(self):
76 """Check validate_only_config calls only validate."""
77 patch = self.PatchObject(router.Router, "ListMethods")
Alex Klein231d2da2019-07-22 16:44:45 -060078
Alex Klein1699fab2022-09-08 08:46:06 -060079 api_controller.GetMethods(
80 self.request, self.response, self.validate_only_config
81 )
Alex Klein231d2da2019-07-22 16:44:45 -060082
Alex Klein1699fab2022-09-08 08:46:06 -060083 patch.assert_not_called()
Alex Klein104b0672019-06-28 15:43:00 -060084
85
Alex Klein231d2da2019-07-22 16:44:45 -060086class GetVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -060087 """GetVersion tests."""
Alex Klein104b0672019-06-28 15:43:00 -060088
Alex Klein1699fab2022-09-08 08:46:06 -060089 def setUp(self):
90 self.PatchObject(api_controller, "VERSION_MAJOR", new=1)
91 self.PatchObject(api_controller, "VERSION_MINOR", new=2)
92 self.PatchObject(api_controller, "VERSION_BUG", new=3)
Alex Klein104b0672019-06-28 15:43:00 -060093
Alex Klein1699fab2022-09-08 08:46:06 -060094 self.request = api_pb2.VersionGetRequest()
95 self.response = api_pb2.VersionGetResponse()
Alex Klein104b0672019-06-28 15:43:00 -060096
Alex Klein1699fab2022-09-08 08:46:06 -060097 def testGetVersion(self):
98 """Simple GetVersion check."""
99 api_controller.GetVersion(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -0600100
Alex Klein1699fab2022-09-08 08:46:06 -0600101 self.assertEqual(self.response.version.major, 1)
102 self.assertEqual(self.response.version.minor, 2)
103 self.assertEqual(self.response.version.bug, 3)
Alex Klein231d2da2019-07-22 16:44:45 -0600104
Alex Klein1699fab2022-09-08 08:46:06 -0600105 def testValidateOnly(self):
106 """Check validate_only_config calls only validate."""
107 api_controller.GetVersion(
108 self.request, self.response, self.validate_only_config
109 )
Alex Klein231d2da2019-07-22 16:44:45 -0600110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 self.assertFalse(self.response.version.major)
112 self.assertFalse(self.response.version.minor)
113 self.assertFalse(self.response.version.bug)