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