Skip to content

Commit 4a8837d

Browse files
Added automation of virsh commands
This PR refactors the Virsh Commands into a modular suite of independent KVM test cases. Key Changes: Modular Architecture: Split the original script into 5 standalone test cases: Kvm-Setup: Defines and starts the VM. Kvm-Reboot: Verifies reboot functionality. Kvm-Suspend: Verifies suspend/pause functionality. Kvm-Resume: Verifies resume functionality. Kvm-UnDefine: Verifies destroy and undefine functionality. Common Library: Created kvm_common.sh to handle shared logic (define, start, clean, state checks) and reduce code duplication. Robust State Verification: Replaced simple exit code checks with a check_vm_state function that parses virsh domstate output to ensure the VM is actually in the expected state (e.g., "running", "paused", "shut off"). Independence: Each test script now manages its own lifecycle (Setup -> Test -> UnDefine), allowing them to be run individually or in any order. Reporting: Added .res file generation (PASS/FAIL) and LAVA-compatible .yaml metadata for automation integration. Signed-off-by: Rohan Dutta <rohadutt@qti.qualcomm.com>
1 parent 89e3067 commit 4a8837d

16 files changed

Lines changed: 603 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
metadata:
2+
name: kvm-reboot
3+
format: "Lava-Test Test Definition 1.0"
4+
description: "Reboot an active Gunyah KVM Virtual Machine"
5+
os:
6+
- linux
7+
scope:
8+
- functional
9+
10+
run:
11+
steps:
12+
- REPO_PATH=$PWD
13+
- cd Runner/suites/Kernel/Virtualization/Kvm-Reboot
14+
- ./run.sh || true
15+
- $REPO_PATH/Runner/utils/send-to-lava.sh Kvm-Reboot.res || true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Kvm-Reboot Test
2+
© Qualcomm Technologies, Inc. and/or its subsidiaries.
3+
SPDX-License-Identifier: BSD-3-Clause-Clear
4+
5+
## Overview
6+
This test validates the reboot functionality of the Guest VM. It issues a reboot command via `virsh` and ensures the VM returns to a running state.
7+
8+
### The test checks for:
9+
- Successful execution of `virsh reboot`.
10+
- VM returning to **"running"** state.
11+
12+
## Usage
13+
### Quick Example
14+
```bash
15+
cd /var/Runner/suites/Kernel/Virtualization/Kvm-Reboot
16+
./run.sh
17+
```
18+
19+
### Result Format
20+
Test result will be saved in Kvm-Reboot.res.
21+
22+
### Output
23+
Kvm-Reboot PASS OR Kvm-Reboot FAIL
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4+
# SPDX-License-Identifier: BSD-3-Clause-Clear
5+
6+
# Robustly find and source init_env
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
SEARCH_PATH="$SCRIPT_DIR"
9+
LIB_PATH=""
10+
while [ "$SEARCH_PATH" != "/" ]; do
11+
if [ -f "$SEARCH_PATH/utils/kvm_common.sh" ]; then
12+
LIB_PATH="$SEARCH_PATH/utils/kvm_common.sh"
13+
break
14+
fi
15+
SEARCH_PATH=$(dirname "$SEARCH_PATH")
16+
done
17+
18+
if [ -f "$LIB_PATH" ]; then
19+
# shellcheck disable=SC1090
20+
. "$LIB_PATH"
21+
else
22+
echo "[ERROR] Lib not found"
23+
exit 1
24+
fi
25+
26+
TESTNAME="Kvm-Reboot"
27+
RES_FILE="${TESTNAME}.res"
28+
rm -f "$RES_FILE"
29+
30+
# Clean up old stdout logs from previous runs
31+
rm -f *_stdout_*.log
32+
33+
log_info "----------- KVM Reboot -----------"
34+
35+
if virsh list --all | grep -q -w "$VM_NAME"; then
36+
log_info "Existing VM instance found. Cleaning up..."
37+
vm_clean
38+
fi
39+
40+
vm_define && vm_start
41+
if [ $? -ne 0 ]; then echo "$TESTNAME FAIL" > "$RES_FILE"; exit 1; fi
42+
43+
# Test: Reboot
44+
log_info "Rebooting $VM_NAME"
45+
virsh reboot "$VM_NAME"
46+
sleep 5
47+
48+
# Verify Running
49+
check_vm_state "$VM_NAME" "running"
50+
if [ $? -eq 0 ]; then
51+
log_pass "VM rebooted successfully."
52+
echo "$TESTNAME PASS" > "$RES_FILE"
53+
else
54+
log_fail "VM not running after reboot."
55+
echo "$TESTNAME FAIL" > "$RES_FILE"
56+
fi
57+
58+
# Cleanup
59+
vm_clean
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
metadata:
2+
name: kvm-resume
3+
format: "Lava-Test Test Definition 1.0"
4+
description: "Resume a paused Gunyah KVM Virtual Machine"
5+
os:
6+
- linux
7+
scope:
8+
- functional
9+
10+
run:
11+
steps:
12+
- REPO_PATH=$PWD
13+
- cd Runner/suites/Kernel/Virtualization/Kvm-Resume
14+
- ./run.sh || true
15+
- $REPO_PATH/Runner/utils/send-to-lava.sh Kvm-Resume.res || true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Kvm-Resume Test
2+
© Qualcomm Technologies, Inc. and/or its subsidiaries.
3+
SPDX-License-Identifier: BSD-3-Clause-Clear
4+
5+
## Overview
6+
This test validates that a suspended VM can be resumed to active execution.
7+
8+
### The test checks for:
9+
- Successful execution of `virsh resume`.
10+
- VM state transition back to **"running"**.
11+
12+
## Usage
13+
### Quick Example
14+
```bash
15+
cd /var/Runner/suites/Kernel/Virtualization/Kvm-Resume
16+
./run.sh
17+
```
18+
19+
### Result Format
20+
Test result will be saved in Kvm-Reboot.res.
21+
22+
### Output
23+
Kvm-Reboot PASS OR Kvm-Reboot FAIL
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/bash
2+
3+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4+
# SPDX-License-Identifier: BSD-3-Clause-Clear
5+
6+
# Robustly find and source init_env
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
SEARCH_PATH="$SCRIPT_DIR"
9+
LIB_PATH=""
10+
while [ "$SEARCH_PATH" != "/" ]; do
11+
if [ -f "$SEARCH_PATH/utils/kvm_common.sh" ]; then
12+
LIB_PATH="$SEARCH_PATH/utils/kvm_common.sh"
13+
break
14+
fi
15+
SEARCH_PATH=$(dirname "$SEARCH_PATH")
16+
done
17+
18+
if [ -f "$LIB_PATH" ]; then
19+
# shellcheck disable=SC1090
20+
. "$LIB_PATH"
21+
else
22+
echo "[ERROR] Lib not found"
23+
exit 1
24+
fi
25+
26+
TESTNAME="Kvm-Resume"
27+
RES_FILE="${TESTNAME}.res"
28+
rm -f "$RES_FILE"
29+
30+
# Clean up old stdout logs from previous runs
31+
rm -f *_stdout_*.log
32+
33+
log_info "----------- KVM Resume -----------"
34+
35+
if virsh list --all | grep -q -w "$VM_NAME"; then
36+
log_info "Existing VM instance found. Cleaning up..."
37+
vm_clean
38+
fi
39+
40+
vm_define && vm_start
41+
if [ $? -ne 0 ]; then echo "$TESTNAME FAIL" > "$RES_FILE"; exit 1; fi
42+
43+
# Pre-req: Must be suspended first
44+
log_info "Suspending VM for resume test..."
45+
virsh suspend "$VM_NAME"
46+
sleep 2
47+
48+
# Test: Resume
49+
log_info "Resuming $VM_NAME"
50+
virsh resume "$VM_NAME"
51+
52+
# Verify Running
53+
check_vm_state "$VM_NAME" "running"
54+
if [ $? -eq 0 ]; then
55+
log_pass "VM resumed successfully."
56+
echo "$TESTNAME PASS" > "$RES_FILE"
57+
else
58+
log_fail "VM failed to resume."
59+
echo "$TESTNAME FAIL" > "$RES_FILE"
60+
fi
61+
62+
# Cleanup
63+
vm_clean
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
metadata:
2+
name: kvm-setup
3+
format: "Lava-Test Test Definition 1.0"
4+
description: "Setup and Start Gunyah KVM Virtual Machine"
5+
os:
6+
- linux
7+
scope:
8+
- functional
9+
10+
run:
11+
steps:
12+
- REPO_PATH=$PWD
13+
- cd Runner/suites/Kernel/Virtualization/Kvm-Setup
14+
- ./run.sh || true
15+
- $REPO_PATH/Runner/utils/send-to-lava.sh Kvm-Setup.res || true
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Kvm-Setup Test
2+
© Qualcomm Technologies, Inc. and/or its subsidiaries.
3+
SPDX-License-Identifier: BSD-3-Clause-Clear
4+
5+
## Overview
6+
This test case initializes the KVM environment by defining the Virtual Machine using an XML configuration and starting the domain. It verifies that the VM transitions to the `running` state.
7+
8+
### The test checks for:
9+
- Successful **definition** of the VM from `vm.xml`.
10+
- Successful **start** command execution.
11+
- Verification that the VM state is **"running"**.
12+
13+
## Usage
14+
### Instructions:
15+
1. Ensure the `vm.xml` and guest image files are present in `/var/gunyah/`.
16+
2. Navigate to the test directory.
17+
3. Run the script.
18+
19+
### Quick Example
20+
```bash
21+
cd /var/Runner/suites/Kernel/Virtualization/Kvm-Setup
22+
./run.sh
23+
```
24+
25+
### Prerequisites
26+
1. virsh tool must be installed.
27+
2. Gunyah hypervisor must be enabled.
28+
3. Root access is required.
29+
30+
### Result Format
31+
Test result will be saved in Kvm-Setup.res.
32+
33+
### Output
34+
Kvm-Setup PASS OR Kvm-Setup FAIL
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
3+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4+
# SPDX-License-Identifier: BSD-3-Clause-Clear
5+
6+
# Robustly find and source init_env
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
SEARCH_PATH="$SCRIPT_DIR"
9+
LIB_PATH=""
10+
while [ "$SEARCH_PATH" != "/" ]; do
11+
if [ -f "$SEARCH_PATH/utils/kvm_common.sh" ]; then
12+
LIB_PATH="$SEARCH_PATH/utils/kvm_common.sh"
13+
break
14+
fi
15+
SEARCH_PATH=$(dirname "$SEARCH_PATH")
16+
done
17+
18+
if [ -f "$LIB_PATH" ]; then
19+
# shellcheck disable=SC1090
20+
. "$LIB_PATH"
21+
else
22+
echo "[ERROR] Lib not found"
23+
exit 1
24+
fi
25+
26+
# Define Test Metadata
27+
TESTNAME="Kvm-Setup"
28+
RES_FILE="${TESTNAME}.res"
29+
30+
# Clean up old stdout logs from previous runs
31+
rm -f *_stdout_*.log
32+
33+
# Execution
34+
log_info "----------- KVM Setup: Define and Start -----------"
35+
36+
# Clean up any existing result file
37+
rm -f "$RES_FILE"
38+
39+
# Define VM
40+
if virsh list --all | grep -q -w "$VM_NAME"; then
41+
log_info "Existing VM instance found. Cleaning up..."
42+
vm_clean
43+
fi
44+
45+
# 2. Define
46+
vm_define
47+
if [ $? -ne 0 ]; then echo "$TESTNAME FAIL" > "$RES_FILE"; exit 1; fi
48+
49+
# 3. Start
50+
vm_start
51+
if [ $? -ne 0 ]; then echo "$TESTNAME FAIL" > "$RES_FILE"; exit 1; fi
52+
53+
# 4. Verify
54+
check_vm_state "$VM_NAME" "running"
55+
if [ $? -eq 0 ]; then
56+
log_pass "VM is running."
57+
echo "$TESTNAME PASS" > "$RES_FILE"
58+
else
59+
log_fail "VM failed to reach 'running' state."
60+
echo "$TESTNAME FAIL" > "$RES_FILE"
61+
exit 1
62+
fi
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
metadata:
2+
name: kvm-suspend
3+
format: "Lava-Test Test Definition 1.0"
4+
description: "Suspend a running Gunyah KVM Virtual Machine"
5+
os:
6+
- linux
7+
scope:
8+
- functional
9+
10+
run:
11+
steps:
12+
- REPO_PATH=$PWD
13+
- cd Runner/suites/Kernel/Virtualization/Kvm-Suspend
14+
- ./run.sh || true
15+
- $REPO_PATH/Runner/utils/send-to-lava.sh Kvm-Suspend.res || true

0 commit comments

Comments
 (0)