Skip to content

Commit

Permalink
Changed script to flag based instead of interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
smickovskid committed Dec 11, 2023
1 parent fffd8a7 commit e546099
Showing 1 changed file with 119 additions and 84 deletions.
203 changes: 119 additions & 84 deletions integration-tests/smoke/run_test.sh
Original file line number Diff line number Diff line change
@@ -1,73 +1,138 @@
#!/bin/bash

config_loaded=0

read_input() {
local prompt=$1
local allow_empty=$2
local input_type=$3
local input

while true; do
read -p "$prompt" input

if [[ "$input_type" == "number" ]]; then
if [[ $input =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
break
else
echo "Please enter a valid number."
continue
fi
fi
# Get working dir of script
SCRIPT=$(readlink -f "$0")
TEST_PATH=$(dirname "$SCRIPT")

if [[ -n "$input" || "$allow_empty" == "true" ]]; then
break
else
echo "This is a required field. Please enter a value."
fi
done
# Init
SELECTED_NETWORKS=""
http_url=""
ws_url=""
user=""
log_level="DEBUG" # Default value
private_key=""
docker_image_repo=""
funding_per_node="0.1" # Default value
docker_image_version=""
additional_args=""
product=""
list_networks=0
load_config=""
show_help=0

echo "$input"
# Help
usage() {
echo "Usage: $0 [--list-networks] [--load-config file] [--selected-networks SELECTED_NETWORKS] [--http-url http_url] [--ws-url ws_url] [--user user] [--log-level log_level] [--private-key private_key] [--docker-image-repo docker_image_repo] [--funding-per-node funding_per_node] [--docker-image-version docker_image_version] [--test-suite test_suite] [--product product]"
echo
echo "Options:"
echo " --list-networks List all available networks"
echo " --load-config Load configuration from a file"
echo " --selected-networks Specify the selected networks"
echo " --http-url Specify HTTP RPC url"
echo " --ws-url Specify WS RPC url"
echo " --user Specify the user"
echo " --log-level Specify the log level (INFO, DEBUG, ERROR). Default: DEBUG"
echo " --private-key Specify the private key"
echo " --docker-image-repo Specify docker image repository"
echo " --funding-per-node Specify funding per node. Default: 0.1"
echo " --docker-image-version Specify docker image version"
echo " --test-suite Specify additional test suite args"
echo " --product Specify the product (Automation)"
exit 0
}

select_network_and_set_urls() {
echo "Select a network to run the test on:"
select network in SIMULATED SIMULATED_1 SIMULATED_2 SIMULATED_BESU_NONDEV_1 SIMULATED_BESU_NONDEV_2 SIMULATED_NONDEV ETHEREUM_MAINNET GOERLI SEPOLIA KLAYTN_MAINNET KLAYTN_BAOBAB METIS_ANDROMEDA METIS_STARDUST ARBITRUM_MAINNET ARBITRUM_GOERLI ARBITRUM_SEPOLIA OPTIMISM_MAINNET OPTIMISM_GOERLI OPTIMISM_SEPOLIA BASE_GOERLI BASE_SEPOLIA CELO_ALFAJORES CELO_MAINNET RSK POLYGON_MUMBAI POLYGON_MAINNET AVALANCHE_FUJI AVALANCHE_MAINNET QUORUM SCROLL_SEPOLIA SCROLL_MAINNET BASE_MAINNET BSC_TESTNET BSC_MAINNET LINEA_GOERLI LINEA_MAINNET POLYGON_ZKEVM_GOERLI POLYGON_ZKEVM_MAINNET FANTOM_TESTNET FANTOM_MAINNET WEMIX_TESTNET WEMIX_MAINNET KROMA_SEPOLIA KROMA_MAINNET ZK_SYNC_GOERLI ZK_SYNC_MAINNET; do
if [[ -n "$network" ]]; then
SELECTED_NETWORKS="$network"
break
else
echo "Invalid selection. Please try again."
list_networks() {
echo "Available networks:"
networks="SIMULATED SIMULATED_1 SIMULATED_2 SIMULATED_BESU_NONDEV_1 SIMULATED_BESU_NONDEV_2 SIMULATED_NONDEV ETHEREUM_MAINNET GOERLI SEPOLIA KLAYTN_MAINNET KLAYTN_BAOBAB METIS_ANDROMEDA METIS_STARDUST ARBITRUM_MAINNET ARBITRUM_GOERLI ARBITRUM_SEPOLIA OPTIMISM_MAINNET OPTIMISM_GOERLI OPTIMISM_SEPOLIA BASE_GOERLI BASE_SEPOLIA CELO_ALFAJORES CELO_MAINNET RSK POLYGON_MUMBAI POLYGON_MAINNET AVALANCHE_FUJI AVALANCHE_MAINNET QUORUM SCROLL_SEPOLIA SCROLL_MAINNET BASE_MAINNET BSC_TESTNET BSC_MAINNET LINEA_GOERLI LINEA_MAINNET POLYGON_ZKEVM_GOERLI POLYGON_ZKEVM_MAINNET FANTOM_TESTNET FANTOM_MAINNET WEMIX_TESTNET WEMIX_MAINNET KROMA_SEPOLIA KROMA_MAINNET ZK_SYNC_GOERLI ZK_SYNC_MAINNET"
read -r -a network_array <<< "$networks"
columns=4
for (( i=0; i<${#network_array[@]}; i++ )); do
# Print network name
printf '%-30s' "${network_array[i]}"

# New line every 'columns' networks
if (( (i + 1) % columns == 0 )); then
echo
fi
done

http_url=$(read_input "Enter HTTP RPC url for $SELECTED_NETWORKS: " false)
ws_url=$(read_input "Enter WS RPC url for $SELECTED_NETWORKS: " false)
echo
exit 0
}

# Check for an existing config
load_previous_config() {
local config_file="/tmp/${product}_config.env"
if [ -f "$config_file" ]; then
read -p "Previous configuration for $product found. Load it? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Loading previous configuration..."
source "$config_file"
config_loaded=1
return 0
# Validate config
load_and_validate_config() {
if [ -f "$load_config" ]; then
source "$load_config"

# Validate mandatory variables
if [ -z "$SELECTED_NETWORKS" ]; then
echo "Error: SELECTED_NETWORKS variable is missing in the configuration file."
exit 1
fi
else
echo "Error: Configuration file '$load_config' not found."
exit 1
fi
return 1
}

# Parse command-line options
while [[ $# -gt 0 ]]; do
case "$1" in
--list-networks ) list_networks=1; shift ;;
--help ) show_help=1; shift ;;
--load-config ) load_config="$2"; shift 2 ;;
--selected-networks ) SELECTED_NETWORKS="$2"; shift 2 ;;
--http-url ) http_url="$2"; shift 2 ;;
--ws-url ) ws_url="$2"; shift 2 ;;
--user ) user="$2"; shift 2 ;;
--log-level ) log_level="$2"; shift 2 ;;
--private-key ) private_key="$2"; shift 2 ;;
--docker-image-repo ) docker_image_repo="$2"; shift 2 ;;
--funding-per-node ) funding_per_node="$2"; shift 2 ;;
--docker-image-version ) docker_image_version="$2"; shift 2 ;;
--test-suite ) additional_args="$2"; shift 2 ;;
--product ) product="$2"; shift 2 ;;
--) shift; break ;;
*) break ;;
esac
done

