Path: blob/main/tests/integration_tests/build/test_binary_size.py
1958 views
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.1# SPDX-License-Identifier: Apache-2.02"""Tests that check if the release binary sizes fall within expected size."""34import os5import platform6import pytest78import host_tools.cargo_build as host910MACHINE = platform.machine()11""" Platform definition used to select the correct size target"""1213SIZES_DICT = {14"x86_64": {15"FC_BINARY_SIZE_TARGET": 2067944,16"JAILER_BINARY_SIZE_TARGET": 1439512,17"FC_BINARY_SIZE_LIMIT": 2171516,18"JAILER_BINARY_SIZE_LIMIT": 1511488,19},20"aarch64": {21"FC_BINARY_SIZE_TARGET": 2080000,22"JAILER_BINARY_SIZE_TARGET": 1338312,23"FC_BINARY_SIZE_LIMIT": 2170000,24"JAILER_BINARY_SIZE_LIMIT": 1511488,25}26}2728FC_BINARY_SIZE_TARGET = SIZES_DICT[MACHINE]["FC_BINARY_SIZE_TARGET"]29"""Firecracker target binary size in bytes"""3031FC_BINARY_SIZE_LIMIT = SIZES_DICT[MACHINE]["FC_BINARY_SIZE_LIMIT"]32"""Firecracker maximum binary size in bytes"""3334JAILER_BINARY_SIZE_TARGET = SIZES_DICT[MACHINE]["JAILER_BINARY_SIZE_TARGET"]35"""Jailer target binary size in bytes"""3637JAILER_BINARY_SIZE_LIMIT = SIZES_DICT[MACHINE]["JAILER_BINARY_SIZE_LIMIT"]38"""Jailer maximum binary size in bytes"""3940BINARY_SIZE_TOLERANCE = 0.0541"""Tolerance of 5% allowed for binary size"""424344@pytest.mark.timeout(500)45def test_firecracker_binary_size():46"""47Test if the size of the firecracker binary is within expected ranges.4849@type: build50"""51fc_binary, _ = host.get_firecracker_binaries()5253result = check_binary_size("firecracker", fc_binary, FC_BINARY_SIZE_TARGET,54BINARY_SIZE_TOLERANCE, FC_BINARY_SIZE_LIMIT)5556return f"{result} B", \57f"{FC_BINARY_SIZE_TARGET} +/- {BINARY_SIZE_TOLERANCE * 100}% B"585960@pytest.mark.timeout(500)61def test_jailer_binary_size():62"""63Test if the size of the jailer binary is within expected ranges.6465@type: build66"""67_, jailer_binary = host.get_firecracker_binaries()6869result = check_binary_size("jailer", jailer_binary,70JAILER_BINARY_SIZE_TARGET,71BINARY_SIZE_TOLERANCE, JAILER_BINARY_SIZE_LIMIT)7273return f"{result} B", \74f"{JAILER_BINARY_SIZE_TARGET} +/- {BINARY_SIZE_TOLERANCE * 100}% B"757677def check_binary_size(name, binary_path, size_target, tolerance, limit):78"""Check if the specified binary falls within the expected range."""79# Get the size of the release binary.80binary_size = os.path.getsize(binary_path)8182# Get the name of the variable that needs updating.83namespace = globals()84size_target_name = [name for name in namespace if namespace[name]85is size_target][0]8687# Compute concrete binary size difference.88delta_size = size_target - binary_size8990binary_low_msg = (91'Current {} binary size of {} bytes is below the target'92' of {} bytes with {} bytes.\n'93'Update the {} threshold'94.format(name, binary_size, size_target, delta_size, size_target_name)95)9697assert binary_size > size_target * (1 - tolerance), binary_low_msg9899binary_high_msg = (100'Current {} binary size of {} bytes is above the target'101' of {} bytes with {} bytes.\n'102.format(name, binary_size, size_target, -delta_size)103)104105assert binary_size < size_target * (1 + tolerance), binary_high_msg106107binary_limit_msg = (108'Current {} binary size of {} bytes is above the limit'109' of {} bytes with {} bytes.\n'110.format(name, binary_size, limit, binary_size - limit)111)112113assert binary_size < limit, binary_limit_msg114115return binary_size116117118