blob: 957e73c8ee97e470893dffa0293392e939d62ecf [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 Klein5bcb4d22019-03-21 13:51:54 -060010import os
11
Alex Klein7107bdd2019-03-14 17:14:31 -060012from chromite.api.gen.chromite.api import build_api_test_pb2
Alex Klein2bfacb22019-02-04 11:42:17 -070013from chromite.lib import cros_build_lib
Alex Kleinf4dc4f52018-12-05 13:55:12 -070014from chromite.lib import cros_test_lib
Alex Klein5bcb4d22019-03-21 13:51:54 -060015from chromite.lib import osutils
Ned Nguyen5aa5d9a2019-02-25 10:34:23 -070016from chromite.scripts import build_api
Alex Kleinf4dc4f52018-12-05 13:55:12 -070017
18
Alex Klein00aa8072019-04-15 16:36:00 -060019class RouterTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Kleinf4dc4f52018-12-05 13:55:12 -070020 """Test Router functionality."""
Alex Klein00aa8072019-04-15 16:36:00 -060021 _INPUT_JSON_TEMPLATE = '{"id":"Input ID", "chroot":{"path": "%s"}}'
Alex Kleinf4dc4f52018-12-05 13:55:12 -070022
23 def setUp(self):
24 self.router = build_api.Router()
25 self.router.Register(build_api_test_pb2)
26
Alex Klein5bcb4d22019-03-21 13:51:54 -060027 self.input_file = os.path.join(self.tempdir, 'input.json')
28 self.output_file = os.path.join(self.tempdir, 'output.json')
29
Alex Klein00aa8072019-04-15 16:36:00 -060030 self.chroot_dir = os.path.join(self.tempdir, 'chroot')
31 chroot_tmp = os.path.join(self.chroot_dir, 'tmp')
32 # Make the tmp dir for the re-exec inside chroot input/output files.
33 osutils.SafeMakedirs(chroot_tmp)
34
35 osutils.WriteFile(self.input_file,
36 self._INPUT_JSON_TEMPLATE % self.chroot_dir)
Alex Klein5bcb4d22019-03-21 13:51:54 -060037
Alex Kleinf4dc4f52018-12-05 13:55:12 -070038 def testInputOutputMethod(self):
39 """Test input/output handling."""
40 def impl(input_msg, output_msg):
41 self.assertIsInstance(input_msg, build_api_test_pb2.TestRequestMessage)
42 self.assertIsInstance(output_msg, build_api_test_pb2.TestResultMessage)
43
44 self.PatchObject(self.router, '_GetMethod', return_value=impl)
45
46 self.router.Route('chromite.api.TestApiService', 'InputOutputMethod',
Alex Klein5bcb4d22019-03-21 13:51:54 -060047 self.input_file, self.output_file)
Alex Kleinf4dc4f52018-12-05 13:55:12 -070048
Alex Klein2bfacb22019-02-04 11:42:17 -070049 def testRenameMethod(self):
50 """Test implementation name config."""
51 def _GetMethod(_, method_name):
52 self.assertEqual('CorrectName', method_name)
Alex Klein7a115172019-02-08 14:14:20 -070053 return lambda x, y: None
Alex Klein2bfacb22019-02-04 11:42:17 -070054
55 self.PatchObject(self.router, '_GetMethod', side_effect=_GetMethod)
56
57 self.router.Route('chromite.api.TestApiService', 'RenamedMethod',
Alex Klein5bcb4d22019-03-21 13:51:54 -060058 self.input_file, self.output_file)
Alex Klein2bfacb22019-02-04 11:42:17 -070059
Alex Klein00aa8072019-04-15 16:36:00 -060060 def _mock_callable(self, expect_called):
61 """Helper to create the implementation mock to test chroot assertions.
62
63 Args:
64 expect_called (bool): Whether the implementation should be called.
65 When False, an assertion will fail if it is called.
66
67 Returns:
68 callable - The implementation.
69 """
Alex Klein7a115172019-02-08 14:14:20 -070070 def impl(_input_msg, _output_msg):
Alex Klein00aa8072019-04-15 16:36:00 -060071 self.assertTrue(expect_called,
Alex Klein2bfacb22019-02-04 11:42:17 -070072 'The implementation should not have been called.')
Alex Klein2bfacb22019-02-04 11:42:17 -070073
Alex Klein00aa8072019-04-15 16:36:00 -060074 return impl
Alex Klein2bfacb22019-02-04 11:42:17 -070075
Alex Klein00aa8072019-04-15 16:36:00 -060076 def testInsideServiceInsideMethodInsideChroot(self):
77 """Test inside/inside/inside works correctly."""
78 self.PatchObject(self.router, '_GetMethod',
79 return_value=self._mock_callable(expect_called=True))
80 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
Alex Klein2bfacb22019-02-04 11:42:17 -070081 self.router.Route('chromite.api.InsideChrootApiService',
Alex Klein5bcb4d22019-03-21 13:51:54 -060082 'InsideServiceInsideMethod', self.input_file,
83 self.output_file)
Alex Klein2bfacb22019-02-04 11:42:17 -070084
Alex Klein00aa8072019-04-15 16:36:00 -060085 def testInsideServiceOutsideMethodOutsideChroot(self):
86 """Test the outside method override works as expected."""
87 self.PatchObject(self.router, '_GetMethod',
88 return_value=self._mock_callable(expect_called=True))
89 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
90 self.router.Route('chromite.api.InsideChrootApiService',
91 'InsideServiceOutsideMethod', self.input_file,
92 self.output_file)
93
94 def testInsideServiceInsideMethodOutsideChroot(self):
95 """Test calling an inside method from outside the chroot."""
96 self.PatchObject(self.router, '_GetMethod',
97 return_value=self._mock_callable(expect_called=False))
98 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
99 self.rc.return_value = cros_build_lib.CommandResult(returncode=0)
100
101 service = 'chromite.api.InsideChrootApiService'
102 method = 'InsideServiceInsideMethod'
103 service_method = '%s/%s' % (service, method)
104 self.router.Route(service, method, self.input_file, self.output_file)
105
106 self.assertCommandContains(['build_api', service_method])
107
108 def testInsideServiceOutsideMethodInsideChroot(self):
109 """Test inside chroot for outside method raises an error."""
110 self.PatchObject(self.router, '_GetMethod',
111 return_value=self._mock_callable(expect_called=False))
112 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
Alex Klein2bfacb22019-02-04 11:42:17 -0700113 with self.assertRaises(cros_build_lib.DieSystemExit):
114 self.router.Route('chromite.api.InsideChrootApiService',
Alex Klein5bcb4d22019-03-21 13:51:54 -0600115 'InsideServiceOutsideMethod', self.input_file,
116 self.output_file)
Alex Klein2bfacb22019-02-04 11:42:17 -0700117
Alex Klein00aa8072019-04-15 16:36:00 -0600118 def testOutsideServiceOutsideMethodOutsideChroot(self):
119 """Test outside/outside/outside works correctly."""
120 self.PatchObject(self.router, '_GetMethod',
121 return_value=self._mock_callable(expect_called=True))
122 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Alex Klein2bfacb22019-02-04 11:42:17 -0700123 self.router.Route('chromite.api.OutsideChrootApiService',
Alex Klein5bcb4d22019-03-21 13:51:54 -0600124 'OutsideServiceOutsideMethod', self.input_file,
125 self.output_file)
Alex Klein2bfacb22019-02-04 11:42:17 -0700126
Alex Klein00aa8072019-04-15 16:36:00 -0600127 def testOutsideServiceInsideMethodInsideChroot(self):
128 """Test the inside method assertion override works properly."""
129 self.PatchObject(self.router, '_GetMethod',
130 return_value=self._mock_callable(expect_called=True))
131 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
Alex Klein2bfacb22019-02-04 11:42:17 -0700132 self.router.Route('chromite.api.OutsideChrootApiService',
Alex Klein5bcb4d22019-03-21 13:51:54 -0600133 'OutsideServiceInsideMethod', self.input_file,
134 self.output_file)
Alex Klein00aa8072019-04-15 16:36:00 -0600135
136 def testOutsideServiceInsideMethodOutsideChroot(self):
137 """Test calling an inside override method from outside the chroot."""
138 self.PatchObject(self.router, '_GetMethod',
139 return_value=self._mock_callable(expect_called=False))
140 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
141 self.rc.return_value = cros_build_lib.CommandResult(returncode=0)
142
143 service = 'chromite.api.OutsideChrootApiService'
144 method = 'OutsideServiceInsideMethod'
145 service_method = '%s/%s' % (service, method)
146 self.router.Route(service, method, self.input_file, self.output_file)
147
148 self.assertCommandContains(['build_api', service_method])