if [[ "$list_networks" -eq 1 ]]; then
list_networks
fi

# Run tests
run_automation() {
echo "Running command with provided environment variables..."
go test -v -test.run "$SUITE" "${TEST_PATH}/automation_test.go"
}

# Load config
if [[ -n "$load_config" ]]; then
load_and_validate_config
run_automation
exit 0
fi

if [[ "$show_help" -eq 1 ]]; then
usage
exit 0
fi

# Check arguments
if [[ -z "$SELECTED_NETWORKS" || -z "$http_url" || -z "$ws_url" || -z "$user" || -z "$private_key" || -z "$docker_image_repo" || -z "$docker_image_version" || -z "$additional_args" || -z "$product" ]]; then
echo "All options are mandatory."
usage
exit 1
fi

# Save config
save_current_config() {
local config_file="/tmp/${product}_config.env"
cat << EOF > "$config_file"
export SELECTED_NETWORKS="$SELECTED_NETWORKS"
export ${SELECTED_NETWORKS}_HTTP_URLS="$http_url"
export ${SELECTED_NETWORKS}_URLS="$ws_url"
export ${SELECTED_NETWORKS}_WS_URLS="$ws_url"
export CHAINLINK_ENV_USER="$user"
export TEST_LOG_LEVEL="$log_level"
export ${SELECTED_NETWORKS}_KEYS="$private_key"
Expand All @@ -77,41 +142,11 @@ export CHAINLINK_VERSION="$docker_image_version"
export product="$product"
export SUITE="$additional_args"
EOF
source "$config_file"
source "$config_file"
}

# Run tests
run_automation() {
echo "Running command with provided environment variables..."
go test -v -test.run "$SUITE" ./automation_test.go
}

# Catch errors
trap 'echo "An error occurred. Exiting..."; exit 1;' ERR

PS3="Please select a product: "
options=("Automation" "Exit")
select product in "${options[@]}"; do
case $product in
"Automation")
if ! load_previous_config; then
user=$(read_input "Please enter a user that the test will run under: " false)
log_level=$(read_input "Select test log level (INFO, DEBUG, ERROR): " false)
select_network_and_set_urls
private_key=$(read_input "Enter the private key the test should use: " false)
docker_image_repo=$(read_input "Enter docker image repository for core: " false)
funding_per_node=$(read_number "Enter funding per node: " false, "number")
docker_image_version=$(read_input "Enter docker image version for core: " false)
additional_args=$(read_input "Enter the test suite: " false)
save_current_config
fi
run_automation
break
;;
"Exit")
echo "Exiting the wizard."
exit 0
;;
*) echo "Invalid option $REPLY";;
esac
done
# Main execution
save_current_config
run_automation

0 comments on commit e546099

Please sign in to comment.