Skip to content

Commit 2656f1b

Browse files
committed
add tests for multiple OS support to run when relevant files changed
Signed-off-by: Mary Dickson <[email protected]>
1 parent d000f95 commit 2656f1b

File tree

3 files changed

+351
-1
lines changed

3 files changed

+351
-1
lines changed

.github/workflows/docker-compose-test.yml

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,111 @@ name: Docker Compose Stack Test
33
on:
44
push:
55
branches: [ main ]
6+
paths:
7+
- 'static/quickstart/**'
8+
- 'tests/**'
9+
- '.github/workflows/docker-compose-test.yml'
10+
- 'docs/getting-started/**'
611
pull_request:
712
branches: [ main ]
13+
paths:
14+
- 'static/quickstart/**'
15+
- 'tests/**'
16+
- '.github/workflows/docker-compose-test.yml'
17+
- 'docs/getting-started/**'
818
workflow_dispatch:
919

1020
jobs:
21+
test-quickstart-scripts:
22+
runs-on: ${{ matrix.os }}
23+
timeout-minutes: 10
24+
name: Test quickstart scripts on ${{ matrix.os }}
25+
strategy:
26+
matrix:
27+
os: [ubuntu-latest, macos-latest, windows-latest]
28+
29+
steps:
30+
- name: Checkout code
31+
uses: actions/checkout@v4
32+
33+
- name: Install BATS and shellcheck (Ubuntu)
34+
if: runner.os == 'Linux'
35+
run: |
36+
sudo apt-get update
37+
sudo apt-get install -y bats shellcheck
38+
39+
- name: Install BATS and shellcheck (macOS)
40+
if: runner.os == 'macOS'
41+
run: |
42+
brew install bats-core shellcheck
43+
44+
- name: Install shellcheck (Windows)
45+
if: runner.os == 'Windows'
46+
run: |
47+
choco install shellcheck -y
48+
49+
- name: Install BATS via npm (Windows)
50+
if: runner.os == 'Windows'
51+
run: |
52+
npm install -g bats
53+
54+
- name: Run BATS tests (Linux - full suite)
55+
if: runner.os == 'Linux'
56+
run: |
57+
bats tests/quickstart.bats
58+
59+
- name: Run BATS tests (macOS - non-Docker tests)
60+
if: runner.os == 'macOS'
61+
shell: bash
62+
run: |
63+
# Run only non-Docker tests on macOS
64+
bats tests/quickstart.bats --filter "shellcheck"
65+
bats tests/quickstart.bats --filter "bash syntax"
66+
bats tests/quickstart.bats --filter "port validation"
67+
bats tests/quickstart.bats --filter "defines.*function"
68+
bats tests/quickstart.bats --filter "properly quotes"
69+
bats tests/quickstart.bats --filter "properly declares"
70+
bats tests/quickstart.bats --filter "detect platform"
71+
bats tests/quickstart.bats --filter "readable output"
72+
bats tests/quickstart.bats --filter "color codes"
73+
74+
- name: Run BATS tests (Windows - non-Docker tests)
75+
if: runner.os == 'Windows'
76+
shell: bash
77+
run: |
78+
# Run only non-Docker tests on Windows
79+
bats tests/quickstart.bats --filter "shellcheck"
80+
bats tests/quickstart.bats --filter "bash syntax"
81+
bats tests/quickstart.bats --filter "port validation"
82+
bats tests/quickstart.bats --filter "defines.*function"
83+
bats tests/quickstart.bats --filter "properly quotes"
84+
bats tests/quickstart.bats --filter "properly declares"
85+
bats tests/quickstart.bats --filter "detect platform"
86+
bats tests/quickstart.bats --filter "readable output"
87+
bats tests/quickstart.bats --filter "color codes"
88+
89+
- name: Test check.sh execution (macOS and Windows)
90+
if: runner.os != 'Linux'
91+
shell: bash
92+
run: |
93+
# Test that check.sh can execute and handle missing Docker gracefully
94+
echo "Testing check.sh on ${{ runner.os }}..."
95+
bash static/quickstart/check.sh || true
96+
echo "check.sh execution test completed"
97+
98+
- name: Test install.sh OS detection (macOS and Windows)
99+
if: runner.os != 'Linux'
100+
shell: bash
101+
run: |
102+
# Source the script and test OS detection function
103+
echo "Testing install.sh OS detection on ${{ runner.os }}..."
104+
bash -c 'source static/quickstart/install.sh && detect_os_and_arch' || echo "OS detection tested"
105+
11106
test-stack:
12107
runs-on: ubuntu-latest
13108
timeout-minutes: 15
14-
109+
needs: test-quickstart-scripts
110+
15111
steps:
16112
- name: Checkout code
17113
uses: actions/checkout@v4

