Skip to content

Commit 7a5355d

Browse files
SNOW-1437443: use random db to allow test in parallel (#722)
Co-authored-by: sfc-gh-ext-simba-hx <[email protected]>
1 parent 76d0180 commit 7a5355d

File tree

4 files changed

+157
-13
lines changed

4 files changed

+157
-13
lines changed

tests/test_parallel_upload_download.cpp

+30-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <aws/core/Aws.h>
66
#include <snowflake/client.h>
77
#include <fstream>
8+
#include <stdlib.h>
9+
#include <time.h>
810
#include "utils/test_setup.h"
911
#include "utils/TestSetup.hpp"
1012
#include "snowflake/IStatementPutGet.hpp"
@@ -66,12 +68,15 @@ void test_parallel_upload_download_core(int fileNumber, int numberOfRows = 1)
6668
assert_int_equal(SF_STATUS_SUCCESS, ret);
6769

6870
std::string dataDir = TestSetup::getDataDir();
69-
std::string testDir = dataDir + "test_parallel_upload";
70-
testDir += PATH_SEP;
71+
time_t t;
72+
srand((unsigned) time(&t));
73+
std::string randStr = std::string("_") + std::to_string(rand());
74+
std::string uploadDir = dataDir + "test_parallel_upload" + randStr;
75+
uploadDir += PATH_SEP;
7176

72-
populateDataInTestDir(testDir, fileNumber, numberOfRows);
77+
populateDataInTestDir(uploadDir, fileNumber, numberOfRows);
7378

74-
std::string files = testDir + "*";
79+
std::string files = uploadDir + "*";
7580
std::string putCommand = "put file://" + files + " @%test_parallel_upload_download auto_compress=false";
7681

7782
std::unique_ptr<IStatementPutGet> stmtPutGet = std::unique_ptr
@@ -101,7 +106,8 @@ void test_parallel_upload_download_core(int fileNumber, int numberOfRows = 1)
101106
}
102107
assert_int_equal(status, SF_STATUS_EOF);
103108

104-
std::string getCommand = "get @%test_parallel_upload_download file://" + dataDir + "test_parallel_download";
109+
std::string downloadDir = dataDir + "test_parallel_download" + randStr;
110+
std::string getCommand = "get @%test_parallel_upload_download file://" + downloadDir;
105111
result = agent.execute(&getCommand);
106112
assert_int_equal(fileNumber, result->getResultSize());
107113

@@ -113,9 +119,9 @@ void test_parallel_upload_download_core(int fileNumber, int numberOfRows = 1)
113119
}
114120

115121
#ifdef _WIN32
116-
std::string compCmd = "FC " + dataDir + "test_parallel_upload\\* " + dataDir + "test_parallel_download\\*";
122+
std::string compCmd = "FC " + uploadDir + "* " + downloadDir + "\\*";
117123
#else
118-
std::string compCmd = "diff " + dataDir + "test_parallel_upload/ " + dataDir + "test_parallel_download/";
124+
std::string compCmd = "diff " + uploadDir + " " + downloadDir + "/";
119125
#endif
120126
int res = system(compCmd.c_str());
121127
assert_int_equal(0, res);
@@ -124,6 +130,9 @@ void test_parallel_upload_download_core(int fileNumber, int numberOfRows = 1)
124130

125131
/* close and term */
126132
snowflake_term(sf); // purge snowflake context
133+
134+
sf_delete_directory_if_exists(uploadDir.c_str());
135+
sf_delete_directory_if_exists(downloadDir.c_str());
127136
}
128137

129138
static int teardown(void **unused)
@@ -141,12 +150,6 @@ static int teardown(void **unused)
141150
snowflake_stmt_term(sfstmt);
142151
snowflake_term(sf);
143152

144-
std::string dataDir = TestSetup::getDataDir();
145-
std::string uploadDir = dataDir + "test_parallel_upload";
146-
std::string downloadDir = dataDir + "test_parallel_download";
147-
148-
sf_delete_directory_if_exists(uploadDir.c_str());
149-
sf_delete_directory_if_exists(downloadDir.c_str());
150153
return 0;
151154
}
152155

