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