test-compose-template-errors.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #!/usr/bin/env bash
  2. #
  3. # Test script for template rendering error handling
  4. # This script creates various templates with errors and validates them
  5. # to test the improved error handling and display
  6. set -e
  7. # Colors for output
  8. RED='\033[0;31m'
  9. GREEN='\033[0;32m'
  10. YELLOW='\033[1;33m'
  11. BLUE='\033[0;34m'
  12. NC='\033[0m' # No Color
  13. # Test directory
  14. TEST_DIR="/tmp/boilerplates-error-tests"
  15. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  16. PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
  17. echo -e "${BLUE}========================================${NC}"
  18. echo -e "${BLUE}Template Error Handling Tests${NC}"
  19. echo -e "${BLUE}========================================${NC}"
  20. echo ""
  21. # Clean up test directory
  22. rm -rf "$TEST_DIR"
  23. mkdir -p "$TEST_DIR"
  24. # Function to create a test template
  25. create_test_template() {
  26. local name=$1
  27. local description=$2
  28. local template_content=$3
  29. local spec_content=$4
  30. local template_dir="$TEST_DIR/$name"
  31. mkdir -p "$template_dir"
  32. # Create template.yaml
  33. cat > "$template_dir/template.yaml" <<EOF
  34. ---
  35. kind: compose
  36. metadata:
  37. name: $name Test
  38. description: $description
  39. version: 0.1.0
  40. author: Test Suite
  41. date: '2025-01-09'
  42. $spec_content
  43. EOF
  44. # Create compose.yaml.j2
  45. echo "$template_content" > "$template_dir/compose.yaml.j2"
  46. echo "$template_dir"
  47. }
  48. # Function to run a test
  49. run_test() {
  50. local test_name=$1
  51. local template_path=$2
  52. local expected_to_fail=$3
  53. echo -e "${YELLOW}Test: $test_name${NC}"
  54. echo -e "${BLUE}Template path: $template_path${NC}"
  55. echo ""
  56. # Run validation with debug mode
  57. if python3 -m cli --log-level DEBUG compose validate --path "$template_path" 2>&1; then
  58. if [ "$expected_to_fail" = "true" ]; then
  59. echo -e "${RED}✗ UNEXPECTED: Test passed but was expected to fail${NC}"
  60. return 1
  61. else
  62. echo -e "${GREEN}✓ Test passed as expected${NC}"
  63. return 0
  64. fi
  65. else
  66. if [ "$expected_to_fail" = "true" ]; then
  67. echo -e "${GREEN}✓ Test failed as expected${NC}"
  68. return 0
  69. else
  70. echo -e "${RED}✗ UNEXPECTED: Test failed but was expected to pass${NC}"
  71. return 1
  72. fi
  73. fi
  74. }
  75. echo -e "${BLUE}========================================${NC}"
  76. echo -e "${BLUE}Test 1: Undefined Variable Error${NC}"
  77. echo -e "${BLUE}========================================${NC}"
  78. echo ""
  79. TEMPLATE_1=$(create_test_template \
  80. "undefined-variable" \
  81. "Template with undefined variable" \
  82. 'version: "3.8"
  83. services:
  84. {{ service_name }}:
  85. image: nginx:{{ nginx_version }}
  86. container_name: {{ undefined_variable }}
  87. ' \
  88. 'spec:
  89. general:
  90. vars:
  91. service_name:
  92. type: str
  93. description: Service name
  94. default: myservice
  95. nginx_version:
  96. type: str
  97. description: Nginx version
  98. default: latest
  99. ')
  100. run_test "Undefined Variable" "$TEMPLATE_1" "true"
  101. echo ""
  102. echo -e "${BLUE}========================================${NC}"
  103. echo -e "${BLUE}Test 2: Jinja2 Syntax Error - Missing endif${NC}"
  104. echo -e "${BLUE}========================================${NC}"
  105. echo ""
  106. TEMPLATE_2=$(create_test_template \
  107. "syntax-error-endif" \
  108. "Template with missing endif" \
  109. 'version: "3.8"
  110. services:
  111. {{ service_name }}:
  112. image: nginx:latest
  113. {% if enable_ports %}
  114. ports:
  115. - "80:80"
  116. # Missing {% endif %}
  117. ' \
  118. 'spec:
  119. general:
  120. vars:
  121. service_name:
  122. type: str
  123. description: Service name
  124. default: myservice
  125. enable_ports:
  126. type: bool
  127. description: Enable ports
  128. default: true
  129. ')
  130. run_test "Syntax Error - Missing endif" "$TEMPLATE_2" "true"
  131. echo ""
  132. echo -e "${BLUE}========================================${NC}"
  133. echo -e "${BLUE}Test 3: Jinja2 Syntax Error - Unclosed bracket${NC}"
  134. echo -e "${BLUE}========================================${NC}"
  135. echo ""
  136. TEMPLATE_3=$(create_test_template \
  137. "syntax-error-bracket" \
  138. "Template with unclosed bracket" \
  139. 'version: "3.8"
  140. services:
  141. {{ service_name }}:
  142. image: nginx:{{ version
  143. container_name: {{ service_name }}
  144. ' \
  145. 'spec:
  146. general:
  147. vars:
  148. service_name:
  149. type: str
  150. description: Service name
  151. default: myservice
  152. version:
  153. type: str
  154. description: Version
  155. default: latest
  156. ')
  157. run_test "Syntax Error - Unclosed Bracket" "$TEMPLATE_3" "true"
  158. echo ""
  159. echo -e "${BLUE}========================================${NC}"
  160. echo -e "${BLUE}Test 4: Filter Error - Unknown filter${NC}"
  161. echo -e "${BLUE}========================================${NC}"
  162. echo ""
  163. TEMPLATE_4=$(create_test_template \
  164. "filter-error" \
  165. "Template with unknown filter" \
  166. 'version: "3.8"
  167. services:
  168. {{ service_name }}:
  169. image: nginx:{{ version | unknown_filter }}
  170. ' \
  171. 'spec:
  172. general:
  173. vars:
  174. service_name:
  175. type: str
  176. description: Service name
  177. default: myservice
  178. version:
  179. type: str
  180. description: Version
  181. default: latest
  182. ')
  183. run_test "Filter Error - Unknown Filter" "$TEMPLATE_4" "true"
  184. echo ""
  185. echo -e "${BLUE}========================================${NC}"
  186. echo -e "${BLUE}Test 5: Valid Template - Should Pass${NC}"
  187. echo -e "${BLUE}========================================${NC}"
  188. echo ""
  189. TEMPLATE_5=$(create_test_template \
  190. "valid-template" \
  191. "Valid template that should pass validation" \
  192. 'version: "3.8"
  193. services:
  194. {{ service_name }}:
  195. image: nginx:{{ version }}
  196. container_name: {{ service_name }}
  197. {% if enable_ports %}
  198. ports:
  199. - "{{ port }}:80"
  200. {% endif %}
  201. ' \
  202. 'spec:
  203. general:
  204. vars:
  205. service_name:
  206. type: str
  207. description: Service name
  208. default: myservice
  209. version:
  210. type: str
  211. description: Version
  212. default: latest
  213. enable_ports:
  214. type: bool
  215. description: Enable ports
  216. default: true
  217. port:
  218. type: int
  219. description: External port
  220. default: 8080
  221. ')
  222. run_test "Valid Template" "$TEMPLATE_5" "false"
  223. echo ""
  224. echo -e "${BLUE}========================================${NC}"
  225. echo -e "${BLUE}Test 6: Nested Variable with Typo${NC}"
  226. echo -e "${BLUE}========================================${NC}"
  227. echo ""
  228. TEMPLATE_6=$(create_test_template \
  229. "typo-variable" \
  230. "Template with typo in variable name" \
  231. 'version: "3.8"
  232. services:
  233. {{ service_name }}:
  234. image: nginx:latest
  235. environment:
  236. - SERVICE_NAME={{ servce_name }}
  237. ' \
  238. 'spec:
  239. general:
  240. vars:
  241. service_name:
  242. type: str
  243. description: Service name
  244. default: myservice
  245. ')
  246. run_test "Typo in Variable Name" "$TEMPLATE_6" "true"
  247. echo ""
  248. echo -e "${BLUE}========================================${NC}"
  249. echo -e "${BLUE}Test 7: Template with Default Filter${NC}"
  250. echo -e "${BLUE}========================================${NC}"
  251. echo ""
  252. TEMPLATE_7=$(create_test_template \
  253. "default-filter" \
  254. "Template using default filter - should pass" \
  255. 'version: "3.8"
  256. services:
  257. {{ service_name }}:
  258. image: nginx:{{ version | default("latest") }}
  259. container_name: {{ container_name | default(service_name) }}
  260. ' \
  261. 'spec:
  262. general:
  263. vars:
  264. service_name:
  265. type: str
  266. description: Service name
  267. default: myservice
  268. version:
  269. type: str
  270. description: Version
  271. default: ""
  272. ')
  273. run_test "Default Filter Usage" "$TEMPLATE_7" "false"
  274. echo ""
  275. echo -e "${BLUE}========================================${NC}"
  276. echo -e "${BLUE}Test Summary${NC}"
  277. echo -e "${BLUE}========================================${NC}"
  278. echo ""
  279. echo -e "${GREEN}All tests completed!${NC}"
  280. echo -e "${YELLOW}Check the output above for detailed error messages.${NC}"
  281. echo ""
  282. echo -e "${BLUE}Test directory: $TEST_DIR${NC}"
  283. echo -e "${YELLOW}Note: Test directory has been preserved for manual inspection${NC}"