Skip to content

Commit 3c6c6a8

Browse files
authored
Merge pull request #150 from Azure/refactor-unit-tests
Agnostic Unit Test Script and Disk Test Fixes
2 parents fe04b26 + 7c11811 commit 3c6c6a8

4 files changed

Lines changed: 95 additions & 8 deletions

File tree

customTests/azure_nvme_count.nhc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22

3-
source /etc/nhc/scripts/azure_common.nhc
3+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4+
source "${SCRIPT_DIR}/azure_common.nhc"
45

56
function get_device_count() {
67
DIR="$1"

test/README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
# Unit Test Scripts #
1+
# Tests Scripts #
22

3-
The [run_tests script](./unit-tests/run_tests.sh) can be used to run unit tests. The unit test use [Bash Automated Testing System](https://github.com/bats-core/bats-core) or bats for short.
3+
| Test Suite | Script Command |
4+
| ------------- | ------------- |
5+
| Integration Tests | [./unit-tests/run_tests.sh](./unit-tests/run_tests.sh) |
6+
| Unit Tests | [./unit-tests/run_tests_agnostic.sh](./unit-tests/run_tests_agnostic.sh) |
47

5-
The run script will attempt to install bats if it is not found.
8+
The unit test use [Bash Automated Testing System](https://github.com/bats-core/bats-core) or bats for short.
9+
10+
The [run_tests script](./unit-tests/run_tests.sh) can be used to run integration tests. These are expected to run on HPC hardware. The run_tests script will attempt to install bats if it is not found.
11+
12+
The [run_tests_agnostic script](./unit-tests/run_tests_agnostic.sh) doesn't require HPC hardware.
613

714
## Usage ##
815

9-
- ```./unit-tests/run_tests.sh```
16+
- ```./unit-tests/run_tests.sh $NHC_PATH_OPTIONAL```
17+
- ```./unit-tests/run_tests_agnostic.sh -d $NHC_PATH_OPTIONAL```
1018

1119
## Expected output ##
1220

test/unit-tests/nhc-hardware-test.sh

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
#!/usr/bin/env bats
22

3-
source $AZ_NHC_ROOT/customTests/azure_common.nhc
4-
source $AZ_NHC_ROOT/test/unit-tests/nhc-test-common.sh
3+
# Hardware tests for Azure NHC that are not GPU or CPU specific
54

5+
source $NHC_DIR/customTests/azure_common.nhc
6+
source $NHC_DIR/test/unit-tests/nhc-test-common.sh
7+
8+
# Enumerate over currently tested hardware checks
69
hardware_test=( "azure_nvme_count.nhc" )
710

11+
# Load the hardware checks
812
for check in "${hardware_test[@]}" ; do
9-
source $AZ_NHC_ROOT/customTests/$check
13+
source $NHC_DIR/customTests/$check
1014
done
1115

16+
## NVME Count Unit Tests
17+
18+
# Given a directory and a count, create mock NVMe devices as empty files
19+
# Ex -
20+
# Input - /tmp/tmp.mNtJgqCZC5 4
21+
# Output -
22+
# /tmp/tmp.mNtJgqCZC5/dev/nvme3n1
23+
# /tmp/tmp.mNtJgqCZC5/dev/nvme1n1
24+
# /tmp/tmp.mNtJgqCZC5/dev/nvme0n1
25+
# /tmp/tmp.mNtJgqCZC5/dev/nvme2n1
26+
1227
create_mock_nvme_devices() {
1328
DIR="$1"
1429
local count=$2
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/sh
2+
3+
# SKU agnostic Unit Tests
4+
# This script is designed to run unit tests that are not specific to any SKU.
5+
6+
print_help() {
7+
echo "Usage: $0 [OPTIONS]"
8+
echo "Options:"
9+
echo " -h, --help Show this help message"
10+
echo " -d, --dir DIR Set the NHC directory"
11+
}
12+
13+
# Parse options using getopt
14+
options=$(getopt -o hd: -l help,dir: -- "$@")
15+
if [ $? -ne 0 ]; then
16+
print_help
17+
exit 1
18+
fi
19+
20+
eval set -- "$options"
21+
22+
while true; do
23+
case "$1" in
24+
-h|--help)
25+
print_help
26+
exit 0
27+
;;
28+
-d|--dir)
29+
NHC_DIR="$2"
30+
shift 2
31+
;;
32+
--)
33+
shift
34+
break
35+
;;
36+
*)
37+
echo "Unknown option: $1"
38+
print_help
39+
exit 1
40+
;;
41+
esac
42+
done
43+
44+
# Check if NHC_DIR is set and is a directory
45+
if [ -n "$NHC_DIR" ] && [ ! -d "$NHC_DIR" ]; then
46+
echo "Error: Directory '$NHC_DIR' does not exist or is not a directory."
47+
exit 1
48+
fi
49+
50+
# Set NHC_DIR if empty
51+
script_path="$(realpath "$0")"
52+
parent_dir="$(dirname "$script_path")"
53+
if [ -z "$NHC_DIR" ]; then
54+
NHC_DIR="$(realpath "$parent_dir/../..")"
55+
echo "NHC_DIR not set. Using default: $NHC_DIR"
56+
fi
57+
58+
# Get Common Functions for Testing
59+
export NHC_DIR
60+
source "$NHC_DIR/test/unit-tests/nhc-test-common.sh"
61+
62+
# Run Unit Tests
63+
bats --pretty "$NHC_DIR/test/unit-tests/nhc-hardware-test.sh"

0 commit comments

Comments
 (0)