FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testRun.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
3  SPDX-FileCopyrightText: © 2018 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <unistd.h>
12 #include "CUnit/CUnit.h"
13 #include "CUnit/Automated.h"
14 #include "testRun.h"
15 
21 char *DBConfFile = NULL;
22 
23 extern CU_SuiteInfo suites[];
24 
29 char* getUser()
30 {
31  char CMD[200], *user;
32  FILE *db_conf;
33  int len;
34  memset(CMD, '\0', sizeof(CMD));
35  user = malloc(20 * sizeof(char));
36  memset(user, '\0', 20);
37 
38  sprintf(CMD, "awk -F \"=\" '/user/ {print $2}' %s | tr -d '; '", DBConfFile);
39  db_conf = popen(CMD, "r");
40  if (db_conf != NULL)
41  {
42  if(fgets(user, sizeof(user)-1, db_conf) != NULL)
43  {
44  len = strlen(user);
45  user[len-1] = '\0';
46  }
47  }
48  pclose(db_conf);
49  return user;
50 }
51 
57 {
58  char CMD[256];
59  int rc;
60 
61  char cwd[2048];
62  char* confDir = NULL;
63  char* user = NULL;
64  char* db_name = NULL;
65 
66  if(getcwd(cwd, sizeof(cwd)) != NULL)
67  {
68  confDir = createTestConfDir(cwd, "delagent");
69  }
70 
71  rc = create_db_repo_sysconf(0, "delagent", confDir);
72  if (rc != 0)
73  {
74  printf("Database initialize ERROR!\n");
75  DelagentClean();
76  return -1;
77  }
79  user = getUser();
80  db_name = get_db_name();
81 
82  memset(CMD, '\0', sizeof(CMD));
83  sprintf(CMD, "gunzip -c ../testdata/testdb_all.gz | psql -U %s -d %s >/dev/null", user, db_name);
84  rc = system(CMD);
85  if (user != NULL)
86  {
87  free(user);
88  }
89  if (WEXITSTATUS(rc) != 0)
90  {
91  printf("Database initialize ERROR!\n");
92  DelagentClean();
93  return -1;
94  }
95 
96  return 0;
97 }
102 {
104  return 0;
105 }
106 
112 {
113  char CMD[256];
114  int rc;
115 
116  if (DelagentDBInit()!=0) return -1;
117 
118  memset(CMD, '\0', sizeof(CMD));
119  sprintf(CMD, "sh testInitRepo.sh %s", get_repodir());
120  rc = system(CMD);
121  if (rc != 0)
122  {
123  printf("Repository Init ERROR!\n");
124  DelagentClean();
125  return -1;
126  }
127 
128  return 0;
129 }
130 
134 int main( int argc, char *argv[] )
135 {
136  return focunit_main(argc, argv, "delagent_Tests", suites);
137 }
138 
int main(int argc, char *argv[])
main test function
Definition: testRun.c:134
char * getUser()
Helper function to get db owner.
Definition: testRun.c:29
int DelagentDBInit()
initialize db
Definition: testRun.c:56
int DelagentClean()
clean db
Definition: testRun.c:101
int DelagentInit()
init db and repo
Definition: testRun.c:111
CU_SuiteInfo suites[]
all test suites for delagent
Definition: testRun.h:36
cmdlist CMD[]
Global command table.
char * get_repodir()
get repo path just created by create_db_repo_sysconf()
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 * DBConfFile
DB conf file location.
Definition: testRun.c:21