docker-entrypoint.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/bin/bash
  2. # Docker entrypoint for Playwright E2E tests
  3. set -euo pipefail
  4. # Utility functions
  5. print_status() { echo -e '\033[34m🔧\033[0m' "$1"; }
  6. print_success() { echo -e '\033[32m✅\033[0m' "$1"; }
  7. print_error() { echo -e '\033[31m❌\033[0m' "$1"; }
  8. print_warning() { echo -e '\033[33m⚠️\033[0m' "$1"; }
  9. validate_environment() {
  10. print_status "Starting Playwright E2E Tests in Docker"
  11. print_status "Working directory: $(pwd)"
  12. print_status "Environment: NODE_ENV=${NODE_ENV:-development}, CI=${CI:-false}"
  13. if [[ -z "${OPENROUTER_API_KEY:-}" ]]; then
  14. print_error "OPENROUTER_API_KEY not set"
  15. exit 1
  16. fi
  17. print_success "Environment validation passed"
  18. }
  19. setup_environment() {
  20. print_status "Setting up environment..."
  21. # Initialize D-Bus machine ID if it doesn't exist
  22. if [[ ! -f /var/lib/dbus/machine-id ]]; then
  23. dbus-uuidgen > /var/lib/dbus/machine-id 2>/dev/null || true
  24. fi
  25. ####### Set up D-Bus ######
  26. # This is necessary to enable IPC messaging between core and webview
  27. runtime_dir="/tmp/runtime-$(id -u)"
  28. if [[ ! -d "$runtime_dir" ]]; then
  29. mkdir -p "$runtime_dir"
  30. chmod 700 "$runtime_dir"
  31. fi
  32. export XDG_RUNTIME_DIR="$runtime_dir"
  33. # Start D-Bus session
  34. if command -v dbus-launch >/dev/null 2>&1; then
  35. if dbus_output=$(dbus-launch --sh-syntax 2>/dev/null); then
  36. eval "$dbus_output"
  37. export DBUS_SESSION_BUS_ADDRESS
  38. else
  39. print_warning "Failed to start D-Bus session, continuing anyway"
  40. fi
  41. else
  42. print_warning "dbus-launch not available, continuing anyway"
  43. fi
  44. print_status "Setting up keyring services for VS Code secrets API..."
  45. ####### Set up Keyrings ######
  46. # This is needed for VS Code secret storage to work properly
  47. mkdir -p ~/.cache ~/.local/share/keyrings
  48. # Set environment variables for keyring
  49. export XDG_CURRENT_DESKTOP=Unity
  50. export GNOME_KEYRING_CONTROL=1
  51. # Start gnome-keyring with empty password (headless mode)
  52. if command -v gnome-keyring-daemon >/dev/null 2>&1; then
  53. # Initialize keyring with empty password
  54. if keyring_output=$(printf '\n' | gnome-keyring-daemon --unlock 2>/dev/null); then
  55. eval "$keyring_output" 2>/dev/null || true
  56. fi
  57. # Start keyring daemon
  58. if keyring_start=$(printf '\n' | gnome-keyring-daemon --start 2>/dev/null); then
  59. eval "$keyring_start" 2>/dev/null || true
  60. export GNOME_KEYRING_CONTROL
  61. print_success "Keyring services initialized"
  62. else
  63. print_warning "Failed to start keyring daemon - VS Code will fall back to environment variables"
  64. export VSCODE_SECRETS_FALLBACK=true
  65. fi
  66. else
  67. print_warning "gnome-keyring-daemon not available - VS Code will fall back to environment variables"
  68. export VSCODE_SECRETS_FALLBACK=true
  69. fi
  70. # Test keyring functionality (optional debugging)
  71. if command -v secret-tool >/dev/null 2>&1 && [[ "${VSCODE_SECRETS_FALLBACK:-}" != "true" ]]; then
  72. if secret-tool store --label="test" test-key test-value 2>/dev/null; then
  73. secret-tool clear test-key test-value 2>/dev/null || true
  74. print_success "Keyring functionality verified"
  75. else
  76. print_warning "Keyring test failed - enabling fallback mode"
  77. export VSCODE_SECRETS_FALLBACK=true
  78. fi
  79. fi
  80. print_success "Environment setup complete!"
  81. }
  82. setup_local_env_secrets() {
  83. print_status "Setting up .env.local with secrets..."
  84. # Create .env.local with API key
  85. echo "OPENROUTER_API_KEY=${OPENROUTER_API_KEY}" > /tmp/.env.local
  86. print_success "Secrets setup in /tmp/.env.local!"
  87. }
  88. run_playwright_tests() {
  89. print_status "Running Playwright tests..."
  90. # Change to the playwright-e2e directory where node_modules and config are located
  91. cd /workspace/apps/playwright-e2e
  92. # Set environment variable for HTML report output directory
  93. export PLAYWRIGHT_HTML_REPORT="/workspace/apps/playwright-e2e/playwright-report"
  94. # Build test command
  95. test_cmd=(
  96. xvfb-run --auto-servernum --server-num=1
  97. npx dotenvx run -f /tmp/.env.local --
  98. playwright test
  99. --output /workspace/apps/playwright-e2e/test-results
  100. )
  101. # Add any additional arguments passed to the container
  102. test_cmd+=("$@")
  103. # Run the tests
  104. if "${test_cmd[@]}"; then
  105. print_success "Playwright tests completed successfully!"
  106. return 0
  107. else
  108. print_error "Playwright tests failed"
  109. return 1
  110. fi
  111. }
  112. # Signal handlers
  113. cleanup() {
  114. print_warning "Received signal, shutting down gracefully"
  115. exit 0
  116. }
  117. trap cleanup SIGTERM SIGINT
  118. # Main execution
  119. main() {
  120. validate_environment
  121. setup_environment
  122. setup_local_env_secrets
  123. if run_playwright_tests "$@"; then
  124. exit_code=0
  125. else
  126. exit_code=1
  127. fi
  128. print_success "Playwright execution completed with exit code: $exit_code"
  129. exit $exit_code
  130. }
  131. # Run main function with all arguments
  132. main "$@"