FOSSology  4.4.0
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;");
118  db_result = database_exec(scheduler, sql);
119  //printf("result: %s", PQget(db_result, 0, "job_name"));
120  if(PQresultStatus(db_result) == PGRES_TUPLES_OK && PQntuples(db_result) != 0)
121  {
122  FO_ASSERT_STRING_EQUAL(PQget(db_result, 0, "job_name"), "testing file");
123  FO_ASSERT_EQUAL(atoi(PQget(db_result, 0, "job_user_fk")), 1);
124  }
125  PQclear(db_result);
126 
127  database_reset_queue(scheduler);
128 
129  scheduler_destroy(scheduler);
130 }
131 
142 {
143  scheduler_t* scheduler;
144  job_t* job;
145  arg_int* params;
146  int jq_pk;
147  job_t tmp_job;
148 
149  scheduler = scheduler_init(testdb, NULL);
150 
151  FO_ASSERT_PTR_NULL(scheduler->db_conn);
152  database_init(scheduler);
153  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
154 
155  jq_pk = Prepare_Testing_Data(scheduler);
156 
157  params = g_new0(arg_int, 1);
158  params->second = jq_pk;
159  params->first = g_tree_lookup(scheduler->job_list, &params->second);
160  job = params->first;
161  if(params->first == NULL)
162  {
163  tmp_job.id = params->second;
164  tmp_job.status = JB_NOT_AVAILABLE;
165  tmp_job.running_agents = NULL;
166  tmp_job.message = NULL;
167 
168  job = &tmp_job;
169  }
170 
171  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_NOT_AVAILABLE");
172  database_update_job(scheduler, job, JB_PAUSED);
173  //job = g_tree_lookup(scheduler->job_list, &params->second);
174  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_NOT_AVAILABLE");
175 
176  g_free(params);
177  scheduler_destroy(scheduler);
178 }
179 
191 {
192  scheduler_t* scheduler;
193  job_t* job;
194  arg_int* params;
195  int jq_pk;
196  job_t tmp_job;
197 
198  scheduler = scheduler_init(testdb, NULL);
199 
200  FO_ASSERT_PTR_NULL(scheduler->db_conn);
201  database_init(scheduler);
202  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
203 
204  jq_pk = Prepare_Testing_Data(scheduler);
205 
206  params = g_new0(arg_int, 1);
207  params->second = jq_pk;
208  params->first = g_tree_lookup(scheduler->job_list, &params->second);
209  job = params->first;
210  if(params->first == NULL)
211  {
212  tmp_job.id = params->second;
213  tmp_job.status = JB_NOT_AVAILABLE;
214  tmp_job.running_agents = NULL;
215  tmp_job.message = NULL;
216 
217  job = &tmp_job;
218  }
219 
220  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_NOT_AVAILABLE");
221 
222  database_job_processed(jq_pk, 2);
223  database_job_log(jq_pk, "test log");
224  database_job_priority(scheduler, job, 1);
225 
226  g_free(params);
227  scheduler_destroy(scheduler);
228 }
229 
238 {
239  scheduler_t* scheduler;
240  job_t* job;
241  int jq_pk;
242 
243  scheduler = scheduler_init(testdb, NULL);
244 
245  FO_ASSERT_PTR_NULL(scheduler->db_conn);
246  database_init(scheduler);
247  email_init(scheduler);
248  FO_ASSERT_PTR_NOT_NULL(scheduler->db_conn);
249 
250  jq_pk = Prepare_Testing_Data(scheduler);
251  job = job_init(scheduler->job_list, scheduler->job_queue, "ununpack", "localhost", -1, 0, 0, 0, 0, NULL);
252  job->id = jq_pk;
253 
254  database_update_job(scheduler, job, JB_FAILED);
255  FO_ASSERT_STRING_EQUAL(job_status_strings[job->status], "JOB_CHECKEDOUT");
256 
257  scheduler_destroy(scheduler);
258 }
259 /* ************************************************************************** */
260 /* **** suite declaration *************************************************** */
261 /* ************************************************************************** */
262 
263 CU_TestInfo tests_database[] =
264 {
265  {"Test database_init", test_database_init },
266  {"Test database_exec_event", test_database_exec_event },
267  {"Test database_update_event", test_database_update_event},
268  {"Test database_update_job", test_database_update_job },
269  {"Test database_job", test_database_job },
270  CU_TEST_INFO_NULL
271 };
272 
273 CU_TestInfo tests_email[] =
274 {
275  {"Test email_notify", test_email_notify },
276  CU_TEST_INFO_NULL
277 };
278 
279 
280 
281 
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:803
void database_reset_queue(scheduler_t *scheduler)
Resets any jobs in the job queue that are not completed.
Definition: database.c:931
void database_init(scheduler_t *scheduler)
Definition: database.c:770
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:1021
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:945
void database_update_event(scheduler_t *scheduler, void *unused)
Checks the job queue for any new entries.
Definition: database.c:841
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:1006
void database_exec_event(scheduler_t *scheduler, char *sql)
Definition: database.c:827
void email_init(scheduler_t *scheduler)
Loads information about the email that will be sent for job notifications.
Definition: database.c:557
void database_job_processed(int j_id, int num)
Updates the number of items that a job queue entry has processed.
Definition: database.c:992
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:362
int Prepare_Testing_Data(scheduler_t *scheduler)
Definition: utils.c:23
Definition: event.h:46
The job structure.
Definition: job.h:51
int32_t id
The identifier for this job.
Definition: job.h:73
job_status status
The current status for the job.
Definition: job.h:61
gchar * message
Message that will be sent with job notification email.
Definition: job.h:69
GList * running_agents
The list of agents assigned to this job that are still working.
Definition: job.h:55
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:141
char * jobsql_processed
void test_email_notify()
Test for email_notification()
Definition: testDatabase.c:237
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:190
void test_database_exec_event()
Test for database_exec_event()
Definition: testDatabase.c:77