Skip to content

Commit

Permalink
Merge branch 'main' into feat/chain-spec-mutator
Browse files Browse the repository at this point in the history
  • Loading branch information
Fahrrader committed Jun 6, 2023
2 parents 277230e + a9e9d75 commit c7bf197
Show file tree
Hide file tree
Showing 26 changed files with 696 additions and 575 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/parser-wrapper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dsl-parser-wrapper"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
description = "Zombienet DSL parser: produces a test definition, in json format, that can be used with the ZombieNet's test-runnner."
license = "GPL-3.0-or-later"
Expand Down
16 changes: 16 additions & 0 deletions crates/parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ pub enum Operator {
IsAtMost,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Copy, Clone)]
pub enum MathOps {
Minus,
Plus,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Comparison {
pub op: Operator,
Expand Down Expand Up @@ -43,6 +49,16 @@ pub enum AssertionKind {
#[serde(with = "optional_timeout")]
timeout: Option<Duration>,
},
CalcMetrics {
node_name: NodeName,
metric_name_a: String,
math_ops: MathOps,
metric_name_b: String,
op: Operator,
target_value: u64,
#[serde(with = "optional_timeout")]
timeout: Option<Duration>,
},
ParaRuntimeUpgrade {
node_name: NodeName,
para_id: ParaId,
Expand Down
44 changes: 43 additions & 1 deletion crates/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ fn parse_taget_value(pair: Pair<Rule>) -> Result<u64, ParserError> {
.map_err(|_| ParserError::ParseError(format!("Can't parse {target_str} as u64")))
}

fn parse_math_ops(pair: Pair<Rule>) -> Result<ast::MathOps, ParserError> {
let mut pairs = pair.into_inner();
let math_ops = get_pair(&mut pairs, "math_ops")?;
let sign = match math_ops.as_rule() {
Rule::plus => ast::MathOps::Plus,
Rule::minus => ast::MathOps::Minus,
_ => return Err(ParserError::UnreachableRule(format!("{math_ops:?}"))),
};

Ok(sign)
}

fn parse_comparison(pair: Pair<Rule>) -> Result<ast::Comparison, ParserError> {
let mut inner_pairs = pair.into_inner();
let op_rule = get_pair(&mut inner_pairs, "op_rule")?;
Expand Down Expand Up @@ -333,6 +345,36 @@ pub fn parse(unparsed_file: &str) -> Result<ast::TestDefinition, errors::ParserE

assertions.push(assertion);
}
Rule::calc_metrics => {
// Pairs should be in order:
// name, para_id, block_height, math_ops, finalized_height, comparison [timeout]
let mut pairs = record.into_inner();
let name = parse_name(get_pair(&mut pairs, "name")?)?;
let metric_name_a = get_pair(&mut pairs, "metric_name")?.as_str().to_string();
let math_ops = parse_math_ops(get_pair(&mut pairs, "math_ops")?)?;
let metric_name_b = get_pair(&mut pairs, "metric_name")?.as_str().to_string();
let comparison = parse_comparison(get_pair(&mut pairs, "comparison")?)?;
let timeout: Option<Duration> = if let Some(within_rule) = pairs.next() {
Some(parse_within(within_rule)?)
} else {
None
};

let assertion = Assertion {
parsed: AssertionKind::CalcMetrics {
node_name: name.to_owned(),
metric_name_a,
math_ops,
metric_name_b,
op: comparison.op,
target_value: comparison.target_value,
timeout,
},
original_line,
};

assertions.push(assertion);
}
Rule::para_runtime_upgrade => {
// Pairs should be in order:
// name, para_id, file_or_uri, [timeout]
Expand Down Expand Up @@ -655,7 +697,7 @@ fn get_pair<'a>(
match pairs.next() {
Some(p) => Ok(p),
None => Err(ParserError::Unexpected(format!(
"Pair {rule_name} should exists"
"Pair {rule_name} should exist"
))),
}
}
32 changes: 32 additions & 0 deletions crates/parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,38 @@ fn report_parse_ok() {
assert_eq!(result, t);
}

#[test]
fn report_parse_calc_ok() {
let line: &str =
r#"alice: reports block height minus finalised block is at least 10 within 200 seconds"#;
let data = r#"{
"description": null,
"network": "./a.toml",
"creds": "config",
"assertions": [
{
"original_line": "alice: reports block height minus finalised block is at least 10 within 200 seconds",
"parsed": {
"fn": "CalcMetrics",
"args": {
"node_name": "alice",
"metric_name_a": "block height",
"math_ops": "Minus",
"metric_name_b": "finalised block",
"op": "IsAtLeast",
"target_value": 10,
"timeout": 200
}
}
}
]
}"#;
let t: TestDefinition = serde_json::from_str(data).unwrap();

let result = parse(&[NETWORK, CREDS, line].join("\n")).unwrap();
assert_eq!(result, t);
}

#[test]
fn para_dummy_upgrade_parse_ok() {
let line: &str = r#"alice: parachain 100 perform dummy upgrade within 200 seconds"#;
Expand Down
9 changes: 8 additions & 1 deletion crates/parser/src/zombienet.pest
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ op_lt = { "<" | "is lower than" }
op_gt = { ">" | "is greater than" | "greater than" }
op_eq = { "==" | "=" | "equals" | "is equal to" | "is" }
op_ineq = { "!=" }
comparison = { (op_lte | op_gte | op_gt | op_lt | op_eq | op_ineq) ~ int+ }
comparison = { (op_lte | op_gte | op_gt | op_lt | op_eq | op_ineq) ~ int+ }

// Math ops
minus = { "-" | "minus" }
plus = { "+" | "plus" }
math_ops = { plus | minus }

// commons
node_name = { name ~ colon }
Expand All @@ -59,6 +64,7 @@ creds = { "Creds:" ~ ("config" | file_path) }
is_up = { node_name ~ "is up" ~ within? }
para_is_registered = { node_name ~ parachain ~ "is registered" ~ within? }
para_block_height = { node_name ~ parachain ~ "block height" ~ comparison ~ within? }
calc_metrics = { node_name ~ "reports" ~ metric_name ~ math_ops ~ metric_name ~ comparison ~ within? }
para_runtime_upgrade = { node_name ~ parachain ~ "perform upgrade with" ~ ( uri | file_path ) ~ within? }
para_runtime_dummy_upgrade = { node_name ~ parachain ~ "perform dummy upgrade" ~ within? }
histogram = { node_name ~ "reports histogram" ~ metric_name ~ "has" ~ (comparison | int+) ~ "samples in buckets" ~ square_brackets_strings ~ within? }
Expand Down Expand Up @@ -90,6 +96,7 @@ file = { SOI ~ (
is_up |
para_is_registered |
para_block_height |
calc_metrics |
para_runtime_upgrade |
para_runtime_dummy_upgrade |
histogram |
Expand Down
6 changes: 5 additions & 1 deletion examples/0001-small-network.zndsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ alice: system event matches glob "*was backed*" within 10 seconds

#parachain tests
alice: parachain 100 is registered within 225 seconds
alice: parachain 100 block height is at least 10 within 200 seconds
alice: parachain 100 block height is at least 10 within 200 seconds

## test the block height - (or minus) finalised block
alice: reports block height minus finalised block is lower than 10 within 20 seconds
alice: reports block height - finalised block is lower than 10 within 20 seconds
4 changes: 4 additions & 0 deletions examples/0003-big-network.zndsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ Creds: config
# metrics
a: reports node_roles is 4
b: reports sub_libp2p_is_major_syncing is 0

## test the block height - (or minus) finalised block
a: reports block height minus finalised block is lower than 10 within 20 seconds
b: reports block height - finalised block is lower than 10 within 20 seconds
82 changes: 27 additions & 55 deletions flake-module.nix
Original file line number Diff line number Diff line change
@@ -1,64 +1,36 @@
{ self, ... }: {
perSystem = { config, self', inputs', pkgs, system, ... }:
let
# reuse existing ignores to clone source
cleaned-javascript-src = pkgs.lib.cleanSourceWith {
src = pkgs.lib.cleanSource ./javascript;
filter = pkgs.nix-gitignore.gitignoreFilterPure
(name: type:
# nix files are not used as part of build
(type == "regular" && pkgs.lib.strings.hasSuffix ".nix" name)
== false) [ ./.gitignore ] ./javascript;
};
version = (builtins.fromJSON (builtins.readFile ./javascript/packages/cli/package.json)).version;
name = "zombienet";
releases = "https://github.com/paritytech/${name}/releases/download";
src =
if pkgs.stdenv.isDarwin then {
url = "${releases}/v${version}/${name}-macos";
sha256 = "sha256-piaiv6hFTIZOVZNgo7oooNe+TwRrcNzn4SiT4n1jEBQ=";
} else
if system == "aarch64-linux" then {
url = "${releases}/v${version}/${name}-linux-arm64";
sha256 = "sha256-tXZt8Q6R8jh/UgdmS2jQf3IWGd4wx3u7tY+etYLIXYg=";
} else {
url = "${releases}/v${version}/${name}-linux-x64";
sha256 = "sha256-uf4eykvGEvqtCBfX4eCQe4f4SpagV4VBYVA4hgBzg1w=";
};
in
{
packages = rec {
# output is something like what npm 'pkg` does, but more sandboxed
default = pkgs.buildNpmPackage rec {
# root hash (hash of hashes of each dependnecies)
# this should be updated on each dependency change (use `prefetch-npm-deps` to get new hash)
npmDepsHash = "sha256-lA8xOKnzthgscr0pMmQ6KcZjYxNdOK5lfZ301PZ29Xg=";

pname = "zombienet";
name = pname;
src = cleaned-javascript-src;
npmBuildScript = "build";
npmBuildFlag = "--workspaces";

# just for safety of mac as it is used here often
nativeBuildInputs = with pkgs; [
python3
nodePackages.node-gyp-build
nodePackages.node-gyp
] ++ pkgs.lib.optional pkgs.stdenv.isDarwin (with pkgs;
with darwin.apple_sdk.frameworks; [
Security
SystemConfiguration
]);

runtimeDeps = with pkgs;
# these are used behind the scenes
# can provide nix `devenv` with running podman based kubernetes as process/service
[ bash coreutils procps findutils podman kubectl ]
++ lib.optional stdenv.isLinux glibc.bin;
# uncomment if need to debug build
#npmFlags = "--verbose";

# unfortunately current fetcher(written in rust) has bugs for workspaes, so this is ugly workaround https://github.com/NixOS/nixpkgs/issues/219673
preBuild = ''
patchShebangs packages
'';
postBuild = ''
echo "Generating `dist` of `workspace`"
npm run build --workspace=packages/utils
npm run build --workspace=packages/orchestrator
'';
postInstall = ''
echo "Copying `dist` of `workspace` to output"
cp --recursive packages/orchestrator/dist/ $out/lib/node_modules/zombienet/node_modules/@zombienet/orchestrator/dist/
cp --recursive packages/utils/dist/ $out/lib/node_modules/zombienet/node_modules/@zombienet/utils/dist/
'';
};
default = pkgs.stdenv.mkDerivation
{
runtimeInputs = with pkgs; [ bash coreutils procps findutils podman kubectl ] ++ lib.optional stdenv.isLinux glibc.bin;
inherit name;
src = pkgs.fetchurl src;
phases = [ "installPhase" "patchPhase" ];
installPhase = ''
mkdir -p $out/bin
cp $src $out/bin/${name}
chmod +x $out/bin/${name}
'';
};
};
};
}
Empty file added javascript/flake.lock
Empty file.
Loading

0 comments on commit c7bf197

Please sign in to comment.