Skip to content

Commit

Permalink
fix(tests): sql_cmd_test
Browse files Browse the repository at this point in the history
1. recycle_bin_root_path does not setted up randomly and get cleaned up
after test down.
2. UTs should ensure cleanup even test fails, missed cleanup might
   affect subsequent tests
3. cleanup tmp dirs even on signal
4. workaround: standalone & cluster env require distinct db_root_path
  • Loading branch information
aceforeverd committed Mar 27, 2024
1 parent 9726852 commit 4e44f62
Show file tree
Hide file tree
Showing 33 changed files with 158 additions and 168 deletions.
1 change: 0 additions & 1 deletion hybridse/src/planv2/planner_v2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ base::Status SimplePlannerV2::CreateASTScriptPlan(const zetasql::ASTScript *scri
CHECK_STATUS(ConvertASTScript(script, node_manager_, &resolved_trees));
CHECK_TRUE(nullptr != resolved_trees && resolved_trees->GetSize() > 0, common::kPlanError,
"fail to create plan, sql trees is null or empty");
DLOG(INFO) << "parsed SqlNode:\n" << resolved_trees->GetTreeString();
CHECK_STATUS(CreatePlanTree(resolved_trees->GetList(), plan_trees));
DLOG(INFO) << "PlanNode:";
for (decltype(plan_trees.size()) i = 0; i < plan_trees.size(); ++i) {
Expand Down
2 changes: 2 additions & 0 deletions src/apiserver/api_server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "rapidjson/error/en.h"
#include "rapidjson/rapidjson.h"
#include "sdk/mini_cluster.h"
#include "test/util.h"

DEFINE_int32(zk_port, 6181, "zk port");
DEFINE_string(api_server_port, "8084", "api server port");
Expand Down Expand Up @@ -1412,5 +1413,6 @@ int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
::google::ParseCommandLineFlags(&argc, &argv, true);
::openmldb::base::SetupGlog(true);
::openmldb::test::InitRandomDiskFlags("api_server_test");

Check warning on line 1416 in src/apiserver/api_server_test.cc

View check run for this annotation

Codecov / codecov/patch

src/apiserver/api_server_test.cc#L1416

Added line #L1416 was not covered by tests
return RUN_ALL_TESTS();
}
1 change: 1 addition & 0 deletions src/catalog/distribute_iterator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1011,5 +1011,6 @@ TEST_F(DistributeIteratorTest, IteratorZero) {

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
::openmldb::test::InitRandomDiskFlags("distribute_iterator_test");
return RUN_ALL_TESTS();
}
2 changes: 2 additions & 0 deletions src/cmd/single_tablet_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ int main(int argc, char** argv) {
::hybridse::vm::Engine::InitializeGlobalLLVM();
::testing::InitGoogleTest(&argc, argv);
::google::ParseCommandLineFlags(&argc, &argv, true);
::openmldb::test::InitRandomDiskFlags("single_tablet_test");

FLAGS_zk_session_timeout = 100000;
FLAGS_enable_distsql = true;
mc.SetUp(1);
Expand Down
77 changes: 37 additions & 40 deletions src/cmd/sql_cmd_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
DECLARE_string(host);
DECLARE_int32(port);
DECLARE_uint32(traverse_cnt_limit);
DECLARE_string(ssd_root_path);
DECLARE_string(hdd_root_path);
DECLARE_string(recycle_bin_ssd_root_path);
DECLARE_string(recycle_bin_hdd_root_path);
DECLARE_uint32(get_table_status_interval);

::openmldb::sdk::StandaloneEnv env;
Expand Down Expand Up @@ -1129,15 +1125,25 @@ TEST_P(DBSDKTest, DeployWithBias) {
TEST_P(DBSDKTest, Truncate) {
auto cli = GetParam();
sr = cli->sr;
std::string db_name = "test2";
std::string table_name = "test1";
std::string ddl = "create table test1 (c1 string, c2 int, c3 bigint, INDEX(KEY=c1, ts=c3));";
std::string db_name = absl::StrCat("db_", GenRand());
std::string table_name = absl::StrCat("tb_", GenRand());
std::string ddl =
absl::Substitute("create table $0 (c1 string, c2 int, c3 bigint, INDEX(KEY=c1, ts=c3))", table_name);
ProcessSQLs(sr, {
"set @@execute_mode = 'online'",
absl::StrCat("create database ", db_name, ";"),
absl::StrCat("use ", db_name, ";"),
ddl,
});
absl::Cleanup c = [&]() {
ProcessSQLs(sr, {
absl::StrCat("use ", db_name, ";"),
absl::StrCat("drop table ", table_name),
absl::StrCat("drop database ", db_name),
});
};

absl::SleepFor(absl::Seconds(5));
hybridse::sdk::Status status;
sr->ExecuteSQL(absl::StrCat("truncate table ", table_name, ";"), &status);
ASSERT_TRUE(status.IsOK()) << status.ToString();
Expand All @@ -1150,6 +1156,7 @@ TEST_P(DBSDKTest, Truncate) {
sr->ExecuteSQL(absl::StrCat("insert into ", table_name, " values ('", key, "', 11, ", ts, ");"), &status);
}
}
absl::SleepFor(absl::Seconds(5));

res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 100);
Expand All @@ -1160,11 +1167,6 @@ TEST_P(DBSDKTest, Truncate) {
sr->ExecuteSQL(absl::StrCat("insert into ", table_name, " values ('aa', 11, 100);"), &status);
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 1);
ProcessSQLs(sr, {
absl::StrCat("use ", db_name, ";"),
absl::StrCat("drop table ", table_name),
absl::StrCat("drop database ", db_name),
});
}

TEST_P(DBSDKTest, DeletetRange) {
Expand Down Expand Up @@ -3458,6 +3460,7 @@ TEST_P(DBSDKTest, MapTypeTable) {
});
absl::Cleanup clean = [&]() {
ProcessSQLs(sr, {
absl::StrCat("use ", db),
absl::Substitute("drop table $0", table),
absl::Substitute("drop database $0", db),
});
Expand Down Expand Up @@ -3636,6 +3639,11 @@ TEST_P(DBSDKTest, ShowTableStatusUnderRoot) {
});
// reset to empty db
sr->SetDatabase("");
absl::Cleanup c = [&]() {
ProcessSQLs(sr, {absl::StrCat("use ", db_name), absl::StrCat("drop table ", tb_name),
absl::StrCat("drop database ", db_name)});
sr->SetDatabase("");
};

// sleep for 4s, name server should updated TableInfo in schedule
absl::SleepFor(absl::Seconds(4));
Expand All @@ -3656,11 +3664,6 @@ TEST_P(DBSDKTest, ShowTableStatusUnderRoot) {
}
// runs HandleSQL only for the purpose of pretty print result in console
HandleSQL("show table status");

// teardown
ProcessSQLs(sr, {absl::StrCat("use ", db_name), absl::StrCat("drop table ", tb_name),
absl::StrCat("drop database ", db_name)});
sr->SetDatabase("");
}