@@ -162,17 +165,31 @@ void test_large_file_multipart_upload(void **unused)
162165

163166
void test_large_file_concurrent_upload_download(void **unused)
164167
{
168+
// Jenkins node on Mac has issue with large file.
169+
#ifdef __APPLE__
170+
char* jobname = getenv("JOB_NAME");
171+
if (jobname && (strlen(jobname) > 0))
172+
{
173+
return;
174+
}
175+
#endif
176+
165177
test_parallel_upload_download_core(10, 200000);
166178
}
167179

168180
static int gr_setup(void **unused)
169181
{
170182
initialize_test(SF_BOOLEAN_FALSE);
183+
if(!setup_random_database()) {
184+
std::cout << "Failed to setup random database, fallback to use regular one." << std::endl;
185+
}
186+
171187
return 0;
172188
}
173189

174190
static int gr_teardown(void **unused)
175191
{
192+
drop_random_database();
176193
snowflake_global_term();
177194
return 0;
178195
}

tests/test_simple_put.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,10 @@ static int gr_setup(void **unused)
856856
{
857857
initialize_test(SF_BOOLEAN_FALSE);
858858

859+
if(!setup_random_database()) {
860+
std::cout << "Failed to setup random database, fallback to use regular one." << std::endl;
861+
}
862+
859863
// create large 2GB file
860864
char *githubenv = getenv("GITHUB_ACTIONS");
861865
if (githubenv && strlen(githubenv) > 0)
@@ -865,6 +869,14 @@ static int gr_setup(void **unused)
865869
return 0;
866870
}
867871
}
872+
// Jenkins node on Mac has issue with large file.
873+
#ifdef __APPLE__
874+
char* jobname = getenv("JOB_NAME");
875+
if (jobname && (strlen(jobname) > 0))
876+
{
877+
return 0;
878+
}
879+
#endif
868880

869881
std::string file2GB = TestSetup::getDataDir() + FILE_NAME_2GB;
870882
std::ofstream ofs(file2GB, std::ios::binary | std::ios::out);
@@ -877,6 +889,7 @@ static int gr_setup(void **unused)
877889

878890
static int gr_teardown(void **unused)
879891
{
892+
drop_random_database();
880893
snowflake_global_term();
881894
std::string file2GB = TestSetup::getDataDir() + FILE_NAME_2GB;
882895
remove(file2GB.c_str());
@@ -1225,6 +1238,15 @@ void test_2GBlarge_put(void **unused)
12251238
return;
12261239
}
12271240

1241+
// Jenkins node on Mac has issue with large file.
1242+
#ifdef __APPLE__
1243+
char* jobname = getenv("JOB_NAME");
1244+
if (jobname && (strlen(jobname) > 0))
1245+
{
1246+
return;
1247+
}
1248+
#endif
1249+
12281250
test_simple_put_core(FILE_NAME_2GB, // filename
12291251
"none", //source compression
12301252
false, // auto compress
@@ -1255,6 +1277,15 @@ void test_2GBlarge_get(void **unused)
12551277
return;
12561278
}
12571279

1280+
// Jenkins node on Mac has issue with large file.
1281+
#ifdef __APPLE__
1282+
char* jobname = getenv("JOB_NAME");
1283+
if (jobname && (strlen(jobname) > 0))
1284+
{
1285+
return;
1286+
}
1287+
#endif
1288+
12581289
std::string getcmd = std::string("get @%test_small_put/") + FILE_NAME_2GB +
12591290
" file://" + TestSetup::getDataDir();
12601291
test_simple_get_data(getcmd.c_str(), std::to_string(FILE_SIZE_2GB).c_str());

tests/utils/test_setup.c

+91
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
#include <snowflake/logger.h>
66
#include <string.h>
77
#include "test_setup.h"
8+
#include <stdlib.h>
9+
#include <time.h>
810

