Skip to content

Commit 381bdf3

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 381bdf3

File tree

16 files changed

+488
-0
lines changed

16 files changed

+488
-0
lines changed
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
[ -f "$LIB_PATH" ] && . "$LIB_PATH" || { echo "[ERROR] Lib not found"; exit 1; }
18+
19+
20+
TESTNAME="Kvm-Reboot"
21+
RES_FILE="${TESTNAME}.res"
22+
rm -f "$RES_FILE"
23+
24+
log_info "----------- KVM Reboot -----------"
25+
26+
log_info "Rebooting VM: $VM_NAME"
27+
virsh reboot "$VM_NAME"
28+
if [ $? -ne 0 ]; then
29+
log_fail "Failed to issue reboot command."
30+
echo "$TESTNAME FAIL" > "$RES_FILE"
31+
exit 1
32+
fi
33+
34+
# Wait a moment for transition
35+
sleep 5
36+
37+
# Check if it is still running (or came back up)
38+
check_vm_state "$VM_NAME" "running"
39+
if [ $? -eq 0 ]; then
40+
log_pass "VM rebooted and is running."
41+
echo "$TESTNAME PASS" > "$RES_FILE"
42+
else
43+
log_fail "VM is not running after reboot."
44+
echo "$TESTNAME FAIL" > "$RES_FILE"
45+
exit 1
46+
fi
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
[ -f "$LIB_PATH" ] && . "$LIB_PATH" || { echo "[ERROR] Lib not found"; exit 1; }
18+
19+
TESTNAME="Kvm-Resume"
20+
RES_FILE="${TESTNAME}.res"
21+
rm -f "$RES_FILE"
22+
23+
log_info "----------- KVM Resume -----------"
24+
25+
log_info "Resuming VM: $VM_NAME"
26+
virsh resume "$VM_NAME"
27+
if [ $? -ne 0 ]; then
28+
log_fail "Failed to issue resume command."
29+
echo "$TESTNAME FAIL" > "$RES_FILE"
30+
exit 1
31+
fi
32+
33+
check_vm_state "$VM_NAME" "running"
34+
if [ $? -eq 0 ]; then
35+
log_pass "VM successfully resumed."
36+
echo "$TESTNAME PASS" > "$RES_FILE"
37+
else
38+
log_fail "VM failed to return to 'running' state."
39+
echo "$TESTNAME FAIL" > "$RES_FILE"
40+
exit 1
41+
fi
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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
[ -f "$LIB_PATH" ] && . "$LIB_PATH" || { echo "[ERROR] utils/kvm_common.sh not found"; exit 1; }
18+
19+
# Define Test Metadata
20+
TESTNAME="Kvm-Setup"
21+
RES_FILE="${TESTNAME}.res"
22+
23+
# Execution
24+
log_info "----------- KVM Setup: Define and Start -----------"
25+
26+
# Clean up any existing result file
27+
rm -f "$RES_FILE"
28+
29+
# Define VM
30+
log_info "Defining VM from XML: $XML_FILE"
31+
virsh define "$XML_FILE"
32+
if [ $? -ne 0 ]; then
33+
log_fail "Failed to define VM."
34+
echo "$TESTNAME FAIL" > "$RES_FILE"
35+
exit 1
36+
fi
37+
38+
# Start VM
39+
log_info "Starting VM: $VM_NAME"
40+
virsh start "$VM_NAME"
41+
if [ $? -ne 0 ]; then
42+
log_fail "Failed to start VM."
43+
echo "$TESTNAME FAIL" > "$RES_FILE"
44+
exit 1
45+
fi
46+
47+
# Check State
48+
check_vm_state "$VM_NAME" "running"
49+
if [ $? -eq 0 ]; then
50+
log_pass "VM is running."
51+
echo "$TESTNAME PASS" > "$RES_FILE"
52+
else
53+
log_fail "VM failed to reach 'running' state."
54+
echo "$TESTNAME FAIL" > "$RES_FILE"
55+
exit 1
56+
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)