blob: 29c31f8ab9df5277bc73e3662a7606b56655d04a [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 Klein231d2da2019-07-22 16:44:45 -06007from chromite.api import api_config
Alex Klein104b0672019-06-28 15:43:00 -06008from chromite.api import router
9from chromite.api.controller import api as api_controller
10from chromite.api.gen.chromite.api import api_pb2
Alex Klein5dffe5e2021-02-19 15:50:00 -070011from chromite.lib import constants
Alex Klein104b0672019-06-28 15:43:00 -060012from chromite.lib import cros_test_lib
13
14
Alex Klein1699fab2022-09-08 08:46:06 -060015class CompileProtoTest(
16 cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin
17):
18 """CompileProto tests."""
Alex Klein5dffe5e2021-02-19 15:50:00 -070019
Alex Klein1699fab2022-09-08 08:46:06 -060020 def setUp(self):
21 self.request = api_pb2.CompileProtoRequest()
22 self.response = api_pb2.CompileProtoResponse()
Alex Klein5dffe5e2021-02-19 15:50:00 -070023
Alex Klein1699fab2022-09-08 08:46:06 -060024 def testCompileProto(self):
25 """Quick CompileProto functional check."""
26 self.rc.SetDefaultCmdResult(stdout=" M foo/bar.py")
Mike Frysingera69df982023-03-21 16:52:27 -040027 expected = [str(constants.CHROMITE_DIR / "foo" / "bar.py")]
Alex Klein5dffe5e2021-02-19 15:50:00 -070028
Alex Klein1699fab2022-09-08 08:46:06 -060029 api_controller.CompileProto(
30 self.request, self.response, self.api_config
31 )
32 returned = [f.path for f in self.response.modified_files]
Alex Klein5dffe5e2021-02-19 15:50:00 -070033
Alex Klein1699fab2022-09-08 08:46:06 -060034 self.assertCountEqual(expected, returned)
35 self.assertTrue(all(f in returned for f in expected))
36 self.assertTrue(all(f in expected for f in returned))
Alex Klein5dffe5e2021-02-19 15:50:00 -070037
Alex Klein1699fab2022-09-08 08:46:06 -060038 def testValidateOnly(self):
39 """Verify validate only calls do not execute logic."""
40 api_controller.CompileProto(
41 self.request, self.response, self.validate_only_config
42 )
Alex Klein5dffe5e2021-02-19 15:50:00 -070043
Alex Klein1699fab2022-09-08 08:46:06 -060044 self.assertFalse(self.rc.call_count)
Alex Klein5dffe5e2021-02-19 15:50:00 -070045
Alex Klein1699fab2022-09-08 08:46:06 -060046 def testMockSuccess(self):
47 """Verify mock success calls do not execute logic."""
48 api_controller.CompileProto(
49 self.request, self.response, self.mock_call_config
50 )
51 self.assertTrue(len(self.response.modified_files))
52 self.assertFalse(self.rc.call_count)
Alex Klein5dffe5e2021-02-19 15:50:00 -070053
54
Alex Klein231d2da2019-07-22 16:44:45 -060055class GetMethodsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -060056 """GetMethods tests."""
Alex Klein104b0672019-06-28 15:43:00 -060057
Alex Klein1699fab2022-09-08 08:46:06 -060058 def setUp(self):
59 self.request = api_pb2.MethodGetRequest()
60 self.response = api_pb2.MethodGetResponse()
Alex Klein231d2da2019-07-22 16:44:45 -060061
Alex Klein1699fab2022-09-08 08:46:06 -060062 def testGetMethods(self):
63 """Simple GetMethods check."""
64 methods = ["foo", "bar"]
65 self.PatchObject(router.Router, "ListMethods", return_value=methods)
Alex Klein104b0672019-06-28 15:43:00 -060066
Alex Klein1699fab2022-09-08 08:46:06 -060067 api_controller.GetMethods(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060068
Alex Klein1699fab2022-09-08 08:46:06 -060069 self.assertCountEqual(
70 methods, [m.method for m in self.response.methods]
71 )
Alex Klein104b0672019-06-28 15:43:00 -060072
Alex Klein1699fab2022-09-08 08:46:06 -060073 def testValidateOnly(self):
74 """Check validate_only_config calls only validate."""
75 patch = self.PatchObject(router.Router, "ListMethods")
Alex Klein231d2da2019-07-22 16:44:45 -060076
Alex Klein1699fab2022-09-08 08:46:06 -060077 api_controller.GetMethods(
78 self.request, self.response, self.validate_only_config
79 )
Alex Klein231d2da2019-07-22 16:44:45 -060080
Alex Klein1699fab2022-09-08 08:46:06 -060081 patch.assert_not_called()
Alex Klein104b0672019-06-28 15:43:00 -060082
83
Alex Klein231d2da2019-07-22 16:44:45 -060084class GetVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -060085 """GetVersion tests."""
Alex Klein104b0672019-06-28 15:43:00 -060086
Alex Klein1699fab2022-09-08 08:46:06 -060087 def setUp(self):
88 self.PatchObject(api_controller, "VERSION_MAJOR", new=1)
89 self.PatchObject(api_controller, "VERSION_MINOR", new=2)
90 self.PatchObject(api_controller, "VERSION_BUG", new=3)
Alex Klein104b0672019-06-28 15:43:00 -060091
Alex Klein1699fab2022-09-08 08:46:06 -060092 self.request = api_pb2.VersionGetRequest()
93 self.response = api_pb2.VersionGetResponse()
Alex Klein104b0672019-06-28 15:43:00 -060094
Alex Klein1699fab2022-09-08 08:46:06 -060095 def testGetVersion(self):
96 """Simple GetVersion check."""
97 api_controller.GetVersion(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060098
Alex Klein1699fab2022-09-08 08:46:06 -060099 self.assertEqual(self.response.version.major, 1)
100 self.assertEqual(self.response.version.minor, 2)
101 self.assertEqual(self.response.version.bug, 3)
Alex Klein231d2da2019-07-22 16:44:45 -0600102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 def testValidateOnly(self):
104 """Check validate_only_config calls only validate."""
105 api_controller.GetVersion(
106 self.request, self.response, self.validate_only_config
107 )
Alex Klein231d2da2019-07-22 16:44:45 -0600108
Alex Klein1699fab2022-09-08 08:46:06 -0600109 self.assertFalse(self.response.version.major)
110 self.assertFalse(self.response.version.minor)
111 self.assertFalse(self.response.version.bug)