Skip to content

Commit 0ef5de6

Browse files
committed
feat: run components of testinfra locally as distinct apps
1 parent 24a6ee5 commit 0ef5de6

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 { };
@@ -752,54 +754,67 @@
752754
} ''
753755
mkdir -p $out/bin
754756
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
770786
done
771787
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]"
776791
exit 1
777792
fi
778793
779-
# Check for AMI name argument
780-
if [ -z "''${1:-}" ]; then
794+
if [ -z "$AMI_NAME" ]; then
781795
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]"
783797
exit 1
784798
fi
785799
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
789804
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
794806
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
800811
}
801812
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
803818
EOL
804819
chmod +x $out/bin/run-testinfra
805820
'';

0 commit comments

Comments
 (0)