blob: cf21e477db6e27ec89f686eeb150e4cfe2b0fb12 [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)