blob: 95c0eaa944ae41c5f53a35cdf2f75c4ad0d16a1b [file] [log] [blame]
Alex Klein104b0672019-06-28 15:43:00 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# 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 Klein5dffe5e2021-02-19 15:50:00 -070017class CompileProtoTest(cros_test_lib.RunCommandTestCase,
18 api_config.ApiConfigMixin):
19 """CompileProto tests."""
20
21 def setUp(self):
22 self.request = api_pb2.CompileProtoRequest()
23 self.response = api_pb2.CompileProtoResponse()
24
25 def testCompileProto(self):
26 """Quick CompileProto functional check."""
27 self.rc.SetDefaultCmdResult(stdout=' M foo/bar.py')
28 expected = [os.path.join(constants.CHROMITE_DIR, 'foo/bar.py')]
29
30 api_controller.CompileProto(self.request, self.response, self.api_config)
31 returned = [f.path for f in self.response.modified_files]
32
33 self.assertCountEqual(expected, returned)
34 self.assertTrue(all(f in returned for f in expected))
35 self.assertTrue(all(f in expected for f in returned))
36
37 def testValidateOnly(self):
38 """Verify validate only calls do not execute logic."""
39 api_controller.CompileProto(self.request, self.response,
40 self.validate_only_config)
41
42 self.assertFalse(self.rc.call_count)
43
44 def testMockSuccess(self):
45 """Verify mock success calls do not execute logic."""
46 api_controller.CompileProto(self.request, self.response,
47 self.mock_call_config)
48 self.assertTrue(len(self.response.modified_files))
49 self.assertFalse(self.rc.call_count)
50
51
Alex Klein231d2da2019-07-22 16:44:45 -060052class GetMethodsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein104b0672019-06-28 15:43:00 -060053 """GetMethods tests."""
54
Alex Klein231d2da2019-07-22 16:44:45 -060055 def setUp(self):
56 self.request = api_pb2.MethodGetRequest()
57 self.response = api_pb2.MethodGetResponse()
58
Alex Klein104b0672019-06-28 15:43:00 -060059 def testGetMethods(self):
Ram Chandrasekarf2669772022-06-24 20:11:20 +000060 """Simple GetMethods check."""
Alex Klein104b0672019-06-28 15:43:00 -060061 methods = ['foo', 'bar']
62 self.PatchObject(router.Router, 'ListMethods', return_value=methods)
63
Alex Klein231d2da2019-07-22 16:44:45 -060064 api_controller.GetMethods(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060065
Mike Frysinger678735c2019-09-28 18:23:28 -040066 self.assertCountEqual(methods, [m.method for m in self.response.methods])
Alex Klein104b0672019-06-28 15:43:00 -060067
Alex Klein231d2da2019-07-22 16:44:45 -060068 def testValidateOnly(self):
Ram Chandrasekarf2669772022-06-24 20:11:20 +000069 """Check validate_only_config calls only validate."""
Alex Klein231d2da2019-07-22 16:44:45 -060070 patch = self.PatchObject(router.Router, 'ListMethods')
71
72 api_controller.GetMethods(self.request, self.response,
73 self.validate_only_config)
74
75 patch.assert_not_called()
Alex Klein104b0672019-06-28 15:43:00 -060076
77
Alex Klein231d2da2019-07-22 16:44:45 -060078class GetVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein104b0672019-06-28 15:43:00 -060079 """GetVersion tests."""
80
Alex Klein231d2da2019-07-22 16:44:45 -060081 def setUp(self):
Alex Klein104b0672019-06-28 15:43:00 -060082 self.PatchObject(api_controller, 'VERSION_MAJOR', new=1)
83 self.PatchObject(api_controller, 'VERSION_MINOR', new=2)
84 self.PatchObject(api_controller, 'VERSION_BUG', new=3)
85
Alex Klein231d2da2019-07-22 16:44:45 -060086 self.request = api_pb2.VersionGetRequest()
87 self.response = api_pb2.VersionGetResponse()
Alex Klein104b0672019-06-28 15:43:00 -060088
Alex Klein231d2da2019-07-22 16:44:45 -060089 def testGetVersion(self):
Ram Chandrasekarf2669772022-06-24 20:11:20 +000090 """Simple GetVersion check."""
Alex Klein231d2da2019-07-22 16:44:45 -060091 api_controller.GetVersion(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060092
Alex Klein231d2da2019-07-22 16:44:45 -060093 self.assertEqual(self.response.version.major, 1)
94 self.assertEqual(self.response.version.minor, 2)
95 self.assertEqual(self.response.version.bug, 3)
96
97 def testValidateOnly(self):
Ram Chandrasekarf2669772022-06-24 20:11:20 +000098 """Check validate_only_config calls only validate."""
Alex Klein231d2da2019-07-22 16:44:45 -060099 api_controller.GetVersion(self.request, self.response,
100 self.validate_only_config)
101
102 self.assertFalse(self.response.version.major)
103 self.assertFalse(self.response.version.minor)
104 self.assertFalse(self.response.version.bug)