tests/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Quickstart Script Tests
2+
3+
This directory contains BATS (Bash Automated Testing System) tests for the quickstart installation scripts.
4+
5+
## Prerequisites
6+
7+
### macOS
8+
```bash
9+
brew install bats-core shellcheck
10+
```
11+
12+
### Ubuntu/Debian
13+
```bash
14+
sudo apt-get update
15+
sudo apt-get install -y bats shellcheck
16+
```
17+
18+
### Fedora/RHEL
19+
```bash
20+
sudo dnf install -y bats ShellCheck
21+
```
22+
23+
## Running Tests Locally
24+
25+
**Important:** Run all BATS commands from the repository root directory, not from the `tests/` directory.
26+
27+
### Run all tests
28+
```bash
29+
bats tests/quickstart.bats
30+
```
31+
32+
### Run a specific test
33+
```bash
34+
bats tests/quickstart.bats --filter "check.sh passes shellcheck"
35+
```
36+
37+
### Run with verbose output
38+
```bash
39+
bats tests/quickstart.bats --tap
40+
```
41+
42+
### Run tests in parallel (requires bats-core 1.5+)
43+
```bash
44+
bats tests/quickstart.bats --jobs 4
45+
```
46+
47+
## Test Coverage
48+
49+
The test suite covers:
50+
51+
1. **Static Analysis** (tested on Linux, macOS, Windows)
52+
- Shellcheck compliance
53+
- Bash syntax validation
54+
- Variable quoting
55+
- Function definitions
56+
57+
2. **Script Execution Testing**
58+
- **Linux** (via Docker containers): Full end-to-end testing
59+
- Ubuntu 22.04, 24.04
60+
- Debian Bookworm
61+
- Fedora 39
62+
- Alpine Linux
63+
- **macOS**: Direct script execution, OS detection, graceful handling of missing Docker
64+
- **Windows**: Direct script execution, OS detection, graceful handling of missing Docker
65+
66+
3. **Functionality**
67+
- OS detection
68+
- Dependency checking
69+
- Port availability validation
70+
- Error handling
71+
72+
4. **Code Quality**
73+
- Output formatting
74+
- Color code usage
75+
76+
## Docker Requirements
77+
78+
Many tests use Docker to simulate different Linux distributions. Ensure Docker is running before executing the full test suite:
79+
80+
```bash
81+
docker ps
82+
```
83+
84+
## Continuous Integration
85+
86+
These tests run automatically in GitHub Actions on every push and pull request. See [.github/workflows/docker-compose-test.yml](../.github/workflows/docker-compose-test.yml) for the CI configuration.
87+
88+
## Writing New Tests
89+
90+
Follow the BATS test format:
91+
92+
```bash
93+
@test "description of what this tests" {
94+
run your_command
95+
[ "$status" -eq 0 ]
96+
[[ "$output" =~ "expected string" ]]
97+
}
98+
```
99+
100+
See the [BATS documentation](https://bats-core.readthedocs.io/) for more details.

tests/quickstart.bats

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#!/usr/bin/env bats
2+
3+
# Tests for quickstart scripts across different operating systems
4+
5+
setup() {
6+
# Get the script directory
7+
SCRIPT_DIR="$BATS_TEST_DIRNAME/../static/quickstart"
8+
}
9+
10+
# Shellcheck validation tests
11+
@test "check.sh passes shellcheck" {
12+
shellcheck "$SCRIPT_DIR/check.sh"
13+
}
14+
15+
@test "install.sh passes shellcheck" {
16+
shellcheck "$SCRIPT_DIR/install.sh"
17+
}
18+
19+
# OS detection tests
20+
@test "check.sh detects OS on Ubuntu 22.04" {
21+
run docker run --rm -v "$PWD:/work" -w /work ubuntu:22.04 bash -c "
22+
apt-get update -qq && apt-get install -y -qq lsof curl >/dev/null 2>&1
23+
bash static/quickstart/check.sh
24+
"
25+
[ "$status" -eq 0 ] || [ "$status" -eq 1 ] # Script may exit 1 if ports in use
26+
}
27+
28+
@test "check.sh detects OS on Ubuntu 24.04" {
29+
run docker run --rm -v "$PWD:/work" -w /work ubuntu:24.04 bash -c "
30+
apt-get update -qq && apt-get install -y -qq lsof curl >/dev/null 2>&1
31+
bash static/quickstart/check.sh
32+
"
33+
[ "$status" -eq 0 ] || [ "$status" -eq 1 ]
34+
}
35+
36+
@test "check.sh detects OS on Debian Bookworm" {
37+
run docker run --rm -v "$PWD:/work" -w /work debian:bookworm bash -c "
38+
apt-get update -qq && apt-get install -y -qq lsof curl >/dev/null 2>&1
39+
bash static/quickstart/check.sh
40+
"
41+
[ "$status" -eq 0 ] || [ "$status" -eq 1 ]
42+
}
43+
44+
@test "check.sh detects OS on Fedora 39" {
45+
run docker run --rm -v "$PWD:/work" -w /work fedora:39 bash -c "
46+
dnf install -y -q lsof curl >/dev/null 2>&1
47+
bash static/quickstart/check.sh
48+
"
49+
[ "$status" -eq 0 ] || [ "$status" -eq 1 ]
50+
}
51+
52+
@test "check.sh detects OS on Alpine Linux" {
53+
run docker run --rm -v "$PWD:/work" -w /work alpine:latest sh -c "
54+
apk add --no-cache bash lsof curl >/dev/null 2>&1
55+
bash static/quickstart/check.sh
56+
"
57+
[ "$status" -eq 0 ] || [ "$status" -eq 1 ]
58+
}
59+
60+
# Script syntax validation
61+
@test "check.sh has valid bash syntax" {
62+
bash -n "$SCRIPT_DIR/check.sh"
63+
}
64+
65+
@test "install.sh has valid bash syntax" {
66+
bash -n "$SCRIPT_DIR/install.sh"
67+
}
68+
69+
# Dependency checking tests
70+
@test "check.sh detects missing docker" {
71+
run docker run --rm -v "$PWD:/work" -w /work ubuntu:22.04 bash -c "
72+
apt-get update -qq >/dev/null 2>&1
73+
PATH=/usr/bin:/bin bash static/quickstart/check.sh
74+
"
75+
echo "Output: $output"
76+
[[ "$output" =~ "docker" ]] || [[ "$output" =~ "Docker" ]]
77+
}
78+
79+
@test "check.sh detects docker when installed" {
80+
skip "Requires docker-in-docker setup"
81+
}
82+
83+
# Port checking tests
84+
@test "check.sh port validation works" {
85+
run bash -c "grep -q 'check_port.*8080' $SCRIPT_DIR/check.sh"
86+
[ "$status" -eq 0 ]
87+
88+
run bash -c "grep -q 'check_port.*8443' $SCRIPT_DIR/check.sh"
89+
[ "$status" -eq 0 ]
90+
91+
run bash -c "grep -q 'check_port.*9443' $SCRIPT_DIR/check.sh"
92+
[ "$status" -eq 0 ]
93+
}
94+
95+
# Script structure tests
96+
@test "check.sh defines print_status function" {
97+
grep -q "print_status()" "$SCRIPT_DIR/check.sh"
98+
}
99+
100+
@test "check.sh defines print_warning function" {
101+
grep -q "print_warning()" "$SCRIPT_DIR/check.sh"
102+
}
103+
104+
@test "check.sh defines check_port function" {
105+
grep -q "check_port()" "$SCRIPT_DIR/check.sh"
106+
}
107+
108+
@test "install.sh defines install_otdfctl function" {
109+
grep -q "install_otdfctl()" "$SCRIPT_DIR/install.sh"
110+
}
111+
112+
# Variable quoting tests (shellcheck compliance)
113+
@test "check.sh properly quotes variables in print_status" {
114+
grep -q 'if \[ "$1" -eq 0 \]' "$SCRIPT_DIR/check.sh"
115+
}
116+
117+
@test "check.sh properly quotes variables in check_port" {
118+
grep -q 'lsof -Pi :"$1"' "$SCRIPT_DIR/check.sh"
119+
}
120+
121+
@test "install.sh properly declares TEMP_DIR variable" {
122+
grep -q 'local TEMP_DIR$' "$SCRIPT_DIR/install.sh"
123+
grep -q 'TEMP_DIR=$(mktemp -d)' "$SCRIPT_DIR/install.sh"
124+
}
125+
126+
# Install script validation
127+
@test "install.sh can detect platform architecture" {
128+
run bash -c "source $SCRIPT_DIR/install.sh; detect_os_and_arch"
129+
echo "Output: $output"
130+
# Should not error out
131+
[ "$status" -eq 0 ] || [ "$status" -eq 1 ]
132+
}
133+
134+
# Error handling tests
135+
@test "check.sh handles missing lsof gracefully on Ubuntu" {
136+
run docker run --rm -v "$PWD:/work" -w /work ubuntu:22.04 bash -c "
137+
bash static/quickstart/check.sh
138+
"
139+
# Should exit with error but not crash
140+
[[ "$output" =~ "lsof" ]] || [ "$status" -ne 127 ]
141+
}
142+
143+
# Output format tests
144+
@test "check.sh produces readable output" {
145+
run bash "$SCRIPT_DIR/check.sh" 2>&1 || true
146+
# Should have some output
147+
[ -n "$output" ]
148+
}
149+
150+
@test "check.sh uses color codes" {
151+
grep -q "GREEN=" "$SCRIPT_DIR/check.sh"
152+
grep -q "RED=" "$SCRIPT_DIR/check.sh"
153+
grep -q "YELLOW=" "$SCRIPT_DIR/check.sh"
154+
}

0 commit comments

Comments
 (0)