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 'userdebug', 30 nsjail_bin='/bin/true', 31 chroot='/chroot', 32 dist_dir='/dist_dir', 33 build_id='0', 34 max_cpus=1, 35 build_goals=['droid', 'dist']) 36 37 self.assertEqual( 38 commands, 39 [ 40 '/bin/true', 41 '--env', 'USER=nobody', 42 '--config', '/nsjail.cfg', 43 '--env', 'BUILD_NUMBER=0', 44 '--max_cpus=1', 45 '--env', 'DIST_DIR=/dist', 46 '--bindmount', '/:/src', 47 '--bindmount', '/dist_dir:/dist', 48 '--', 49 '/src/tools/treble/build/sandbox/build_android_target.sh', 50 'target_name-userdebug', 51 '/src', 52 'make', '-j', 'droid', 'dist', 53 ] 54 ) 55 56 def testBuildCommand(self): 57 build_android_sandboxed.nsjail.__file__ = '/' 58 os.chdir('/') 59 commands = build_android_sandboxed.build( 60 'target_name', 61 'userdebug', 62 nsjail_bin='/bin/true', 63 command_wrapper='/command/wrapper', 64 chroot='/chroot', 65 dist_dir='/dist_dir', 66 build_id='0', 67 max_cpus=1, 68 build_goals=['droid', 'dist']) 69 70 self.assertEqual( 71 commands, 72 [ 73 '/bin/true', 74 '--env', 'USER=nobody', 75 '--config', '/nsjail.cfg', 76 '--env', 'BUILD_NUMBER=0', 77 '--max_cpus=1', 78 '--env', 'DIST_DIR=/dist', 79 '--bindmount', '/:/src', 80 '--bindmount', '/dist_dir:/dist', 81 '--', 82 '/command/wrapper', 83 'target_name-userdebug', 84 '/src', 85 'make', '-j', 'droid', 'dist', 86 ] 87 ) 88 89 def testUser(self): 90 build_android_sandboxed.nsjail.__file__ = '/' 91 os.chdir('/') 92 commands = build_android_sandboxed.build( 93 'target_name', 94 'user', 95 nsjail_bin='/bin/true', 96 chroot='/chroot', 97 dist_dir='/dist_dir', 98 build_id='0', 99 max_cpus=1, 100 build_goals=['droid', 'dist']) 101 102 self.assertEqual( 103 commands, 104 [ 105 '/bin/true', 106 '--env', 'USER=nobody', 107 '--config', '/nsjail.cfg', 108 '--env', 'BUILD_NUMBER=0', 109 '--max_cpus=1', 110 '--env', 'DIST_DIR=/dist', 111 '--bindmount', '/:/src', 112 '--bindmount', '/dist_dir:/dist', 113 '--', 114 '/src/tools/treble/build/sandbox/build_android_target.sh', 115 'target_name-user', 116 '/src', 117 'make', '-j', 'droid', 'dist', 118 ] 119 ) 120 121 def testExtraBuildGoals(self): 122 build_android_sandboxed.nsjail.__file__ = '/' 123 os.chdir('/') 124 commands = build_android_sandboxed.build( 125 'target_name', 126 'userdebug', 127 nsjail_bin='/bin/true', 128 chroot='/chroot', 129 dist_dir='/dist_dir', 130 build_id='0', 131 max_cpus=1, 132 build_goals=['droid', 'dist', 'extra_build_target']) 133 134 self.assertEqual( 135 commands, 136 [ 137 '/bin/true', 138 '--env', 'USER=nobody', 139 '--config', '/nsjail.cfg', 140 '--env', 'BUILD_NUMBER=0', 141 '--max_cpus=1', 142 '--env', 'DIST_DIR=/dist', 143 '--bindmount', '/:/src', 144 '--bindmount', '/dist_dir:/dist', 145 '--', 146 '/src/tools/treble/build/sandbox/build_android_target.sh', 147 'target_name-userdebug', 148 '/src', 149 'make', '-j', 'droid', 'dist', 150 'extra_build_target' 151 ] 152 ) 153 154 def testSkipBuildTag(self): 155 TEST_CONFIG_XML = """<config> 156 <target name="target_skip" tags="skip"> 157 <build_config> 158 <goal name="droid"/> 159 </build_config> 160 </target> 161 </config> 162 """ 163 with tempfile.NamedTemporaryFile('w+t') as test_config: 164 test_config.write(TEST_CONFIG_XML) 165 test_config.flush() 166 build_android_sandboxed.nsjail.__file__ = '/' 167 os.chdir('/') 168 skip_commands = build_android_sandboxed.build( 169 'target_skip', 170 'userdebug', 171 nsjail_bin='/bin/true', 172 chroot='/chroot', 173 dist_dir='/dist_dir', 174 build_id='0', 175 max_cpus=1, 176 build_goals=[], 177 config_file=test_config.name) 178 self.assertFalse(skip_commands) 179 180 def testEnv(self): 181 build_android_sandboxed.nsjail.__file__ = '/' 182 os.chdir('/') 183 commands = build_android_sandboxed.build( 184 'target_name', 185 'userdebug', 186 nsjail_bin='/bin/true', 187 chroot='/chroot', 188 dist_dir='/dist_dir', 189 build_id='0', 190 max_cpus=1, 191 build_goals=['droid', 'dist'], 192 env=['first_env_var=first_value', 'second_env_var=second_value']) 193 194 self.assertEqual( 195 commands, 196 [ 197 '/bin/true', 198 '--env', 'USER=nobody', 199 '--config', '/nsjail.cfg', 200 '--env', 'BUILD_NUMBER=0', 201 '--max_cpus=1', 202 '--env', 'DIST_DIR=/dist', 203 '--bindmount', '/:/src', 204 '--bindmount', '/dist_dir:/dist', 205 '--env', 'first_env_var=first_value', 206 '--env', 'second_env_var=second_value', 207 '--', 208 '/src/tools/treble/build/sandbox/build_android_target.sh', 209 'target_name-userdebug', 210 '/src', 211 'make', '-j', 'droid', 'dist', 212 ] 213 ) 214 215if __name__ == '__main__': 216 unittest.main() 217