#!/bin/bash # Quick Test: Fast Validation for Development # Runs subset of critical tests for rapid feedback during development # Usage: bash labs/lab-02-network/tests/quick-test.sh set -euo pipefail # Color definitions RED='\033[0;31m' GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' CYAN='\033[0;36m' BOLD='\033[1m' NC='\033[0m' # Get script directory TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$TEST_DIR/../.." && pwd)" # Counter helpers pass_count=0 fail_count=0 inc_pass() { ((pass_count++)) || true; } inc_fail() { ((fail_count++)) || true; } # Helper functions print_header() { echo -e "${CYAN}╔═══════════════════════════════════════════════════════════════╗${NC}" echo -e "${CYAN}║${NC} ${BOLD}$1${NC}" echo -e "${CYAN}╚═══════════════════════════════════════════════════════════════╝${NC}" } print_test() { echo -e "\n${BLUE}[TEST]${NC} $1" } print_pass() { echo -e " ${GREEN}[✓]${NC} $1" inc_pass } print_fail() { echo -e " ${RED}[✗]${NC} $1" inc_fail } print_info() { echo -e " ${CYAN}[i]${NC} $1" } # Main header clear print_header "Lab 02: Quick Test (Fast Validation)" echo "" echo -e "Running critical tests only (< 30 seconds)" echo -e "For full test suite, run: ${YELLOW}bash run-all-tests.sh${NC}" echo "" # Quick Test 1: Docker availability print_test "Docker is available" if command -v docker &> /dev/null; then print_pass "Docker command found" print_info "$(docker --version)" else print_fail "Docker not found" exit 1 fi # Quick Test 2: Docker Compose file exists print_test "docker-compose.yml exists" COMPOSE_FILE="$PROJECT_ROOT/labs/lab-02-network/docker-compose.yml" if [[ -f "$COMPOSE_FILE" ]]; then print_pass "docker-compose.yml found" else print_fail "docker-compose.yml not found (expected after Tutorial 1)" print_info "This is OK if you're starting the lab" fi # Quick Test 3: Validate compose syntax (if file exists) if [[ -f "$COMPOSE_FILE" ]]; then print_test "docker-compose.yml has valid syntax" if docker compose -f "$COMPOSE_FILE" config &> /dev/null; then print_pass "Compose file is valid YAML" else print_fail "Compose file has syntax errors" print_info "Run: docker compose -f docker-compose.yml config" fi # Quick Test 4: INF-02 compliance (no 0.0.0.0 bindings) print_test "INF-02 compliance (no 0.0.0.0 bindings)" ZERO_COUNT=$(grep -c -E '0\.0\.0\.0:[0-9]+' "$COMPOSE_FILE" 2>/dev/null || echo "0") if [[ $ZERO_COUNT -eq 0 ]]; then print_pass "No 0.0.0.0 bindings (secure)" else print_fail "Found $ZERO_COUNT 0.0.0.0 bindings (INF-02 violation)" fi fi # Quick Test 5: Docker networks can be created print_test "Docker network creation works" if docker network create --driver bridge --subnet 10.0.99.0/24 quick-test-net &> /dev/null; then print_pass "Can create bridge network with custom subnet" docker network rm quick-test-net &> /dev/null else print_fail "Failed to create test network" fi # Quick Test 6: Network isolation works print_test "Network isolation verification" # Create two networks if docker network create --driver bridge --subnet 10.0.98.0/24 quick-test-net1 &> /dev/null && \ docker network create --driver bridge --subnet 10.0.97.0/24 quick-test-net2 &> /dev/null; then # Create test containers if docker run -d --name qt-c1 --network quick-test-net1 alpine:3.19 sleep 60 &> /dev/null && \ docker run -d --name qt-c2 --network quick-test-net2 alpine:3.19 sleep 60 &> /dev/null; then # Test cross-network isolation (should fail) if docker exec qt-c1 ping -c 1 -W 1 qt-c2 &> /dev/null; then print_fail "Cross-network communication works (isolation broken!)" else print_pass "Cross-network communication blocked (isolation works)" fi # Cleanup docker stop qt-c1 qt-c2 &> /dev/null docker rm qt-c1 qt-c2 &> /dev/null else print_fail "Failed to create test containers" fi # Cleanup networks docker network rm quick-test-net1 quick-test-net2 &> /dev/null else print_fail "Failed to create test networks" fi # Quick Test 7: Test scripts exist print_test "Test infrastructure present" TEST_COUNT=0 if [[ -f "$TEST_DIR/01-network-creation-test.sh" ]]; then ((TEST_COUNT++)) || true; fi if [[ -f "$TEST_DIR/02-isolation-verification-test.sh" ]]; then ((TEST_COUNT++)) || true; fi if [[ -f "$TEST_DIR/03-inf02-compliance-test.sh" ]]; then ((TEST_COUNT++)) || true; fi if [[ $TEST_COUNT -eq 3 ]]; then print_pass "All test scripts present ($TEST_COUNT/3)" else print_fail "Some test scripts missing ($TEST_COUNT/3)" fi # Quick Test 8: Documentation exists print_test "Documentation files present" DOC_COUNT=0 if [[ -f "$TEST_DIR/../tutorial/01-create-networks.md" ]]; then ((DOC_COUNT++)) || true; fi if [[ -f "$TEST_DIR/../tutorial/02-deploy-containers.md" ]]; then ((DOC_COUNT++)) || true; fi if [[ -f "$TEST_DIR/../tutorial/03-verify-isolation.md" ]]; then ((DOC_COUNT++)) || true; fi if [[ $DOC_COUNT -ge 1 ]]; then print_pass "Documentation present ($DOC_COUNT tutorial files)" else print_info "No documentation yet (expected during development)" fi # Summary print_header "Quick Test Summary" echo -e "Tests run: ${BOLD}$((pass_count + fail_count))${NC}" echo -e " ${GREEN}Passed:${NC} $pass_count" if [[ $fail_count -gt 0 ]]; then echo -e " ${RED}Failed:${NC} $fail_count" fi echo "" # Verdict if [[ $fail_count -eq 0 ]]; then echo -e "${GREEN}${BOLD}✓ ALL QUICK TESTS PASSED${NC}" echo "" echo -e "Quick validation successful!" echo "" echo -e "Next steps:" echo -e " 1. Run full test suite: ${CYAN}bash run-all-tests.sh${NC}" echo -e " 2. Run final verification: ${CYAN}bash 99-final-verification.sh${NC}" echo "" exit 0 else echo -e "${RED}${BOLD}✗ QUICK TESTS FAILED${NC}" echo "" echo -e "Some critical tests failed. Please review:" echo -e " 1. Check Docker is running: ${CYAN}docker ps${NC}" echo -e " 2. Verify compose file: ${CYAN}cd labs/lab-02-network && docker compose config${NC}" echo -e " 3. Run full test suite for details: ${CYAN}bash run-all-tests.sh${NC}" echo "" exit 1 fi