From 70852e9400ea7931d01be4ddd670d8aa94a49bbc Mon Sep 17 00:00:00 2001
From: Tsvetomir Dimitrov <tsvetomir@parity.io>
Date: Sat, 28 Jan 2023 14:00:45 +0200
Subject: [PATCH] Make zombienet tests native friendly (#6588)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Make zombienet tests native friendly

* Update zombienet_tests/README.md

Co-authored-by: Bastian Köcher <info@kchr.de>

* Get rid of `{{ZOMBIENET_NATIVE_BINARY_PREFIX}}`

* Remove pointless changes

* add set_env helper and doc in readme

* changes from feedback

---------

Co-authored-by: Bastian Köcher <info@kchr.de>
Co-authored-by: Javier Viola <javier@parity.io>
---
 polkadot/zombienet_tests/.set_env             | 20 +++++++++
 polkadot/zombienet_tests/README.md            | 43 +++++++++++++++++++
 .../functional/0001-parachains-pvf.zndsl      |  6 +--
 .../0003-parachains-garbage-candidate.toml    |  2 +-
 .../zombienet_tests/misc/0001-paritydb.toml   |  2 +-
 .../smoke/0001-parachains-smoke-test.toml     |  2 +-
 ...3-deregister-register-validator-smoke.toml |  1 -
 7 files changed, 69 insertions(+), 7 deletions(-)
 create mode 100644 polkadot/zombienet_tests/.set_env

diff --git a/polkadot/zombienet_tests/.set_env b/polkadot/zombienet_tests/.set_env
new file mode 100644
index 00000000000..f5d09f21873
--- /dev/null
+++ b/polkadot/zombienet_tests/.set_env
@@ -0,0 +1,20 @@
+pathprepend() {
+  for ((i=$#; i>0; i--));
+  do
+    ARG="$@[$i]"
+    if [ -d "$ARG" ] && [[ ":$PATH:" != *":$ARG:"* ]]; then
+        PATH="$ARG${PATH:+":$PATH"}"
+    fi
+  done
+}
+
+# paths are prepend in order, so you can manage with version will run
+# by the order of this array
+CUSTOM_PATHS=(
+  "~/polkadot/target/release"
+  "~/polkadot/target/testnet"
+  "~/cumulus/target/release"
+)
+
+pathprepend $CUSTOM_PATHS
+export PATH=$PATH
diff --git a/polkadot/zombienet_tests/README.md b/polkadot/zombienet_tests/README.md
index 5760e203d95..021d3643e7f 100644
--- a/polkadot/zombienet_tests/README.md
+++ b/polkadot/zombienet_tests/README.md
@@ -12,6 +12,49 @@ _The content of this directory is meant to be used by Parity's private CI/CD inf
 * [zombienet repo](https://github.com/paritytech/zombienet)
 * [zombienet book](https://paritytech.github.io/zombienet/)
 
+## Running tests locally
+
+To run any test locally use the native provider (`zombienet test -p native ...`) you need first build the binaries. They are:
+
+* adder-collator -> polkadot/target/testnet/adder-collator
+* malus -> polkadot/target/testnet/malus
+* polkadot -> polkadot/target/testnet/polkadot
+* polkadot-collator -> cumulus/target/release/polkadot-parachain
+* undying-collator -> polkadot/target/testnet/undying-collator
+
+To build them use:
+* adder-collator -> `cargo build --profile testnet -p test-parachain-adder-collator`
+* undying-collator -> `cargo build --profile testnet -p test-parachain-undying-collator`
+* malus -> cargo build --profile testnet -p polkadot-test-malus
+* polkadot (in polkadot repo) and polkadot-collator (in cumulus repo) -> `cargo build --profile testnet`
+
+One solution is to use the `.set_env` file (from this directory) and fill the `CUSTOM_PATHS` before *source* it to patch the PATH of your system to find the binaries you just built.
+
+E.g.:
+```
+$ cat .set_env
+(...)
+# by the order of this array
+CUSTOM_PATHS=(
+  "~/polkadot/target/release"
+  "~/polkadot/target/testnet"
+  "~/cumulus/target/release"
+)
+(...)
+
+source .set_env
+```
+
+Then you have your `PATH` customized and ready to run `zombienet`.
+ **NOTE**: You should need to do this ones per terminal session, since we are patching the `PATH` and re-exporting. **Or** you can also `source` this file in your `.bashrc` file to get executed automatically in each new session.
+
+Example:
+
+You can run a test locally by executing:
+```sh
+zombienet test -p native 0001-parachains-pvf.zndsl
+```
+
 ## Questions / permissions
 
 Ping in element Javier (@javier:matrix.parity.io) to ask questions or grant permission to run the test from your local setup.
diff --git a/polkadot/zombienet_tests/functional/0001-parachains-pvf.zndsl b/polkadot/zombienet_tests/functional/0001-parachains-pvf.zndsl
index 1f187498d78..259f05b8f68 100644
--- a/polkadot/zombienet_tests/functional/0001-parachains-pvf.zndsl
+++ b/polkadot/zombienet_tests/functional/0001-parachains-pvf.zndsl
@@ -63,7 +63,7 @@ eve: reports histogram polkadot_pvf_preparation_time has at least 1 samples in b
 one: reports histogram polkadot_pvf_preparation_time has at least 1 samples in buckets ["0.1", "0.5", "1", "2", "3", "10"] within 10 seconds
 two: reports histogram polkadot_pvf_preparation_time has at least 1 samples in buckets ["0.1", "0.5", "1", "2", "3", "10"] within 10 seconds
 
-# Check all buckets >= 20.             
+# Check all buckets >= 20.
 alice: reports histogram polkadot_pvf_preparation_time has 0 samples in buckets ["20", "30", "60", "180", "+Inf"] within 10 seconds
 bob: reports histogram polkadot_pvf_preparation_time has 0 samples in buckets ["20", "30", "60", "180", "+Inf"] within 10 seconds
 charlie: reports histogram polkadot_pvf_preparation_time has 0 samples in buckets ["20", "30", "60", "180", "+Inf"] within 10 seconds
@@ -78,8 +78,8 @@ two: reports histogram polkadot_pvf_preparation_time has 0 samples in buckets ["
 # APPROVAL_EXECUTION_TIMEOUT(6s). Currently these are not differentiated by metrics
 # because the metrics are defined in `polkadot-node-core-pvf` which is a level below
 # the relevant subsystems.
-# That being said, we will take the simplifying assumption of testing only the 
-# 2s timeout. 
+# That being said, we will take the simplifying assumption of testing only the
+# 2s timeout.
 # We do this check by ensuring all executions fall into bucket le="2" or lower.
 # First, check if we have at least 1 sample, but we should have many more.
 alice: reports histogram polkadot_pvf_execution_time has at least 1 samples in buckets ["0.1", "0.5", "1", "2"] within 10 seconds
diff --git a/polkadot/zombienet_tests/functional/0003-parachains-garbage-candidate.toml b/polkadot/zombienet_tests/functional/0003-parachains-garbage-candidate.toml
index 8f82d30e2ac..7c4f5a9f1bc 100644
--- a/polkadot/zombienet_tests/functional/0003-parachains-garbage-candidate.toml
+++ b/polkadot/zombienet_tests/functional/0003-parachains-garbage-candidate.toml
@@ -27,7 +27,7 @@ requests = { memory = "2G", cpu = "1" }
   command = "malus suggest-garbage-candidate"
   args = ["-lparachain=debug,MALUS=trace"]
   count = 1
- 
+
 {% for id in range(2000,2003) %}
 [[parachains]]
 id = {{id}}
diff --git a/polkadot/zombienet_tests/misc/0001-paritydb.toml b/polkadot/zombienet_tests/misc/0001-paritydb.toml
index c40e0969d08..38fa5689819 100644
--- a/polkadot/zombienet_tests/misc/0001-paritydb.toml
+++ b/polkadot/zombienet_tests/misc/0001-paritydb.toml
@@ -20,7 +20,7 @@ requests = { memory = "2G", cpu = "1" }
   name = "validator"
   count = 10
   args = ["-lparachain=debug", "--db=paritydb"]
- 
+
 {% for id in range(2000,2010) %}
 [[parachains]]
 id = {{id}}
diff --git a/polkadot/zombienet_tests/smoke/0001-parachains-smoke-test.toml b/polkadot/zombienet_tests/smoke/0001-parachains-smoke-test.toml
index 4d4324822f4..d132945aeea 100644
--- a/polkadot/zombienet_tests/smoke/0001-parachains-smoke-test.toml
+++ b/polkadot/zombienet_tests/smoke/0001-parachains-smoke-test.toml
@@ -21,7 +21,7 @@ addToGenesis = false
   [parachains.collator]
   name = "collator01"
   image = "{{COL_IMAGE}}"
-  command = "/usr/local/bin/adder-collator"
+  command = "adder-collator"
   args = [ "-lruntime=debug,parachain=trace" ]
 
 [types.Header]
diff --git a/polkadot/zombienet_tests/smoke/0003-deregister-register-validator-smoke.toml b/polkadot/zombienet_tests/smoke/0003-deregister-register-validator-smoke.toml
index bc1dbe6d3ed..48c79a861be 100644
--- a/polkadot/zombienet_tests/smoke/0003-deregister-register-validator-smoke.toml
+++ b/polkadot/zombienet_tests/smoke/0003-deregister-register-validator-smoke.toml
@@ -21,4 +21,3 @@ command = "polkadot"
   [[relaychain.nodes]]
   name = "dave"
   args = [ "-lruntime=debug,parachain=trace" ]
-  
\ No newline at end of file
-- 
GitLab