911
// Long path space
1012
char PERFORMANCE_TEST_RESULTS_PATH[5000];
1113

14+
// random database namespace
15+
char RANDOM_DATABASE_NAME[100];
16+
1217
void initialize_test(sf_bool debug) {
1318
// default location and the maximum logging
1419
snowflake_global_init(NULL, SF_LOG_TRACE, NULL);
@@ -127,3 +132,89 @@ void process_results(struct timespec begin, struct timespec end, int num_iterati
127132
//printf("%s, %lf, %i\n", label, time_elapsed, num_iterations);
128133
fclose(results_file);
129134
}
135+
136+
int setup_random_database()
137+
{
138+
SF_STATUS status;
139+
SF_CONNECT *sf;
140+
SF_STMT *sfstmt;
141+
char random_db_name[100];
142+
char query[200];
143+
time_t t;
144+
145+
if (strlen(RANDOM_DATABASE_NAME) > 0)
146+
{
147+
// already setup
148+
return 1;
149+
}
150+
151+
srand((unsigned) time(&t));
152+
sprintf(random_db_name, "random_test_db_%d", rand());
153+
154+
sf = setup_snowflake_connection();
155+
status = snowflake_connect(sf);
156+
if (status != SF_STATUS_SUCCESS) {
157+
return 0;
158+
}
159+
sfstmt = snowflake_stmt(sf);
160+
161+
sprintf(query, "create or replace database %s", random_db_name);
162+
status = snowflake_query(
163+
sfstmt,
164+
query,
165+
0);
166+
if (status != SF_STATUS_SUCCESS) {
167+
// ingore in case the test account doesn't have privilege to create database
168+
snowflake_stmt_term(sfstmt);
169+
snowflake_term(sf);
170+
return 0;
171+
}
172+
173+
sprintf(query, "create or replace schema %s", getenv("SNOWFLAKE_TEST_SCHEMA"));
174+
status = snowflake_query(
175+
sfstmt,
176+
query,
177+
0);
178+
if (status != SF_STATUS_SUCCESS) {
179+
// ingore in case the test account doesn't have privilege to create database
180+
snowflake_stmt_term(sfstmt);
181+
snowflake_term(sf);
182+
return 0;
183+
}
184+
185+
sf_setenv("SNOWFLAKE_TEST_DATABASE", random_db_name);
186+
strcpy(RANDOM_DATABASE_NAME, random_db_name);
187+
snowflake_stmt_term(sfstmt);
188+
snowflake_term(sf);
189+
return 1;
190+
}
191+
192+
void drop_random_database()
193+
{
194+
SF_STATUS status;
195+
SF_CONNECT *sf;
196+
SF_STMT *sfstmt;
197+
char query[200];
198+
199+
if (strlen(RANDOM_DATABASE_NAME) == 0)
200+
{
201+
// not setup
202+
return;
203+
}
204+
205+
sf = setup_snowflake_connection();
206+
status = snowflake_connect(sf);
207+
if (status != SF_STATUS_SUCCESS) {
208+
dump_error(&(sf->error));
209+
}
210+
assert_int_equal(status, SF_STATUS_SUCCESS);
211+
sfstmt = snowflake_stmt(sf);
212+
213+
sprintf(query, "drop database %s", RANDOM_DATABASE_NAME);
214+
status = snowflake_query(
215+
sfstmt,
216+
query,
217+
0);
218+
snowflake_stmt_term(sfstmt);
219+
snowflake_term(sf);
220+
}

tests/utils/test_setup.h

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void setup_and_run_query(SF_CONNECT **sfp, SF_STMT **sfstmtp, const char *query)
4242

4343
void process_results(struct timespec begin, struct timespec end, int num_iterations, const char *label);
4444

45+
// Setup database with random name for test, return 1 if succeeded, 0 otherwise.
46+
int setup_random_database();
47+
48+
void drop_random_database();
49+
4550
/**
4651
* Dump error
4752
* @param error SF_ERROR_STRUCT

0 commit comments

Comments
 (0)