blob: 1b534f6f9c7f17fbca1dfcbdb5c9590e09c630da [file] [log] [blame]
Alex Klein19c4cc42019-02-27 14:47:57 -07001# -*- 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"""SDK tests."""
7
8from __future__ import print_function
9
Alex Klein231d2da2019-07-22 16:44:45 -060010import mock
Alex Klein19c4cc42019-02-27 14:47:57 -070011
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
Alex Klein19c4cc42019-02-27 14:47:57 -070013from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060014from chromite.api.gen.chromite.api import sdk_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070015from chromite.lib import cros_build_lib
Alex Klein231d2da2019-07-22 16:44:45 -060016from chromite.lib import cros_test_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070017from chromite.service import sdk as sdk_service
18
19
Alex Klein231d2da2019-07-22 16:44:45 -060020class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein19c4cc42019-02-27 14:47:57 -070021 """Create tests."""
22
23 def setUp(self):
24 """Setup method."""
25 # We need to run the command outside the chroot.
26 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -060027 self.response = sdk_pb2.CreateResponse()
Alex Klein19c4cc42019-02-27 14:47:57 -070028
29 def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False,
Alex Klein00aa8072019-04-15 16:36:00 -060030 cache_path=None, chroot_path=None):
Alex Klein19c4cc42019-02-27 14:47:57 -070031 """Helper to build a create request message."""
32 request = sdk_pb2.CreateRequest()
33 request.flags.no_replace = no_replace
34 request.flags.bootstrap = bootstrap
35 request.flags.no_use_image = no_use_image
36
37 if cache_path:
Alex Klein00aa8072019-04-15 16:36:00 -060038 request.chroot.cache_dir = cache_path
Alex Klein19c4cc42019-02-27 14:47:57 -070039 if chroot_path:
Alex Klein00aa8072019-04-15 16:36:00 -060040 request.chroot.path = chroot_path
Alex Klein19c4cc42019-02-27 14:47:57 -070041
42 return request
43
Alex Klein231d2da2019-07-22 16:44:45 -060044 def testValidateOnly(self):
45 """Sanity check that a validate only call does not execute any logic."""
46 patch = self.PatchObject(sdk_service, 'Create')
47
48 sdk_controller.Create(self._GetRequest(), self.response,
49 self.validate_only_config)
50 patch.assert_not_called()
51
Alex Klein19c4cc42019-02-27 14:47:57 -070052 def testSuccess(self):
53 """Test the successful call output handling."""
54 self.PatchObject(sdk_service, 'Create', return_value=1)
55
56 request = self._GetRequest()
Alex Klein19c4cc42019-02-27 14:47:57 -070057
Alex Klein231d2da2019-07-22 16:44:45 -060058 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070059
Alex Klein231d2da2019-07-22 16:44:45 -060060 self.assertEqual(1, self.response.version.version)
Alex Klein19c4cc42019-02-27 14:47:57 -070061
Alex Klein231d2da2019-07-22 16:44:45 -060062 def testFalseArguments(self):
Alex Klein19c4cc42019-02-27 14:47:57 -070063 """Test the argument handling."""
Alex Klein19c4cc42019-02-27 14:47:57 -070064 # Create the patches.
65 self.PatchObject(sdk_service, 'Create', return_value=1)
Alex Klein231d2da2019-07-22 16:44:45 -060066 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070067
68 # Flag translation tests.
69 # Test all false values in the message.
70 request = self._GetRequest(no_replace=False, bootstrap=False,
71 no_use_image=False)
Alex Klein231d2da2019-07-22 16:44:45 -060072 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070073 args_patch.assert_called_with(replace=True, bootstrap=False,
Alex Klein231d2da2019-07-22 16:44:45 -060074 use_image=True, paths=mock.ANY)
75
76 def testTrueArguments(self):
77 # Create the patches.
78 self.PatchObject(sdk_service, 'Create', return_value=1)
79 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070080
81 # Test all True values in the message.
82 request = self._GetRequest(no_replace=True, bootstrap=True,
83 no_use_image=True)
Alex Klein231d2da2019-07-22 16:44:45 -060084 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070085 args_patch.assert_called_with(replace=False, bootstrap=True,
Alex Klein231d2da2019-07-22 16:44:45 -060086 use_image=False, paths=mock.ANY)
87
88 def testPathArguments(self):
89 # Create the patches.
90 self.PatchObject(sdk_service, 'Create', return_value=1)
91 paths_patch = self.PatchObject(sdk_service, 'ChrootPaths')
Alex Klein19c4cc42019-02-27 14:47:57 -070092
93 # Test the path arguments get passed through.
94 cache_dir = '/cache/dir'
Alex Klein19c4cc42019-02-27 14:47:57 -070095 chroot_path = '/chroot/path'
Alex Klein00aa8072019-04-15 16:36:00 -060096 request = self._GetRequest(cache_path=cache_dir, chroot_path=chroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -060097 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein00aa8072019-04-15 16:36:00 -060098 paths_patch.assert_called_with(cache_dir=cache_dir, chroot_path=chroot_path)
Alex Kleinaa5c4172019-02-27 17:12:20 -070099
100
Alex Klein231d2da2019-07-22 16:44:45 -0600101class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700102 """Update tests."""
103
104 def setUp(self):
105 """Setup method."""
106 # We need to run the command inside the chroot.
107 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
108
Alex Klein231d2da2019-07-22 16:44:45 -0600109 self.response = sdk_pb2.UpdateResponse()
110
Alex Kleinaa5c4172019-02-27 17:12:20 -0700111 def _GetRequest(self, build_source=False, targets=None):
112 """Helper to simplify building a request instance."""
113 request = sdk_pb2.UpdateRequest()
114 request.flags.build_source = build_source
115
116 for target in targets or []:
117 added = request.toolchain_targets.add()
118 added.name = target
119
120 return request
121
Alex Klein231d2da2019-07-22 16:44:45 -0600122 def testValidateOnly(self):
123 """Sanity check that a validate only call does not execute any logic."""
124 patch = self.PatchObject(sdk_service, 'Update')
125
126 sdk_controller.Update(self._GetRequest(), self.response,
127 self.validate_only_config)
128 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700129
130 def testSuccess(self):
131 """Successful call output handling test."""
132 expected_version = 1
133 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
134 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700135
Alex Klein231d2da2019-07-22 16:44:45 -0600136 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700137
Alex Klein231d2da2019-07-22 16:44:45 -0600138 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700139
140 def testArgumentHandling(self):
141 """Test the proto argument handling."""
142 args = sdk_service.UpdateArguments()
143 self.PatchObject(sdk_service, 'Update', return_value=1)
144 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
145 return_value=args)
146
Alex Kleinaa5c4172019-02-27 17:12:20 -0700147 # No boards and flags False.
148 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600149 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700150 args_patch.assert_called_with(build_source=False, toolchain_targets=[])
151
152 # Multiple boards and flags True.
153 targets = ['board1', 'board2']
154 request = self._GetRequest(build_source=True, targets=targets)
Alex Klein231d2da2019-07-22 16:44:45 -0600155 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700156 args_patch.assert_called_with(build_source=True, toolchain_targets=targets)