From 9491b2dd94c73a47f50bf69669f615189f109281 Mon Sep 17 00:00:00 2001 From: Phil Freeman Date: Wed, 18 Oct 2023 14:25:06 -0700 Subject: [PATCH 1/2] Use latest ndc-spec, add test commands --- Cargo.lock | 5 +- rust-connector-sdk/Cargo.toml | 4 +- rust-connector-sdk/src/default_main.rs | 90 +++++++++++++++++++++----- 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 168e63c5..3f560be4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1005,7 +1005,7 @@ dependencies = [ [[package]] name = "ndc-client" version = "0.1.0" -source = "git+http://github.com/hasura/ndc-spec.git?tag=v0.1.0-rc.7#1edc777373579299fac06149e48e9744865fda49" +source = "git+http://github.com/hasura/ndc-spec.git?rev=2c2def8#2c2def8a01af2f95100b4274599030c930525bee" dependencies = [ "async-trait", "indexmap 1.9.3", @@ -1060,7 +1060,7 @@ dependencies = [ [[package]] name = "ndc-test" version = "0.1.0" -source = "git+http://github.com/hasura/ndc-spec.git?tag=v0.1.0-rc.7#1edc777373579299fac06149e48e9744865fda49" +source = "git+http://github.com/hasura/ndc-spec.git?rev=2c2def8#2c2def8a01af2f95100b4274599030c930525bee" dependencies = [ "async-trait", "clap", @@ -1070,6 +1070,7 @@ dependencies = [ "proptest", "reqwest", "semver", + "serde", "serde_json", "thiserror", "tokio", diff --git a/rust-connector-sdk/Cargo.toml b/rust-connector-sdk/Cargo.toml index 80cd661c..67558514 100644 --- a/rust-connector-sdk/Cargo.toml +++ b/rust-connector-sdk/Cargo.toml @@ -16,8 +16,8 @@ async-trait = "^0.1.68" axum = "^0.6.18" axum-macros = "^0.3.7" clap = { version = "^4.3.9", features = ["derive", "env"] } -ndc-client = { git = "http://github.com/hasura/ndc-spec.git", tag = "v0.1.0-rc.7" } -ndc-test = { git = "http://github.com/hasura/ndc-spec.git", tag = "v0.1.0-rc.7" } +ndc-client = { git = "http://github.com/hasura/ndc-spec.git", rev = "2c2def8" } +ndc-test = { git = "http://github.com/hasura/ndc-spec.git", rev = "2c2def8" } http = "^0.2" opentelemetry = { version = "^0.20", features = [ "rt-tokio", diff --git a/rust-connector-sdk/src/default_main.rs b/rust-connector-sdk/src/default_main.rs index 1adaff62..b6d513d5 100644 --- a/rust-connector-sdk/src/default_main.rs +++ b/rust-connector-sdk/src/default_main.rs @@ -29,9 +29,9 @@ use ndc_test::report; use prometheus::Registry; use schemars::{schema::RootSchema, JsonSchema}; use serde::{de::DeserializeOwned, Serialize}; -use std::error::Error; use std::net; use std::process::exit; +use std::{error::Error, path::PathBuf}; use tower_http::{cors::CorsLayer, trace::TraceLayer}; use self::v2_compat::SourceConfig; @@ -51,6 +51,8 @@ enum Command { #[command()] Test(TestCommand), #[command()] + Replay(ReplayCommand), + #[command()] CheckHealth(CheckHealthCommand), } @@ -98,10 +100,20 @@ struct ServeConfigurationCommand { #[derive(Clone, Parser)] struct TestCommand { - #[arg(long, value_name = "PORT", env = "PORT")] + #[arg(long, value_name = "SEED", env = "SEED")] seed: Option, #[arg(long, value_name = "CONFIGURATION_FILE", env = "CONFIGURATION_FILE")] configuration: String, + #[arg(long, value_name = "DIRECTORY", env = "SNAPSHOTS_DIR")] + snapshots_dir: Option, +} + +#[derive(Clone, Parser)] +struct ReplayCommand { + #[arg(long, value_name = "CONFIGURATION_FILE", env = "CONFIGURATION_FILE")] + configuration: String, + #[arg(long, value_name = "DIRECTORY", env = "SNAPSHOTS_DIR")] + snapshots_dir: PathBuf, } #[derive(Clone, Parser)] @@ -157,6 +169,7 @@ where Command::Serve(serve_command) => serve::(serve_command).await, Command::Configuration(configure_command) => configuration::(configure_command).await, Command::Test(test_command) => test::(test_command).await, + Command::Replay(replay_command) => replay::(replay_command).await, Command::CheckHealth(check_health_command) => check_health(check_health_command).await, } } @@ -592,6 +605,19 @@ where Err(err) => Err(ndc_test::Error::OtherError(err.into())), } } + + async fn mutation( + &self, + request: ndc_client::models::MutationRequest, + ) -> Result { + match C::mutation(&self.configuration, &self.state, request) + .await + .and_then(JsonResponse::into_value) + { + Ok(response) => Ok(response), + Err(err) => Err(ndc_test::Error::OtherError(err.into())), + } + } } async fn test( @@ -602,9 +628,52 @@ where C::Configuration: Sync + Send + 'static, C::State: Send + Sync + 'static, { - let test_configuration = ndc_test::TestConfiguration { seed: command.seed }; + let test_configuration = ndc_test::TestConfiguration { + seed: command.seed, + snapshots_dir: command.snapshots_dir, + }; + + let connector = make_connector_adapter::(command.configuration).await; + let results = ndc_test::test_connector(&test_configuration, &connector).await; + + if !results.failures.is_empty() { + println!(); + println!("{}", report(results)); - let configuration_json = std::fs::read_to_string(command.configuration).unwrap(); + exit(1) + } + + Ok(()) +} + +async fn replay( + command: ReplayCommand, +) -> Result<(), Box> +where + C::RawConfiguration: DeserializeOwned, + C::Configuration: Sync + Send + 'static, + C::State: Send + Sync + 'static, +{ + let connector = make_connector_adapter::(command.configuration).await; + let results = ndc_test::test_snapshots_in_directory(&connector, command.snapshots_dir).await; + + if !results.failures.is_empty() { + println!(); + println!("{}", report(results)); + + exit(1) + } + + Ok(()) +} + +async fn make_connector_adapter( + configuration_path: String, +) -> ConnectorAdapter +where + C::RawConfiguration: DeserializeOwned, +{ + let configuration_json = std::fs::read_to_string(configuration_path).unwrap(); let raw_configuration = serde_json::de::from_str::(configuration_json.as_str()).unwrap(); let configuration = C::validate_raw_configuration(raw_configuration) @@ -616,21 +685,12 @@ where .await .unwrap(); - let connector = ConnectorAdapter:: { + ConnectorAdapter:: { configuration, state, - }; - let results = ndc_test::test_connector(&test_configuration, &connector).await; - - if !results.failures.is_empty() { - println!(); - println!("{}", report(results)); - - exit(1) } - - Ok(()) } + async fn check_health( CheckHealthCommand { host, port }: CheckHealthCommand, ) -> Result<(), Box> { From 5867bcf65e4cf56e9cfcbc330188cbb7720c3ebb Mon Sep 17 00:00:00 2001 From: Phil Freeman Date: Wed, 18 Oct 2023 14:54:43 -0700 Subject: [PATCH 2/2] Use 0.1.0-rc.8 --- Cargo.lock | 4 ++-- rust-connector-sdk/Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3f560be4..c728e525 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1005,7 +1005,7 @@ dependencies = [ [[package]] name = "ndc-client" version = "0.1.0" -source = "git+http://github.com/hasura/ndc-spec.git?rev=2c2def8#2c2def8a01af2f95100b4274599030c930525bee" +source = "git+http://github.com/hasura/ndc-spec.git?tag=v0.1.0-rc.8#2c2def8a01af2f95100b4274599030c930525bee" dependencies = [ "async-trait", "indexmap 1.9.3", @@ -1060,7 +1060,7 @@ dependencies = [ [[package]] name = "ndc-test" version = "0.1.0" -source = "git+http://github.com/hasura/ndc-spec.git?rev=2c2def8#2c2def8a01af2f95100b4274599030c930525bee" +source = "git+http://github.com/hasura/ndc-spec.git?tag=v0.1.0-rc.8#2c2def8a01af2f95100b4274599030c930525bee" dependencies = [ "async-trait", "clap", diff --git a/rust-connector-sdk/Cargo.toml b/rust-connector-sdk/Cargo.toml index 67558514..3acbe44b 100644 --- a/rust-connector-sdk/Cargo.toml +++ b/rust-connector-sdk/Cargo.toml @@ -16,8 +16,8 @@ async-trait = "^0.1.68" axum = "^0.6.18" axum-macros = "^0.3.7" clap = { version = "^4.3.9", features = ["derive", "env"] } -ndc-client = { git = "http://github.com/hasura/ndc-spec.git", rev = "2c2def8" } -ndc-test = { git = "http://github.com/hasura/ndc-spec.git", rev = "2c2def8" } +ndc-client = { git = "http://github.com/hasura/ndc-spec.git", tag = "v0.1.0-rc.8" } +ndc-test = { git = "http://github.com/hasura/ndc-spec.git", tag = "v0.1.0-rc.8" } http = "^0.2" opentelemetry = { version = "^0.20", features = [ "rt-tokio",