blob: 03d924e45d4ea3f76d52dcea9df3ee565396457f [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
10from chromite.lib import cros_test_lib
11
12from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060013from chromite.api.gen.chromite.api import sdk_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070014from chromite.lib import cros_build_lib
15from chromite.service import sdk as sdk_service
16
17
18class SdkCreateTest(cros_test_lib.MockTestCase):
19 """Create tests."""
20
21 def setUp(self):
22 """Setup method."""
23 # We need to run the command outside the chroot.
24 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
25
26 def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False,
Alex Klein00aa8072019-04-15 16:36:00 -060027 cache_path=None, chroot_path=None):
Alex Klein19c4cc42019-02-27 14:47:57 -070028 """Helper to build a create request message."""
29 request = sdk_pb2.CreateRequest()
30 request.flags.no_replace = no_replace
31 request.flags.bootstrap = bootstrap
32 request.flags.no_use_image = no_use_image
33
34 if cache_path:
Alex Klein00aa8072019-04-15 16:36:00 -060035 request.chroot.cache_dir = cache_path
Alex Klein19c4cc42019-02-27 14:47:57 -070036 if chroot_path:
Alex Klein00aa8072019-04-15 16:36:00 -060037 request.chroot.path = chroot_path
Alex Klein19c4cc42019-02-27 14:47:57 -070038
39 return request
40
41 def testSuccess(self):
42 """Test the successful call output handling."""
43 self.PatchObject(sdk_service, 'Create', return_value=1)
44
45 request = self._GetRequest()
46 response = sdk_pb2.CreateResponse()
47
48 sdk_controller.Create(request, response)
49
50 self.assertEqual(1, response.version.version)
51
52 def testArgumentHandling(self):
53 """Test the argument handling."""
54 # Get some defaults to return so it's passing around valid objects.
55 paths_obj = sdk_service.ChrootPaths()
56 args_obj = sdk_service.CreateArguments()
57
58 # Create the patches.
59 self.PatchObject(sdk_service, 'Create', return_value=1)
60 paths_patch = self.PatchObject(sdk_service, 'ChrootPaths',
61 return_value=paths_obj)
62 args_patch = self.PatchObject(sdk_service, 'CreateArguments',
63 return_value=args_obj)
64
65 # Just need a response to pass through.
66 response = sdk_pb2.CreateResponse()
67
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)
72 sdk_controller.Create(request, response)
73 args_patch.assert_called_with(replace=True, bootstrap=False,
74 use_image=True, paths=paths_obj)
75
76 # Test all True values in the message.
77 request = self._GetRequest(no_replace=True, bootstrap=True,
78 no_use_image=True)
79 sdk_controller.Create(request, response)
80 args_patch.assert_called_with(replace=False, bootstrap=True,
81 use_image=False, paths=paths_obj)
82
83 # Test the path arguments get passed through.
84 cache_dir = '/cache/dir'
Alex Klein19c4cc42019-02-27 14:47:57 -070085 chroot_path = '/chroot/path'
Alex Klein00aa8072019-04-15 16:36:00 -060086 request = self._GetRequest(cache_path=cache_dir, chroot_path=chroot_path)
Alex Klein19c4cc42019-02-27 14:47:57 -070087 sdk_controller.Create(request, response)
Alex Klein00aa8072019-04-15 16:36:00 -060088 paths_patch.assert_called_with(cache_dir=cache_dir, chroot_path=chroot_path)
Alex Kleinaa5c4172019-02-27 17:12:20 -070089
90
91class SdkUpdateTest(cros_test_lib.MockTestCase):
92 """Update tests."""
93
94 def setUp(self):
95 """Setup method."""
96 # We need to run the command inside the chroot.
97 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
98
99 def _GetRequest(self, build_source=False, targets=None):
100 """Helper to simplify building a request instance."""
101 request = sdk_pb2.UpdateRequest()
102 request.flags.build_source = build_source
103
104 for target in targets or []:
105 added = request.toolchain_targets.add()
106 added.name = target
107
108 return request
109
110 def _GetResponse(self):
111 """Helper to build an empty response instance."""
112 return sdk_pb2.UpdateResponse()
113
114 def testSuccess(self):
115 """Successful call output handling test."""
116 expected_version = 1
117 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
118 request = self._GetRequest()
119 response = self._GetResponse()
120
121 sdk_controller.Update(request, response)
122
123 self.assertEqual(expected_version, response.version.version)
124
125 def testArgumentHandling(self):
126 """Test the proto argument handling."""
127 args = sdk_service.UpdateArguments()
128 self.PatchObject(sdk_service, 'Update', return_value=1)
129 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
130 return_value=args)
131
132 response = self._GetResponse()
133
134 # No boards and flags False.
135 request = self._GetRequest(build_source=False)
136 sdk_controller.Update(request, response)
137 args_patch.assert_called_with(build_source=False, toolchain_targets=[])
138
139 # Multiple boards and flags True.
140 targets = ['board1', 'board2']
141 request = self._GetRequest(build_source=True, targets=targets)
142 sdk_controller.Update(request, response)
143 args_patch.assert_called_with(build_source=True, toolchain_targets=targets)