Skip to content

Commit

Permalink
feat: integration test suite (#487)
Browse files Browse the repository at this point in the history
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
  • Loading branch information
waynexia authored Nov 22, 2022
1 parent c144a1b commit b407ebf
Show file tree
Hide file tree
Showing 9 changed files with 625 additions and 16 deletions.
250 changes: 234 additions & 16 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ members = [
"src/store-api",
"src/table",
"src/mito",
"tests/runner",
]

[profile.release]
Expand Down
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ coverage:
patch: off
ignore:
- "**/error*.rs" # ignore all error.rs files
- "tests/runner/*.rs" # ignore integration test runner
60 changes: 60 additions & 0 deletions tests/cases/standalone/basic.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
CREATE TABLE system_metrics (
host STRING,
idc STRING,
cpu_util DOUBLE,
memory_util DOUBLE,
disk_util DOUBLE,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(host, idc),
TIME INDEX(ts)
);

MutateResult { success: 1, failure: 0 }

INSERT INTO system_metrics
VALUES
("host1", "idc_a", 11.8, 10.3, 10.3, 1667446797450),
("host2", "idc_a", 80.1, 70.3, 90.0, 1667446797450),
("host1", "idc_b", 50.0, 66.7, 40.6, 1667446797450);

MutateResult { success: 3, failure: 0 }

SELECT * FROM system_metrics;

+-----------------------+----------------------+----------------------------+-------------------------------+-----------------------------+----------------------------+
| host, #Field, #String | idc, #Field, #String | cpu_util, #Field, #Float64 | memory_util, #Field, #Float64 | disk_util, #Field, #Float64 | ts, #Timestamp, #Timestamp |
+-----------------------+----------------------+----------------------------+-------------------------------+-----------------------------+----------------------------+
| host1 | idc_a | 11.8 | 10.3 | 10.3 | 1667446797450 |
| host1 | idc_b | 50 | 66.7 | 40.6 | 1667446797450 |
| host2 | idc_a | 80.1 | 70.3 | 90 | 1667446797450 |
+-----------------------+----------------------+----------------------------+-------------------------------+-----------------------------+----------------------------+

SELECT count(*) FROM system_metrics;

+----------------------------------+
| COUNT(UInt8(1)), #Field, #Uint64 |
+----------------------------------+
| 3 |
+----------------------------------+

SELECT avg(cpu_util) FROM system_metrics;

+------------------------------------------------+
| AVG(system_metrics.cpu_util), #Field, #Float64 |
+------------------------------------------------+
| 47.29999999999999 |
+------------------------------------------------+

SELECT idc, avg(memory_util) FROM system_metrics GROUP BY idc ORDER BY idc;

+----------------------+---------------------------------------------------+
| idc, #Field, #String | AVG(system_metrics.memory_util), #Field, #Float64 |
+----------------------+---------------------------------------------------+
| idc_a | 40.3 |
| idc_b | 66.7 |
+----------------------+---------------------------------------------------+

DROP TABLE system_metrics;

Failed to execute, error: Datanode { code: 1001, msg: "Failed to execute sql, source: Cannot parse SQL, source: SQL statement is not supported: DROP TABLE system_metrics;, keyword: DROP" }

26 changes: 26 additions & 0 deletions tests/cases/standalone/basic.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
CREATE TABLE system_metrics (
host STRING,
idc STRING,
cpu_util DOUBLE,
memory_util DOUBLE,
disk_util DOUBLE,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(host, idc),
TIME INDEX(ts)
);

INSERT INTO system_metrics
VALUES
("host1", "idc_a", 11.8, 10.3, 10.3, 1667446797450),
("host2", "idc_a", 80.1, 70.3, 90.0, 1667446797450),
("host1", "idc_b", 50.0, 66.7, 40.6, 1667446797450);

SELECT * FROM system_metrics;

SELECT count(*) FROM system_metrics;

SELECT avg(cpu_util) FROM system_metrics;

SELECT idc, avg(memory_util) FROM system_metrics GROUP BY idc ORDER BY idc;

DROP TABLE system_metrics;
11 changes: 11 additions & 0 deletions tests/runner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "sqlness-runner"
version = "0.1.0"
edition = "2021"

[dependencies]
async-trait = "0.1"
client = { path = "../../src/client" }
comfy-table = "6.1"
sqlness = { git = "https://github.com/ceresdb/sqlness.git" }
tokio = { version = "1.21", features = ["full"] }
166 changes: 166 additions & 0 deletions tests/runner/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright 2022 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt::Display;
use std::time::Duration;

use async_trait::async_trait;
use client::api::v1::codec::SelectResult;
use client::api::v1::column::SemanticType;
use client::api::v1::ColumnDataType;
use client::{Client, Database as DB, Error as ClientError, ObjectResult, Select};
use comfy_table::{Cell, Table};
use sqlness::{Database, Environment};
use tokio::process::{Child, Command};
use tokio::time;

use crate::util;

pub struct Env {}

#[async_trait]
impl Environment for Env {
type DB = GreptimeDB;

async fn start(&self, mode: &str, _config: Option<String>) -> Self::DB {
match mode {
"local" => Self::start_local().await,
"remote" => Self::start_remote().await,
_ => panic!("Unexpected mode: {}", mode),
}
}

/// Stop one [`Database`].
async fn stop(&self, _mode: &str, mut database: Self::DB) {
database.server_process.kill().await.unwrap()
}
}

impl Env {
pub async fn start_local() -> GreptimeDB {
let server_process = Command::new("cargo")
.current_dir("../")
.args(["run", "--", "standalone", "start"])
.spawn()
.unwrap_or_else(|_| panic!("Failed to start GreptimeDB"));

time::sleep(Duration::from_secs(3)).await;

let client = Client::with_urls(vec!["127.0.0.1:3001"]);
let db = DB::new("greptime", client.clone());

GreptimeDB {
server_process,
client,
db,
}
}

pub async fn start_remote() -> GreptimeDB {
todo!()
}
}

pub struct GreptimeDB {
server_process: Child,
#[allow(dead_code)]
client: Client,
db: DB,
}

#[async_trait]
impl Database for GreptimeDB {
async fn query(&self, query: String) -> Box<dyn Display> {
let sql = Select::Sql(query);
let result = self.db.select(sql).await;
Box::new(ResultDisplayer { result }) as _
}
}

struct ResultDisplayer {
result: Result<ObjectResult, ClientError>,
}

impl Display for ResultDisplayer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.result {
Ok(result) => match result {
ObjectResult::Select(select_result) => {
write!(
f,
"{}",
SelectResultDisplayer {
result: select_result
}
.display()
)
}
ObjectResult::Mutate(mutate_result) => {
write!(f, "{:?}", mutate_result)
}
},
Err(e) => write!(f, "Failed to execute, error: {:?}", e),
}
}
}

