Skip to content

Commit

Permalink
feat: upgrade sqlness to latest version (#517)
Browse files Browse the repository at this point in the history
* feat: upgrade sqlness to latest version

* rename try_new with new

* fix CI

* Update config path

* Switch sqlness to 0.1
  • Loading branch information
jiacai2050 authored Dec 28, 2022
1 parent 3b21fd1 commit bcb5f99
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 146 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

5 changes: 2 additions & 3 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ ROOT = $(shell pwd)
DATA_DIR = /tmp/ceresdb

export CERESDB_BINARY_PATH ?= $(ROOT)/../target/$(MODE)/ceresdb-server
export CERESDB_CONFIG_PATH ?= $(ROOT)/../docs/sqlness.toml
export CERESDB_SERVER_ENDPOINT ?= 127.0.0.1:8831
export CERESDB_TEST_CASE_PATH ?= $(ROOT)/cases
export CERESDB_TEST_SQLNESS ?= $(ROOT)/../target/$(MODE)/ceresdb-test
Expand All @@ -22,10 +21,10 @@ build-sqlness:

build: build-ceresdb build-sqlness

# rename ${case}.out to ${case}.result automatically. fd is required
# rename ${case}.output to ${case}.result automatically. fd is required
# https://github.com/sharkdp/fd
fix:
fd -t f out --exec mv {} {.}.result \;
fd -t f output --exec mv {} {.}.result \;

run: clean build
cd sqlness && $(CERESDB_TEST_SQLNESS)
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/sqlness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ workspace = true
anyhow = "1.0.58"
async-trait = "0.1"
ceresdb-client-rs = { git = "https://github.com/CeresDB/ceresdb-client-rs.git", rev = "a9d9190f4f7b55171ea2ad142fb41dc9909c19c5" }
sqlness = { git = "https://github.com/CeresDB/sqlness.git", rev = "c077b17d73ab25460c152dc34e8f80f904522a57" }
sqlness = "0.1"
tokio = { workspace = true }
51 changes: 0 additions & 51 deletions tests/sqlness/src/client.rs

This file was deleted.

98 changes: 98 additions & 0 deletions tests/sqlness/src/database.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.

use std::{
env,
fmt::Display,
fs::File,
path::Path,
process::{Child, Command},
sync::Arc,
};

use async_trait::async_trait;
use ceresdb_client_rs::{
db_client::{Builder, DbClient, Mode},
model::{display::CsvFormatter, request::QueryRequest},
RpcContext,
};
use sqlness::Database;

const BINARY_PATH_ENV: &str = "CERESDB_BINARY_PATH";
const SERVER_ENDPOINT_ENV: &str = "CERESDB_SERVER_ENDPOINT";
const CERESDB_STDOUT_FILE: &str = "CERESDB_STDOUT_FILE";
const CERESDB_STDERR_FILE: &str = "CERESDB_STDERR_FILE";

pub struct CeresDB {
server_process: Child,
db_client: Arc<dyn DbClient>,
}

#[async_trait]
impl Database for CeresDB {
async fn query(&self, query: String) -> Box<dyn Display> {
Self::execute(query, self.db_client.clone()).await
}
}

impl CeresDB {
pub fn new(config: Option<&Path>) -> Self {
let config = config.unwrap().to_string_lossy();
let bin = env::var(BINARY_PATH_ENV).expect("Cannot parse binary path env");
let stdout = env::var(CERESDB_STDOUT_FILE).expect("Cannot parse stdout env");
let stderr = env::var(CERESDB_STDERR_FILE).expect("Cannot parse stderr env");
let stdout = File::create(stdout).expect("Cannot create stdout");
let stderr = File::create(stderr).expect("Cannot create stderr");

println!("Start {} with {}...", bin, config);

let server_process = Command::new(&bin)
.args(["--config", &config])
.stdout(stdout)
.stderr(stderr)
.spawn()
.unwrap_or_else(|_| panic!("Failed to start server at {:?}", bin));

// Wait for a while
std::thread::sleep(std::time::Duration::from_secs(5));
let endpoint = env::var(SERVER_ENDPOINT_ENV).unwrap_or_else(|_| {
panic!(
"Cannot read server endpoint from env {:?}",
SERVER_ENDPOINT_ENV
)
});

let db_client = Builder::new(endpoint, Mode::Standalone).build();

CeresDB {
db_client,
server_process,
}
}

pub fn stop(mut self) {
self.server_process.kill().unwrap()
}

async fn execute(query: String, client: Arc<dyn DbClient>) -> Box<dyn Display> {
let query_ctx = RpcContext {
tenant: "public".to_string(),
token: "".to_string(),
};
let query_req = QueryRequest {
metrics: vec![],
ql: query,
};
let result = client.query(&query_ctx, &query_req).await;

Box::new(match result {
Ok(resp) => {
if resp.has_schema() {
format!("{}", CsvFormatter { resp })
} else {
format!("affected_rows: {}", resp.affected_rows)
}
}
Err(e) => format!("Failed to execute query, err: {:?}", e),
})
}
}
39 changes: 24 additions & 15 deletions tests/sqlness/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.

#![feature(try_blocks)]

use std::env;
use std::{env, path::Path};

use anyhow::Result;
use sqlness::Runner;
use async_trait::async_trait;
use database::CeresDB;
use sqlness::{EnvController, Runner};

mod client;
mod setup;
mod database;

const CASE_ROOT_PATH_ENV: &str = "CERESDB_TEST_CASE_PATH";

pub struct CeresDBController;

#[async_trait]
impl EnvController for CeresDBController {
type DB = CeresDB;

async fn start(&self, _env: &str, config: Option<&Path>) -> Self::DB {
CeresDB::new(config)
}

async fn stop(&self, _env: &str, database: Self::DB) {
database.stop();
}
}

#[tokio::main]
async fn main() -> Result<()> {
let case_dir = env::var(CASE_ROOT_PATH_ENV)?;
let env = setup::CeresDBEnv::start_server();
let config = sqlness::Config {
case_dir,
test_case_extension: String::from("sql"),
output_result_extension: String::from("output"),
expect_result_extension: String::from("result"),
interceptor_prefix: String::from("-- SQLNESS"),
env_config_file: String::from("config.toml"),
};
let env = CeresDBController;
let config = sqlness::ConfigBuilder::default()
.case_dir(case_dir)
.build()?;
let runner = Runner::new_with_config(config, env).await?;
runner.run().await?;

Expand Down
75 changes: 0 additions & 75 deletions tests/sqlness/src/setup.rs

This file was deleted.

0 comments on commit bcb5f99

Please sign in to comment.