FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testRun.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
3 
4 Copyright (C) 2018, Siemens AG
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *********************************************************************/
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <unistd.h>
24 #include "CUnit/CUnit.h"
25 #include "CUnit/Automated.h"
26 #include "testRun.h"
27 
33 char *DBConfFile = NULL;
34 
35 extern CU_SuiteInfo suites[];
36 
41 char* getUser()
42 {
43  char CMD[200], *user;
44  FILE *db_conf;
45  int len;
46  memset(CMD, '\0', sizeof(CMD));
47  user = malloc(20 * sizeof(char));
48  memset(user, '\0', 20);
49 
50  sprintf(CMD, "awk -F \"=\" '/user/ {print $2}' %s | tr -d '; '", DBConfFile);
51  db_conf = popen(CMD, "r");
52  if (db_conf != NULL)
53  {
54  if(fgets(user, sizeof(user)-1, db_conf) != NULL)
55  {
56  len = strlen(user);
57  user[len-1] = '\0';
58  }
59  }
60  pclose(db_conf);
61  return user;
62 }
63 
69 {
70  char CMD[256];
71  int rc;
72 
73  char cwd[2048];
74  char* confDir = NULL;
75  char* user = NULL;
76  char* db_name = NULL;
77 
78  if(getcwd(cwd, sizeof(cwd)) != NULL)
79  {
80  confDir = createTestConfDir(cwd, "delagent");
81  }
82 
83  rc = create_db_repo_sysconf(0, "delagent", confDir);
84  if (rc != 0)
85  {
86  printf("Database initialize ERROR!\n");
87  DelagentClean();
88  return -1;
89  }
91  user = getUser();
92  db_name = get_db_name();
93 
94  memset(CMD, '\0', sizeof(CMD));
95  sprintf(CMD, "gunzip -c ../testdata/testdb_all.gz | psql -U %s -d %s >/dev/null", user, db_name);
96  rc = system(CMD);
97  if (user != NULL)
98  {
99  free(user);
100  }
101  if (WEXITSTATUS(rc) != 0)
102  {
103  printf("Database initialize ERROR!\n");
104  DelagentClean();
105  return -1;
106  }
107 
108  return 0;
109 }
114 {
116  return 0;
117 }
118 
124 {
125  char CMD[256];
126  int rc;
127 
128  if (DelagentDBInit()!=0) return -1;
129 
130  memset(CMD, '\0', sizeof(CMD));
131  sprintf(CMD, "sh testInitRepo.sh %s", get_repodir());
132  rc = system(CMD);
133  if (rc != 0)
134  {
135  printf("Repository Init ERROR!\n");
136  DelagentClean();
137  return -1;
138  }
139 
140  return 0;
141 }
142 
146 int main( int argc, char *argv[] )
147 {
148  return focunit_main(argc, argv, "delagent_Tests", suites);
149 }
150 
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
int DelagentDBInit()
initialize db
Definition: testRun.c:68
int create_db_repo_sysconf(int type, char *agent_name, char *sysconfdir)
char * createTestConfDir(char *cwd, char *agentName)
create a dummy sysConfDir for a given agent
CU_SuiteInfo suites[]
all test suites for delagent
Definition: testRun.h:54
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
int main(int argc, char *argv[])
main test function
Definition: testRun.c:146
cmdlist CMD[]
Global command table.
int DelagentInit()
init db and repo
Definition: testRun.c:123
int DelagentClean()
clean db
Definition: testRun.c:113
char * get_db_name()
get the DB name just created by create_db_repo_sysconf()
char * getUser()
Helper function to get db owner.
Definition: testRun.c:41
char * get_repodir()
get repo path just created by create_db_repo_sysconf()