// show table status with patterns when no database is selected
Expand All @@ -3683,6 +3686,11 @@ TEST_P(DBSDKTest, ShowTableStatusLike) {
});
// reset to empty db
sr->SetDatabase("");
absl::Cleanup c = [&]() {
ProcessSQLs(sr, {absl::StrCat("use ", db_name), absl::StrCat("drop table ", tb_name),
absl::StrCat("drop database ", db_name)});
sr->SetDatabase("");
};

// sleep for 4s, name server should updated TableInfo in schedule
absl::SleepFor(absl::Seconds(4));
Expand Down Expand Up @@ -3753,11 +3761,6 @@ TEST_P(DBSDKTest, ShowTableStatusLike) {
{{{}, tb_name, db_name, "memory", "1", {{}, "0"}, {{}, "0"}, "1", "0", "1", "NULL", "NULL", "NULL", ""}},
rs.get(), true, false);
}

// teardown
ProcessSQLs(sr, {absl::StrCat("use ", db_name), absl::StrCat("drop table ", tb_name),
absl::StrCat("drop database ", db_name)});
sr->SetDatabase("");
}

TEST_P(DBSDKTest, ShowTableStatusForHddTable) {
Expand Down Expand Up @@ -4003,6 +4006,17 @@ TEST_F(SqlCmdTest, SelectWithAddNewIndex) {
absl::StrCat("CREATE INDEX index1 ON ", db1_name, ".", tb1_name,
" (c2) OPTIONS (ttl=10m, ttl_type=absolute);"),
});
absl::Cleanup clean = [&]() {
ProcessSQLs(sr, {
absl::StrCat("use ", db1_name, ";"),
absl::StrCat("drop table ", tb1_name),
absl::StrCat("drop database ", db1_name),
absl::StrCat("drop database ", db2_name),
});

sr->SetDatabase("");
};