struct SelectResultDisplayer<'a> {
result: &'a SelectResult,
}

impl SelectResultDisplayer<'_> {
fn display(&self) -> impl Display {
let mut table = Table::new();
table.load_preset("||--+-++| ++++++");

if self.result.row_count == 0 {
return table;
}

let mut headers = vec![];
for column in &self.result.columns {
headers.push(Cell::new(format!(
"{}, #{:?}, #{:?}",
column.column_name,
SemanticType::from_i32(column.semantic_type).unwrap(),
ColumnDataType::from_i32(column.datatype).unwrap()
)));
}
table.set_header(headers);

let col_count = self.result.columns.len();
let row_count = self.result.row_count as usize;
let columns = self
.result
.columns
.iter()
.map(|col| {
util::values_to_string(
ColumnDataType::from_i32(col.datatype).unwrap(),
col.values.clone().unwrap(),
)
})
.collect::<Vec<_>>();

for row_index in 0..row_count {
let mut row = Vec::with_capacity(col_count);
for col in columns.iter() {
row.push(col[row_index].clone());
}
table.add_row(row);
}

table
}
}
29 changes: 29 additions & 0 deletions tests/runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2022 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use env::Env;
use sqlness::{ConfigBuilder, Runner};

mod env;
mod util;

#[tokio::main]
async fn main() {
let config = ConfigBuilder::default()
.case_dir("../cases".to_string())
.build()
.unwrap();
let runner = Runner::new_with_config(config, Env {}).await.unwrap();
runner.run().await.unwrap();
}
Loading

0 comments on commit b407ebf

Please sign in to comment.