|
| 1 | +#!/usr/bin/env bash |
| 2 | +# End-to-end integration tests for the socket-basics Docker image. |
| 3 | +# |
| 4 | +# Verifies the full scan pipeline runs correctly without external credentials: |
| 5 | +# 1. socket-basics CLI starts and responds to --help |
| 6 | +# 2. opengrep can scan Python code using the bundled rules (no API key needed) |
| 7 | +# 3. socket-basics runs a scan on a small fixture without crashing |
| 8 | +# |
| 9 | +# Usage: |
| 10 | +# ./scripts/integration-test-docker.sh [--image-tag TAG] |
| 11 | +# ./scripts/integration-test-docker.sh --image-tag socket-basics:1.1.3 |
| 12 | + |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 16 | +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 17 | +IMAGE_TAG="${IMAGE_TAG:-socket-basics:smoke-test}" |
| 18 | +FIXTURE_DIR="$REPO_ROOT/tests/fixtures/integration" |
| 19 | + |
| 20 | +while [[ $# -gt 0 ]]; do |
| 21 | + case "$1" in |
| 22 | + --image-tag) |
| 23 | + [[ $# -lt 2 ]] && { echo "Error: --image-tag requires a value"; exit 1; } |
| 24 | + IMAGE_TAG="$2"; shift 2 |
| 25 | + ;; |
| 26 | + *) echo "Error: unknown option: $1"; exit 1 ;; |
| 27 | + esac |
| 28 | +done |
| 29 | + |
| 30 | +if ! command -v docker >/dev/null 2>&1; then |
| 31 | + echo "ERROR: Docker CLI is not installed or not in PATH." |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +pass() { echo " PASS: $*"; } |
| 36 | +fail() { echo " FAIL: $*"; exit 1; } |
| 37 | + |
| 38 | +echo "==> Integration test: $IMAGE_TAG" |
| 39 | + |
| 40 | +# ── Test 1: CLI starts and responds to --help ───────────────────────────────── |
| 41 | +echo "--> socket-basics --help" |
| 42 | +if docker run --rm --entrypoint /bin/sh "$IMAGE_TAG" -c "socket-basics -h" > /dev/null 2>&1; then |
| 43 | + pass "socket-basics -h exits 0" |
| 44 | +else |
| 45 | + fail "socket-basics -h exited non-zero" |
| 46 | +fi |
| 47 | + |
| 48 | +# ── Test 2: opengrep scans with bundled rules (no API key needed) ───────────── |
| 49 | +# Runs opengrep against the socket_basics Python source using the baked-in |
| 50 | +# rules. Validates: binary works, rules directory is intact, JSON output is |
| 51 | +# valid. opengrep exits 0 (no findings) or 1 (findings found) — both are OK. |
| 52 | +# Exit code 2+ signals a real error. |
| 53 | +echo "--> opengrep scan with bundled rules on internal source" |
| 54 | +opengrep_exit=0 |
| 55 | +opengrep_output=$( |
| 56 | + docker run --rm --entrypoint /bin/sh "$IMAGE_TAG" -c \ |
| 57 | + "opengrep scan \ |
| 58 | + --config /socket-basics/socket_basics/rules/ \ |
| 59 | + --json \ |
| 60 | + /socket-basics/socket_basics/ 2>/dev/null" \ |
| 61 | +) || opengrep_exit=$? |
| 62 | + |
| 63 | +if [[ $opengrep_exit -ge 2 ]]; then |
| 64 | + fail "opengrep exited with error code $opengrep_exit" |
| 65 | +fi |
| 66 | + |
| 67 | +if [[ -z "$opengrep_output" ]]; then |
| 68 | + fail "opengrep produced no output" |
| 69 | +fi |
| 70 | + |
| 71 | +if echo "$opengrep_output" | python3 -c "import sys, json; json.load(sys.stdin)" > /dev/null 2>&1; then |
| 72 | + pass "opengrep produced valid JSON output (exit $opengrep_exit)" |
| 73 | +else |
| 74 | + # Some opengrep versions may emit non-JSON on stdout in certain modes; treat |
| 75 | + # non-empty output without a parse error as a soft pass. |
| 76 | + pass "opengrep ran and produced output (non-JSON format, exit $opengrep_exit)" |
| 77 | +fi |
| 78 | + |
| 79 | +# ── Test 3: socket-basics scan on fixture (no API key) ──────────────────────── |
| 80 | +# Runs a real scan on a small clean Python fixture. We don't assert specific |
| 81 | +# findings — only that the process runs and does not crash. A non-zero exit is |
| 82 | +# acceptable (may indicate findings or missing API key for enterprise features). |
| 83 | +echo "--> socket-basics scan on fixture: $FIXTURE_DIR" |
| 84 | +scan_output=$( |
| 85 | + docker run --rm \ |
| 86 | + -v "${FIXTURE_DIR}:/workspace:ro" \ |
| 87 | + --entrypoint /bin/sh \ |
| 88 | + "$IMAGE_TAG" \ |
| 89 | + -c "socket-basics --workspace /workspace --python --console-tabular-enabled 2>&1" \ |
| 90 | +) || true # accept non-zero exit |
| 91 | + |
| 92 | +if [[ -z "$scan_output" ]]; then |
| 93 | + fail "socket-basics produced no output on fixture scan" |
| 94 | +fi |
| 95 | + |
| 96 | +# Detect hard crashes: Go panic, segfault, unhandled Python traceback |
| 97 | +if echo "$scan_output" | grep -qiE "^(panic:|fatal error:)|segmentation fault|Traceback \(most recent call last\)$"; then |
| 98 | + echo " Scan output:" |
| 99 | + echo "$scan_output" | head -30 |
| 100 | + fail "socket-basics crashed during scan" |
| 101 | +fi |
| 102 | + |
| 103 | +pass "socket-basics ran on fixture without crashing" |
| 104 | + |
| 105 | +echo "==> Integration test passed" |
0 commit comments