Skip to content

Commit

Permalink
chore: reduce insertion size of fuzz test (#4243)
Browse files Browse the repository at this point in the history
* chore: reduce size of fuzz test

* chore: get env cfg variables
  • Loading branch information
WenyXu authored Jul 2, 2024
1 parent 2261360 commit be29e48
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ GT_MYSQL_ADDR = localhost:4002
# Setting for unstable fuzz tests
GT_FUZZ_BINARY_PATH=/path/to/
GT_FUZZ_INSTANCE_ROOT_DIR=/tmp/unstable_greptime
GT_FUZZ_INPUT_MAX_ROWS=2048
GT_FUZZ_INPUT_MAX_TABLES=32
GT_FUZZ_INPUT_MAX_COLUMNS=32
GT_FUZZ_INPUT_MAX_ALTER_ACTIONS=256
GT_FUZZ_INPUT_MAX_INSERT_ACTIONS=8
17 changes: 17 additions & 0 deletions tests-fuzz/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,20 @@ pub async fn compact_table(e: &Pool<MySql>, table_name: &Ident) -> Result<()> {

Ok(())
}

pub const GT_FUZZ_INPUT_MAX_ROWS: &str = "GT_FUZZ_INPUT_MAX_ROWS";
pub const GT_FUZZ_INPUT_MAX_TABLES: &str = "GT_FUZZ_INPUT_MAX_TABLES";
pub const GT_FUZZ_INPUT_MAX_COLUMNS: &str = "GT_FUZZ_INPUT_MAX_COLUMNS";
pub const GT_FUZZ_INPUT_MAX_ALTER_ACTIONS: &str = "GT_FUZZ_INPUT_MAX_ALTER_ACTIONS";
pub const GT_FUZZ_INPUT_MAX_INSERT_ACTIONS: &str = "GT_FUZZ_INPUT_MAX_INSERT_ACTIONS";

/// Retrieves a value from the environment variables
/// or returns a default value if the environment variable is not set.
pub fn get_from_env_or_default_value(key: &str, default_value: usize) -> usize {
let _ = dotenv::dotenv();
if let Ok(value) = env::var(key) {
value.parse().unwrap()
} else {
default_value
}
}
18 changes: 12 additions & 6 deletions tests-fuzz/targets/failover/fuzz_failover_mito_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ use tests_fuzz::utils::partition::{
};
use tests_fuzz::utils::pod_failure::{inject_datanode_pod_failure, recover_pod_failure};
use tests_fuzz::utils::{
compact_table, flush_memtable, init_greptime_connections_via_env, Connections,
GT_FUZZ_CLUSTER_NAME, GT_FUZZ_CLUSTER_NAMESPACE,
compact_table, flush_memtable, get_from_env_or_default_value,
init_greptime_connections_via_env, Connections, GT_FUZZ_CLUSTER_NAME,
GT_FUZZ_CLUSTER_NAMESPACE, GT_FUZZ_INPUT_MAX_COLUMNS, GT_FUZZ_INPUT_MAX_INSERT_ACTIONS,
GT_FUZZ_INPUT_MAX_ROWS, GT_FUZZ_INPUT_MAX_TABLES,
};
use tests_fuzz::validator::row::count_values;
use tokio::sync::Semaphore;
Expand Down Expand Up @@ -83,10 +85,14 @@ impl Arbitrary<'_> for FuzzInput {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
let seed = u.int_in_range(u64::MIN..=u64::MAX)?;
let mut rng = ChaChaRng::seed_from_u64(seed);
let columns = rng.gen_range(2..64);
let rows = rng.gen_range(2..2048);
let tables = rng.gen_range(1..64);
let inserts = rng.gen_range(2..8);
let max_columns = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_COLUMNS, 64);
let columns = rng.gen_range(2..max_columns);
let max_rows = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_ROWS, 2048);
let rows = rng.gen_range(2..max_rows);
let max_tables = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_TABLES, 64);
let tables = rng.gen_range(1..max_tables);
let max_inserts = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_INSERT_ACTIONS, 8);
let inserts = rng.gen_range(2..max_inserts);
Ok(FuzzInput {
columns,
rows,
Expand Down
8 changes: 6 additions & 2 deletions tests-fuzz/targets/fuzz_alter_logical_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ use tests_fuzz::ir::{
use tests_fuzz::translator::mysql::alter_expr::AlterTableExprTranslator;
use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator;
use tests_fuzz::translator::DslTranslator;
use tests_fuzz::utils::{init_greptime_connections_via_env, Connections};
use tests_fuzz::utils::{
get_from_env_or_default_value, init_greptime_connections_via_env, Connections,
GT_FUZZ_INPUT_MAX_ALTER_ACTIONS,
};
use tests_fuzz::validator;

struct FuzzContext {
Expand All @@ -65,7 +68,8 @@ impl Arbitrary<'_> for FuzzInput {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
let seed = u.int_in_range(u64::MIN..=u64::MAX)?;
let mut rng = ChaChaRng::seed_from_u64(seed);
let actions = rng.gen_range(1..256);
let max_actions = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_ALTER_ACTIONS, 256);
let actions = rng.gen_range(1..max_actions);

Ok(FuzzInput { seed, actions })
}
Expand Down
8 changes: 6 additions & 2 deletions tests-fuzz/targets/fuzz_alter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ use tests_fuzz::ir::{droppable_columns, modifiable_columns, AlterTableExpr, Crea
use tests_fuzz::translator::mysql::alter_expr::AlterTableExprTranslator;
use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator;
use tests_fuzz::translator::DslTranslator;
use tests_fuzz::utils::{init_greptime_connections_via_env, Connections};
use tests_fuzz::utils::{
get_from_env_or_default_value, init_greptime_connections_via_env, Connections,
GT_FUZZ_INPUT_MAX_COLUMNS,
};
use tests_fuzz::validator;
struct FuzzContext {
greptime: Pool<MySql>,
Expand All @@ -67,7 +70,8 @@ enum AlterTableOption {
}

fn generate_create_table_expr<R: Rng + 'static>(rng: &mut R) -> Result<CreateTableExpr> {
let columns = rng.gen_range(2..30);
let max_columns = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_COLUMNS, 30);
let columns = rng.gen_range(2..max_columns);
let create_table_generator = CreateTableExprGeneratorBuilder::default()
.name_generator(Box::new(MappedGenerator::new(
WordGenerator,
Expand Down
8 changes: 6 additions & 2 deletions tests-fuzz/targets/fuzz_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ use tests_fuzz::generator::Generator;
use tests_fuzz::ir::CreateTableExpr;
use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator;
use tests_fuzz::translator::DslTranslator;
use tests_fuzz::utils::{init_greptime_connections_via_env, Connections};
use tests_fuzz::utils::{
get_from_env_or_default_value, init_greptime_connections_via_env, Connections,
GT_FUZZ_INPUT_MAX_COLUMNS,
};
use tests_fuzz::validator;

struct FuzzContext {
Expand All @@ -54,7 +57,8 @@ impl Arbitrary<'_> for FuzzInput {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
let seed = u.int_in_range(u64::MIN..=u64::MAX)?;
let mut rng = ChaChaRng::seed_from_u64(seed);
let columns = rng.gen_range(2..30);
let max_columns = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_COLUMNS, 30);
let columns = rng.gen_range(2..max_columns);
Ok(FuzzInput { columns, seed })
}
}
Expand Down
11 changes: 8 additions & 3 deletions tests-fuzz/targets/fuzz_insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ use tests_fuzz::ir::{
use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator;
use tests_fuzz::translator::mysql::insert_expr::InsertIntoExprTranslator;
use tests_fuzz::translator::DslTranslator;
use tests_fuzz::utils::{flush_memtable, init_greptime_connections_via_env, Connections};
use tests_fuzz::utils::{
flush_memtable, get_from_env_or_default_value, init_greptime_connections_via_env, Connections,
GT_FUZZ_INPUT_MAX_COLUMNS, GT_FUZZ_INPUT_MAX_ROWS,
};
use tests_fuzz::validator;

struct FuzzContext {
Expand All @@ -63,8 +66,10 @@ impl Arbitrary<'_> for FuzzInput {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
let seed = u.int_in_range(u64::MIN..=u64::MAX)?;
let mut rng = ChaChaRng::seed_from_u64(seed);
let columns = rng.gen_range(2..30);
let rows = rng.gen_range(1..4096);
let max_columns = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_COLUMNS, 30);
let columns = rng.gen_range(2..max_columns);
let max_row = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_ROWS, 2048);
let rows = rng.gen_range(1..max_row);
Ok(FuzzInput {
columns,
rows,
Expand Down
17 changes: 12 additions & 5 deletions tests-fuzz/targets/fuzz_insert_logical_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator;
use tests_fuzz::translator::mysql::insert_expr::InsertIntoExprTranslator;
use tests_fuzz::translator::DslTranslator;
use tests_fuzz::utils::{
compact_table, flush_memtable, init_greptime_connections_via_env, Connections,
compact_table, flush_memtable, get_from_env_or_default_value,
init_greptime_connections_via_env, Connections, GT_FUZZ_INPUT_MAX_ROWS,
GT_FUZZ_INPUT_MAX_TABLES,
};
use tests_fuzz::validator;
struct FuzzContext {
Expand All @@ -60,14 +62,18 @@ impl FuzzContext {
struct FuzzInput {
seed: u64,
tables: usize,
rows: usize,
}

impl Arbitrary<'_> for FuzzInput {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
let seed = u.int_in_range(u64::MIN..=u64::MAX)?;
let mut rng = ChaChaRng::seed_from_u64(seed);
let tables = rng.gen_range(1..256);
Ok(FuzzInput { tables, seed })
let max_tables = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_TABLES, 32);
let tables = rng.gen_range(1..max_tables);
let max_row = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_ROWS, 2048);
let rows = rng.gen_range(1..max_row);
Ok(FuzzInput { tables, seed, rows })
}
}

Expand Down Expand Up @@ -204,11 +210,11 @@ async fn validate_values(
}

async fn insert_values<R: Rng + 'static>(
rows: usize,
ctx: &FuzzContext,
rng: &mut R,
logical_table_ctx: TableContextRef,
) -> Result<InsertIntoExpr> {
let rows = rng.gen_range(1..2048);
let insert_expr = generate_insert_expr(rows, rng, logical_table_ctx.clone())?;
let translator = InsertIntoExprTranslator;
let sql = translator.translate(&insert_expr)?;
Expand Down Expand Up @@ -257,7 +263,8 @@ async fn execute_insert(ctx: FuzzContext, input: FuzzInput) -> Result<()> {
info!("Create logical table: {sql}, result: {result:?}");
let logical_table_ctx = Arc::new(TableContext::from(&create_logical_table_expr));

let insert_expr = insert_values(&ctx, &mut rng, logical_table_ctx.clone()).await?;
let insert_expr =
insert_values(input.rows, &ctx, &mut rng, logical_table_ctx.clone()).await?;
validate_values(&ctx, logical_table_ctx.clone(), &insert_expr).await?;
tables.insert(logical_table_ctx.name.clone(), logical_table_ctx.clone());
if rng.gen_bool(0.1) {
Expand Down
13 changes: 8 additions & 5 deletions tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator;
use tests_fuzz::translator::DslTranslator;
use tests_fuzz::utils::config::{get_conf_path, write_config_file};
use tests_fuzz::utils::health::HttpHealthChecker;
use tests_fuzz::utils::load_unstable_test_env_variables;
use tests_fuzz::utils::process::{ProcessManager, ProcessState, UnstableProcessController};
use tests_fuzz::utils::{
get_from_env_or_default_value, load_unstable_test_env_variables, GT_FUZZ_INPUT_MAX_TABLES,
};
use tests_fuzz::{error, validator};
use tokio::sync::watch;

Expand All @@ -61,15 +63,16 @@ impl FuzzContext {
#[derive(Clone, Debug)]
struct FuzzInput {
seed: u64,
num: usize,
tables: usize,
}

impl Arbitrary<'_> for FuzzInput {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
let seed = u.int_in_range(u64::MIN..=u64::MAX)?;
let mut rng = ChaChaRng::seed_from_u64(seed);
let num = rng.gen_range(1..500);
Ok(FuzzInput { seed, num })
let max_tables = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_TABLES, 256);
let tables = rng.gen_range(1..max_tables);
Ok(FuzzInput { seed, tables })
}
}

Expand Down Expand Up @@ -134,7 +137,7 @@ async fn execute_unstable_create_table(
let ctx = FuzzContext { greptime: mysql };
let mut table_states = HashMap::new();

for _ in 0..input.num {
for _ in 0..input.tables {
let expr = generate_create_table_expr(&mut rng);
let table_ctx = Arc::new(TableContext::from(&expr));
let table_name = expr.table_name.to_string();
Expand Down

0 comments on commit be29e48

Please sign in to comment.