absl::SleepFor(absl::Seconds(10));
hybridse::sdk::Status status;
auto res = sr->ExecuteSQL(absl::StrCat("use ", db1_name, ";"), &status);
Expand All @@ -4016,14 +4030,6 @@ TEST_F(SqlCmdTest, SelectWithAddNewIndex) {
absl::SleepFor(absl::Seconds(2));
res = sr->ExecuteSQL(absl::StrCat("select id,c1,c2,c3 from ", tb1_name, " where c2=1;"), &status);
ASSERT_EQ(res->Size(), 3);

ProcessSQLs(sr, {
absl::StrCat("use ", db1_name, ";"),
absl::StrCat("drop table ", tb1_name),
absl::StrCat("drop database ", db1_name),
});

sr->SetDatabase("");
}

// --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -4205,17 +4211,8 @@ int main(int argc, char** argv) {
FLAGS_traverse_cnt_limit = 500;
FLAGS_zk_session_timeout = 100000;
FLAGS_get_table_status_interval = 1000;
// enable disk table flags
std::filesystem::path tmp_path = std::filesystem::temp_directory_path() / "openmldb";
absl::Cleanup clean = [&tmp_path]() { std::filesystem::remove_all(tmp_path); };

const std::string& tmp_path_str = tmp_path.string();
FLAGS_ssd_root_path = absl::StrCat(tmp_path_str, "/ssd_root_random_", ::openmldb::test::GenRand());
FLAGS_hdd_root_path = absl::StrCat(tmp_path_str, "/hdd_root_random_", ::openmldb::test::GenRand());
FLAGS_recycle_bin_hdd_root_path =
absl::StrCat(tmp_path_str, "/recycle_hdd_root_random_", ::openmldb::test::GenRand());
FLAGS_recycle_bin_ssd_root_path =
absl::StrCat(tmp_path_str, "/recycle_ssd_root_random_", ::openmldb::test::GenRand());
::openmldb::test::InitRandomDiskFlags("sql_cmd_test");

