Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/admin_socket_dump.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: admin_socket_dump Tests

on:
push:
paths:
- 'admin_socket_dump/**'
pull_request:
paths:
- 'admin_socket_dump/**'

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install bats
run: |
sudo apt-get update
sudo apt-get install -y bats

- name: Run test suite
run: |
cd admin_socket_dump
test/test_suite.sh
57 changes: 57 additions & 0 deletions admin_socket_dump/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# What it is

Two implementations (Python and Shell) that extract the Varnish admin socket configuration given a work directory:
- Management socket addresses and ports (from `-T` argument files)
- Secret file path (from `-S` argument files)

Both implementations produce identical JSON output for non-errors and handle edge cases gracefully.

# How it is built

No need to build anything as it's `shell` and `python`, but you'll need:
- Either:
- Python 3.6+ (for the Python implementation)
- A POSIX-compliant shell like `sh` (for the shell implementation) and standard UNIX utilities: `awk`, `tr`, `cat`, `printf`
- `bats` (for running tests)


# How it works

Both scripts accept a directory path (defaults to `/var/lib/varnish/varnishd`) and output JSON:

```bash
# Python
python3 varnish_socket.py [directory]

# Shell
sh varnish_socket.sh [directory]
```

Output format:

```json
{
"secret": "/path/to/secret",
"socks": [
{
"addr": "127.0.0.1",
"port": 8080
}
]
}
```

Error cases (missing index, unreadable files) return JSON with an `"error"` field and exit code 1.

# How it is tested

Run tests for both implementations
```bash
test/test_suite.sh
```

