17 #include <sys/types.h>
21 #include <libfossscheduler.h>
22 #include <libfossdb.h>
23 #include "libfodbreposysconf.h"
27 #define TESTDBDIR "../../../testing/db/"
31 static char* Sysconf = NULL;
32 static char DBName[ARRAY_LENGTH];
33 static char DBConf[ARRAY_LENGTH];
34 static char RepoDir[ARRAY_LENGTH];
35 static char confFile[ARRAY_LENGTH];
37 fo_dbManager* createTestEnvironment(
const char* srcDirs,
const char* doConnectAsAgent,
int initDbTables) {
38 GString* gString = g_string_new(TESTDBDIR
"/createTestEnvironment.php");
40 g_string_append_printf(gString,
" -d '%s'", srcDirs);
43 g_string_append_printf(gString,
" -f");
45 gchar* cmd = g_string_free(gString, FALSE);
47 FILE* pipe = popen(cmd,
"r");
50 printf(
"cannot run create test environment script: %s\n", cmd);
54 Sysconf = calloc(1, ARRAY_LENGTH + 1);
55 size_t count = fread(Sysconf, 1, ARRAY_LENGTH, pipe);
57 int rv = pclose(pipe);
59 if (rv != 0 || count == 0) {
60 printf(
"command %s failed with output:\n%s\n", cmd, Sysconf);
67 if (doConnectAsAgent) {
68 char* argv[] = {(
char*) doConnectAsAgent,
"-c", Sysconf};
73 char buffer[ARRAY_LENGTH + 1];
74 snprintf(
buffer, ARRAY_LENGTH,
"%s/Db.conf", Sysconf);
75 char* errorMsg = NULL;
79 result = fo_dbManager_new_withConf(conn,
buffer);
81 printf(
"error connecting: %s\n", errorMsg);
97 if (doConnectAsAgent) {
102 char buffer[ARRAY_LENGTH];
103 snprintf(
buffer, ARRAY_LENGTH, TESTDBDIR
"/purgeTestEnvironment.php -d '%s' -c '%s'", srcDir, Sysconf);
104 FILE* pipe = popen(
buffer,
"r");
107 printf(
"cannot run purge test environment script: %s\n",
buffer);
123 char tmp[ARRAY_LENGTH];
127 stream = popen(command,
"r");
128 if (!stream) status = 1;
129 memset(tmp,
'\0',
sizeof(tmp));
130 if (fgets(tmp, ARRAY_LENGTH, stream) != NULL) {
131 while ((tmp[i] !=
'\n') && (tmp[i] !=
' ') && (tmp[i] != EOF))
134 memcpy(Sysconf, tmp, i);
137 int rc = pclose(stream);
138 if (rc != 0) status = 1;
140 printf(
"Failed to run %s, exit code is:%d .\n", command, rc >> 8);
158 sysconfdir = getenv (
"SYSCONFDIR");
159 if (sysconfdir == NULL)
161 printf (
"The SYSCONFDIR enviroment variable is not existed.\n");
165 #define CREATEDB TESTDBDIR "/createTestDB.php"
166 const char INIT_CMD[] = CREATEDB;
169 CMD = (
char *)malloc(strlen(INIT_CMD) + 1);
174 sprintf(
CMD,
"%s", INIT_CMD);
178 tmp = (
char *)malloc(strlen(
CMD) + 4 + strlen(sysconfdir) + 1);
184 sprintf(tmp,
"%s -c %s",
CMD, sysconfdir);
192 tmp = (
char *)malloc(strlen(
CMD) + 4);
198 sprintf(tmp,
"%s -e",
CMD);
211 char* argv[] = {agent_name,
"-c", Sysconf};
217 printf(
"create_db_repo_sysconf sucessfully\n");
228 char CMD[ARRAY_LENGTH];
229 memset(
CMD,
'\0',
sizeof(
CMD));
230 sprintf(
CMD,
"%s/createTestDB.php -d %s", TESTDBDIR, DBName);
233 printf(
"remove DBName is:%s\n", DBName);
239 printf(
"drop_db_repo_sysconf sucessfully\n");
249 char* TestName = strstr(Sysconf,
"Conf") + 4;
251 printf(
"TestName is:%s\n", TestName);
262 memset(DBName,
'\0',
sizeof(DBName));
264 sprintf(DBName,
"fosstest%s", TestName);
266 printf(
"DBName is:%s\n", DBName);
278 printf(
"Sysconf is:%s\n", Sysconf);
289 memset(DBConf,
'\0',
sizeof(DBConf));
290 sprintf(DBConf,
"%s/Db.conf", Sysconf);
294 char* get_confFile() {
295 memset(confFile,
'\0',
sizeof(confFile));
296 sprintf(confFile,
"%s/fossology.conf", Sysconf);
305 strncpy(RepoDir, Sysconf, ARRAY_LENGTH);
306 RepoDir[ARRAY_LENGTH-1] =
'\0';
308 char* test_name_tmp = strstr(RepoDir,
"testDbConf");
311 *test_name_tmp =
'\0';
314 char *tmp = malloc(strlen(RepoDir) + 1);
319 sprintf(tmp,
"%s", RepoDir);
325 printf(
"RepoDir is:%s\n", RepoDir);
340 struct stat st = {0};
345 char *confDir = malloc((strlen(cwd) + 10) *
sizeof(
char));
349 if(cwd == NULL || agentName == NULL || cwd[0] ==
'\0' || agentName[0] ==
'\0')
354 sprintf(confDir,
"%s/testconf", cwd);
355 sprintf(confFile,
"%s/fossology.conf", confDir);
356 sprintf(agentDir,
"%s/..", cwd);
358 if (stat(confDir, &st) == -1)
360 mkdir(confDir, 0775);
363 memset(
CMD,
'\0',
sizeof(
CMD));
364 sprintf(
CMD,
"%s/mods-enabled/%s", confDir, agentName);
365 if (stat(
CMD, &st) == -1)
370 testConfFile = fopen(confFile,
"w");
371 fprintf(testConfFile,
";fossology.conf for testing\n");
372 fprintf(testConfFile,
"[FOSSOLOGY]\nport = 24693\n");
373 fprintf(testConfFile,
"address = localhost\n");
374 fprintf(testConfFile,
"depth = 0\n");
375 fprintf(testConfFile,
"path = %s\n", confDir);
376 fprintf(testConfFile,
"[HOSTS]\n");
377 fprintf(testConfFile,
"localhost = localhost AGENT_DIR 10\n");
378 fprintf(testConfFile,
"[REPOSITORY]\n");
379 fprintf(testConfFile,
"localhost = * 00 ff\n");
380 fprintf(testConfFile,
"[DIRECTORIES]\n");
381 fprintf(testConfFile,
"PROJECTUSER=fossy\n");
382 fprintf(testConfFile,
"PROJECTGROUP=fossy\n");
383 fprintf(testConfFile,
"MODDIR=%s/../../../..\n", cwd);
384 fprintf(testConfFile,
"LOGDIR=%s\n", confDir);
385 fclose(testConfFile);
387 memset(
CMD,
'\0',
sizeof(
CMD));
388 sprintf(
CMD,
"install -D %s/../VERSION %s/VERSION", cwd, confDir);
391 memset(
CMD,
'\0',
sizeof(
CMD));
392 sprintf(
CMD,
"install -D %s/../../../install/gen/Db.conf %s/Db.conf", cwd, confDir);
395 memset(
CMD,
'\0',
sizeof(
CMD));
396 sprintf(
CMD,
"install -D %s/VERSION %s/mods-enabled/%s/VERSION", agentDir, confDir, agentName);
399 memset(
CMD,
'\0',
sizeof(
CMD));
400 sprintf(
CMD,
"ln -fs %s/agent %s/mods-enabled/%s", agentDir, confDir, agentName);
cmdlist CMD[]
Global command table.
char * get_test_name()
get the test name just created by create_db_repo_sysconf()
char * get_repodir()
get repo path just created by create_db_repo_sysconf()
static void command_output(char *command)
get command output
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
char * get_db_name()
get the DB name just created by create_db_repo_sysconf()
char * createTestConfDir(char *cwd, char *agentName)
create a dummy sysConfDir for a given agent
int create_db_repo_sysconf(int type, char *agent_name, char *sysconfdir)
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.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
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.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
char buffer[2048]
The last thing received from the scheduler.
fo_dbManager * dbManager
fo_dbManager object