Skip to content

Commit

Permalink
add native toolchain definition to connector metadata
Browse files Browse the repository at this point in the history
add CLI tests
  • Loading branch information
paritosh-08 committed Feb 11, 2025
1 parent 52f9a8c commit 0dfda7c
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ tokio = { workspace = true, features = ["full"] }
[build-dependencies]
build-data = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
serde_json = { workspace = true }
tempfile = { workspace = true }

[package.metadata.cargo-machete]
ignored = ["build_data", "build-data"] # apparently cargo-machete doesn't find dependencies used by build scripts
12 changes: 12 additions & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ async fn initialize(with_metadata: bool, context: Context<impl Environment>) ->
action: metadata::DockerComposeWatchAction::SyncAndRestart,
ignore: vec![],
}],
native_toolchain_definition: Some(metadata::NativeToolchainDefinition {
commands: vec![
("start".to_string(), metadata::CommandDefinition::ShellScript {
bash: "#!/usr/bin/env bash\nset -eu -o pipefail\nHASURA_CONFIGURATION_DIRECTORY=\"$HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH\" ndc-bigquery serve".to_string(),
powershell: "$ErrorActionPreference = \"Stop\"\n$env:HASURA_CONFIGURATION_DIRECTORY=\"$env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH\"; & ndc-bigquery.exe serve".to_string(),
}),
("update".to_string(), metadata::CommandDefinition::ShellScript {
bash: "#!/usr/bin/env bash\nset -eu -o pipefail\n\"$HOME/.ddn/plugins/store/ndc-bigquery/$BIGQUERY_VERSION/hasura-ndc-bigquery\" update".to_string(),
powershell: "$ErrorActionPreference = \"Stop\"\n& \"$env:USERPROFILE\\.ddn\\plugins\\store\\ndc-bigquery\\$env:BIGQUERY_VERSION\\hasura-ndc-bigquery.exe\" update".to_string(),
}),
].into_iter().collect(),
})
};

fs::write(metadata_file, serde_yaml::to_string(&metadata)?).await?;
Expand Down
22 changes: 22 additions & 0 deletions crates/cli/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct ConnectorMetadataDefinition {
pub cli_plugin: Option<CliPluginDefinition>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub docker_compose_watch: DockerComposeWatch,
#[serde(skip_serializing_if = "Option::is_none")]
pub native_toolchain_definition: Option<NativeToolchainDefinition>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -75,3 +77,23 @@ pub enum DockerComposeWatchAction {
#[serde(rename = "sync+restart")]
SyncAndRestart,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeToolchainDefinition {
pub commands: std::collections::BTreeMap<CommandName, CommandDefinition>,
}

pub type CommandName = String;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase", tag = "type")]
pub enum CommandDefinition {
#[serde(rename_all = "camelCase")]
ShellScript { bash: String, powershell: String },
#[serde(rename_all = "camelCase")]
DockerizedCommand {
docker_image: String,
command_args: Vec<String>,
},
}
15 changes: 15 additions & 0 deletions crates/cli/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![allow(dead_code)] // required because this is included mulitple times

use std::path::Path;

use tokio::fs;

pub async fn assert_file_ends_with_newline(path: impl AsRef<Path>) -> anyhow::Result<()> {
let contents = fs::read_to_string(path).await?;
assert_ends_with_newline(&contents);
Ok(())
}

pub fn assert_ends_with_newline(contents: &str) {
assert_eq!(contents.chars().last(), Some('\n'));
}
169 changes: 169 additions & 0 deletions crates/cli/tests/initialize_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
mod common;

use tokio::fs;

use ndc_bigquery_cli::*;
use ndc_bigquery_configuration as configuration;
use ndc_bigquery_configuration::ParsedConfiguration;

#[tokio::test]
async fn test_initialize_directory() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;

let context = Context {
context_path: dir.path().to_owned(),
environment: configuration::environment::EmptyEnvironment,
release_version: None,
};
run(
Command::Initialize {
with_metadata: false,
},
context,
)
.await?;

let configuration_file_path = dir.path().join("configuration.json");
assert!(configuration_file_path.exists());
let contents = fs::read_to_string(configuration_file_path).await?;
common::assert_ends_with_newline(&contents);
let _: ParsedConfiguration = configuration::parse_configuration(&dir).await?;

let metadata_file_path = dir
.path()
.join(".hasura-connector")
.join("connector-metadata.yaml");
assert!(!metadata_file_path.exists());

Ok(())
}

#[tokio::test]
async fn test_initialize_version_is_unchanged() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;

let context = Context {
context_path: dir.path().to_owned(),
environment: configuration::environment::EmptyEnvironment,
release_version: None,
};
run(
Command::Initialize {
with_metadata: false,
},
context,
)
.await?;

let configuration_file_path = dir.path().join("configuration.json");
assert!(configuration_file_path.exists());
let configuration_value: serde_json::Value =
serde_json::from_str(fs::read_to_string(configuration_file_path).await?.as_str())?;

let version = configuration_value
.as_object()
.unwrap()
.get("version")
.unwrap();

insta::assert_snapshot!(version.to_string());
Ok(())
}

#[tokio::test]
async fn test_do_not_initialize_when_files_already_exist() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
fs::write(
dir.path().join("random.file"),
"this directory is no longer empty",
)
.await?;

