blob: 799a43d2c76822332fe257f55f6a70f23d778496 [file] [log] [blame]
Alex Klein104b0672019-06-28 15:43:00 -06001# -*- 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
8from __future__ import print_function
9
Alex Klein5dffe5e2021-02-19 15:50:00 -070010import os
11
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
Alex Klein104b0672019-06-28 15:43:00 -060013from chromite.api import router
14from chromite.api.controller import api as api_controller
15from chromite.api.gen.chromite.api import api_pb2
Alex Klein5dffe5e2021-02-19 15:50:00 -070016from chromite.lib import constants
Alex Klein104b0672019-06-28 15:43:00 -060017from chromite.lib import cros_test_lib
18
19
Alex Klein5dffe5e2021-02-19 15:50:00 -070020class 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 Klein231d2da2019-07-22 16:44:45 -060055class GetMethodsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein104b0672019-06-28 15:43:00 -060056 """GetMethods tests."""
57
Alex Klein231d2da2019-07-22 16:44:45 -060058 def setUp(self):
59 self.request = api_pb2.MethodGetRequest()
60 self.response = api_pb2.MethodGetResponse()
61
Alex Klein104b0672019-06-28 15:43:00 -060062 def testGetMethods(self):
63 """Simple GetMethods sanity check."""
64 methods = ['foo', 'bar']
65 self.PatchObject(router.Router, 'ListMethods', return_value=methods)
66
Alex Klein231d2da2019-07-22 16:44:45 -060067 api_controller.GetMethods(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060068
Mike Frysinger678735c2019-09-28 18:23:28 -040069 self.assertCountEqual(methods, [m.method for m in self.response.methods])
Alex Klein104b0672019-06-28 15:43:00 -060070
Alex Klein231d2da2019-07-22 16:44:45 -060071 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 Klein104b0672019-06-28 15:43:00 -060079
80
Alex Klein231d2da2019-07-22 16:44:45 -060081class GetVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein104b0672019-06-28 15:43:00 -060082 """GetVersion tests."""
83
Alex Klein231d2da2019-07-22 16:44:45 -060084 def setUp(self):
Alex Klein104b0672019-06-28 15:43:00 -060085 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 Klein231d2da2019-07-22 16:44:45 -060089 self.request = api_pb2.VersionGetRequest()
90 self.response = api_pb2.VersionGetResponse()
Alex Klein104b0672019-06-28 15:43:00 -060091
Alex Klein231d2da2019-07-22 16:44:45 -060092 def testGetVersion(self):
93 """Simple GetVersion sanity check."""
94 api_controller.GetVersion(self.request, self.response, self.api_config)
Alex Klein104b0672019-06-28 15:43:00 -060095
Alex Klein231d2da2019-07-22 16:44:45 -060096 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)