Path: blob/main/tests/integration_tests/functional/test_cmd_line_start.py
1958 views
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.1# SPDX-License-Identifier: Apache-2.02"""Tests microvm start with configuration file as command line parameter."""34import json5import os6import re78from retry.api import retry_call910import pytest1112import framework.utils as utils131415def _configure_vm_from_json(test_microvm, vm_config_file):16"""Configure a microvm using a file sent as command line parameter.1718Create resources needed for the configuration of the microvm and19set as configuration file a copy of the file that was passed as20parameter to this helper function.21"""22test_microvm.create_jailed_resource(test_microvm.kernel_file,23create_jail=True)24test_microvm.create_jailed_resource(test_microvm.rootfs_file,25create_jail=True)2627# vm_config_file is the source file that keeps the desired vmm28# configuration. vm_config_path is the configuration file we29# create inside the jail, such that it can be accessed by30# firecracker after it starts.31vm_config_path = os.path.join(test_microvm.path,32os.path.basename(vm_config_file))33with open(vm_config_file) as f1:34with open(vm_config_path, "w") as f2:35for line in f1:36f2.write(line)37test_microvm.create_jailed_resource(vm_config_path, create_jail=True)38test_microvm.jailer.extra_args = {'config-file': os.path.basename(39vm_config_file)}404142@pytest.mark.parametrize(43"vm_config_file",44["framework/vm_config.json"]45)46def test_config_start_with_api(test_microvm_with_ssh, vm_config_file):47"""Test if a microvm configured from file boots successfully."""48test_microvm = test_microvm_with_ssh4950_configure_vm_from_json(test_microvm, vm_config_file)51test_microvm.spawn()5253response = test_microvm.machine_cfg.get()54assert test_microvm.api_session.is_status_ok(response.status_code)55assert test_microvm.state == "Running"5657# Validate full vm configuration.58response = test_microvm.full_cfg.get()59assert test_microvm.api_session.is_status_ok(response.status_code)60with open(vm_config_file) as json_file:61assert response.json() == json.load(json_file)626364@pytest.mark.parametrize(65"vm_config_file",66["framework/vm_config.json"]67)68def test_config_start_no_api(test_microvm_with_ssh, vm_config_file):69"""Test microvm start when API server thread is disabled."""70test_microvm = test_microvm_with_ssh7172_configure_vm_from_json(test_microvm, vm_config_file)73test_microvm.jailer.extra_args.update({'no-api': None})7475test_microvm.spawn()7677# Get Firecracker PID so we can check the names of threads.78firecracker_pid = test_microvm.jailer_clone_pid7980# Get names of threads in Firecracker.81cmd = 'ps -T --no-headers -p {} | awk \'{{print $5}}\''.format(82firecracker_pid83)8485# Retry running 'ps' in case it failed to list the firecracker process86# The regex matches any expression that contains 'firecracker' and does87# not contain 'fc_api'88retry_call(89utils.search_output_from_cmd,90fkwargs={91"cmd": cmd,92"find_regex": re.compile("^(?!.*fc_api)(?:.*)?firecracker",93re.DOTALL)94},95exceptions=RuntimeError,96tries=10,97delay=1)9899100