Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/integration_tests/functional/test_cmd_line_start.py
1958 views
1
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Tests microvm start with configuration file as command line parameter."""
4
5
import json
6
import os
7
import re
8
9
from retry.api import retry_call
10
11
import pytest
12
13
import framework.utils as utils
14
15
16
def _configure_vm_from_json(test_microvm, vm_config_file):
17
"""Configure a microvm using a file sent as command line parameter.
18
19
Create resources needed for the configuration of the microvm and
20
set as configuration file a copy of the file that was passed as
21
parameter to this helper function.
22
"""
23
test_microvm.create_jailed_resource(test_microvm.kernel_file,
24
create_jail=True)
25
test_microvm.create_jailed_resource(test_microvm.rootfs_file,
26
create_jail=True)
27
28
# vm_config_file is the source file that keeps the desired vmm
29
# configuration. vm_config_path is the configuration file we
30
# create inside the jail, such that it can be accessed by
31
# firecracker after it starts.
32
vm_config_path = os.path.join(test_microvm.path,
33
os.path.basename(vm_config_file))
34
with open(vm_config_file) as f1:
35
with open(vm_config_path, "w") as f2:
36
for line in f1:
37
f2.write(line)
38
test_microvm.create_jailed_resource(vm_config_path, create_jail=True)
39
test_microvm.jailer.extra_args = {'config-file': os.path.basename(
40
vm_config_file)}
41
42
43
@pytest.mark.parametrize(
44
"vm_config_file",
45
["framework/vm_config.json"]
46
)
47
def test_config_start_with_api(test_microvm_with_ssh, vm_config_file):
48
"""Test if a microvm configured from file boots successfully."""
49
test_microvm = test_microvm_with_ssh
50
51
_configure_vm_from_json(test_microvm, vm_config_file)
52
test_microvm.spawn()
53
54
response = test_microvm.machine_cfg.get()
55
assert test_microvm.api_session.is_status_ok(response.status_code)
56
assert test_microvm.state == "Running"
57
58
# Validate full vm configuration.
59
response = test_microvm.full_cfg.get()
60
assert test_microvm.api_session.is_status_ok(response.status_code)
61
with open(vm_config_file) as json_file:
62
assert response.json() == json.load(json_file)
63
64
65
@pytest.mark.parametrize(
66
"vm_config_file",
67
["framework/vm_config.json"]
68
)
69
def test_config_start_no_api(test_microvm_with_ssh, vm_config_file):
70
"""Test microvm start when API server thread is disabled."""
71
test_microvm = test_microvm_with_ssh
72
73
_configure_vm_from_json(test_microvm, vm_config_file)
74
test_microvm.jailer.extra_args.update({'no-api': None})
75
76
test_microvm.spawn()
77
78
# Get Firecracker PID so we can check the names of threads.
79
firecracker_pid = test_microvm.jailer_clone_pid
80
81
# Get names of threads in Firecracker.
82
cmd = 'ps -T --no-headers -p {} | awk \'{{print $5}}\''.format(
83
firecracker_pid
84
)
85
86
# Retry running 'ps' in case it failed to list the firecracker process
87
# The regex matches any expression that contains 'firecracker' and does
88
# not contain 'fc_api'
89
retry_call(
90
utils.search_output_from_cmd,
91
fkwargs={
92
"cmd": cmd,
93
"find_regex": re.compile("^(?!.*fc_api)(?:.*)?firecracker",
94
re.DOTALL)
95
},
96
exceptions=RuntimeError,
97
tries=10,
98
delay=1)
99
100