feat: integration test suite (#487)

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
Ruihang Xia
2022-11-22 15:34:13 +08:00
committed by GitHub
parent c144a1b20e
commit b407ebf6bb
9 changed files with 625 additions and 16 deletions

View File

@@ -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" }

View File

@@ -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
tests/runner/Cargo.toml Normal file
View File

@@ -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
tests/runner/src/env.rs Normal file
View File

@@ -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
tests/runner/src/main.rs Normal file
View File

@@ -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();
}

97
tests/runner/src/util.rs Normal file
View File

@@ -0,0 +1,97 @@
// 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 client::api::v1::column::Values;
use client::api::v1::ColumnDataType;
pub fn values_to_string(data_type: ColumnDataType, values: Values) -> Vec<String> {
match data_type {
ColumnDataType::Int64 => values
.i64_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::Float64 => values
.f64_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::String => values.string_values,
ColumnDataType::Boolean => values
.bool_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::Int8 => values
.i8_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::Int16 => values
.i16_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::Int32 => values
.i32_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::Uint8 => values
.u8_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::Uint16 => values
.u16_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::Uint32 => values
.u32_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::Uint64 => values
.u64_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::Float32 => values
.f32_values
.into_iter()
.map(|val| val.to_string())
.collect(),
ColumnDataType::Binary => values
.binary_values
.into_iter()
.map(|val| format!("{:?}", val))
.collect(),
ColumnDataType::Datetime => values
.i64_values
.into_iter()
.map(|v| v.to_string())
.collect(),
ColumnDataType::Date => values
.i32_values
.into_iter()
.map(|v| v.to_string())
.collect(),
ColumnDataType::Timestamp => values
.ts_millis_values
.into_iter()
.map(|v| v.to_string())
.collect(),
}
}