FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_fossscheduler.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
12 /* includes for files that will be tested */
13 #include <libfossscheduler.h>
14 
15 /* library includes */
16 #include <string.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 
20 /* cunit includes */
21 #include <libfocunit.h>
22 
23 #ifndef COMMIT_HASH
24 #define COMMIT_HASH "COMMIT_HASH Unknown"
25 #endif
26 
27 /* ************************************************************************** */
28 /* *** declaration of private members *************************************** */
29 /* ************************************************************************** */
30 
31 extern int items_processed;
32 extern int valid;
33 extern int sscheduler;
34 extern int agent_verbose;
35 extern void fo_heartbeat();
36 
37 /* ************************************************************************** */
38 /* *** set up and tear down for fossschduler ******************************** */
39 /* ************************************************************************** */
40 
41 char tbuffer[1024];
42 int in_sub[2];
43 int out_sub[2];
44 int stdin_t;
45 int stdout_t;
46 FILE* read_from;
47 FILE* write_to;
48 
49 #define FROM_UNIT "UNIT\n"
50 #define VERBOSE_TEST 7
51 #define NC_TEST "Not a Command\n"
52 
53 #define write_con(...) \
54  fprintf(write_to, __VA_ARGS__); \
55  fflush(write_to);
56 
65 void set_up(void)
66 {
67  FO_ASSERT_TRUE_FATAL(!pipe(in_sub));
68  FO_ASSERT_TRUE_FATAL(!pipe(out_sub));
69 
70  stdin_t = dup(fileno(stdin));
71  stdout_t = dup(fileno(stdout));
72 
73  dup2(out_sub[1], fileno(stdout));
74  dup2(in_sub[0], fileno(stdin));
75  read_from = fdopen(out_sub[0], "rb");
76  write_to = fdopen(in_sub[1], "wb");
77 
78  memset(tbuffer, '\0', sizeof(tbuffer));
79 }
80 
88 void tear_down(void)
89 {
90  fclose(read_from);
91  fclose(write_to);
92 
93  close(in_sub[0]);
94  close(in_sub[1]);
95  close(out_sub[0]);
96  close(out_sub[1]);
97 
98  dup2(stdin_t, fileno(stdin));
99  dup2(stdout_t, fileno(stdout));
100 }
101 
113 {
114  FO_ASSERT_FALSE(valid);
115  FO_ASSERT_STRING_EQUAL(fgets(tbuffer, sizeof(tbuffer), read_from), "OK\n");
116 
117  write_con("CLOSE\n");
118 }
119 
130 {
131  FO_ASSERT_FALSE(valid);
132  FO_ASSERT_EQUAL(agent_verbose, VERBOSE_TEST);
133 
134  agent_verbose = 0;
135 
136  write_con("CLOSE\n");
137 }
138 
146 {
147  FO_ASSERT_FALSE(valid);
148  FO_ASSERT_PTR_NOT_NULL(fgets(tbuffer, sizeof(tbuffer), read_from));
149  tbuffer[strlen(tbuffer) - 1] = '\0';
150  FO_ASSERT_STRING_EQUAL(tbuffer, COMMIT_HASH);
151 
152  write_con("CLOSE\n");
153 }
154 
155 /* ************************************************************************** */
156 /* *** tests **************************************************************** */
157 /* ************************************************************************** */
158 
168 {
169  int argc = 2;
170  char* argv[] = {"./test_clibs", "--config=./scheddata"};
171 
172  fo_scheduler_connect(&argc, argv, NULL);
173 
174  FO_ASSERT_FALSE(sscheduler);
175  FO_ASSERT_EQUAL(items_processed, 0);
176  FO_ASSERT_FALSE(valid);
177  FO_ASSERT_FALSE(agent_verbose);
178 
179  /* make sure that fo_scheduler_connect didn't write anything to stdout */
180  fprintf(stdout, FROM_UNIT);
181  FO_ASSERT_PTR_NOT_NULL(fgets(tbuffer, sizeof(tbuffer), read_from));
182  FO_ASSERT_STRING_EQUAL(tbuffer, FROM_UNIT);
183 
184  /* reset stdout for the next test */
185  while (strcmp(tbuffer, FROM_UNIT) != 0)
186  FO_ASSERT_PTR_NOT_NULL(fgets(tbuffer, sizeof(tbuffer), read_from));
187 }
188 
200 {
201  int argc = 2;
202  char* argv[] = {"./test_clibs", "--config=./scheddata", "--scheduler_start"};
203  char* tmp;
204 
205  fo_scheduler_connect(&argc, argv, NULL);
206 
207  FO_ASSERT_TRUE(sscheduler);
208  FO_ASSERT_EQUAL(items_processed, 0);
209  FO_ASSERT_FALSE(valid);
210  FO_ASSERT_FALSE(agent_verbose);
211 
212  /* check that the correct stuff was written to stdout */
213  memset(tbuffer, '\0', sizeof(tbuffer));
214  tmp = fgets(tbuffer, sizeof(tbuffer), read_from);
215  FO_ASSERT_PTR_NOT_NULL(tmp);
216  FO_ASSERT_STRING_EQUAL(tbuffer, COMMIT_HASH);
217 
218  tmp = fgets(tbuffer, sizeof(tbuffer), read_from);
219  FO_ASSERT_PTR_NOT_NULL(tmp);
220  FO_ASSERT_STRING_EQUAL(tmp, "OK\n");
221 
222  ualarm(10, 0);
223  usleep(20);
224 
225  FO_ASSERT_STRING_EQUAL(
226  fgets(tbuffer, sizeof(tbuffer), read_from),
227  "HEART: 0\n");
228 }
229 
239 {
240  write_con("CLOSE\n");
241 
242  FO_ASSERT_PTR_NULL(fo_scheduler_next());
243  FO_ASSERT_FALSE(valid);
244 }
245 
256 {
257  write_con("END\n");
258 
259  signal(SIGALRM, signal_connect_end);
260  ualarm(10, 0);
261 
262  FO_ASSERT_PTR_NULL(fo_scheduler_next());
263  FO_ASSERT_FALSE(valid);
264 }
265 
277 {
278  write_con("VERBOSE %d\n", VERBOSE_TEST);
279 
280  signal(SIGALRM, signal_connect_verbose);
281  ualarm(10, 0);
282 
283  FO_ASSERT_PTR_NULL(fo_scheduler_next());
284  FO_ASSERT_FALSE(valid);
285 }
286 
298 {
299  write_con("VERSION\n");
300 
301  signal(SIGALRM, signal_connect_version);
302  ualarm(10, 0);
303 
304  FO_ASSERT_PTR_NULL(fo_scheduler_next());
305  FO_ASSERT_FALSE(valid);
306 }
307 
318 {
319  char* ret;
320 
321  write_con(NC_TEST);
322 
323  FO_ASSERT_PTR_NOT_NULL((ret = fo_scheduler_next()));
324  FO_ASSERT_STRING_EQUAL(ret, NC_TEST);
325  FO_ASSERT_TRUE(valid);
326 }
327 
336 {
337  FO_ASSERT_STRING_EQUAL(fo_scheduler_current(), NC_TEST);
338 
339  write_con("CLOSE\n");
340 
341  FO_ASSERT_PTR_NULL(fo_scheduler_next());
342  FO_ASSERT_PTR_NULL(fo_scheduler_current());
343 }
344 
353 {
354  sscheduler = 1;
355 
357  FO_ASSERT_STRING_EQUAL(fgets(tbuffer, sizeof(tbuffer), read_from), "BYE 2\n");
358  FO_ASSERT_FALSE(valid);
359  FO_ASSERT_FALSE(sscheduler);
360 }
361 
374 {
375  FO_ASSERT_EQUAL(items_processed, 0);
377  FO_ASSERT_EQUAL(items_processed, 1);
378  fo_scheduler_heart(10);
379  FO_ASSERT_EQUAL(items_processed, 11);
380 
381  signal(SIGALRM, fo_heartbeat);
382  ualarm(10, 0);
383  usleep(20);
384 
385  FO_ASSERT_STRING_EQUAL(
386  fgets(tbuffer, sizeof(tbuffer), read_from),
387  "HEART: 11\n");
388 }
389 
390 /* ************************************************************************** */
391 /* *** cunit test info ****************************************************** */
392 /* ************************************************************************** */
393 
394 CU_TestInfo fossscheduler_testcases[] =
395  {
396  {"fossscheduler set up", set_up},
397  {"fossscheduler no connect", test_scheduler_no_connect},
398  {"fossscheduler connect", test_scheduler_connect},
399  {"fossscheduler next close", test_scheduler_next_close},
400  {"fossscheduler next end", test_scheduler_next_end},
401  {"fossscheduler next verbose", test_scheduler_next_verbose},
402  {"fossscheduler next version", test_scheduler_next_version},
403  {"fossscheduler next oth", test_scheduler_next_oth},
404  {"fossscheduler current", test_scheduler_current},
405  {"fossscheduler disconnect", test_scheduler_disconnect},
406  {"fossscheduler heat", test_scheduler_heart},
407  {"fossscheduler tear down", tear_down},
408  CU_TEST_INFO_NULL
409  };
410 
411 
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
void fo_scheduler_heart(int i)
This function must be called by agents to let the scheduler know they are alive and how many items th...
char * fo_scheduler_current()
Get the last read string from the scheduler.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
void set_up(void)
Since the fossscheduler library depends on reading and writing very specific data to stdin and stdout...
void fo_heartbeat()
Internal function to send a heartbeat to the scheduler along with the number of items processed.
void test_scheduler_connect()
Test for fo_scheduler_connect() with a new connection.
void test_scheduler_next_oth()
Tests scheduler for non commands.
void test_scheduler_no_connect()
Test for fo_scheduler_connect() with no new connection.
void test_scheduler_next_version()
Tests sending "VERSION\n" to the stdin for the scheduler next function.
void tear_down(void)
This function closes the pipes created in the setup function and returns stdin and stdout to their or...
void test_scheduler_next_end()
Tests sending "END\n" to the stdin for the scheduler next function.
void signal_connect_end()
Test for fo_scheduler_next() blocking.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
int items_processed
The number of items processed by the agent.
void test_scheduler_heart()
Test the scheduler heart function. This function must set up the heartbeat again so that it can check...
void signal_connect_verbose()
Serves the same purpose for the verbose command as the signal_connect_end() function does for the end...
void test_scheduler_current()
Tests the scheduler current function.
void signal_connect_version()
Test for version string.
void test_scheduler_next_verbose()
Tests sending "VERBOSE #\n" to the stdin for the scheduler next function.
void test_scheduler_next_close()
Tests sending "CLOSE\n" to stdin for the scheduler next function.
int sscheduler
Whether the agent was started by the scheduler.
int valid
If the information stored in buffer is valid.
void test_scheduler_disconnect()
Tests the scheduler disconnection function.