1# Copyright 2020 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Test build_android_sandboxed.""" 15 16import os 17import tempfile 18import unittest 19from . import build_android_sandboxed 20 21 22class BuildAndroidSandboxedTest(unittest.TestCase): 23 24 def testBasic(self): 25 build_android_sandboxed.nsjail.__file__ = '/' 26 os.chdir('/') 27 commands = build_android_sandboxed.build( 28 'target_name', 29 'release_target', 30 'userdebug', 31 nsjail_bin='/bin/true', 32 chroot='/chroot', 33 dist_dir='/dist_dir', 34 build_id='0', 35 max_cpus=1, 36 build_goals=['droid', 'dist']) 37 38 self.assertEqual( 39 commands, 40 [ 41 '/bin/true', 42 '--env', 'USER=nobody', 43 '--config', '/nsjail.cfg', 44 '--env', 'BUILD_NUMBER=0', 45 '--max_cpus=1', 46 '--env', 'DIST_DIR=/dist', 47 '--bindmount', '/:/src', 48 '--bindmount', '/dist_dir:/dist', 49 '--', 50 '/src/tools/treble/build/sandbox/build_android_target.sh', 51 'target_name-release_target-userdebug', 52 '/src', 53 'make', '-j', 'droid', 'dist', 54 ] 55 ) 56 57 def testBuildCommand(self): 58 build_android_sandboxed.nsjail.__file__ = '/' 59 os.chdir('/') 60 commands = build_android_sandboxed.build( 61 'target_name', 62 'release_target', 63 'userdebug', 64 nsjail_bin='/bin/true', 65 command_wrapper='/command/wrapper', 66 chroot='/chroot', 67 dist_dir='/dist_dir', 68 build_id='0', 69 max_cpus=1, 70 build_goals=['droid', 'dist']) 71 72 self.assertEqual( 73 commands, 74 [ 75 '/bin/true', 76 '--env', 'USER=nobody', 77 '--config', '/nsjail.cfg', 78 '--env', 'BUILD_NUMBER=0', 79 '--max_cpus=1', 80 '--env', 'DIST_DIR=/dist', 81 '--bindmount', '/:/src', 82 '--bindmount', '/dist_dir:/dist', 83 '--', 84 '/command/wrapper', 85 'target_name-release_target-userdebug', 86 '/src', 87 'make', '-j', 'droid', 'dist', 88 ] 89 ) 90 91 def testUser(self): 92 build_android_sandboxed.nsjail.__file__ = '/' 93 os.chdir('/') 94 commands = build_android_sandboxed.build( 95 'target_name', 96 'release_target', 97 'user', 98 nsjail_bin='/bin/true', 99 chroot='/chroot', 100 dist_dir='/dist_dir', 101 build_id='0', 102 max_cpus=1, 103 build_goals=['droid', 'dist']) 104 105 self.assertEqual( 106 commands, 107 [ 108 '/bin/true', 109 '--env', 'USER=nobody', 110 '--config', '/nsjail.cfg', 111 '--env', 'BUILD_NUMBER=0', 112 '--max_cpus=1', 113 '--env', 'DIST_DIR=/dist', 114 '--bindmount', '/:/src', 115 '--bindmount', '/dist_dir:/dist', 116 '--', 117 '/src/tools/treble/build/sandbox/build_android_target.sh', 118 'target_name-release_target-user', 119 '/src', 120 'make', '-j', 'droid', 'dist', 121 ] 122 ) 123 124 def testExtraBuildGoals(self): 125 build_android_sandboxed.nsjail.__file__ = '/' 126 os.chdir('/') 127 commands = build_android_sandboxed.build( 128 'target_name', 129 'release_target', 130 'userdebug', 131 nsjail_bin='/bin/true', 132 chroot='/chroot', 133 dist_dir='/dist_dir', 134 build_id='0', 135 max_cpus=1, 136 build_goals=['droid', 'dist', 'extra_build_target']) 137 138 self.assertEqual( 139 commands, 140 [ 141 '/bin/true', 142 '--env', 'USER=nobody', 143 '--config', '/nsjail.cfg', 144 '--env', 'BUILD_NUMBER=0', 145 '--max_cpus=1', 146 '--env', 'DIST_DIR=/dist', 147 '--bindmount', '/:/src', 148 '--bindmount', '/dist_dir:/dist', 149 '--', 150 '/src/tools/treble/build/sandbox/build_android_target.sh', 151 'target_name-release_target-userdebug', 152 '/src', 153 'make', '-j', 'droid', 'dist', 154 'extra_build_target' 155 ] 156 ) 157 158 def testSkipBuildTag(self): 159 TEST_CONFIG_XML = """<config> 160 <target name="target_skip" tags="skip"> 161 <build_config> 162 <goal name="droid"/> 163 </build_config> 164 </target> 165 </config> 166 """ 167 with tempfile.NamedTemporaryFile('w+t') as test_config: 168 test_config.write(TEST_CONFIG_XML) 169 test_config.flush() 170 build_android_sandboxed.nsjail.__file__ = '/' 171 os.chdir('/') 172 skip_commands = build_android_sandboxed.build( 173 'target_skip', 174 'release_target', 175 'userdebug', 176 nsjail_bin='/bin/true', 177 chroot='/chroot', 178 dist_dir='/dist_dir', 179 build_id='0', 180 max_cpus=1, 181 build_goals=[], 182 config_file=test_config.name) 183 self.assertFalse(skip_commands) 184 185 def testEnv(self): 186 build_android_sandboxed.nsjail.__file__ = '/' 187 os.chdir('/') 188 commands = build_android_sandboxed.build( 189 'target_name', 190 'release_target', 191 'userdebug', 192 nsjail_bin='/bin/true', 193 chroot='/chroot', 194 dist_dir='/dist_dir', 195 build_id='0', 196 max_cpus=1, 197 build_goals=['droid', 'dist'], 198 env=['first_env_var=first_value', 'second_env_var=second_value']) 199 200 self.assertEqual( 201 commands, 202 [ 203 '/bin/true', 204 '--env', 'USER=nobody', 205 '--config', '/nsjail.cfg', 206 '--env', 'BUILD_NUMBER=0', 207 '--max_cpus=1', 208 '--env', 'DIST_DIR=/dist', 209 '--bindmount', '/:/src', 210 '--bindmount', '/dist_dir:/dist', 211 '--env', 'first_env_var=first_value', 212 '--env', 'second_env_var=second_value', 213 '--', 214 '/src/tools/treble/build/sandbox/build_android_target.sh', 215 'target_name-release_target-userdebug', 216 '/src', 217 'make', '-j', 'droid', 'dist', 218 ] 219 ) 220 221if __name__ == '__main__': 222 unittest.main() 223