-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest-local.sh
More file actions
executable file
·123 lines (99 loc) · 2.64 KB
/
test-local.sh
File metadata and controls
executable file
·123 lines (99 loc) · 2.64 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
#!/bin/bash
set -e
# Configuration
ECR_REPOSITORY_NAME="pandoc-lambda"
IMAGE_TAG="local-test"
# Enable debug mode
set -x
# Check if Docker is installed
if ! command -v docker &> /dev/null; then
echo "Docker not found. Please install it first."
exit 1
fi
# Build the Docker image using the same configuration as AWS
echo "Building Docker image..."
docker buildx build \
--platform linux/amd64 \
--provenance=false \
--load \
-t ${ECR_REPOSITORY_NAME}:${IMAGE_TAG} \
.
# Create a sample markdown file for testing
echo "Creating sample markdown file..."
cat > sample.md << 'EOL'
# Sample Markdown
This is a **sample** markdown file to test the Pandoc Lambda function.
## Features
- Document conversion
- Multiple formats
- AWS Lambda integration
- PDF support via LaTeX
## Code Example
```python
def hello_world():
print("Hello, World!")
```
## Math Example
$E = mc^2$
EOL
# Encode the content as base64
CONTENT_BASE64=$(cat sample.md | base64)
# Create a test event file
echo "Creating test event file..."
cat > test-event.json << EOL
{
"input_format": "markdown",
"output_format": "pdf",
"content": "${CONTENT_BASE64}",
"options": ["--pdf-engine=pdflatex"]
}
EOL
# Create test script
echo "Creating test script..."
cat > test.py << 'EOL'
import json
import base64
import os
import subprocess
# Print system information
print("\nSystem information:")
subprocess.run(["ls", "-la", "/usr/bin/pandoc"])
subprocess.run(["which", "pdflatex"])
# Print pandoc version
try:
version = subprocess.check_output(["pandoc", "--version"]).decode()
print("\nPandoc version information:")
print(version)
except Exception as e:
print(f"Error checking pandoc version: {e}")
# Import the handler from app.py
from app import handler
# Load test event
with open("/function/test-event.json", "r") as f:
event = json.load(f)
# Call the handler
result = handler(event, {})
# Save the output
if result["statusCode"] == 200:
output_content = base64.b64decode(result["body"])
with open("/tmp/output/output.pdf", "wb") as f:
f.write(output_content)
print("Conversion successful! Output saved to output.pdf")
else:
print("Conversion failed:", result["body"])
EOL
# Check if output directory exists
if [ ! -d "output" ]; then
mkdir -p output
fi
# Run the Docker container with the test event
echo "Running Lambda function locally..."
docker run --rm \
--platform linux/amd64 \
-v "$(pwd)/test-event.json:/function/test-event.json" \
-v "$(pwd)/test.py:/function/test.py" \
-v "$(pwd)/output:/tmp/output" \
--entrypoint python3 \
${ECR_REPOSITORY_NAME}:${IMAGE_TAG} \
/function/test.py
echo "Test completed!"