Or test a specific implementation
```bash
VARNISH_SOCKET_TOOL=python test/test_suite.sh
VARNISH_SOCKET_TOOL=shell test/test_suite.sh
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/var/secret
Empty file.
3 changes: 3 additions & 0 deletions admin_socket_dump/test/assets/empty_socks/_.vsm_mgt/_.index
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Both present but socks file is empty
+ _.Arg.t_file 0 32 Arg -T
+ _.Arg.s_file 0 40 Arg -S
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/var/secret
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
not_an_ip_port
127.0.0.1
just_one_value
192.168.1.1 9000
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Index with malformed socks data
+ _.Arg.t_file 0 32 Arg -T
+ _.Arg.s_file 0 40 Arg -S
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
127.0.0.1 8080
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Index with -S reference but file doesn't exist
+ _.Arg.t_file 0 32 Arg -T
+ _.Arg.s_file 0 40 Arg -S
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/etc/varnish/secret
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Index with -T reference but file doesn't exist
+ _.Arg.t_file 0 32 Arg -T
+ _.Arg.s_file 0 40 Arg -S
2 changes: 2 additions & 0 deletions admin_socket_dump/test/assets/neither/_.vsm_mgt/_.index
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Neither -T nor -S present
+ _.OtherStuff 0 100 Other
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
192.168.1.1 9000
2 changes: 2 additions & 0 deletions admin_socket_dump/test/assets/no_secret/_.vsm_mgt/_.index
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Only -T, no -S
+ _.Arg.t_file 0 32 Arg -T
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/etc/varnish/secret
2 changes: 2 additions & 0 deletions admin_socket_dump/test/assets/no_socks/_.vsm_mgt/_.index
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Only -S, no -T
+ _.Arg.s_file 0 40 Arg -S
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tmp/test.secret
2 changes: 2 additions & 0 deletions admin_socket_dump/test/assets/normal/_.vsm_mgt/_.Arg.t_file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
127.0.0.1 8080
::1 8081
3 changes: 3 additions & 0 deletions admin_socket_dump/test/assets/normal/_.vsm_mgt/_.index
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Normal case with both -T and -S
+ _.Arg.t_file 0 32 Arg -T
+ _.Arg.s_file 0 40 Arg -S
145 changes: 145 additions & 0 deletions admin_socket_dump/test/test_suite.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env bats

# Test suite for Varnish configuration extractors
# Tests Python or shell implementation based on VARNISH_SOCKET_TOOL variable
#
# Usage:
# VARNISH_SOCKET_TOOL=python bats test/test_suite.bats
# VARNISH_SOCKET_TOOL=shell bats test/test_suite.bats
# Or use test/test_suite.sh to test both implementations

setup() {
ASSETS_DIR="$BATS_TEST_DIRNAME/assets"

case "$VARNISH_SOCKET_TOOL" in
python)
SCRIPT="python3 $BATS_TEST_DIRNAME/../varnish_socket.py"
;;
shell)
SCRIPT="sh $BATS_TEST_DIRNAME/../varnish_socket.sh"
;;
*)
echo "VARNISH_SOCKET_TOOL must be set to 'python' or 'shell'" >&2
exit 1
;;
esac
}

@test "normal case: both secret and socks present" {
run $SCRIPT "$ASSETS_DIR/normal"
[ "$status" -eq 0 ]

expected='{
"secret": "/tmp/test.secret",
"socks": [
{
"addr": "127.0.0.1",
"port": 8080
},
{
"addr": "::1",
"port": 8081
}
]
}'
[ "$output" = "$expected" ]
}

@test "no secret: only socks present" {
run $SCRIPT "$ASSETS_DIR/no_secret"
[ "$status" -eq 0 ]

expected='{
"secret": null,
"socks": [
{
"addr": "192.168.1.1",
"port": 9000
}
]
}'
[ "$output" = "$expected" ]
}

@test "no socks: only secret present" {
run $SCRIPT "$ASSETS_DIR/no_socks"
[ "$status" -eq 0 ]

expected='{
"secret": "/etc/varnish/secret",
"socks": []
}'
[ "$output" = "$expected" ]
}

@test "empty socks: secret present but socks file is empty" {
run $SCRIPT "$ASSETS_DIR/empty_socks"
[ "$status" -eq 0 ]

expected='{
"secret": "/var/secret",
"socks": []
}'
[ "$output" = "$expected" ]
}

@test "neither: no -T or -S arguments in index" {
run $SCRIPT "$ASSETS_DIR/neither"
[ "$status" -eq 0 ]

expected='{
"secret": null,
"socks": []
}'
[ "$output" = "$expected" ]
}

@test "invalid directory returns error" {
run $SCRIPT "$ASSETS_DIR/invalid_dir"
[ "$status" -eq 1 ]

# Both implementations return error JSON, but with different messages
case "$output" in
*'"error"'*) ;;
*) return 1 ;;
esac
}

@test "missing -T file: gracefully returns empty socks" {
run $SCRIPT "$ASSETS_DIR/missing_t_file"
[ "$status" -eq 1 ]

# File is referenced in index but doesn't exist - should error
case "$output" in
*'"error"'*) ;;
*) return 1 ;;
esac
}

@test "missing -S file: gracefully returns null secret" {
run $SCRIPT "$ASSETS_DIR/missing_s_file"
[ "$status" -eq 1 ]

# File is referenced in index but doesn't exist - should error
case "$output" in
*'"error"'*) ;;
*) return 1 ;;
esac
}

@test "malformed socks data: skips invalid lines" {
run $SCRIPT "$ASSETS_DIR/malformed_socks"
[ "$status" -eq 0 ]

# Should skip malformed lines and only return valid socket entries
expected='{
"secret": "/var/secret",
"socks": [
{
"addr": "192.168.1.1",
"port": 9000
}
]
}'
[ "$output" = "$expected" ]
}
22 changes: 22 additions & 0 deletions admin_socket_dump/test/test_suite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh
#
# Test runner for Varnish socket extractors
# Runs BATS tests for one or both implementations

set -e

TEST_DIR="$(cd "$(dirname "$0")" && pwd)"
BATS_FILE="$TEST_DIR/test_suite.bats"

# If VARNISH_SOCKET_TOOL is not set, test both implementations
if [ -z "$VARNISH_SOCKET_TOOL" ]; then
echo "Running tests for Python implementation..."
VARNISH_SOCKET_TOOL=python bats "$BATS_FILE"

echo ""
echo "Running tests for Shell implementation..."
VARNISH_SOCKET_TOOL=shell bats "$BATS_FILE"
else
# Run tests for the specified implementation
bats "$BATS_FILE"
fi
Loading
Loading