test-compose-templates.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env bash
  2. # Test script for validating all compose templates with dry-run
  3. # This script iterates through all templates and runs a non-interactive dry-run
  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 test a single template
  55. test_template() {
  56. local template_id=$1
  57. local template_name=$2
  58. TOTAL=$((TOTAL + 1))
  59. print_status "INFO" "Testing: ${template_id}"
  60. # Run the generate command with dry-run and no-interactive
  61. # Capture stderr for error reporting
  62. local temp_stderr=$(mktemp)
  63. if python3 -m cli compose generate "${template_id}" \
  64. --dry-run \
  65. --no-interactive \
  66. > /dev/null 2>"${temp_stderr}"; then
  67. print_status "SUCCESS" "${template_id}"
  68. PASSED=$((PASSED + 1))
  69. rm -f "${temp_stderr}"
  70. return 0
  71. else
  72. print_status "ERROR" "${template_id}"
  73. FAILED=$((FAILED + 1))
  74. FAILED_TEMPLATES+=("${template_id}")
  75. # Show error message from stderr
  76. if [[ -s "${temp_stderr}" ]]; then
  77. local error_msg=$(cat "${temp_stderr}" | tr '\n' ' ')
  78. if [[ -n "${error_msg}" ]]; then
  79. echo " └─ ${error_msg}"
  80. fi
  81. fi
  82. rm -f "${temp_stderr}"
  83. return 1
  84. fi
  85. }
  86. # Main execution
  87. main() {
  88. cd "${PROJECT_ROOT}"
  89. echo "=========================================="
  90. echo "Compose Template Dry-Run Tests"
  91. echo "=========================================="
  92. print_status "INFO" "Working directory: ${PROJECT_ROOT}"
  93. echo ""
  94. # Get list of all compose templates
  95. local templates
  96. if ! templates=$(python3 -m cli compose list --raw 2>&1); then
  97. print_status "ERROR" "Failed to retrieve template list"
  98. echo "${templates}"
  99. exit 1
  100. fi
  101. # Count total templates
  102. local template_count
  103. template_count=$(echo "${templates}" | wc -l | tr -d ' ')
  104. print_status "INFO" "Found ${template_count} templates to test"
  105. echo ""
  106. # Iterate through each template
  107. while IFS=$'\t' read -r template_id template_name tags version library; do
  108. # Continue even if test fails (don't let set -e stop us)
  109. test_template "${template_id}" "${template_name}" || true
  110. done <<< "${templates}"
  111. # Print summary
  112. echo ""
  113. echo "=========================================="
  114. echo "Test Summary"
  115. echo "=========================================="
  116. echo "Total: ${TOTAL}"
  117. echo "Passed: ${PASSED}"
  118. echo "Failed: ${FAILED}"
  119. # List failed templates if any
  120. if [ "${FAILED}" -gt 0 ]; then
  121. echo ""
  122. echo "Failed templates:"
  123. for template in "${FAILED_TEMPLATES[@]}"; do
  124. echo " - ${template}"
  125. done
  126. echo ""
  127. print_status "ERROR" "${FAILED} template(s) failed validation"
  128. exit 1
  129. else
  130. echo ""
  131. print_status "SUCCESS" "All templates passed validation!"
  132. fi
  133. }
  134. # Run main function
  135. main "$@"