Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | from chromite.lib import cros_test_lib |
| 11 | |
| 12 | from chromite.api.controller import sdk as sdk_controller |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 13 | from chromite.api.gen.chromite.api import sdk_pb2 |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.service import sdk as sdk_service |
| 16 | |
| 17 | |
| 18 | class 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 Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 27 | cache_path=None, chroot_path=None): |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 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: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 35 | request.chroot.cache_dir = cache_path |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 36 | if chroot_path: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 37 | request.chroot.path = chroot_path |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 38 | |
| 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 85 | chroot_path = '/chroot/path' |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 86 | request = self._GetRequest(cache_path=cache_dir, chroot_path=chroot_path) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 87 | sdk_controller.Create(request, response) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 88 | paths_patch.assert_called_with(cache_dir=cache_dir, chroot_path=chroot_path) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 89 | |
| 90 | |
| 91 | class 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) |