FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testDatabase.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *********************************************************************/
22 /* include functions to test */
23 #include <testRun.h>
24 
25 /* scheduler includes */
26 #include <database.h>
27 #include <scheduler.h>
28 
29 /* library includes */
30 #include <utils.h>
31 
32 /* testing sql statements */
33 char sqltmp[1024] = {0};
34 extern char* check_scheduler_tables;
35 extern char* jobsql_processed;
36 
37 /* ************************************************************************** */
38 /* **** database function tests ******************************************** */
39 /* ************************************************************************** */
40 
49 {
50  scheduler_t* scheduler;
51  PGresult* db_result;
52  GString* sql;
53 
54  scheduler = scheduler_init(testdb, NULL);
55  database_init(scheduler);
56 
57  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
58 
59  sprintf(sqltmp, check_scheduler_tables, PQdb(scheduler->db_conn));
60  sql = g_string_new(sqltmp);
61  g_string_append(sql, "'users';");
62 
63  /* get the url for the fossology instance */
64  db_result = database_exec(scheduler, sql->str);
65  //printf("sql: %s\n", sql->str);
66  // TODO skip this test since the order reported here is random, also it will crash if PQntuples < 5
67  #if 0
68  if(PQresultStatus(db_result) == PGRES_TUPLES_OK && PQntuples(db_result) != 0)
69  {
70  //printf("result: %s\n", g_strdup(PQgetvalue(db_result, 0, 0)));
71  FO_ASSERT_STRING_EQUAL(g_strdup(PQgetvalue(db_result, 0, 0)), "user_pk");
72  FO_ASSERT_STRING_EQUAL(g_strdup(PQgetvalue(db_result, 1, 0)), "user_name");
73  FO_ASSERT_STRING_EQUAL(g_strdup(PQgetvalue(db_result, 2, 0)), "root_folder_fk");
74  FO_ASSERT_STRING_EQUAL(g_strdup(PQgetvalue(db_result, 3, 0)), "user_desc");
75  FO_ASSERT_STRING_EQUAL(g_strdup(PQgetvalue(db_result, 4, 0)), "user_seed");
76  }
77  #endif
78  PQclear(db_result);
79  g_string_free(sql, TRUE);
80  scheduler_destroy(scheduler);
81 }
82 
89 {
90  scheduler_t* scheduler;
91  gchar* sql = NULL;
92 
93  scheduler = scheduler_init(testdb, NULL);
94 
95  FO_ASSERT_PTR_NULL(scheduler->db_conn);
96  database_init(scheduler);
97  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
98 
99  sql = g_strdup_printf(jobsql_processed, 0, 123);
100 
101  database_exec_event(scheduler, sql);
102  scheduler_destroy(scheduler);
103 }
104 
114 {
115  scheduler_t* scheduler;
116  char sql[512];
117  PGresult* db_result;
118 
119  scheduler = scheduler_init(testdb, NULL);
120 
121  FO_ASSERT_PTR_NULL(scheduler->db_conn);
122  database_init(scheduler);
123  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
124 
125  Prepare_Testing_Data(scheduler);
126 
127  database_update_event(scheduler, NULL);
128  sprintf(sql, "SELECT * FROM job;");
129  db_result = database_exec(scheduler, sql);
130  //printf("result: %s", PQget(db_result, 0, "job_name"));
131  if(PQresultStatus(db_result) == PGRES_TUPLES_OK && PQntuples(db_result) != 0)
132  {
133  FO_ASSERT_STRING_EQUAL(PQget(db_result, 0, "job_name"), "testing file");
134  FO_ASSERT_EQUAL(atoi(PQget(db_result, 0, "job_user_fk")), 1);
135  }
136  PQclear(db_result);
137 
138  database_reset_queue(scheduler);
139 
140  scheduler_destroy(scheduler);
141 }
142 
153 {
154  scheduler_t* scheduler;
155  job_t* job;
156  arg_int* params;
157  int jq_pk;
158  job_t tmp_job;
159 
160  scheduler = scheduler_init(testdb, NULL);
161 
162  FO_ASSERT_PTR_NULL(scheduler->db_conn);
163  database_init(scheduler);
164  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
165 
166  jq_pk = Prepare_Testing_Data(scheduler);
167 
168  params = g_new0(arg_int, 1);
169  params->second = jq_pk;
170  params->first = g_tree_lookup(scheduler->job_list, &params->second);
171  job = params->first;
172  if(params->first == NULL)
173  {
174  tmp_job.id = params->second;
175  tmp_job.status = JB_NOT_AVAILABLE;
176  tmp_job.running_agents = NULL;
177  tmp_job.message = NULL;
178 
179  job = &tmp_job;
180  }
181 
182  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_NOT_AVAILABLE");
183  database_update_job(scheduler, job, JB_PAUSED);
184  //job = g_tree_lookup(scheduler->job_list, &params->second);
185  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_NOT_AVAILABLE");
186 
187  g_free(params);
188  scheduler_destroy(scheduler);
189 }
190 
202 {
203  scheduler_t* scheduler;
204  job_t* job;
205  arg_int* params;
206  int jq_pk;
207  job_t tmp_job;
208 
209  scheduler = scheduler_init(testdb, NULL);
210 
211  FO_ASSERT_PTR_NULL(scheduler->db_conn);
212  database_init(scheduler);
213  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
214 
215  jq_pk = Prepare_Testing_Data(scheduler);
216 
217  params = g_new0(arg_int, 1);
218  params->second = jq_pk;
219  params->first = g_tree_lookup(scheduler->job_list, &params->second);
220  job = params->first;
221  if(params->first == NULL)
222  {
223  tmp_job.id = params->second;
224  tmp_job.status = JB_NOT_AVAILABLE;
225  tmp_job.running_agents = NULL;
226  tmp_job.message = NULL;
227 
228  job = &tmp_job;
229  }
230 
231  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_NOT_AVAILABLE");
232 
233  database_job_processed(jq_pk, 2);
234  database_job_log(jq_pk, "test log");
235  database_job_priority(scheduler, job, 1);
236 
237  g_free(params);
238  scheduler_destroy(scheduler);
239 }
240 
249 {
250  scheduler_t* scheduler;
251  job_t* job;
252  int jq_pk;
253 
254  scheduler = scheduler_init(testdb, NULL);
255 
256  FO_ASSERT_PTR_NULL(scheduler->db_conn);
257  database_init(scheduler);
258  email_init(scheduler);
259  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
260 
261  jq_pk = Prepare_Testing_Data(scheduler);
262  job = job_init(scheduler->job_list, scheduler->job_queue, "ununpack", "localhost", -1, 0, 0, 0, 0, NULL);
263  job->id = jq_pk;
264 
265  database_update_job(scheduler, job, JB_FAILED);
266  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_CHECKEDOUT");
267 
268  scheduler_destroy(scheduler);
269 }
270 /* ************************************************************************** */
271 /* **** suite declaration *************************************************** */
272 /* ************************************************************************** */
273 
274 CU_TestInfo tests_database[] =
275 {
276  {"Test database_init", test_database_init },
277  {"Test database_exec_event", test_database_exec_event },
278  {"Test database_update_event", test_database_update_event},
279  {"Test database_update_job", test_database_update_job },
280  {"Test database_job", test_database_job },
281  CU_TEST_INFO_NULL
282 };
283 
284 CU_TestInfo tests_email[] =
285 {
286  {"Test email_notify", test_email_notify },
287  CU_TEST_INFO_NULL
288 };
289 
290 
291 
292 
PGconn * db_conn
The database connection.
Definition: scheduler.h:187
int Prepare_Testing_Data(scheduler_t *scheduler)
Definition: utils.c:32
void database_reset_queue(scheduler_t *scheduler)
Resets any jobs in the job queue that are not completed.
Definition: database.c:942
void test_database_exec_event()
Test for database_exec_event()
Definition: testDatabase.c:88
void scheduler_destroy(scheduler_t *scheduler)
Free any memory associated with a scheduler_t.
Definition: scheduler.c:373
void email_init(scheduler_t *scheduler)
Loads information about the email that will be sent for job notifications.
Definition: database.c:568
void database_init(scheduler_t *scheduler)
Definition: database.c:781
void database_update_event(scheduler_t *scheduler, void *unused)
Checks the job queue for any new entries.
Definition: database.c:852
char * check_scheduler_tables
Definition: sqlstatements.h:33
void database_update_job(scheduler_t *scheduler, job_t *job, job_status status)
Change the status of a job in the database.
Definition: database.c:956
void database_job_log(int j_id, char *log_name)
Enters the name of the log file for a job into the database.
Definition: database.c:1017
GSequence * job_queue
heap of jobs that still need to be started
Definition: scheduler.h:184
PGresult * database_exec(scheduler_t *scheduler, const char *sql)
Executes an sql statement for the scheduler.
Definition: database.c:814
char * jobsql_processed
void test_email_notify()
Test for email_notification()
Definition: testDatabase.c:248
GTree * job_list
List of jobs that have been created.
Definition: scheduler.h:183
void test_database_update_job()
Test for database_update_job()
Definition: testDatabase.c:152
GList * running_agents
The list of agents assigned to this job that are still working.
Definition: job.h:66
scheduler_t * scheduler_init(gchar *sysconfigdir, log_t *log)
Create a new scheduler object.
Definition: scheduler.c:260
void database_exec_event(scheduler_t *scheduler, char *sql)
Definition: database.c:838
void test_database_update_event()
Test for database_update_event()
Definition: testDatabase.c:113
void database_job_processed(int j_id, int num)
Updates the number of items that a job queue entry has processed.
Definition: database.c:1003
gchar * message
Message that will be sent with job notification email.
Definition: job.h:80
The job structure.
Definition: job.h:61
Definition: event.h:56
job_t * job_init(GTree *job_list, GSequence *job_queue, char *type, char *host, int id, int parent_id, int user_id, int group_id, int priority, char *jq_cmd_args)
Create a new job.
Definition: job.c:175
void database_job_priority(scheduler_t *scheduler, job_t *job, int priority)
Changes the priority of a job queue entry in the database.
Definition: database.c:1032
void test_database_job()
Test for database_job_processed(),database_job_log(),database_job_priority()
Definition: testDatabase.c:201
job_status status
The current status for the job.
Definition: job.h:72
void test_database_init()
Test for database_init()
Definition: testDatabase.c:48
int32_t id
The identifier for this job.
Definition: job.h:84