::openmldb::sdk::MiniCluster mc(6181);
::openmldb::cmd::mc_ = &mc;
Expand Down
1 change: 1 addition & 0 deletions src/nameserver/name_server_create_remote_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,7 @@ int main(int argc, char** argv) {
srand(time(NULL));
::openmldb::base::SetLogLevel(INFO);
::google::ParseCommandLineFlags(&argc, &argv, true);
::openmldb::test::InitRandomDiskFlags("name_server_create_remote_test");
FLAGS_system_table_replica_num = 0;
return RUN_ALL_TESTS();
}
5 changes: 1 addition & 4 deletions src/nameserver/name_server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1291,10 +1291,7 @@ int main(int argc, char** argv) {
::openmldb::base::SetLogLevel(INFO);
::google::ParseCommandLineFlags(&argc, &argv, true);
FLAGS_zk_cluster = "127.0.0.1:6181";
::openmldb::test::TempPath tmp_path;
FLAGS_db_root_path = tmp_path.GetTempPath("memory");
FLAGS_ssd_root_path = tmp_path.GetTempPath("ssd");
FLAGS_hdd_root_path = tmp_path.GetTempPath("hdd");
::openmldb::test::InitRandomDiskFlags("name_server_test");
FLAGS_system_table_replica_num = 0;
return RUN_ALL_TESTS();
}
1 change: 1 addition & 0 deletions src/nameserver/new_server_env_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ int main(int argc, char** argv) {
srand(time(NULL));
::openmldb::base::SetLogLevel(INFO);
::google::ParseCommandLineFlags(&argc, &argv, true);
::openmldb::test::InitRandomDiskFlags("new_server_env_test");
FLAGS_system_table_replica_num = 0;
return RUN_ALL_TESTS();
}
4 changes: 1 addition & 3 deletions src/nameserver/standalone_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
::openmldb::base::SetLogLevel(INFO);
::google::ParseCommandLineFlags(&argc, &argv, true);
::openmldb::test::TempPath tmp_path;
FLAGS_db_root_path = tmp_path.GetTempPath();
FLAGS_hdd_root_path = tmp_path.GetTempPath();
::openmldb::test::InitRandomDiskFlags("standalone_test");
return RUN_ALL_TESTS();
}
4 changes: 1 addition & 3 deletions src/nameserver/system_table_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

DECLARE_string(zk_cluster);
DECLARE_string(zk_root_path);
DECLARE_string(db_root_path);
DECLARE_int32(zk_session_timeout);

