FOSSology  4.7.1
Open Source License Compliance by Open Source Software
testDatabase.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2013 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
11 /* include functions to test */
12 #include <testRun.h>
13 
14 /* scheduler includes */
15 #include <database.h>
16 #include <scheduler.h>
17 
18 /* library includes */
19 #include <utils.h>
20 
21 /* testing sql statements */
22 char sqltmp[1024] = {0};
23 extern char* check_scheduler_tables;
24 extern char* jobsql_processed;
25 
26 /* ************************************************************************** */
27 /* **** database function tests ******************************************** */
28 /* ************************************************************************** */
29 
38 {
39  scheduler_t* scheduler;
40  PGresult* db_result;
41  GString* sql;
42 
43  scheduler = scheduler_init(testdb, NULL);
44  database_init(scheduler);
45 
46  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
47 
48  sprintf(sqltmp, check_scheduler_tables, PQdb(scheduler->db_conn));
49  sql = g_string_new(sqltmp);
50  g_string_append(sql, "'users';");
51 
52  /* get the url for the fossology instance */
53  db_result = database_exec(scheduler, sql->str);
54  //printf("sql: %s\n", sql->str);
55  // TODO skip this test since the order reported here is random, also it will crash if PQntuples < 5
56  #if 0
57  if(PQresultStatus(db_result) == PGRES_TUPLES_OK && PQntuples(db_result) != 0)
58  {
59  //printf("result: %s\n", g_strdup(PQgetvalue(db_result, 0, 0)));
60  FO_ASSERT_STRING_EQUAL(g_strdup(PQgetvalue(db_result, 0, 0)), "user_pk");
61  FO_ASSERT_STRING_EQUAL(g_strdup(PQgetvalue(db_result, 1, 0)), "user_name");
62  FO_ASSERT_STRING_EQUAL(g_strdup(PQgetvalue(db_result, 2, 0)), "root_folder_fk");
63  FO_ASSERT_STRING_EQUAL(g_strdup(PQgetvalue(db_result, 3, 0)), "user_desc");
64  FO_ASSERT_STRING_EQUAL(g_strdup(PQgetvalue(db_result, 4, 0)), "user_seed");
65  }
66  #endif
67  PQclear(db_result);
68  g_string_free(sql, TRUE);
69  scheduler_destroy(scheduler);
70 }
71 
78 {
79  scheduler_t* scheduler;
80  gchar* sql = NULL;
81 
82  scheduler = scheduler_init(testdb, NULL);
83 
84  FO_ASSERT_PTR_NULL(scheduler->db_conn);
85  database_init(scheduler);
86  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
87 
88  sql = g_strdup_printf(jobsql_processed, 0, 123);
89 
90  database_exec_event(scheduler, sql);
91  scheduler_destroy(scheduler);
92 }
93 
103 {
104  scheduler_t* scheduler;
105  char sql[512];
106  PGresult* db_result;
107 
108  scheduler = scheduler_init(testdb, NULL);
109 
110  FO_ASSERT_PTR_NULL(scheduler->db_conn);
111  database_init(scheduler);
112  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
113 
114  Prepare_Testing_Data(scheduler);
115 
116  database_update_event(scheduler, NULL);
117  sprintf(sql, "SELECT * FROM job WHERE job_name = 'testing file' ORDER BY job_pk DESC LIMIT 1;");
118  db_result = database_exec(scheduler, sql);
119  if(PQresultStatus(db_result) == PGRES_TUPLES_OK && PQntuples(db_result) != 0)
120  {
121  FO_ASSERT_STRING_EQUAL(PQget(db_result, 0, "job_name"), "testing file");
122  FO_ASSERT_NOT_EQUAL(atoi(PQget(db_result, 0, "job_user_fk")), 0);
123  }
124  PQclear(db_result);
125 
126  database_reset_queue(scheduler);
127 
128  scheduler_destroy(scheduler);
129 }
130 
141 {
142  scheduler_t* scheduler;
143  job_t* job;
144  arg_int* params;
145  int jq_pk;
146  job_t tmp_job;
147 
148  scheduler = scheduler_init(testdb, NULL);
149 
150  FO_ASSERT_PTR_NULL(scheduler->db_conn);
151  database_init(scheduler);
152  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
153 
154  jq_pk = Prepare_Testing_Data(scheduler);
155 
156  params = g_new0(arg_int, 1);
157  params->second = jq_pk;
158  params->first = g_tree_lookup(scheduler->job_list, &params->second);
159  job = params->first;
160  if(params->first == NULL)
161  {
162  tmp_job.id = params->second;
163  tmp_job.status = JB_NOT_AVAILABLE;
164  tmp_job.running_agents = NULL;
165  tmp_job.message = NULL;
166 
167  job = &tmp_job;
168  }
169 
170  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_NOT_AVAILABLE");
171  database_update_job(scheduler, job, JB_PAUSED);
172  //job = g_tree_lookup(scheduler->job_list, &params->second);
173  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_NOT_AVAILABLE");
174 
175  g_free(params);
176  scheduler_destroy(scheduler);
177 }
178 
190 {
191  scheduler_t* scheduler;
192  job_t* job;
193  arg_int* params;
194  int jq_pk;
195  job_t tmp_job;
196 
197  scheduler = scheduler_init(testdb, NULL);
198 
199  FO_ASSERT_PTR_NULL(scheduler->db_conn);
200  database_init(scheduler);
201  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
202 
203  jq_pk = Prepare_Testing_Data(scheduler);
204 
205  params = g_new0(arg_int, 1);
206  params->second = jq_pk;
207  params->first = g_tree_lookup(scheduler->job_list, &params->second);
208  job = params->first;
209  if(params->first == NULL)
210  {
211  tmp_job.id = params->second;
212  tmp_job.status = JB_NOT_AVAILABLE;
213  tmp_job.running_agents = NULL;
214  tmp_job.message = NULL;
215 
216  job = &tmp_job;
217  }
218 
219  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_NOT_AVAILABLE");
220 
221  database_job_processed(jq_pk, 2);
222  database_job_log(jq_pk, "test log");
223  database_job_priority(scheduler, job, 1);
224 
225  g_free(params);
226  scheduler_destroy(scheduler);
227 }
228 
237 {
238  scheduler_t* scheduler;
239  job_t* job;
240  int jq_pk;
241 
242  scheduler = scheduler_init(testdb, NULL);
243 
244  FO_ASSERT_PTR_NULL(scheduler->db_conn);
245  database_init(scheduler);
246  email_init(scheduler);
247  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
248 
249  jq_pk = Prepare_Testing_Data(scheduler);
250  job = job_init(scheduler->job_list, scheduler->job_queue, "ununpack", "localhost", -1, 0, 0, 0, 0, NULL);
251  job->id = jq_pk;
252 
253  database_update_job(scheduler, job, JB_FAILED);
254  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_CHECKEDOUT");
255 
256  scheduler_destroy(scheduler);
257 }
258 /* ************************************************************************** */
259 /* **** suite declaration *************************************************** */
260 /* ************************************************************************** */
261 
262 CU_TestInfo tests_database[] =
263 {
264  {"Test database_init", test_database_init },
265  {"Test database_exec_event", test_database_exec_event },
266  {"Test database_update_event", test_database_update_event},
267  {"Test database_update_job", test_database_update_job },
268  {"Test database_job", test_database_job },
269  CU_TEST_INFO_NULL
270 };
271 
272 CU_TestInfo tests_email[] =
273 {
274  {"Test email_notify", test_email_notify },
275  CU_TEST_INFO_NULL
276 };
277 
278 
279 
280 
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:164
PGresult * database_exec(scheduler_t *scheduler, const char *sql)
Executes an sql statement for the scheduler.
Definition: database.c:804
void database_reset_queue(scheduler_t *scheduler)
Resets any jobs in the job queue that are not completed.
Definition: database.c:928
void database_init(scheduler_t *scheduler)
Definition: database.c:771
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:1028
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:946
void database_update_event(scheduler_t *scheduler, void *unused)
Checks the job queue for any new entries.
Definition: database.c:846
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:1013
void database_exec_event(scheduler_t *scheduler, char *sql)
Definition: database.c:828
void email_init(scheduler_t *scheduler)
Loads information about the email that will be sent for job notifications.
Definition: database.c:558
void database_job_processed(int j_id, int num)
Updates the number of items that a job queue entry has processed.
Definition: database.c:999
scheduler_t * scheduler_init(gchar *sysconfigdir, log_t *log)
Create a new scheduler object.
Definition: scheduler.c:249
void scheduler_destroy(scheduler_t *scheduler)
Free any memory associated with a scheduler_t.
Definition: scheduler.c:364
int Prepare_Testing_Data(scheduler_t *scheduler)
Definition: utils.c:23
Definition: event.h:46
The job structure.
Definition: job.h:52
int32_t id
The identifier for this job.
Definition: job.h:74
job_status status
The current status for the job.
Definition: job.h:62
gchar * message
Message that will be sent with job notification email.
Definition: job.h:70
GList * running_agents
The list of agents assigned to this job that are still working.
Definition: job.h:56
GTree * job_list
List of jobs that have been created.
Definition: scheduler.h:172
PGconn * db_conn
The database connection.
Definition: scheduler.h:176
GSequence * job_queue
heap of jobs that still need to be started
Definition: scheduler.h:173
void test_database_init()
Test for database_init()
Definition: testDatabase.c:37
char * check_scheduler_tables
Definition: sqlstatements.h:22
void test_database_update_job()
Test for database_update_job()
Definition: testDatabase.c:140
char * jobsql_processed
void test_email_notify()
Test for email_notification()
Definition: testDatabase.c:236
void test_database_update_event()
Test for database_update_event()
Definition: testDatabase.c:102
void test_database_job()
Test for database_job_processed(),database_job_log(),database_job_priority()
Definition: testDatabase.c:189
void test_database_exec_event()
Test for database_exec_event()
Definition: testDatabase.c:77