Path: blob/main/tests/integration_tests/functional/test_shut_down.py
1958 views
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1# SPDX-License-Identifier: Apache-2.02"""Tests scenarios for shutting down Firecracker/VM."""3import os4import time5import json6import platform78import framework.utils as utils910import host_tools.logging as log_tools11import host_tools.network as net_tools # pylint: disable=import-error121314def test_reboot(test_microvm_with_ssh, network_config):15"""Test reboot from guest kernel."""16test_microvm = test_microvm_with_ssh17test_microvm.jailer.daemonize = False18test_microvm.spawn()1920# We don't need to monitor the memory for this test because we are21# just rebooting and the process dies before pmap gets the RSS.22test_microvm.memory_monitor = None2324# Set up the microVM with 4 vCPUs, 256 MiB of RAM, 0 network ifaces, and25# a root file system with the rw permission. The network interfaces is26# added after we get a unique MAC and IP.27test_microvm.basic_config(vcpu_count=4)28_tap, _, _ = test_microvm.ssh_network_config(network_config, '1')2930# Configure metrics system.31metrics_fifo_path = os.path.join(test_microvm.path, 'metrics_fifo')32metrics_fifo = log_tools.Fifo(metrics_fifo_path)33response = test_microvm.metrics.put(34metrics_path=test_microvm.create_jailed_resource(metrics_fifo.path)35)36assert test_microvm.api_session.is_status_no_content(response.status_code)3738test_microvm.start()3940# Get Firecracker PID so we can count the number of threads.41firecracker_pid = test_microvm.jailer_clone_pid4243# Get number of threads in Firecracker44cmd = 'ps -o nlwp {} | tail -1 | awk \'{{print $1}}\''.format(45firecracker_pid46)47_, stdout, _ = utils.run_cmd(cmd)48nr_of_threads = stdout.rstrip()49assert int(nr_of_threads) == 65051# Consume existing metrics52lines = metrics_fifo.sequential_reader(100)53assert len(lines) == 154# Rebooting Firecracker sends an exit event and should gracefully kill.55# the instance.56ssh_connection = net_tools.SSHConnection(test_microvm.ssh_config)5758ssh_connection.execute_command("reboot")5960while True:61# Pytest's timeout will kill the test even if the loop doesn't exit.62try:63os.kill(firecracker_pid, 0)64time.sleep(0.01)65except OSError:66break6768# Consume existing metrics69lines = metrics_fifo.sequential_reader(100)70assert len(lines) == 17172if platform.machine() != "x86_64":73log_data = test_microvm.log_data74assert "Received KVM_SYSTEM_EVENT: type: 2, event: 0" in log_data75assert "Vmm is stopping." in log_data7677# Make sure that the FC process was not killed by a seccomp fault78assert json.loads(lines[0])["seccomp"]["num_faults"] == 0798081