Skip to content

Commit e59e966

Browse files
committed
feat: run components of testinfra locally as distinct apps
1 parent 965766a commit e59e966

File tree

1 file changed

+50
-35
lines changed

1 file changed

+50
-35
lines changed

flake.nix

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
pytest
9494
pytest-testinfra
9595
requests
96+
ec2instanceconnectcli
97+
paramiko
9698
]);
9799
sfcgal = pkgs.callPackage ./nix/ext/sfcgal/sfcgal.nix { };
98100
supabase-groonga = pkgs.callPackage ./nix/supabase-groonga.nix { };
@@ -750,54 +752,67 @@
750752
} ''
751753
mkdir -p $out/bin
752754
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
768784
done
769785
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]"
774789
exit 1
775790
fi
776791
777-
# Check for AMI name argument
778-
if [ -z "''${1:-}" ]; then
792+
if [ -z "$AMI_NAME" ]; then
779793
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]"
781795
exit 1
782796
fi
783797
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
787802
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
792804
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
798809
}
799810
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
801816
EOL
802817
chmod +x $out/bin/run-testinfra
803818
'';

0 commit comments

Comments
 (0)