blob: a86d488c33e14fc7966e3de0916c16e63657dfcc [file] [log] [blame]
Alex Kleinf4dc4f52018-12-05 13:55:12 -07001# -*- coding: utf-8 -*-
2# Copyright 2018 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
Alex Klein2bfacb22019-02-04 11:42:17 -07006"""Tests for the build_api script covering the base Build API functionality."""
7
Alex Kleinf4dc4f52018-12-05 13:55:12 -07008from __future__ import print_function
9
Alex Kleinf4dc4f52018-12-05 13:55:12 -070010from chromite.api.gen import build_api_test_pb2
Alex Klein2bfacb22019-02-04 11:42:17 -070011from chromite.lib import cros_build_lib
Alex Kleinf4dc4f52018-12-05 13:55:12 -070012from chromite.lib import cros_test_lib
Ned Nguyen5aa5d9a2019-02-25 10:34:23 -070013from chromite.scripts import build_api
Alex Kleinf4dc4f52018-12-05 13:55:12 -070014
15
16class RouterTest(cros_test_lib.MockTestCase):
17 """Test Router functionality."""
18 _INPUT_JSON = '{"id":"Input ID"}'
19
20 def setUp(self):
21 self.router = build_api.Router()
22 self.router.Register(build_api_test_pb2)
23
24 def testInputOutputMethod(self):
25 """Test input/output handling."""
26 def impl(input_msg, output_msg):
27 self.assertIsInstance(input_msg, build_api_test_pb2.TestRequestMessage)
28 self.assertIsInstance(output_msg, build_api_test_pb2.TestResultMessage)
29
30 self.PatchObject(self.router, '_GetMethod', return_value=impl)
31
32 self.router.Route('chromite.api.TestApiService', 'InputOutputMethod',
33 self._INPUT_JSON)
34
Alex Klein2bfacb22019-02-04 11:42:17 -070035 def testRenameMethod(self):
36 """Test implementation name config."""
37 def _GetMethod(_, method_name):
38 self.assertEqual('CorrectName', method_name)
Alex Klein7a115172019-02-08 14:14:20 -070039 return lambda x, y: None
Alex Klein2bfacb22019-02-04 11:42:17 -070040
41 self.PatchObject(self.router, '_GetMethod', side_effect=_GetMethod)
42
43 self.router.Route('chromite.api.TestApiService', 'RenamedMethod',
44 self._INPUT_JSON)
45
46 def testInsideServiceChrootAsserts(self):
47 """Test the chroot assertion handling with service inside configured."""
48 # Helper variables/functions to make the patches simpler.
49 should_be_called = False
50 is_inside = False
Alex Klein7a115172019-02-08 14:14:20 -070051 def impl(_input_msg, _output_msg):
Alex Klein2bfacb22019-02-04 11:42:17 -070052 self.assertTrue(should_be_called,
53 'The implementation should not have been called.')
54 def inside():
55 return is_inside
56
57 self.PatchObject(self.router, '_GetMethod', return_value=impl)
58 self.PatchObject(cros_build_lib, 'IsInsideChroot', side_effect=inside)
59
60 # Not inside chroot with inside requirement should raise an error.
61 with self.assertRaises(cros_build_lib.DieSystemExit):
62 self.router.Route('chromite.api.InsideChrootApiService',
63 'InsideServiceInsideMethod', self._INPUT_JSON)
64
65 # Inside chroot with inside requirement.
66 is_inside = should_be_called = True
67 self.router.Route('chromite.api.InsideChrootApiService',
68 'InsideServiceInsideMethod', self._INPUT_JSON)
69
70 # Inside chroot with outside override should raise assertion.
71 is_inside = True
72 should_be_called = False
73 with self.assertRaises(cros_build_lib.DieSystemExit):
74 self.router.Route('chromite.api.InsideChrootApiService',
75 'InsideServiceOutsideMethod', self._INPUT_JSON)
76
77 is_inside = False
78 should_be_called = True
79 self.router.Route('chromite.api.InsideChrootApiService',
80 'InsideServiceOutsideMethod', self._INPUT_JSON)
81
82 def testOutsideServiceChrootAsserts(self):
83 """Test the chroot assertion handling with service outside configured."""
84 # Helper variables/functions to make the patches simpler.
85 should_be_called = False
86 is_inside = False
Alex Klein7a115172019-02-08 14:14:20 -070087 def impl(_input_msg, _output_msg):
Alex Klein2bfacb22019-02-04 11:42:17 -070088 self.assertTrue(should_be_called,
89 'The implementation should not have been called.')
90
91 self.PatchObject(self.router, '_GetMethod', return_value=impl)
92 self.PatchObject(cros_build_lib, 'IsInsideChroot',
93 side_effect=lambda: is_inside)
94
95 # Outside chroot with outside requirement should be fine.
96 is_inside = False
97 should_be_called = True
98 self.router.Route('chromite.api.OutsideChrootApiService',
99 'OutsideServiceOutsideMethod', self._INPUT_JSON)
100
101 # Inside chroot with outside requirement should raise error.
102 is_inside = True
103 should_be_called = False
104 with self.assertRaises(cros_build_lib.DieSystemExit):
105 self.router.Route('chromite.api.OutsideChrootApiService',
106 'OutsideServiceOutsideMethod', self._INPUT_JSON)
107
108 # Outside chroot with inside override should raise error.
109 is_inside = should_be_called = False
110 with self.assertRaises(cros_build_lib.DieSystemExit):
111 self.router.Route('chromite.api.OutsideChrootApiService',
112 'OutsideServiceInsideMethod', self._INPUT_JSON)
113
114 # Inside chroot with inside override should be fine.
115 is_inside = should_be_called = True
116 self.router.Route('chromite.api.OutsideChrootApiService',
117 'OutsideServiceInsideMethod', self._INPUT_JSON)