diff options
author | Cole Faust <colefaust@google.com> | 2024-04-03 18:24:16 -0700 |
---|---|---|
committer | Cole Faust <colefaust@google.com> | 2024-04-03 18:24:16 -0700 |
commit | 65502eef3e16a8e03c3c16eab8aab29fbfcc9e6a (patch) | |
tree | 30c0ff04640b4c22f8ec080fc03e91fc621571a2 | |
parent | b804362338384b98bf7d06bc84d9c1112d9f9283 (diff) | |
download | apex-65502eef3e16a8e03c3c16eab8aab29fbfcc9e6a.tar.gz |
Set embedded_launcher: true on apexer_test and apex_compression_test
Bug: 331488610
Test: m apexer_test apex_compression_test && ./out/host/linux-x86/nativetest64/apex_compression_test/apex_compression_test && ./out/host/linux-x86/nativetest64/apexer_test/apexer_test
Change-Id: I9c04e436032c64c3832e4e22122f0e1f29e20951
-rw-r--r-- | apexer/Android.bp | 6 | ||||
-rw-r--r-- | apexer/apexer_test.py | 44 | ||||
-rw-r--r-- | tools/Android.bp | 6 | ||||
-rw-r--r-- | tools/apex_compression_test.py | 68 |
4 files changed, 75 insertions, 49 deletions
diff --git a/apexer/Android.bp b/apexer/Android.bp index 20b9e833..be8f1d16 100644 --- a/apexer/Android.bp +++ b/apexer/Android.bp @@ -98,6 +98,7 @@ python_binary_host { // TODO(b/148659029): this test can't run in TEST_MAPPING. python_test_host { name: "apexer_test", + pkg_path: "apexer_test", main: "apexer_test.py", srcs: [ "apexer_test.py", @@ -118,6 +119,11 @@ python_test_host { libs: [ "apex_manifest", ], + version: { + py3: { + embedded_launcher: true, + }, + }, } apexer_deps_minus_go_tools = apexer_tools + [ diff --git a/apexer/apexer_test.py b/apexer/apexer_test.py index 7915a8d7..fb355d9a 100644 --- a/apexer/apexer_test.py +++ b/apexer/apexer_test.py @@ -25,6 +25,7 @@ import stat import subprocess import tempfile import unittest +from importlib import resources from zipfile import ZipFile from apex_manifest import ValidateApexManifest @@ -125,12 +126,6 @@ def get_sha1sum(file_path): return h.hexdigest() -def get_current_dir(): - """Returns the current dir, relative to the script dir.""" - # The script dir is the one we want, which could be different from pwd. - current_dir = os.path.dirname(os.path.realpath(__file__)) - return current_dir - def round_up(size, unit): assert unit & (unit - 1) == 0 return (size + unit - 1) & (~(unit - 1)) @@ -163,7 +158,7 @@ DEBUG_TEST = False class ApexerRebuildTest(unittest.TestCase): def setUp(self): self._to_cleanup = [] - self._get_host_tools(os.path.join(get_current_dir(), "apexer_test_host_tools.zip")) + self._get_host_tools() def tearDown(self): if not DEBUG_TEST: @@ -176,11 +171,11 @@ class ApexerRebuildTest(unittest.TestCase): else: print(self._to_cleanup) - def _get_host_tools(self, host_tools_file_path): + def _get_host_tools(self): dir_name = tempfile.mkdtemp(prefix=self._testMethodName+"_host_tools_") self._to_cleanup.append(dir_name) - if os.path.isfile(host_tools_file_path): - with ZipFile(host_tools_file_path, 'r') as zip_obj: + with resources.files("apexer_test").joinpath("apexer_test_host_tools.zip").open('rb') as f: + with ZipFile(f, 'r') as zip_obj: zip_obj.extractall(path=dir_name) files = {} @@ -196,7 +191,7 @@ class ApexerRebuildTest(unittest.TestCase): self.host_tools = files self.host_tools_path = os.path.join(dir_name, "bin") - path = os.path.join(dir_name, "bin") + path = self.host_tools_path if "PATH" in os.environ: path += ":" + os.environ["PATH"] os.environ["PATH"] = path @@ -208,6 +203,15 @@ class ApexerRebuildTest(unittest.TestCase): ld_library_path += ":" + os.path.join(os.environ["ANDROID_HOST_OUT"], "lib64") os.environ["LD_LIBRARY_PATH"] = ld_library_path + def _extract_resource(self, resource_name): + with ( + resources.files("apexer_test").joinpath(resource_name).open('rb') as f, + tempfile.NamedTemporaryFile(prefix=resource_name.replace('/', '_'), delete=False) as f2, + ): + self._to_cleanup.append(f2.name) + shutil.copyfileobj(f, f2) + return f2.name + def _get_container_files(self, apex_file_path): dir_name = tempfile.mkdtemp(prefix=self._testMethodName+"_container_files_") self._to_cleanup.append(dir_name) @@ -285,8 +289,8 @@ class ApexerRebuildTest(unittest.TestCase): if not payload_only and "assets" in container_files: cmd.extend(["--assets_dir", container_files["assets"]]) if not unsigned_payload_only: - cmd.extend(["--key", os.path.join(get_current_dir(), TEST_PRIVATE_KEY)]) - cmd.extend(["--pubkey", os.path.join(get_current_dir(), TEST_AVB_PUBLIC_KEY)]) + cmd.extend(["--key", self._extract_resource(TEST_PRIVATE_KEY)]) + cmd.extend(["--pubkey", self._extract_resource(TEST_AVB_PUBLIC_KEY)]) cmd.extend(args) # Decide on output file name @@ -333,8 +337,8 @@ class ApexerRebuildTest(unittest.TestCase): "-Djava.library.path=" + java_dep_lib, "-jar", self.host_tools['signapk.jar'], "-a", "4096", "--align-file-size", - os.path.join(get_current_dir(), TEST_X509_KEY), - os.path.join(get_current_dir(), TEST_PK8_KEY), + self._extract_resource(TEST_X509_KEY), + self._extract_resource(TEST_PK8_KEY), unsigned_apex, fn] run_and_check_output(cmd) return fn @@ -351,7 +355,7 @@ class ApexerRebuildTest(unittest.TestCase): cmd.append('--do_not_generate_fec') cmd.extend(['--algorithm', 'SHA256_RSA4096']) cmd.extend(['--hash_algorithm', 'sha256']) - cmd.extend(['--key', os.path.join(get_current_dir(), TEST_PRIVATE_KEY)]) + cmd.extend(['--key', self._extract_resource(TEST_PRIVATE_KEY)]) manifest_apex = ParseApexManifest(container_files["apex_manifest.pb"]) ValidateApexManifest(manifest_apex) cmd.extend(['--prop', 'apex.key:' + manifest_apex.name]) @@ -371,7 +375,7 @@ class ApexerRebuildTest(unittest.TestCase): run_and_check_output(cmd) def _run_build_test(self, apex_name): - apex_file_path = os.path.join(get_current_dir(), apex_name + ".apex") + apex_file_path = self._extract_resource(apex_name + ".apex") if DEBUG_TEST: fd, fn = tempfile.mkstemp(prefix=self._testMethodName+"_input_", suffix=".apex") os.close(fd) @@ -393,7 +397,7 @@ class ApexerRebuildTest(unittest.TestCase): """Assert that payload-only output from apexer is same as the payload we get by unzipping apex. """ - apex_file_path = os.path.join(get_current_dir(), TEST_APEX + ".apex") + apex_file_path = self._extract_resource(TEST_APEX + ".apex") container_files = self._get_container_files(apex_file_path) payload_dir = self._extract_payload(apex_file_path) payload_only_file_path = self._run_apexer(container_files, payload_dir, ["--payload_only"]) @@ -405,7 +409,7 @@ class ApexerRebuildTest(unittest.TestCase): """Assert that when unsigned-payload-only output from apexer is signed by the avb key, it is same as the payload we get by unzipping apex. """ - apex_file_path = os.path.join(get_current_dir(), TEST_APEX + ".apex") + apex_file_path = self._extract_resource(TEST_APEX + ".apex") container_files = self._get_container_files(apex_file_path) payload_dir = self._extract_payload(apex_file_path) unsigned_payload_only_file_path = self._run_apexer(container_files, payload_dir, @@ -433,7 +437,7 @@ class ApexerRebuildTest(unittest.TestCase): def test_conv_apex_manifest(self): # .pb generation from json - manifest_json_path = os.path.join(get_current_dir(), TEST_MANIFEST_JSON) + manifest_json_path = self._extract_resource(TEST_MANIFEST_JSON) fd, fn = tempfile.mkstemp(prefix=self._testMethodName + "_manifest_", suffix=".pb") os.close(fd) diff --git a/tools/Android.bp b/tools/Android.bp index e77945b8..a7ff261d 100644 --- a/tools/Android.bp +++ b/tools/Android.bp @@ -74,6 +74,7 @@ python_binary_host { python_test_host { name: "apex_compression_test", + pkg_path: "apex_compression_test", main: "apex_compression_test.py", srcs: [ "apex_compression_test.py", @@ -93,6 +94,11 @@ python_test_host { test_options: { unit_test: true, }, + version: { + py3: { + embedded_launcher: true, + }, + }, } cc_binary_host { diff --git a/tools/apex_compression_test.py b/tools/apex_compression_test.py index 2a30007a..042a2781 100644 --- a/tools/apex_compression_test.py +++ b/tools/apex_compression_test.py @@ -19,9 +19,11 @@ import hashlib import logging import os import shutil +import stat import subprocess import tempfile import unittest +from importlib import resources from zipfile import ZipFile, ZIP_STORED, ZIP_DEFLATED import apex_manifest_pb2 @@ -70,15 +72,6 @@ def run(args, verbose=None, **kwargs): return subprocess.Popen(args, **kwargs) -def run_host_command(args, verbose=None, **kwargs): - host_build_top = os.environ.get('ANDROID_BUILD_TOP') - if host_build_top: - host_command_dir = os.path.join(host_build_top, - 'out/host/linux-x86/bin') - args[0] = os.path.join(host_command_dir, args[0]) - return run_and_check_output(args, verbose, **kwargs) - - def run_and_check_output(args, verbose=None, **kwargs): """Runs the given command and returns the output. @@ -135,6 +128,7 @@ def get_sha1sum(file_path): class ApexCompressionTest(unittest.TestCase): def setUp(self): self._to_cleanup = [] + self._get_host_tools() def tearDown(self): if not DEBUG_TEST: @@ -147,18 +141,33 @@ class ApexCompressionTest(unittest.TestCase): else: print('Cleanup: ' + str(self._to_cleanup)) - def _run_apex_compression_tool(self, args): - cmd = ['apex_compression_tool'] - host_build_top = os.environ.get('ANDROID_BUILD_TOP') - if host_build_top: - os.environ['APEX_COMPRESSION_TOOL_PATH'] = ( - os.path.join(host_build_top, 'out/host/linux-x86/bin') - + ':' + os.path.join(host_build_top, 'prebuilts/sdk/tools/linux/bin')) - else: - os.environ['APEX_COMPRESSION_TOOL_PATH'] = os.path.dirname( - shutil.which('apex_compression_tool')) - cmd.extend(args) - run_host_command(cmd, True) + def _get_host_tools(self): + dir_name = tempfile.mkdtemp(prefix=self._testMethodName+"_host_tools_") + self._to_cleanup.append(dir_name) + for tool in ["avbtool", "conv_apex_manifest", "apex_compression_tool", "deapexer", "soong_zip"]: + with ( + resources.files("apex_compression_test").joinpath(tool).open('rb') as tool_resource, + open(os.path.join(dir_name, tool), 'wb') as f + ): + shutil.copyfileobj(tool_resource, f) + os.chmod(os.path.join(dir_name, tool), stat.S_IRUSR | stat.S_IXUSR) + os.environ['APEX_COMPRESSION_TOOL_PATH'] = dir_name + path = dir_name + if "PATH" in os.environ: + path += ":" + os.environ["PATH"] + os.environ["PATH"] = path + + def _get_test_apex(self): + tmpdir = tempfile.mkdtemp() + self._to_cleanup.append(tmpdir) + apexPath = os.path.join(tmpdir, TEST_APEX + '.apex') + with ( + resources.files('apex_compression_test').joinpath(TEST_APEX + '.apex').open('rb') as f, + open(apexPath, 'wb') as f2, + ): + shutil.copyfileobj(f, f2) + + return apexPath def _get_container_files(self, apex_file_path): dir_name = tempfile.mkdtemp( @@ -183,7 +192,7 @@ class ApexCompressionTest(unittest.TestCase): avbtool_cmd = ['avbtool', 'print_partition_digests', '--image', files['apex_payload']] # avbtool_cmd output has format "<name>: <value>" - files['digest'] = run_host_command( + files['digest'] = run_and_check_output( avbtool_cmd, True).split(': ')[1].strip() return files @@ -194,7 +203,7 @@ class ApexCompressionTest(unittest.TestCase): 'print', manifest_path ]) - return run_host_command(cmd, 'True') + return run_and_check_output(cmd, 'True') # Mutates the manifest located at |manifest_path| def _unset_original_apex_digest(self, manifest_path): @@ -213,7 +222,8 @@ class ApexCompressionTest(unittest.TestCase): suffix='.capex') os.close(fd) self._to_cleanup.append(compressed_apex_fp) - self._run_apex_compression_tool([ + run_and_check_output([ + 'apex_compression_tool', 'compress', '--input', uncompressed_apex_fp, '--output', compressed_apex_fp @@ -232,7 +242,7 @@ class ApexCompressionTest(unittest.TestCase): '--input', compressed_apex_fp, '--output', decompressed_apex_fp ]) - run_host_command(cmd, True) + run_and_check_output(cmd, True) self.assertTrue(os.path.exists(decompressed_apex_fp), 'Decompressed APEX does not exist') @@ -241,10 +251,10 @@ class ApexCompressionTest(unittest.TestCase): def _get_type(self, apex_file_path): cmd = ['deapexer', 'info', '--print-type', apex_file_path] - return run_host_command(cmd, True).strip() + return run_and_check_output(cmd, True).strip() def test_compression(self): - uncompressed_apex_fp = os.path.join(get_current_dir(), TEST_APEX + '.apex') + uncompressed_apex_fp = self._get_test_apex() # TODO(samiul): try compressing a compressed APEX compressed_apex_fp = self._compress_apex(uncompressed_apex_fp) @@ -284,7 +294,7 @@ class ApexCompressionTest(unittest.TestCase): def test_decompression(self): # setup: create compressed APEX - uncompressed_apex_fp = os.path.join(get_current_dir(), TEST_APEX + '.apex') + uncompressed_apex_fp = self._get_test_apex() compressed_apex_fp = self._compress_apex(uncompressed_apex_fp) # Decompress it @@ -306,7 +316,7 @@ class ApexCompressionTest(unittest.TestCase): + ' is not a compressed APEX', str(error.exception)) def test_only_original_apex_is_compressed(self): - uncompressed_apex_fp = os.path.join(get_current_dir(), TEST_APEX + '.apex') + uncompressed_apex_fp = self._get_test_apex() compressed_apex_fp = self._compress_apex(uncompressed_apex_fp) with ZipFile(compressed_apex_fp, 'r') as zip_obj: |