|
93 | 93 | pytest
|
94 | 94 | pytest-testinfra
|
95 | 95 | requests
|
| 96 | + ec2instanceconnectcli |
| 97 | + paramiko |
96 | 98 | ]);
|
97 | 99 | sfcgal = pkgs.callPackage ./nix/ext/sfcgal/sfcgal.nix { };
|
98 | 100 | supabase-groonga = pkgs.callPackage ./nix/supabase-groonga.nix { };
|
|
752 | 754 | } ''
|
753 | 755 | mkdir -p $out/bin
|
754 | 756 | cat > $out/bin/run-testinfra << 'EOL'
|
755 |
| - #!/usr/bin/env bash |
756 |
| - set -euo pipefail |
757 |
| -
|
758 |
| - export PATH="${pkgs.lib.makeBinPath (with pkgs; [ |
759 |
| - pythonEnv |
760 |
| - awscli2 |
761 |
| - aws-vault |
762 |
| - ])}:$PATH" |
763 |
| -
|
764 |
| - # Check for required tools |
765 |
| - for cmd in aws-vault; do |
766 |
| - if ! command -v $cmd &> /dev/null; then |
767 |
| - echo "Error: $cmd is required but not found" |
768 |
| - exit 1 |
769 |
| - fi |
| 757 | + #!/bin/sh |
| 758 | + set -e |
| 759 | +
|
| 760 | + # Parse command line arguments |
| 761 | + PROFILE="" |
| 762 | + AMI_NAME="" |
| 763 | + PYTEST_ARGS="" |
| 764 | + while [ $# -gt 0 ]; do |
| 765 | + case "$1" in |
| 766 | + --profile) |
| 767 | + PROFILE="$2" |
| 768 | + shift 2 |
| 769 | + ;; |
| 770 | + --) |
| 771 | + shift |
| 772 | + PYTEST_ARGS="$*" |
| 773 | + break |
| 774 | + ;; |
| 775 | + *) |
| 776 | + if [ -z "$AMI_NAME" ]; then |
| 777 | + AMI_NAME="$1" |
| 778 | + else |
| 779 | + echo "Error: Unexpected argument: $1" |
| 780 | + echo "Usage: aws-vault exec <profile> -- nix run .#run-testinfra -- --profile <profile> <ami-name> [-- pytest-args]" |
| 781 | + exit 1 |
| 782 | + fi |
| 783 | + shift |
| 784 | + ;; |
| 785 | + esac |
770 | 786 | done
|
771 | 787 |
|
772 |
| - # Check AWS Vault profile |
773 |
| - if [ -z "''${AWS_VAULT:-}" ]; then |
774 |
| - echo "Error: AWS_VAULT environment variable must be set with the profile name" |
775 |
| - echo "Usage: aws-vault exec supabase-dev -- nix run .#run-testinfra <ami-name>" |
| 788 | + if [ -z "$PROFILE" ]; then |
| 789 | + echo "Error: --profile must be specified" |
| 790 | + echo "Usage: aws-vault exec <profile> -- nix run .#run-testinfra -- --profile <profile> <ami-name> [-- pytest-args]" |
776 | 791 | exit 1
|
777 | 792 | fi
|
778 | 793 |
|
779 |
| - # Check for AMI name argument |
780 |
| - if [ -z "''${1:-}" ]; then |
| 794 | + if [ -z "$AMI_NAME" ]; then |
781 | 795 | echo "Error: AMI name must be provided"
|
782 |
| - echo "Usage: aws-vault exec supabase-dev -- nix run .#run-testinfra <ami-name>" |
| 796 | + echo "Usage: aws-vault exec <profile> -- nix run .#run-testinfra -- --profile <profile> <ami-name> [-- pytest-args]" |
783 | 797 | exit 1
|
784 | 798 | fi
|
785 | 799 |
|
786 |
| - AMI_NAME="$1" |
787 |
| - REGION="ap-southeast-1" |
788 |
| - RUN_ID=$(date +%s) |
| 800 | + # Set environment variables |
| 801 | + export AWS_VAULT="$PROFILE" |
| 802 | + export AMI_NAME="$AMI_NAME" |
| 803 | + export TESTINFRAPY_TIMEOUT="300" # 5 minutes timeout |
789 | 804 |
|
790 |
| - # Run tests |
791 |
| - ${pythonEnv}/bin/pytest -vv -s testinfra/test_ami_nix.py --ami-name="$AMI_NAME" |
792 |
| -
|
793 |
| - # Cleanup test instance |
| 805 | + # Function to cleanup instance on exit |
794 | 806 | cleanup_instance() {
|
795 |
| - aws ec2 --region $REGION describe-instances \ |
796 |
| - --filters "Name=tag:testinfra-run-id,Values=$RUN_ID" \ |
797 |
| - --query "Reservations[].Instances[].InstanceId" \ |
798 |
| - --output text | xargs -r aws ec2 terminate-instances \ |
799 |
| - --region $REGION --instance-ids || true |
| 807 | + if [ -n "$INSTANCE_ID" ]; then |
| 808 | + echo "Cleaning up instance $INSTANCE_ID..." |
| 809 | + aws-vault exec "$AWS_VAULT" -- aws ec2 terminate-instances --instance-ids "$INSTANCE_ID" |
| 810 | + fi |
800 | 811 | }
|
801 | 812 |
|
802 |
| - trap cleanup_instance EXIT |
| 813 | + # Set up trap to cleanup on exit or interruption |
| 814 | + trap cleanup_instance EXIT INT TERM |
| 815 | +
|
| 816 | + # Run tests with AMI name as environment variable and any additional pytest arguments |
| 817 | + ${pythonEnv}/bin/pytest -vv -s $PYTEST_ARGS testinfra/test_ami_nix.py |
803 | 818 | EOL
|
804 | 819 | chmod +x $out/bin/run-testinfra
|
805 | 820 | '';
|
|
0 commit comments