blob: 7492b26b0da63247df96243d1d9b2c7857cf49f0 [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
13from chromite.api.gen import sdk_pb2
14from 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,
27 cache_path=None, chrome_path=None, chroot_path=None):
28 """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:
35 request.paths.cache = cache_path
36 if chrome_path:
37 request.paths.chrome = chrome_path
38 if chroot_path:
39 request.paths.chroot = chroot_path
40
41 return request
42
43 def testSuccess(self):
44 """Test the successful call output handling."""
45 self.PatchObject(sdk_service, 'Create', return_value=1)
46
47 request = self._GetRequest()
48 response = sdk_pb2.CreateResponse()
49
50 sdk_controller.Create(request, response)
51
52 self.assertEqual(1, response.version.version)
53
54 def testArgumentHandling(self):
55 """Test the argument handling."""
56 # Get some defaults to return so it's passing around valid objects.
57 paths_obj = sdk_service.ChrootPaths()
58 args_obj = sdk_service.CreateArguments()
59
60 # Create the patches.
61 self.PatchObject(sdk_service, 'Create', return_value=1)
62 paths_patch = self.PatchObject(sdk_service, 'ChrootPaths',
63 return_value=paths_obj)
64 args_patch = self.PatchObject(sdk_service, 'CreateArguments',
65 return_value=args_obj)
66
67 # Just need a response to pass through.
68 response = sdk_pb2.CreateResponse()
69
70 # Flag translation tests.
71 # Test all false values in the message.
72 request = self._GetRequest(no_replace=False, bootstrap=False,
73 no_use_image=False)
74 sdk_controller.Create(request, response)
75 args_patch.assert_called_with(replace=True, bootstrap=False,
76 use_image=True, paths=paths_obj)
77
78 # Test all True values in the message.
79 request = self._GetRequest(no_replace=True, bootstrap=True,
80 no_use_image=True)
81 sdk_controller.Create(request, response)
82 args_patch.assert_called_with(replace=False, bootstrap=True,
83 use_image=False, paths=paths_obj)
84
85 # Test the path arguments get passed through.
86 cache_dir = '/cache/dir'
87 chrome_root = '/chrome/path'
88 chroot_path = '/chroot/path'
89 request = self._GetRequest(cache_path=cache_dir, chrome_path=chrome_root,
90 chroot_path=chroot_path)
91 sdk_controller.Create(request, response)
92 paths_patch.assert_called_with(cache_dir=cache_dir, chrome_root=chrome_root,
93 chroot_path=chroot_path)
Alex Kleinaa5c4172019-02-27 17:12:20 -070094
95
96class SdkUpdateTest(cros_test_lib.MockTestCase):
97 """Update tests."""
98
99 def setUp(self):
100 """Setup method."""
101 # We need to run the command inside the chroot.
102 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
103
104 def _GetRequest(self, build_source=False, targets=None):
105 """Helper to simplify building a request instance."""
106 request = sdk_pb2.UpdateRequest()
107 request.flags.build_source = build_source
108
109 for target in targets or []:
110 added = request.toolchain_targets.add()
111 added.name = target
112
113 return request
114
115 def _GetResponse(self):
116 """Helper to build an empty response instance."""
117 return sdk_pb2.UpdateResponse()
118
119 def testSuccess(self):
120 """Successful call output handling test."""
121 expected_version = 1
122 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
123 request = self._GetRequest()
124 response = self._GetResponse()
125
126 sdk_controller.Update(request, response)
127
128 self.assertEqual(expected_version, response.version.version)
129
130 def testArgumentHandling(self):
131 """Test the proto argument handling."""
132 args = sdk_service.UpdateArguments()
133 self.PatchObject(sdk_service, 'Update', return_value=1)
134 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
135 return_value=args)
136
137 response = self._GetResponse()
138
139 # No boards and flags False.
140 request = self._GetRequest(build_source=False)
141 sdk_controller.Update(request, response)
142 args_patch.assert_called_with(build_source=False, toolchain_targets=[])
143
144 # Multiple boards and flags True.
145 targets = ['board1', 'board2']
146 request = self._GetRequest(build_source=True, targets=targets)
147 sdk_controller.Update(request, response)
148 args_patch.assert_called_with(build_source=True, toolchain_targets=targets)