namespace openmldb {
Expand Down Expand Up @@ -107,7 +106,6 @@ int main(int argc, char** argv) {
srand(time(NULL));
::openmldb::base::SetLogLevel(INFO);
::google::ParseCommandLineFlags(&argc, &argv, true);
::openmldb::test::TempPath tmp_path;
FLAGS_db_root_path = tmp_path.GetTempPath();
::openmldb::test::InitRandomDiskFlags("system_table_test");
return RUN_ALL_TESTS();
}
1 change: 1 addition & 0 deletions src/replica/binlog_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ int main(int argc, char** argv) {
srand(time(NULL));
::openmldb::base::SetLogLevel(DEBUG);
::google::ParseCommandLineFlags(&argc, &argv, true);
::openmldb::test::InitRandomDiskFlags("binlog_test");
int ret = 0;
std::vector<std::string> vec{"off", "zlib", "snappy"};
::openmldb::test::TempPath tmp_path;
Expand Down
4 changes: 1 addition & 3 deletions src/replica/snapshot_replica_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,6 @@ int main(int argc, char** argv) {
srand(time(NULL));
::openmldb::base::SetLogLevel(INFO);
::google::ParseCommandLineFlags(&argc, &argv, true);
::openmldb::test::TempPath temp_path;
FLAGS_db_root_path = temp_path.GetTempPath();
FLAGS_hdd_root_path = temp_path.GetTempPath();
::openmldb::test::InitRandomDiskFlags("snapshot_replica_test");
return RUN_ALL_TESTS();
}
2 changes: 2 additions & 0 deletions src/sdk/db_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "proto/tablet.pb.h"
#include "proto/type.pb.h"
#include "sdk/mini_cluster.h"
#include "test/util.h"

namespace openmldb::sdk {

Expand Down Expand Up @@ -148,5 +149,6 @@ int main(int argc, char** argv) {
FLAGS_zk_session_timeout = 100000;
srand(time(nullptr));
::openmldb::base::SetupGlog(true);
::openmldb::test::InitRandomDiskFlags("db_sdk_test");
return RUN_ALL_TESTS();
}
12 changes: 4 additions & 8 deletions src/sdk/mini_cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "rpc/rpc_client.h"
#include "sdk/db_sdk.h"
#include "tablet/tablet_impl.h"
#include "test/util.h"

DECLARE_string(endpoint);
DECLARE_string(tablet);
Expand Down Expand Up @@ -75,7 +76,6 @@ class MiniCluster {
if (ns_client_) {
delete ns_client_;
}
base::RemoveDirRecursive(db_root_path_);
}

bool SetUp(int tablet_num = 2) {
Expand All @@ -88,8 +88,6 @@ class MiniCluster {
FLAGS_get_table_diskused_interval = 2000;
FLAGS_sync_deploy_stats_timeout = 2000;
srand(time(NULL));
db_root_path_ = "/tmp/mini_cluster" + GenRand();
FLAGS_db_root_path = db_root_path_;
zk_cluster_ = "127.0.0.1:" + std::to_string(zk_port_);
FLAGS_zk_cluster = zk_cluster_;
std::string ns_endpoint = "127.0.0.1:" + GenRand();
Expand Down Expand Up @@ -220,7 +218,6 @@ class MiniCluster {
::openmldb::client::NsClient* ns_client_;
std::map<std::string, ::openmldb::tablet::TabletImpl*> tablets_;
std::map<std::string, ::openmldb::client::TabletClient*> tb_clients_;
std::string db_root_path_;
};

class StandaloneEnv {
Expand All @@ -233,13 +230,13 @@ class StandaloneEnv {
if (ns_client_) {
delete ns_client_;
}
base::RemoveDirRecursive(db_root_path_);
}

bool SetUp() {
srand(time(nullptr));
db_root_path_ = "/tmp/standalone_env" + std::to_string(GenRand());
FLAGS_db_root_path = db_root_path_;
// shit happens, cluster & standalone require distinct db_root_path
test::TempPath tmp;
FLAGS_db_root_path = tmp.GetTempPath();
if (!StartTablet(&tb_server_)) {
LOG(WARNING) << "fail to start tablet";
return false;
Expand Down Expand Up @@ -333,7 +330,6 @@ class StandaloneEnv {
uint64_t ns_port_ = 0;
::openmldb::client::NsClient* ns_client_;
::openmldb::client::TabletClient* tb_client_;
std::string db_root_path_;
};

} // namespace sdk
Expand Down
2 changes: 2 additions & 0 deletions src/sdk/mini_cluster_batch_bm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "sdk/sql_router.h"
#include "sdk/table_reader.h"
#include "test/base_test.h"
#include "test/util.h"
#include "vm/catalog.h"

DECLARE_bool(enable_distsql);
Expand Down Expand Up @@ -895,6 +896,7 @@ int main(int argc, char** argv) {
::google::ParseCommandLineFlags(&argc, &argv, true);
::openmldb::base::SetupGlog(true);
::hybridse::vm::Engine::InitializeGlobalLLVM();
::openmldb::test::InitRandomDiskFlags("mini_cluster_batch_bm");
FLAGS_enable_distsql = hybridse::sqlcase::SqlCase::IsCluster();
FLAGS_enable_localtablet = !hybridse::sqlcase::SqlCase::IsDisableLocalTablet();
::benchmark::Initialize(&argc, argv);
Expand Down
2 changes: 2 additions & 0 deletions src/sdk/mini_cluster_request_bm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "benchmark/benchmark.h"
#include "sdk/mini_cluster.h"
#include "sdk/mini_cluster_bm.h"
#include "test/util.h"
DECLARE_bool(enable_distsql);
DECLARE_bool(enable_localtablet);
::openmldb::sdk::MiniCluster* mc;
Expand Down Expand Up @@ -56,6 +57,7 @@ int main(int argc, char** argv) {
::google::ParseCommandLineFlags(&argc, &argv, true);
::openmldb::base::SetupGlog(true);
::hybridse::vm::Engine::InitializeGlobalLLVM();
::openmldb::test::InitRandomDiskFlags("min_cluster_request_bm");
FLAGS_enable_distsql = hybridse::sqlcase::SqlCase::IsCluster();
FLAGS_enable_localtablet = !hybridse::sqlcase::SqlCase::IsDisableLocalTablet();
::benchmark::Initialize(&argc, argv);
Expand Down
Loading

0 comments on commit 4e44f62

Please sign in to comment.