From 1be262965ae67c6e2848506d726e635b75fb4aef Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Wed, 29 Jul 2020 13:52:57 -0700 Subject: [PATCH] test: add workflow for ic-ref --- .github/workflows/ic-ref.yml | 85 +++++++++++++++++++++++++++++++ .github/workflows/lint.yml | 3 +- .github/workflows/test.yml | 8 +-- .mergify.yml | 5 -- ic-agent/Cargo.toml | 8 ++- ic-agent/README.adoc | 32 +++++++++++- ic-agent/src/agent/agent_error.rs | 6 +++ ic-agent/tests/ic-ref.rs | 2 + ic-agent/tests/ic_ref/mod.rs | 2 + ic-agent/tests/ic_ref/status.rs | 25 +++++++++ ic-agent/tests/ic_ref/utils.rs | 12 +++++ 11 files changed, 175 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/ic-ref.yml create mode 100644 ic-agent/tests/ic-ref.rs create mode 100644 ic-agent/tests/ic_ref/mod.rs create mode 100644 ic-agent/tests/ic_ref/status.rs create mode 100644 ic-agent/tests/ic_ref/utils.rs diff --git a/.github/workflows/ic-ref.yml b/.github/workflows/ic-ref.yml new file mode 100644 index 0000000..b61b564 --- /dev/null +++ b/.github/workflows/ic-ref.yml @@ -0,0 +1,85 @@ +name: ic-ref + +on: [push] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + matrix: + build: [ linux-0-8 ] + include: + - build: linux-0-8 + ghc: '8.8.3' + spec: 'joachim/test-for-agent-rust' + os: ubuntu-latest + rust: stable + + steps: + - uses: actions/checkout@v2 + with: + path: main + - uses: actions/checkout@v2 + with: + repository: 'dfinity-lab/ic-ref' + # Personal Read-only Access Token created by hans.larsen@dfinity.org + token: ${{ secrets.IC_REF_TOKEN }} + path: ic-ref + ref: ${{ matrix.spec }} + + - name: Cache ~/.cabal/store + uses: actions/cache@v1 + with: + path: ~/.cabal/store + key: ${{ runner.os }}-${{ matrix.ghc }}-cabal-store + - uses: actions/setup-haskell@v1.1 + with: + ghc-version: ${{ matrix.ghc }} + + - name: Cache Cargo registry + uses: actions/cache@v1 + with: + path: ~/.cargo/registry + key: ${{ matrix.build }}-cargo-registry-${{ hashFiles('main/**/Cargo.lock') }} + restore-keys: | + ${{ matrix.build }}-cargo-registry- + - name: Cache Cargo index + uses: actions/cache@v1 + with: + path: ~/.cargo/git + key: ${{ matrix.build }}-cargo-index-${{ hashFiles('main/**/Cargo.lock') }} + restore-keys: | + ${{ matrix.build }}-cargo-index- + - name: Cache Cargo build + uses: actions/cache@v1 + with: + path: main/target + key: ${{ matrix.build }}-target-${{ hashFiles('main/**/Cargo.lock') }} + restore-keys: | + ${{ matrix.build }}-target- + - name: Install Rust + run: | + rustup update ${{ matrix.rust }} --no-self-update + rustup default ${{ matrix.rust }} + + - name: Build ic-ref + run: | + ls -l /opt/ghc/ + export PATH=/opt/ghc/bin:$PATH + cabal --version + ghc --version + mkdir -p $HOME/bin + cd ic-ref/impl + cabal update + cabal install -w ghc-${{ matrix.ghc }} --overwrite-policy=always --installdir=$HOME/bin + + - name: Run Tests + run: | + set -ex + $HOME/bin/ic-ref --pick-port --write-port-to $HOME/ic_ref_port & + sleep 1 + export IC_REF_PORT=$(cat $HOME/ic_ref_port) + cd main/ic-agent + cargo test --features ic_ref_tests --test ic-ref + env: + RUST_BACKTRACE: 1 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 73a97e1..fa45d1c 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -4,8 +4,7 @@ on: [pull_request] jobs: test: - name: Lint - + name: lint runs-on: ubuntu-latest steps: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 04800a1..67c9763 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,11 +1,9 @@ name: Tests -on: [pull_request] +on: [push] jobs: test: - name: Test - runs-on: ${{ matrix.os }} strategy: matrix: @@ -47,6 +45,8 @@ jobs: rustup update ${{ matrix.rust }} --no-self-update rustup default ${{ matrix.rust }} - name: Run Tests - run: cargo test --all-features + run: | + # Do not test all features here. + cargo test env: RUST_BACKTRACE: 1 diff --git a/.mergify.yml b/.mergify.yml index 5442687..625f537 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -3,12 +3,7 @@ pull_request_rules: conditions: - "#approved-reviews-by>=1" - "#changes-requested-reviews-by=0" - - "-conflict" - - "-draft" - status-success=conventional-pr-title - - "#status-success>=1" - - "#status-failure=0" - - base=master - label=automerge-squash actions: merge: diff --git a/ic-agent/Cargo.toml b/ic-agent/Cargo.toml index 42a72dd..8e01b4b 100644 --- a/ic-agent/Cargo.toml +++ b/ic-agent/Cargo.toml @@ -20,10 +20,16 @@ rand = "0.7.2" reqwest = "0.10.4" serde = { version = "1.0.101", features = ["derive"] } serde_bytes = "0.11.2" -serde_cbor = "0.10" +serde_cbor = "0.11.1" url = "2.1.0" [dev-dependencies] +actix-rt = "1.1.1" mockito = "0.27.0" proptest = "0.9.5" tokio = "0.2.6" + +[features] +default = [] + +ic_ref_tests = ['default'] # Used to separate integration tests for ic-ref which need a server running. diff --git a/ic-agent/README.adoc b/ic-agent/README.adoc index 1adc848..94ec7b4 100644 --- a/ic-agent/README.adoc +++ b/ic-agent/README.adoc @@ -5,5 +5,35 @@ This library contains typings and utility functions dealing with the public spec client. It might be shared in the future but for now is separated for the purpose of testing and development. +== Running Tests +Regular tests can be run by anyone using `cargo test`. This will not run a special version of the +tests that runs against a server implementation of the public specification, though those +tests can be run against a compatible Replica. + +=== Public Spec Tests +The `ic-ref` integration tests have to have a locally available server (cannot be run accross +a network). To run those locally, one must set the `IC_REF_PORT` environment variable to the +network port the server listens to. + +If you know a replica is compatible with the ic_api version supported in the code, you can run +the replica (for example, using `dfx replica`) locally and set the environment variable to the +port the replica will listen to. + +Once you have a replica and the environment variable, change the current directory to the +`ic-agent` package, and run the `ic-ref` test suite, using: + +[sources, bash] +---- +cd ic-agent +cargo test --features=ic_ref_tests --test ic-ref +---- + +Alternatively, removing the `--test ic-ref` argument will run ALL tests, including the `ic-ref` +test suite. + +On CI, we use a reference implementation which the Replica follows as well. This ensures +compatibility with the latest version. + == References -The latest version of the https://hydra.dfinity.systems/latest/dfinity-ci-build/dfinity/dfinity.docs.x86_64-linux/dfinity/spec/public/index.html[public spec] is available internally on hydra. +The public specification of the Internet Computer is, at this moment, privately shared. When it +is made public a reference to the version supported will be available here. diff --git a/ic-agent/src/agent/agent_error.rs b/ic-agent/src/agent/agent_error.rs index 1826390..9ee0e3b 100644 --- a/ic-agent/src/agent/agent_error.rs +++ b/ic-agent/src/agent/agent_error.rs @@ -69,3 +69,9 @@ impl From for AgentError { Self::CanisterIdTextError(err) } } + +impl From for String { + fn from(err: AgentError) -> Self { + format!("{:?}", err) + } +} diff --git a/ic-agent/tests/ic-ref.rs b/ic-agent/tests/ic-ref.rs new file mode 100644 index 0000000..06081de --- /dev/null +++ b/ic-agent/tests/ic-ref.rs @@ -0,0 +1,2 @@ +#![cfg(feature = "ic_ref_tests")] +mod ic_ref; diff --git a/ic-agent/tests/ic_ref/mod.rs b/ic-agent/tests/ic_ref/mod.rs new file mode 100644 index 0000000..28474da --- /dev/null +++ b/ic-agent/tests/ic_ref/mod.rs @@ -0,0 +1,2 @@ +mod status; +mod utils; diff --git a/ic-agent/tests/ic_ref/status.rs b/ic-agent/tests/ic_ref/status.rs new file mode 100644 index 0000000..99997b0 --- /dev/null +++ b/ic-agent/tests/ic_ref/status.rs @@ -0,0 +1,25 @@ +use super::utils::create_agent; + +#[actix_rt::test] +async fn status_endpoint_works() -> Result<(), String> { + let agent = create_agent().await?; + agent.ping_once().await?; + + Ok(()) +} + +#[actix_rt::test] +async fn status_endpoint_is_expected() -> Result<(), String> { + let agent = create_agent().await?; + let status = agent.ping_once().await?; + + match status { + serde_cbor::Value::Map(map) => { + let key = serde_cbor::Value::from("ic_api_version".to_string()); + assert_eq!(map.get( &key), Some(&serde_cbor::Value::from("0.8.2".to_string()))); + }, + x => assert!(false, "Invalid status return: {:?}", x), + } + + Ok(()) +} diff --git a/ic-agent/tests/ic_ref/utils.rs b/ic-agent/tests/ic_ref/utils.rs new file mode 100644 index 0000000..03091be --- /dev/null +++ b/ic-agent/tests/ic_ref/utils.rs @@ -0,0 +1,12 @@ +use ic_agent::{Agent, AgentConfig}; + +pub async fn create_agent() -> Result +{ + let port_env = std::env::var("IC_REF_PORT").expect("Need to specify the IC_REF_PORT environment variable."); + let port = port_env.parse::().expect("Could not parse the IC_REF_PORT environment variable as an integer."); + + Ok(ic_agent::Agent::new(AgentConfig { + url: &format!("http://127.0.0.1:{}", port), + ..AgentConfig::default() + })?) +}