Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions simnet_tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Simulation tests, or high level integration tests.


_The content of this directory is meant to be used by Parity's private CI/CD
infrastructure with private tools. At the moment those tools are still early
stage of development and we don't when if / when they will available for
public use._


## Content of this dir.

`configs` dir contains config files in toml format that describe how to
configure the simulation network that you want to launch.

`tests` dir contains [cucumber](https://cucumber.io/) files. Those are
Behavior-Driven Development test files that describe tests in plain English.
Under the hood there are assertions that specific metrics should have specific
values.

At the moment we have only one test for parachains: `/parachains.features`
This test uses a JS script that we added to simnet image and it's launched
by this step in the cucumber file:
` Then launch 'node' with parameters '--unhandled-rejections=strict /usr/local/bin/simnet_scripts test_parachain ./configs/adder.json ws://localhost:11222 100 10'`

`run_test.sh` is a script meant to ease up launching a test.
In order to use this script locally, you need to install
[gurke](https://github.com/paritytech/gurke)
This script also helps preparing the test environment. Once you have access to
a kubernetes cluster (meaning you can do `kubectl get pods`) you can run this
script with no arguments, like `./run_test.sh` and tests should run.
Kubernetes cluster can be local, spawned with
[kind](https://kind.sigs.k8s.io/docs/user/quick-start/#installation)
or an instance living in the
[cloud](https://github.com/paritytech/gurke/blob/main/docs/How-to-setup-access-to-gke-k8s-cluster.md)
7 changes: 7 additions & 0 deletions simnet_tests/configs/adder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Header":{
"number":"u64",
"parent_hash":"Hash",
"post_state":"Hash"
}
}
47 changes: 47 additions & 0 deletions simnet_tests/configs/simple_rococo_testnet.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[settings.setup]
timeout = 300

[settings.defaults]
command = "/usr/local/bin/polkadot"
chain-spec = "rococo-local.json"
timeout = 300

[init_nodes.chainspec]
image = "{{get_env(name="SYNTHIMAGE") | safe }}"
command = "/usr/local/bin/polkadot build-spec --chain rococo-local --disable-default-bootnode --raw > /cfg/rococo-local.json"
fetch-files = [ "/cfg/rococo-local.json" ]
timeout = 300

[init_nodes.parachain-specs]
image = "{{get_env(name="COLIMAGE") | safe }}"
command = """
/usr/local/bin/adder-collator export-genesis-state > /cfg/genesis-state &&
/usr/local/bin/adder-collator export-genesis-wasm > /cfg/genesis-wasm
"""
fetch-files = [ "/cfg/genesis-wasm", "/cfg/genesis-state" ]
timeout = 300

[nodes.alice]
validator = true
extra-args = ["--alice"]

[nodes.bob]
validator = true
extra-args = ["--bob"]

[nodes.collator01]
image = "{{get_env(name="COLIMAGE") | safe }}"
command-with-args = "/usr/local/bin/adder-collator --chain /cfg/rococo-local.json --port 30333 --no-mdns --bootnodes /dns/bootnode/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp"

[nodes.nodejs]
image = "{{get_env(name="SCRIPTSIMAGE") | safe }}"
command-with-args = """
cd simnet_scripts;
npm run build;
node /usr/local/bin/simnet_scripts register_parachain /cfg/genesis-wasm /cfg/genesis-state 100 true ws://bootnode:9944;
tail -f /dev/null
"""
copy-files = [
"genesis-state",
"genesis-wasm",
]
118 changes: 118 additions & 0 deletions simnet_tests/run_huge_network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/bash

### ARGS FOR THIS SCRIPT ###
# ./${SCRIPT_NAME} NAMESPACE LOG_PATH VALIDATOR_NUM
# All args have default values, specify args to override
# e.g: ./${SCRIPT_NAME} radu-test parity/substrate:some_feature /var/log/gurke 20

# =================================================================================================
# Get cmdline arguments or assign default values to them.
# =================================================================================================

NAMESPACE=${1:-gurke-"$(random_string)"-runtest}
LOG_PATH=${3:-"${SCRIPT_PATH}/logs"}
VALIDATOR_NUM=${4:-20}

SCRIPT_NAME="$0"
SCRIPT_PATH=$(dirname "${SCRIPT_NAME}") # relative
SCRIPT_PATH=$(cd "${SCRIPT_PATH}" && pwd) # absolutized and normalized

# =================================================================================================
# Determine if docker or podman is available.
# =================================================================================================

DOCKER_OR_PODMAN=$(which podman)
if [ $? -ne 0 ]; then
DOCKER_OR_PODMAN=$(which docker)
if [ $? -ne 0 ]; then
echo "In order to run this script you need either podman or docker installed"
fi
fi

echo "Running with $DOCKER_OR_PODMAN as container solution"


rm $SCRIPT_PATH/shared/seeds_file.txt

set -eou pipefail

# =================================================================================================
# Generate a default chainspec and add validator keys to it
# =================================================================================================

SPEC_NAME=rococo-local.json
SHARED_DIR="$SCRIPT_PATH/shared"
mkdir -p $SHARED_DIR

echo "Generating a default local rococo chainspec"
$DOCKER_OR_PODMAN run -v -i --log-driver=none -a stdout parity/rococo:rococo-v1 build-spec --chain rococo-local --disable-default-bootnode | cat > $SHARED_DIR/$SPEC_NAME

echo "Removing default validators from spec"

$DOCKER_OR_PODMAN run -v $SHARED_DIR:/mnt/shared paritytech/simnetscripts:latest clear_authorities /mnt/shared/$SPEC_NAME
echo "Adding validators to the spec"

for VALIDATOR_IDX in $(seq 1 $VALIDATOR_NUM)
do
VALIDATOR_SEED="Validator $VALIDATOR_IDX"
echo $VALIDATOR_SEED
echo $VALIDATOR_SEED >> $SCRIPT_PATH/shared/seeds_file.txt
echo "//$VALIDATOR_SEED" > $SCRIPT_PATH/shared/$VALIDATOR_IDX.txt
done

echo "Adding validators"

$DOCKER_OR_PODMAN run -v $SHARED_DIR:/mnt/shared paritytech/simnetscripts:latest add_authorities_from_file /mnt/shared/$SPEC_NAME /mnt/shared/seeds_file.txt

# =================================================================================================
# Run the testing
# =================================================================================================

function random_string {
head -1 <(fold -w 30 <(tr -dc 'a-z0-9' < /dev/urandom))
}

mkdir -p "${SCRIPT_PATH}"/logs

echo "Running tests in namespace: ${NAMESPACE}"
echo "Storing scripts logs to: ${LOG_PATH}"

function forward_port {
if is_port_forward_running ; then
kill_previous_job
fi
start_forwading_job
}

FORWARD_GREP_FILTER='kubectl.*[p]ort-forward.*bootnode'

function is_port_forward_running {
# shellcheck disable=SC2009
ps aux | grep -qE "${FORWARD_GREP_FILTER}"
}

function kill_previous_job {
# shellcheck disable=SC2009
job_pid=$(ps aux | grep -E "${FORWARD_GREP_FILTER}" | awk '{ print $2 }')
echo "INFO Killed forwading port 9944 into bootnode"
kill "${job_pid}"
}

function start_forwading_job {
echo "INFO Started forwading port 9944 into bootnode"
kubectl -n "${NAMESPACE}" \
port-forward pod/bootnode 11222:9944 &> "${LOG_PATH}/forward-${NAMESPACE}.log" &
sleep 2
}

set -x # echo the commands to stdout
export SHARED_DIR=$SHARED_DIR
export NODE_COUNT=$VALIDATOR_NUM
/home/theodor/github.com/montekki/gurke/target/debug/gurke spawn --config "/home/theodor/github.com/montekki/gurke/examples/rococo/huge_local_rococo_testnet.toml" \
-n "fedor-gurke"

forward_port

# Run tests
gurke test "${NAMESPACE}" "${SCRIPT_PATH}"/tests --log-path "${LOG_PATH}"

102 changes: 102 additions & 0 deletions simnet_tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash

### ARGS FOR THIS SCRIPT ###
# ./${SCRIPT_NAME} NAMESPACE IMAGE LOG_PATH
# All args have default values, specify args to override
# e.g: ./${SCRIPT_NAME} radu-test parity/substrate:some_feature /var/log/gurke

set -eou pipefail
SCRIPT_NAME="$0"
SCRIPT_PATH=$(dirname "${SCRIPT_NAME}") # relative
SCRIPT_PATH=$(cd "${SCRIPT_PATH}" && pwd) # absolutized and normalized

function random_string {
head -1 <(fold -w 30 <(tr -dc 'a-z0-9' < /dev/urandom))
}

NAMESPACE=${1:-gurke-"$(random_string)"-runtest}
IMAGE=${2:-"docker.io/paritypr/synth-wave:master"}
LOG_PATH=${3:-"${SCRIPT_PATH}/logs"}
COLIMAGE=${4:-"docker.io/paritypr/colander:master"}
SCRIPTSIMAGE=${5:-"docker.io/paritytech/simnet:latest"}

mkdir -p "${SCRIPT_PATH}"/logs

echo "Running tests in namespace: ${NAMESPACE}"
echo "Testing image: ${IMAGE}"
echo "Storing scripts logs to: ${LOG_PATH}"
echo "Colator image is ${COLIMAGE}"
echo "SCRIPTSIMAGE image is ${SCRIPTSIMAGE}"


function forward_port {

# RUN_IN_CONTAINER is env var that is set in the dockerfile
# use the -v operator to explicitly test if a variable is set
if [[ ! -v RUN_IN_CONTAINER ]] ; then
if is_port_forward_running ; then
kill_previous_job
fi
fi
start_forwading_job
}

FORWARD_GREP_FILTER='kubectl.*[p]ort-forward.*svc/rpc.*11222'

function is_port_forward_running {
# shellcheck disable=SC2009
ps aux | grep -qE "${FORWARD_GREP_FILTER}"
}

function kill_previous_job {
# shellcheck disable=SC2009
job_pid=$(ps aux | grep -E "${FORWARD_GREP_FILTER}" | awk '{ print $2 }')
echo "INFO Killed forwading port 9944 into bootnode"
kill "${job_pid}"
}

function start_forwading_job {
kubectl -n "${NAMESPACE}" \
expose pod bootnode \
--name=rpc \
--type=NodePort \
--target-port=9944 \
--port=9944
kubectl -n "${NAMESPACE}" \
port-forward svc/rpc 11222:9944 &> "${LOG_PATH}/forward-${NAMESPACE}.log" &
sleep 2
echo "INFO Started forwading port 9944 into bootnode"
}

function update_api {
pwd
cd "${SCRIPT_PATH}"/../../simnet_scripts/
npm run build
cd -
pwd
}

echo "INFO: Checking if namespace has no pods"
kubectl -n "${NAMESPACE}" get pods

export NAMESPACE="${NAMESPACE}"
export COLIMAGE="${COLIMAGE}"
export SYNTHIMAGE="${IMAGE}"
export SCRIPTSIMAGE="${SCRIPTSIMAGE}"

cd "${SCRIPT_PATH}"

set -x # echo the commands to stdout
gurke spawn --config "${SCRIPT_PATH}"/configs/simple_rococo_testnet.toml \
-n "${NAMESPACE}" \
--image "${IMAGE}"

echo "INFO: Checking if pods launched correctly"
kubectl -n "${NAMESPACE}" get pods -o wide
echo "INFO: Updating Polkadot JS API"
update_api
forward_port

# Run tests
gurke test "${NAMESPACE}" "${SCRIPT_PATH}"/tests --log-path "${LOG_PATH}"

6 changes: 6 additions & 0 deletions simnet_tests/tests/parachains.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Feature: ParaTesting

Scenario: spawn parachains network and check parachains
Given a test network
Then sleep 200 seconds
Then launch 'node' with parameters '--unhandled-rejections=strict /usr/local/bin/simnet_scripts test_parachain ./configs/adder.json ws://localhost:11222 100 10'