let context = Context {
context_path: dir.path().to_owned(),
environment: configuration::environment::EmptyEnvironment,
release_version: None,
};
match run(
Command::Initialize {
with_metadata: false,
},
context,
)
.await
{
Ok(()) => panic!("Expected the command to fail."),
Err(error) => match error.downcast::<Error>() {
Err(input) => panic!("Expected a CLI error, but got {input}"),
Ok(cli_error) => assert_eq!(cli_error, Error::DirectoryIsNotEmpty),
},
}

Ok(())
}

#[tokio::test]
async fn test_initialize_directory_with_metadata() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;

let context = Context {
context_path: dir.path().to_owned(),
environment: configuration::environment::EmptyEnvironment,
release_version: None,
};
run(
Command::Initialize {
with_metadata: true,
},
context,
)
.await?;

let configuration_file_path = dir.path().join("configuration.json");
assert!(configuration_file_path.exists());
common::assert_file_ends_with_newline(&configuration_file_path).await?;

let metadata_file_path = dir
.path()
.join(".hasura-connector")
.join("connector-metadata.yaml");
assert!(metadata_file_path.exists());
let contents = fs::read_to_string(metadata_file_path).await?;
common::assert_ends_with_newline(&contents);
insta::assert_snapshot!(contents);

Ok(())
}

#[tokio::test]
async fn test_initialize_directory_with_metadata_and_release_version() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;

let context = Context {
context_path: dir.path().to_owned(),
environment: configuration::environment::EmptyEnvironment,
release_version: Some("v1.2.3"),
};
run(
Command::Initialize {
with_metadata: true,
},
context,
)
.await?;

let configuration_file_path = dir.path().join("configuration.json");
assert!(configuration_file_path.exists());
common::assert_file_ends_with_newline(&configuration_file_path).await?;

let metadata_file_path = dir
.path()
.join(".hasura-connector")
.join("connector-metadata.yaml");
assert!(metadata_file_path.exists());
let contents = fs::read_to_string(metadata_file_path).await?;
common::assert_ends_with_newline(&contents);
insta::assert_snapshot!(contents);

Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
source: crates/cli/tests/initialize_tests.rs
expression: contents
---
packagingDefinition:
type: PrebuiltDockerImage
dockerImage: ghcr.io/hasura/ndc-bigquery:latest
supportedEnvironmentVariables:
- name: HASURA_BIGQUERY_SERVICE_KEY
description: The BigQuery service key
- name: HASURA_BIGQUERY_PROJECT_ID
description: The BigQuery project ID/name
- name: HASURA_BIGQUERY_DATASET_ID
description: The BigQuery dataset ID/name
commands:
update: hasura-ndc-bigquery update
cliPlugin:
name: ndc-bigquery
version: latest
dockerComposeWatch:
- path: ./
target: /etc/connector
action: sync+restart
nativeToolchainDefinition:
commands:
start:
type: ShellScript
bash: |-
#!/usr/bin/env bash
set -eu -o pipefail
HASURA_CONFIGURATION_DIRECTORY="$HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH" ndc-bigquery serve
powershell: |-
$ErrorActionPreference = "Stop"
$env:HASURA_CONFIGURATION_DIRECTORY="$env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH"; & ndc-bigquery.exe serve
update:
type: ShellScript
bash: |-
#!/usr/bin/env bash
set -eu -o pipefail
"$HOME/.ddn/plugins/store/ndc-bigquery/$BIGQUERY_VERSION/hasura-ndc-bigquery" update
powershell: |-
$ErrorActionPreference = "Stop"
& "$env:USERPROFILE\.ddn\plugins\store\ndc-bigquery\$env:BIGQUERY_VERSION\hasura-ndc-bigquery.exe" update
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
source: crates/cli/tests/initialize_tests.rs
expression: contents
---
packagingDefinition:
type: PrebuiltDockerImage
dockerImage: ghcr.io/hasura/ndc-bigquery:v1.2.3
supportedEnvironmentVariables:
- name: HASURA_BIGQUERY_SERVICE_KEY
description: The BigQuery service key
- name: HASURA_BIGQUERY_PROJECT_ID
description: The BigQuery project ID/name
- name: HASURA_BIGQUERY_DATASET_ID
description: The BigQuery dataset ID/name
commands:
update: hasura-ndc-bigquery update
cliPlugin:
name: ndc-bigquery
version: v1.2.3
dockerComposeWatch:
- path: ./
target: /etc/connector
action: sync+restart
nativeToolchainDefinition:
commands:
start:
type: ShellScript
bash: |-
#!/usr/bin/env bash
set -eu -o pipefail
HASURA_CONFIGURATION_DIRECTORY="$HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH" ndc-bigquery serve
powershell: |-
$ErrorActionPreference = "Stop"
$env:HASURA_CONFIGURATION_DIRECTORY="$env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH"; & ndc-bigquery.exe serve
update:
type: ShellScript
bash: |-
#!/usr/bin/env bash
set -eu -o pipefail
"$HOME/.ddn/plugins/store/ndc-bigquery/$BIGQUERY_VERSION/hasura-ndc-bigquery" update
powershell: |-
$ErrorActionPreference = "Stop"
& "$env:USERPROFILE\.ddn\plugins\store\ndc-bigquery\$env:BIGQUERY_VERSION\hasura-ndc-bigquery.exe" update
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/cli/tests/initialize_tests.rs
expression: version.to_string()
---
1

0 comments on commit 0dfda7c

Please sign in to comment.