test-compose-templates.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env bash
  2. # Test script for validating all compose templates
  3. # This script iterates through all templates and runs validation
  4. # Output is GitHub Actions friendly and easy to parse
  5. set -euo pipefail
  6. # Colors for output (only if terminal supports it)
  7. if [[ -t 1 ]]; then
  8. RED='\033[0;31m'
  9. GREEN='\033[0;32m'
  10. YELLOW='\033[1;33m'
  11. BLUE='\033[0;34m'
  12. NC='\033[0m'
  13. else
  14. RED=''
  15. GREEN=''
  16. YELLOW=''
  17. BLUE=''
  18. NC=''
  19. fi
  20. # Counters
  21. TOTAL=0
  22. PASSED=0
  23. FAILED=0
  24. FAILED_TEMPLATES=()
  25. # Get script directory and project root
  26. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  27. PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
  28. # Function to print status (GitHub Actions annotations format)
  29. print_status() {
  30. local status=$1
  31. local message=$2
  32. case $status in
  33. "INFO")
  34. echo "[INFO] ${message}"
  35. ;;
  36. "SUCCESS")
  37. echo "[PASS] ${message}"
  38. ;;
  39. "ERROR")
  40. echo "[FAIL] ${message}"
  41. # GitHub Actions error annotation
  42. if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
  43. echo "::error::${message}"
  44. fi
  45. ;;
  46. "WARNING")
  47. echo "[WARN] ${message}"
  48. if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
  49. echo "::warning::${message}"
  50. fi
  51. ;;
  52. esac
  53. }
  54. # Function to validate and test a single template
  55. validate_template() {
  56. local template_id=$1
  57. local template_name=$2
  58. TOTAL=$((TOTAL + 1))
  59. echo -e "${BLUE}────────────────────────────────────────${NC}"
  60. print_status "INFO" "Testing: ${template_id} (${template_name})"
  61. # Step 1: Run validation
  62. echo -e " ${YELLOW}→${NC} Running validation..."
  63. local temp_output=$(mktemp)
  64. if ! python3 -m cli compose validate "${template_id}" \
  65. > "${temp_output}" 2>&1; then
  66. echo -e " ${RED}✗${NC} Validation failed"
  67. print_status "ERROR" "${template_id} - Validation failed"
  68. FAILED=$((FAILED + 1))
  69. FAILED_TEMPLATES+=("${template_id}")
  70. # Show error message (first few lines)
  71. if [[ -s "${temp_output}" ]]; then
  72. echo " └─ Validation error:"
  73. head -n 5 "${temp_output}" | sed 's/^/ /'
  74. fi
  75. rm -f "${temp_output}"
  76. return 1
  77. fi
  78. echo -e " ${GREEN}✓${NC} Validation passed"
  79. rm -f "${temp_output}"
  80. # Step 2: Run dry-run generation with quiet mode
  81. echo -e " ${YELLOW}→${NC} Running dry-run generation..."
  82. local temp_gen=$(mktemp)
  83. if ! python3 -m cli compose generate "${template_id}" \
  84. --dry-run \
  85. --no-interactive \
  86. --quiet \
  87. > "${temp_gen}" 2>&1; then
  88. echo -e " ${RED}✗${NC} Generation failed"
  89. print_status "ERROR" "${template_id} - Generation failed"
  90. FAILED=$((FAILED + 1))
  91. FAILED_TEMPLATES+=("${template_id}")
  92. # Show error message (first few lines)
  93. if [[ -s "${temp_gen}" ]]; then
  94. echo " └─ Generation error:"
  95. head -n 5 "${temp_gen}" | sed 's/^/ /'
  96. fi
  97. rm -f "${temp_gen}"
  98. return 1
  99. fi
  100. echo -e " ${GREEN}✓${NC} Generation passed"
  101. rm -f "${temp_gen}"
  102. # Both validation and generation passed
  103. print_status "SUCCESS" "${template_id}"
  104. PASSED=$((PASSED + 1))
  105. echo ""
  106. return 0
  107. }
  108. # Main execution
  109. main() {
  110. cd "${PROJECT_ROOT}"
  111. echo "=========================================="
  112. echo "Compose Template Validation Tests"
  113. echo "=========================================="
  114. print_status "INFO" "Working directory: ${PROJECT_ROOT}"
  115. echo ""
  116. # Get list of all compose templates
  117. local templates
  118. if ! templates=$(python3 -m cli compose list --raw 2>&1); then
  119. print_status "ERROR" "Failed to retrieve template list"
  120. echo "${templates}"
  121. exit 1
  122. fi
  123. # Count total templates
  124. local template_count
  125. template_count=$(echo "${templates}" | wc -l | tr -d ' ')
  126. print_status "INFO" "Found ${template_count} templates to test"
  127. echo ""
  128. # Iterate through each template
  129. while IFS=$'\t' read -r template_id template_name tags version library; do
  130. # Continue even if validation fails (don't let set -e stop us)
  131. validate_template "${template_id}" "${template_name}" || true
  132. done <<< "${templates}"
  133. # Print summary
  134. echo ""
  135. echo "=========================================="
  136. echo "Test Summary"
  137. echo "=========================================="
  138. echo "Total: ${TOTAL}"
  139. echo "Passed: ${PASSED}"
  140. echo "Failed: ${FAILED}"
  141. # List failed templates if any
  142. if [ "${FAILED}" -gt 0 ]; then
  143. echo ""
  144. echo "Failed templates:"
  145. for template in "${FAILED_TEMPLATES[@]}"; do
  146. echo " - ${template}"
  147. done
  148. echo ""
  149. print_status "ERROR" "${FAILED} template(s) failed validation"
  150. exit 1
  151. else
  152. echo ""
  153. print_status "SUCCESS" "All templates passed validation!"
  154. fi
  155. }
  156. # Run main function
  157. main "$@"