forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatalogMigrationTest.cpp
368 lines (300 loc) · 12.4 KB
/
CatalogMigrationTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Copyright 2020 OmniSci, Inc.
*
* 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.
*/
/**
* @file CatalogMigrationTest.cpp
* @brief Test suite for catalog migrations
*/
#include <gtest/gtest.h>
#include <boost/filesystem.hpp>
#include "Catalog/Catalog.h"
#include "DBHandlerTestHelpers.h"
#include "DataMgr/ForeignStorage/AbstractFileStorageDataWrapper.h"
#include "SqliteConnector/SqliteConnector.h"
#include "TestHelpers.h"
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
extern bool g_enable_fsi;
extern bool g_enable_s3_fsi;
namespace BF = boost::filesystem;
using SC = Catalog_Namespace::SysCatalog;
namespace {
bool table_exists(SqliteConnector& conn, const std::string& table_name) {
conn.query("SELECT name FROM sqlite_master WHERE type='table' AND name='" + table_name +
"'");
return conn.getNumRows() > 0;
}
bool has_result(SqliteConnector& conn, const std::string& query) {
conn.query(query);
return conn.getNumRows() > 0;
}
} // namespace
class CatalogTest : public testing::Test {
protected:
CatalogTest()
: cat_conn_("omnisci", BF::absolute("mapd_catalogs", BASE_PATH).string()) {}
static void SetUpTestSuite() {
SC::instance().init(BASE_PATH, nullptr, {}, nullptr, false, false, {});
}
std::vector<std::string> getTables(SqliteConnector& conn) {
conn.query("SELECT name FROM sqlite_master WHERE type='table';");
std::vector<std::string> tables;
for (size_t i = 0; i < conn.getNumRows(); i++) {
tables.emplace_back(conn.getData<std::string>(i, 0));
}
return tables;
}
std::unique_ptr<Catalog_Namespace::Catalog> initCatalog() {
Catalog_Namespace::DBMetadata db_metadata;
db_metadata.dbName = "omnisci";
std::vector<LeafHostInfo> leaves{};
return std::make_unique<Catalog_Namespace::Catalog>(
BASE_PATH, db_metadata, nullptr, leaves, nullptr, false);
}
SqliteConnector cat_conn_;
};
class SysCatalogTest : public CatalogTest {
protected:
SysCatalogTest()
: syscat_conn_("omnisci_system_catalog",
BF::absolute("mapd_catalogs", BASE_PATH).string()) {}
void TearDown() override {
if (tableExists("mapd_users")) {
syscat_conn_.query("DELETE FROM mapd_users WHERE name='test_user'");
}
if (tableExists("mapd_object_permissions")) {
syscat_conn_.query(
"DELETE FROM mapd_object_permissions WHERE roleName='test_user'");
}
}
bool hasResult(const std::string& query) { return has_result(syscat_conn_, query); }
bool tableExists(const std::string& table_name) {
return table_exists(syscat_conn_, table_name);
}
void createLegacyTestUser() {
// This creates a test user in mapd_users syscat table, but does not properly add it
// to mapd_object_permissions so it is incomplete by current standards.
ASSERT_TRUE(table_exists(syscat_conn_, "mapd_users"));
syscat_conn_.query("DELETE FROM mapd_users WHERE name='test_user'");
syscat_conn_.query_with_text_params(
"INSERT INTO mapd_users (name, passwd_hash, issuper, can_login) VALUES (?, ?, ?, "
"?)",
{"test_user", "passwd", "true", "true"});
}
static void reinitializeSystemCatalog() {
SC::destroy();
SC::instance().init(BASE_PATH, nullptr, {}, nullptr, false, false, {});
}
SqliteConnector syscat_conn_;
};
// Check that we migrate correctly from pre 4.0 catalog.
TEST_F(SysCatalogTest, MigrateRoles) {
// Make sure the post 4.0 tables do not exist to simulate migration.
syscat_conn_.query("DROP TABLE IF EXISTS mapd_roles");
syscat_conn_.query("DROP TABLE IF EXISTS mapd_object_permissions");
createLegacyTestUser();
// Create the pre 4.0 mapd_privileges table.
syscat_conn_.query(
"CREATE TABLE IF NOT EXISTS mapd_privileges (userid integer references mapd_users, "
"dbid integer references mapd_databases, select_priv boolean, insert_priv boolean, "
"UNIQUE(userid, dbid))");
// Copy users who are not the admin (userid 0) into the pre 4.0 mapd_privileges table.
syscat_conn_.query(
"INSERT INTO mapd_privileges (userid, dbid) SELECT userid, default_db FROM "
"mapd_users WHERE userid <> 0");
// Re-initialization should perform migrations.
reinitializeSystemCatalog();
// Users should be inserted into mapd_object_permissions but not mapd_roles on
// migration.
ASSERT_TRUE(tableExists("mapd_roles"));
ASSERT_FALSE(hasResult("SELECT roleName FROM mapd_roles WHERE roleName='test_user'"));
ASSERT_TRUE(tableExists("mapd_object_permissions"));
ASSERT_TRUE(hasResult(
"SELECT roleName FROM mapd_object_permissions WHERE roleName='test_user'"));
}
TEST_F(SysCatalogTest, FixIncorrectRolesMigration) {
ASSERT_TRUE(tableExists("mapd_roles"));
createLegacyTestUser();
// Setup an incorrect migration situation where we have usernames inserted into
// mapd_roles. This could occur between versions 4.0 and 5.7 and should now be fixed.
ASSERT_TRUE(tableExists("mapd_users"));
syscat_conn_.query("DELETE FROM mapd_roles WHERE roleName='test_user'");
syscat_conn_.query_with_text_params("INSERT INTO mapd_roles VALUES (?, ?)",
{"test_user", "test_user"});
ASSERT_TRUE(hasResult("SELECT name FROM mapd_users WHERE name='test_user'"));
ASSERT_TRUE(hasResult("SELECT roleName FROM mapd_roles WHERE roleName='test_user'"));
// When we re-initialize the SysCatalog we should fix incorrect past migrations.
reinitializeSystemCatalog();
ASSERT_TRUE(hasResult("SELECT name FROM mapd_users WHERE name='test_user'"));
ASSERT_FALSE(hasResult("SELECT roleName FROM mapd_roles WHERE roleName='test_user'"));
}
class FsiSchemaTest : public CatalogTest {
protected:
static void SetUpTestSuite() {
g_enable_s3_fsi = true;
CatalogTest::SetUpTestSuite();
}
void SetUp() override {
g_enable_fsi = false;
dropFsiTables();
}
void TearDown() override { dropFsiTables(); }
void assertExpectedDefaultServer(Catalog_Namespace::Catalog* catalog,
const std::string& server_name,
const std::string& data_wrapper,
const int32_t user_id) {
auto foreign_server = catalog->getForeignServerFromStorage(server_name);
ASSERT_GT(foreign_server->id, 0);
ASSERT_EQ(server_name, foreign_server->name);
ASSERT_EQ(data_wrapper, foreign_server->data_wrapper_type);
ASSERT_EQ(user_id, foreign_server->user_id);
ASSERT_TRUE(foreign_server->options.find(
foreign_storage::AbstractFileStorageDataWrapper::STORAGE_TYPE_KEY) !=
foreign_server->options.end());
ASSERT_EQ(foreign_storage::AbstractFileStorageDataWrapper::LOCAL_FILE_STORAGE_TYPE,
foreign_server->options
.find(foreign_storage::AbstractFileStorageDataWrapper::STORAGE_TYPE_KEY)
->second);
ASSERT_TRUE(foreign_server->options.find(
foreign_storage::AbstractFileStorageDataWrapper::BASE_PATH_KEY) ==
foreign_server->options.end());
// check that server loaded from storage matches that in memory
auto foreign_server_in_memory = catalog->getForeignServer(server_name);
ASSERT_EQ(foreign_server->id, foreign_server_in_memory->id);
ASSERT_EQ(foreign_server_in_memory->name, foreign_server->name);
ASSERT_EQ(foreign_server_in_memory->data_wrapper_type,
foreign_server->data_wrapper_type);
ASSERT_EQ(foreign_server_in_memory->user_id, foreign_server->user_id);
ASSERT_TRUE(foreign_server_in_memory->options.find(
foreign_storage::AbstractFileStorageDataWrapper::STORAGE_TYPE_KEY) !=
foreign_server_in_memory->options.end());
ASSERT_EQ(foreign_storage::AbstractFileStorageDataWrapper::LOCAL_FILE_STORAGE_TYPE,
foreign_server_in_memory->options
.find(foreign_storage::AbstractFileStorageDataWrapper::STORAGE_TYPE_KEY)
->second);
ASSERT_TRUE(foreign_server_in_memory->options.find(
foreign_storage::AbstractFileStorageDataWrapper::BASE_PATH_KEY) ==
foreign_server_in_memory->options.end());
}
void assertFsiTablesExist() {
auto tables = getTables(cat_conn_);
ASSERT_FALSE(std::find(tables.begin(), tables.end(), "omnisci_foreign_servers") ==
tables.end());
ASSERT_FALSE(std::find(tables.begin(), tables.end(), "omnisci_foreign_tables") ==
tables.end());
}
void assertFsiTablesDoNotExist() {
auto tables = getTables(cat_conn_);
ASSERT_TRUE(std::find(tables.begin(), tables.end(), "omnisci_foreign_servers") ==
tables.end());
ASSERT_TRUE(std::find(tables.begin(), tables.end(), "omnisci_foreign_tables") ==
tables.end());
}
private:
void dropFsiTables() {
cat_conn_.query("DROP TABLE IF EXISTS omnisci_foreign_servers;");
cat_conn_.query("DROP TABLE IF EXISTS omnisci_foreign_tables;");
}
};
TEST_F(FsiSchemaTest, FsiTablesNotCreatedWhenFsiIsDisabled) {
assertFsiTablesDoNotExist();
auto catalog = initCatalog();
assertFsiTablesDoNotExist();
}
TEST_F(FsiSchemaTest, FsiTablesAreCreatedWhenFsiIsEnabled) {
assertFsiTablesDoNotExist();
g_enable_fsi = true;
auto catalog = initCatalog();
assertFsiTablesExist();
}
TEST_F(FsiSchemaTest, FsiTablesAreNotDroppedWhenFsiIsDisabled) {
assertFsiTablesDoNotExist();
g_enable_fsi = true;
initCatalog();
assertFsiTablesExist();
g_enable_fsi = false;
initCatalog();
assertFsiTablesExist();
}
class ForeignTablesTest : public DBHandlerTestFixture {
protected:
static void SetUpTestSuite() {
g_enable_fsi = true;
DBHandlerTestFixture::SetUpTestSuite();
}
static void TearDownTestSuite() {
DBHandlerTestFixture::TearDownTestSuite();
g_enable_fsi = false;
}
void SetUp() override {
g_enable_fsi = true;
DBHandlerTestFixture::SetUp();
dropTestTables();
}
void TearDown() override {
dropTestTables();
DBHandlerTestFixture::TearDown();
}
private:
void dropTestTables() {
g_enable_fsi = true;
sql("DROP FOREIGN TABLE IF EXISTS test_foreign_table;");
sql("DROP TABLE IF EXISTS test_table;");
sql("DROP VIEW IF EXISTS test_view;");
}
};
TEST_F(ForeignTablesTest, ForeignTablesAreNotDroppedWhenFsiIsDisabled) {
const auto file_path = BF::canonical("../../Tests/FsiDataFiles/example_1.csv").string();
sql("CREATE FOREIGN TABLE test_foreign_table (c1 int) SERVER omnisci_local_csv "
"WITH (file_path = '" +
file_path + "');");
sql("CREATE TABLE test_table (c1 int);");
sql("CREATE VIEW test_view AS SELECT * FROM test_table;");
ASSERT_NE(nullptr, getCatalog().getMetadataForTable("test_foreign_table", false));
ASSERT_NE(nullptr, getCatalog().getMetadataForTable("test_table", false));
ASSERT_NE(nullptr, getCatalog().getMetadataForTable("test_view", false));
g_enable_fsi = false;
resetCatalog();
loginAdmin();
ASSERT_NE(nullptr, getCatalog().getMetadataForTable("test_foreign_table", false));
ASSERT_NE(nullptr, getCatalog().getMetadataForTable("test_table", false));
ASSERT_NE(nullptr, getCatalog().getMetadataForTable("test_view", false));
}
class DefaultForeignServersTest : public FsiSchemaTest {};
TEST_F(DefaultForeignServersTest, DefaultServersAreCreatedWhenFsiIsEnabled) {
g_enable_fsi = true;
auto catalog = initCatalog();
g_enable_fsi = false;
assertExpectedDefaultServer(catalog.get(),
"omnisci_local_csv",
foreign_storage::DataWrapperType::CSV,
OMNISCI_ROOT_USER_ID);
assertExpectedDefaultServer(catalog.get(),
"omnisci_local_parquet",
foreign_storage::DataWrapperType::PARQUET,
OMNISCI_ROOT_USER_ID);
}
int main(int argc, char** argv) {
TestHelpers::init_logger_stderr_only(argc, argv);
testing::InitGoogleTest(&argc, argv);
int err{0};
try {
err = RUN_ALL_TESTS();
} catch (const std::exception& e) {
LOG(ERROR) << e.what();
}
return err;
}