28 #include <sys/types.h> 32 #include <libfossscheduler.h> 33 #include <libfossdb.h> 34 #include "libfodbreposysconf.h" 38 #define TESTDBDIR "../../../testing/db/" 42 static char* Sysconf = NULL;
43 static char DBName[ARRAY_LENGTH];
44 static char DBConf[ARRAY_LENGTH];
45 static char RepoDir[ARRAY_LENGTH];
46 static char confFile[ARRAY_LENGTH];
48 fo_dbManager* createTestEnvironment(
const char* srcDirs,
const char* doConnectAsAgent,
int initDbTables) {
49 GString* gString = g_string_new(TESTDBDIR
"/createTestEnvironment.php");
51 g_string_append_printf(gString,
" -d '%s'", srcDirs);
54 g_string_append_printf(gString,
" -f");
56 gchar* cmd = g_string_free(gString, FALSE);
58 FILE* pipe = popen(cmd,
"r");
61 printf(
"cannot run create test environment script: %s\n", cmd);
65 Sysconf = calloc(1, ARRAY_LENGTH + 1);
66 size_t count = fread(Sysconf, 1, ARRAY_LENGTH, pipe);
68 int rv = fclose(pipe);
70 if (rv != 0 || count == 0) {
71 printf(
"command %s failed with output:\n%s\n", cmd, Sysconf);
78 if (doConnectAsAgent) {
79 char* argv[] = {(
char*) doConnectAsAgent,
"-c", Sysconf};
84 char buffer[ARRAY_LENGTH + 1];
85 snprintf(buffer, ARRAY_LENGTH,
"%s/Db.conf", Sysconf);
86 char* errorMsg = NULL;
90 result = fo_dbManager_new_withConf(conn, buffer);
92 printf(
"error connecting: %s\n", errorMsg);
104 void dropTestEnvironment(
fo_dbManager*
dbManager,
const char* srcDir,
const char* doConnectAsAgent) {
106 fo_dbManager_finish(dbManager);
108 if (doConnectAsAgent) {
113 char buffer[ARRAY_LENGTH];
114 snprintf(buffer, ARRAY_LENGTH, TESTDBDIR
"/purgeTestEnvironment.php -d '%s' -c '%s'", srcDir, Sysconf);
115 FILE* pipe = popen(buffer,
"r");
118 printf(
"cannot run purge test environment script: %s\n", buffer);
134 char tmp[ARRAY_LENGTH];
138 stream = popen(command,
"r");
139 if (!stream) status = 1;
140 memset(tmp,
'\0',
sizeof(tmp));
141 if (fgets(tmp, ARRAY_LENGTH, stream) != NULL) {
142 while ((tmp[i] !=
'\n') && (tmp[i] !=
' ') && (tmp[i] != EOF))
145 memcpy(Sysconf, tmp, i);
148 int rc = pclose(stream);
149 if (rc != 0) status = 1;
151 printf(
"Failed to run %s, exit code is:%d .\n", command, rc >> 8);
169 sysconfdir = getenv (
"SYSCONFDIR");
170 if (sysconfdir == NULL)
172 printf (
"The SYSCONFDIR enviroment variable is not existed.\n");
177 const char INIT_CMD[] =
"../../../testing/db/createTestDB.php";
180 CMD = (
char *)malloc(strlen(INIT_CMD) + 1);
185 sprintf(CMD,
"%s", INIT_CMD);
189 tmp = (
char *)malloc(strlen(CMD) + 4 + strlen(sysconfdir) + 1);
195 sprintf(tmp,
"%s -c %s", CMD, sysconfdir);
203 tmp = (
char *)malloc(strlen(CMD) + 4);
209 sprintf(tmp,
"%s -e", CMD);
222 char* argv[] = {agent_name,
"-c", Sysconf};
228 printf(
"create_db_repo_sysconf sucessfully\n");
239 char CMD[ARRAY_LENGTH];
240 memset(CMD,
'\0',
sizeof(CMD));
241 sprintf(CMD,
"../../../testing/db/createTestDB.php -d %s", DBName);
244 printf(
"remove DBName is:%s\n", DBName);
250 printf(
"drop_db_repo_sysconf sucessfully\n");
260 char* TestName = strstr(Sysconf,
"Conf") + 4;
262 printf(
"TestName is:%s\n", TestName);
273 memset(DBName,
'\0',
sizeof(DBName));
275 sprintf(DBName,
"fosstest%s", TestName);
277 printf(
"DBName is:%s\n", DBName);
289 printf(
"Sysconf is:%s\n", Sysconf);
300 memset(DBConf,
'\0',
sizeof(DBConf));
301 sprintf(DBConf,
"%s/Db.conf", Sysconf);
305 char* get_confFile() {
306 memset(confFile,
'\0',
sizeof(confFile));
307 sprintf(confFile,
"%s/fossology.conf", Sysconf);
316 strncpy(RepoDir, Sysconf, ARRAY_LENGTH);
317 RepoDir[ARRAY_LENGTH-1] =
'\0';
319 char* test_name_tmp = strstr(RepoDir,
"testDbConf");
322 *test_name_tmp =
'\0';
325 char *tmp = malloc(strlen(RepoDir) + 1);
330 sprintf(tmp,
"%s", RepoDir);
336 printf(
"RepoDir is:%s\n", RepoDir);
351 struct stat st = {0};
356 char *confDir = malloc((strlen(cwd) + 10) *
sizeof(
char));
360 if(cwd == NULL || agentName == NULL || cwd[0] ==
'\0' || agentName[0] ==
'\0')
365 sprintf(confDir,
"%s/testconf", cwd);
366 sprintf(confFile,
"%s/fossology.conf", confDir);
367 sprintf(agentDir,
"%s/../..", cwd);
369 if (stat(confDir, &st) == -1)
371 mkdir(confDir, 0775);
374 memset(CMD,
'\0',
sizeof(CMD));
375 sprintf(CMD,
"%s/mods-enabled/%s", confDir, agentName);
376 if (stat(CMD, &st) == -1)
381 testConfFile = fopen(confFile,
"w");
382 fprintf(testConfFile,
";fossology.conf for testing\n");
383 fprintf(testConfFile,
"[FOSSOLOGY]\nport = 24693\n");
384 fprintf(testConfFile,
"address = localhost\n");
385 fprintf(testConfFile,
"depth = 0\n");
386 fprintf(testConfFile,
"path = %s\n", confDir);
387 fprintf(testConfFile,
"[HOSTS]\n");
388 fprintf(testConfFile,
"localhost = localhost AGENT_DIR 10\n");
389 fprintf(testConfFile,
"[REPOSITORY]\n");
390 fprintf(testConfFile,
"localhost = * 00 ff\n");
391 fprintf(testConfFile,
"[DIRECTORIES]\n");
392 fprintf(testConfFile,
"PROJECTUSER=fossy\n");
393 fprintf(testConfFile,
"PROJECTGROUP=fossy\n");
394 fprintf(testConfFile,
"MODDIR=%s/../../../..\n", cwd);
395 fprintf(testConfFile,
"LOGDIR=%s\n", confDir);
396 fclose(testConfFile);
398 memset(CMD,
'\0',
sizeof(CMD));
399 sprintf(CMD,
"install -D %s/../../../../VERSION %s/VERSION", cwd, confDir);
402 memset(CMD,
'\0',
sizeof(CMD));
403 sprintf(CMD,
"install -D %s/../../../../install/defconf/Db.conf %s/Db.conf", cwd, confDir);
406 memset(CMD,
'\0',
sizeof(CMD));
407 sprintf(CMD,
"install -D %s/VERSION %s/mods-enabled/%s/VERSION", agentDir, confDir, agentName);
410 memset(CMD,
'\0',
sizeof(CMD));
411 sprintf(CMD,
"ln -fs %s/agent %s/mods-enabled/%s", agentDir, confDir, agentName);
static void command_output(char *command)
get command output
int create_db_repo_sysconf(int type, char *agent_name, char *sysconfdir)
char * get_test_name()
get the test name just created by create_db_repo_sysconf()
char * createTestConfDir(char *cwd, char *agentName)
create a dummy sysConfDir for a given agent
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
char * get_dbconf()
get Db.conf path just created by create_db_repo_sysconf()
void drop_db_repo_sysconf(char *DBName)
drop db, sysconfig dir and repo
cmdlist CMD[]
Global command table.
char buffer[2048]
The last thing received from the scheduler.
fo_dbManager * dbManager
fo_dbManager object
char * get_db_name()
get the DB name just created by create_db_repo_sysconf()
void fo_scheduler_connect_dbMan(int *argc, char **argv, fo_dbManager **dbManager)
Make a connection from an agent to the scheduler and create a DB manager as well. ...
char * get_sysconfdir()
get sysconfig dir path just created by create_db_repo_sysconf()
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
char * get_repodir()
get repo path just created by create_db_repo_sysconf()