Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/integration_tests/build/test_binary_size.py
1958 views
1
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Tests that check if the release binary sizes fall within expected size."""
4
5
import os
6
import platform
7
import pytest
8
9
import host_tools.cargo_build as host
10
11
MACHINE = platform.machine()
12
""" Platform definition used to select the correct size target"""
13
14
SIZES_DICT = {
15
"x86_64": {
16
"FC_BINARY_SIZE_TARGET": 2067944,
17
"JAILER_BINARY_SIZE_TARGET": 1439512,
18
"FC_BINARY_SIZE_LIMIT": 2171516,
19
"JAILER_BINARY_SIZE_LIMIT": 1511488,
20
},
21
"aarch64": {
22
"FC_BINARY_SIZE_TARGET": 2080000,
23
"JAILER_BINARY_SIZE_TARGET": 1338312,
24
"FC_BINARY_SIZE_LIMIT": 2170000,
25
"JAILER_BINARY_SIZE_LIMIT": 1511488,
26
}
27
}
28
29
FC_BINARY_SIZE_TARGET = SIZES_DICT[MACHINE]["FC_BINARY_SIZE_TARGET"]
30
"""Firecracker target binary size in bytes"""
31
32
FC_BINARY_SIZE_LIMIT = SIZES_DICT[MACHINE]["FC_BINARY_SIZE_LIMIT"]
33
"""Firecracker maximum binary size in bytes"""
34
35
JAILER_BINARY_SIZE_TARGET = SIZES_DICT[MACHINE]["JAILER_BINARY_SIZE_TARGET"]
36
"""Jailer target binary size in bytes"""
37
38
JAILER_BINARY_SIZE_LIMIT = SIZES_DICT[MACHINE]["JAILER_BINARY_SIZE_LIMIT"]
39
"""Jailer maximum binary size in bytes"""
40
41
BINARY_SIZE_TOLERANCE = 0.05
42
"""Tolerance of 5% allowed for binary size"""
43
44
45
@pytest.mark.timeout(500)
46
def test_firecracker_binary_size():
47
"""
48
Test if the size of the firecracker binary is within expected ranges.
49
50
@type: build
51
"""
52
fc_binary, _ = host.get_firecracker_binaries()
53
54
result = check_binary_size("firecracker", fc_binary, FC_BINARY_SIZE_TARGET,
55
BINARY_SIZE_TOLERANCE, FC_BINARY_SIZE_LIMIT)
56
57
return f"{result} B", \
58
f"{FC_BINARY_SIZE_TARGET} +/- {BINARY_SIZE_TOLERANCE * 100}% B"
59
60
61
@pytest.mark.timeout(500)
62
def test_jailer_binary_size():
63
"""
64
Test if the size of the jailer binary is within expected ranges.
65
66
@type: build
67
"""
68
_, jailer_binary = host.get_firecracker_binaries()
69
70
result = check_binary_size("jailer", jailer_binary,
71
JAILER_BINARY_SIZE_TARGET,
72
BINARY_SIZE_TOLERANCE, JAILER_BINARY_SIZE_LIMIT)
73
74
return f"{result} B", \
75
f"{JAILER_BINARY_SIZE_TARGET} +/- {BINARY_SIZE_TOLERANCE * 100}% B"
76
77
78
def check_binary_size(name, binary_path, size_target, tolerance, limit):
79
"""Check if the specified binary falls within the expected range."""
80
# Get the size of the release binary.
81
binary_size = os.path.getsize(binary_path)
82
83
# Get the name of the variable that needs updating.
84
namespace = globals()
85
size_target_name = [name for name in namespace if namespace[name]
86
is size_target][0]
87
88
# Compute concrete binary size difference.
89
delta_size = size_target - binary_size
90
91
binary_low_msg = (
92
'Current {} binary size of {} bytes is below the target'
93
' of {} bytes with {} bytes.\n'
94
'Update the {} threshold'
95
.format(name, binary_size, size_target, delta_size, size_target_name)
96
)
97
98
assert binary_size > size_target * (1 - tolerance), binary_low_msg
99
100
binary_high_msg = (
101
'Current {} binary size of {} bytes is above the target'
102
' of {} bytes with {} bytes.\n'
103
.format(name, binary_size, size_target, -delta_size)
104
)
105
106
assert binary_size < size_target * (1 + tolerance), binary_high_msg
107
108
binary_limit_msg = (
109
'Current {} binary size of {} bytes is above the limit'
110
' of {} bytes with {} bytes.\n'
111
.format(name, binary_size, limit, binary_size - limit)
112
)
113
114
assert binary_size < limit, binary_limit_msg
115
116
return binary_size
117
118