|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to build, test, and cleanup the ndc-python-lambda container |
| 4 | +# This script builds the root Dockerfile, runs it with connector-definition files, |
| 5 | +# tests the /schema and /capabilities endpoints, and cleans up. |
| 6 | + |
| 7 | +set -e # Exit on any error |
| 8 | + |
| 9 | +# Configuration |
| 10 | +IMAGE_NAME="ndc-python-lambda-test" |
| 11 | +CONTAINER_NAME="ndc-python-lambda-test-container" |
| 12 | +CONTAINER_PORT="8080" |
| 13 | +HOST_PORT="8080" |
| 14 | +TIMEOUT=30 # seconds to wait for container to be ready |
| 15 | +TEMP_BUILD_DIR="" # Will be set by mktemp |
| 16 | + |
| 17 | +# Colors for output |
| 18 | +RED='\033[0;31m' |
| 19 | +GREEN='\033[0;32m' |
| 20 | +YELLOW='\033[1;33m' |
| 21 | +NC='\033[0m' # No Color |
| 22 | + |
| 23 | +# Function to print colored output |
| 24 | +print_status() { |
| 25 | + echo -e "${GREEN}[INFO]${NC} $1" |
| 26 | +} |
| 27 | + |
| 28 | +print_warning() { |
| 29 | + echo -e "${YELLOW}[WARN]${NC} $1" |
| 30 | +} |
| 31 | + |
| 32 | +print_error() { |
| 33 | + echo -e "${RED}[ERROR]${NC} $1" |
| 34 | +} |
| 35 | + |
| 36 | +# Function to cleanup on exit |
| 37 | +cleanup() { |
| 38 | + print_status "Cleaning up..." |
| 39 | + |
| 40 | + # Stop and remove container if it exists |
| 41 | + if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then |
| 42 | + print_status "Stopping and removing container: ${CONTAINER_NAME}" |
| 43 | + docker stop "${CONTAINER_NAME}" >/dev/null 2>&1 || true |
| 44 | + docker rm "${CONTAINER_NAME}" >/dev/null 2>&1 || true |
| 45 | + fi |
| 46 | + |
| 47 | + # Remove image if it exists |
| 48 | + if docker images --format '{{.Repository}}:{{.Tag}}' | grep -q "^${IMAGE_NAME}:latest$"; then |
| 49 | + print_status "Removing image: ${IMAGE_NAME}" |
| 50 | + docker rmi "${IMAGE_NAME}" >/dev/null 2>&1 || true |
| 51 | + fi |
| 52 | + |
| 53 | + # Remove temporary build directory if it exists |
| 54 | + if [ -n "${TEMP_BUILD_DIR}" ] && [ -d "${TEMP_BUILD_DIR}" ]; then |
| 55 | + print_status "Removing temporary build directory: ${TEMP_BUILD_DIR}" |
| 56 | + rm -rf "${TEMP_BUILD_DIR:?}" |
| 57 | + fi |
| 58 | + |
| 59 | + print_status "Cleanup completed" |
| 60 | +} |
| 61 | + |
| 62 | +# Set trap to cleanup on script exit |
| 63 | +trap cleanup EXIT |
| 64 | + |
| 65 | +# Function to wait for container to be ready |
| 66 | +wait_for_container() { |
| 67 | + print_status "Waiting for container to be ready..." |
| 68 | + local count=0 |
| 69 | + while [ $count -lt $TIMEOUT ]; do |
| 70 | + if curl -s -f "http://localhost:${HOST_PORT}/health" >/dev/null 2>&1; then |
| 71 | + print_status "Container is ready!" |
| 72 | + return 0 |
| 73 | + fi |
| 74 | + sleep 1 |
| 75 | + count=$((count + 1)) |
| 76 | + echo -n "." |
| 77 | + done |
| 78 | + echo |
| 79 | + print_error "Container failed to become ready within ${TIMEOUT} seconds" |
| 80 | + return 1 |
| 81 | +} |
| 82 | + |
| 83 | +# Function to setup temporary build directory |
| 84 | +setup_temp_build_dir() { |
| 85 | + print_status "Setting up temporary build directory with connector-definition files..." |
| 86 | + |
| 87 | + # Create temporary directory using mktemp |
| 88 | + TEMP_BUILD_DIR=$(mktemp -d) |
| 89 | + print_status "Created temporary build directory: ${TEMP_BUILD_DIR}" |
| 90 | + |
| 91 | + # Copy the Dockerfile to temp directory |
| 92 | + cp "Dockerfile" "${TEMP_BUILD_DIR}/" |
| 93 | + |
| 94 | + # Copy connector-definition files to the locations expected by the Dockerfile |
| 95 | + # The original Dockerfile expects: |
| 96 | + # COPY /docker /scripts -> so we /docker to docker/ |
| 97 | + # COPY /functions /functions -> so we copy connector-definition/template to functions/ |
| 98 | + |
| 99 | + cp -r "docker" "${TEMP_BUILD_DIR}/docker" |
| 100 | + cp -r "connector-definition/template" "${TEMP_BUILD_DIR}/functions" |
| 101 | + |
| 102 | + print_status "Temporary build directory setup complete" |
| 103 | + print_status "Build directory contents:" |
| 104 | + ls -la "${TEMP_BUILD_DIR}" |
| 105 | +} |
| 106 | + |
| 107 | +# Function to test an endpoint |
| 108 | +test_endpoint() { |
| 109 | + local endpoint=$1 |
| 110 | + local description=$2 |
| 111 | + |
| 112 | + print_status "Testing ${description} (${endpoint})..." |
| 113 | + |
| 114 | + local response_code |
| 115 | + response_code=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:${HOST_PORT}${endpoint}") |
| 116 | + |
| 117 | + if [ "$response_code" = "200" ]; then |
| 118 | + print_status "✓ ${description} returned 200 OK" |
| 119 | + return 0 |
| 120 | + else |
| 121 | + print_error "✗ ${description} returned ${response_code} (expected 200)" |
| 122 | + return 1 |
| 123 | + fi |
| 124 | +} |
| 125 | + |
| 126 | +# Main execution |
| 127 | +main() { |
| 128 | + print_status "Starting ndc-python-lambda container test" |
| 129 | + |
| 130 | + # Check if required directories exist |
| 131 | + if [ ! -d "connector-definition/template" ]; then |
| 132 | + print_error "connector-definition/template directory not found" |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | + |
| 136 | + if [ ! -d "connector-definition/scripts" ]; then |
| 137 | + print_error "connector-definition/scripts directory not found" |
| 138 | + exit 1 |
| 139 | + fi |
| 140 | + |
| 141 | + # Setup temporary build directory with connector-definition files |
| 142 | + setup_temp_build_dir |
| 143 | + |
| 144 | + # Build the Docker image using the original Dockerfile from temp directory |
| 145 | + print_status "Building Docker image: ${IMAGE_NAME}" |
| 146 | + docker build -t "${IMAGE_NAME}" "${TEMP_BUILD_DIR}" |
| 147 | + |
| 148 | + # Run the container |
| 149 | + print_status "Starting container: ${CONTAINER_NAME}" |
| 150 | + docker run -d \ |
| 151 | + --name "${CONTAINER_NAME}" \ |
| 152 | + -p "${HOST_PORT}:${CONTAINER_PORT}" \ |
| 153 | + "${IMAGE_NAME}" |
| 154 | + |
| 155 | + # Wait for container to be ready |
| 156 | + if ! wait_for_container; then |
| 157 | + print_error "Container failed to start properly" |
| 158 | + exit 1 |
| 159 | + fi |
| 160 | + |
| 161 | + # Test the endpoints |
| 162 | + local test_failed=false |
| 163 | + |
| 164 | + if ! test_endpoint "/schema" "Schema endpoint"; then |
| 165 | + test_failed=true |
| 166 | + fi |
| 167 | + |
| 168 | + if ! test_endpoint "/capabilities" "Capabilities endpoint"; then |
| 169 | + test_failed=true |
| 170 | + fi |
| 171 | + |
| 172 | + # Report results |
| 173 | + if [ "$test_failed" = true ]; then |
| 174 | + print_error "Some tests failed!" |
| 175 | + exit 1 |
| 176 | + else |
| 177 | + print_status "All tests passed successfully! ✓" |
| 178 | + fi |
| 179 | +} |
| 180 | + |
| 181 | +# Check if Docker is available |
| 182 | +if ! command -v docker &>/dev/null; then |
| 183 | + print_error "Docker is not installed or not in PATH" |
| 184 | + exit 1 |
| 185 | +fi |
| 186 | + |
| 187 | +# Check if curl is available |
| 188 | +if ! command -v curl &>/dev/null; then |
| 189 | + print_error "curl is not installed or not in PATH" |
| 190 | + exit 1 |
| 191 | +fi |
| 192 | + |
| 193 | +# Run main function |
| 194 | +main |
| 195 | + |
| 196 | +print_status "Script completed successfully!" |
0 commit comments