FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_fossscheduler.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2011 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 *********************************************************************/
17 
23 /* includes for files that will be tested */
24 #include <libfossscheduler.h>
25 
26 /* library includes */
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 
31 /* cunit includes */
32 #include <libfocunit.h>
33 
34 #ifndef COMMIT_HASH
35 #define COMMIT_HASH "COMMIT_HASH Unknown"
36 #endif
37 
38 /* ************************************************************************** */
39 /* *** declaration of private members *************************************** */
40 /* ************************************************************************** */
41 
42 extern int items_processed;
43 extern char buffer[];
44 extern int valid;
45 extern int sscheduler;
46 extern int agent_verbose;
47 extern void fo_heartbeat();
48 
49 /* ************************************************************************** */
50 /* *** set up and tear down for fossschduler ******************************** */
51 /* ************************************************************************** */
52 
53 char buffer[1024];
54 int in_sub[2];
55 int out_sub[2];
56 int stdin_t;
57 int stdout_t;
58 FILE* read_from;
59 FILE* write_to;
60 
61 #define FROM_UNIT "UNIT\n"
62 #define VERBOSE_TEST 7
63 #define NC_TEST "Not a Command\n"
64 
65 #define write_con(...) \
66  fprintf(write_to, __VA_ARGS__); \
67  fflush(write_to);
68 
77 void set_up(void)
78 {
79  FO_ASSERT_TRUE_FATAL(!pipe(in_sub));
80  FO_ASSERT_TRUE_FATAL(!pipe(out_sub));
81 
82  stdin_t = dup(fileno(stdin));
83  stdout_t = dup(fileno(stdout));
84 
85  dup2(out_sub[1], fileno(stdout));
86  dup2(in_sub[0], fileno(stdin));
87  read_from = fdopen(out_sub[0], "rb");
88  write_to = fdopen(in_sub[1], "wb");
89 
90  memset(buffer, '\0', sizeof(buffer));
91 }
92 
100 void tear_down(void)
101 {
102  fclose(read_from);
103  fclose(write_to);
104 
105  close(in_sub[0]);
106  close(in_sub[1]);
107  close(out_sub[0]);
108  close(out_sub[1]);
109 
110  dup2(stdin_t, fileno(stdin));
111  dup2(stdout_t, fileno(stdout));
112 }
113 
125 {
126  FO_ASSERT_FALSE(valid);
127  FO_ASSERT_STRING_EQUAL(fgets(buffer, sizeof(buffer), read_from), "OK\n");
128 
129  write_con("CLOSE\n");
130 }
131 
142 {
143  FO_ASSERT_FALSE(valid);
144  FO_ASSERT_EQUAL(agent_verbose, VERBOSE_TEST);
145 
146  agent_verbose = 0;
147 
148  write_con("CLOSE\n");
149 }
150 
158 {
159  FO_ASSERT_FALSE(valid);
160  FO_ASSERT_PTR_NOT_NULL(fgets(buffer, sizeof(buffer), read_from));
161  buffer[strlen(buffer) - 1] = '\0';
162  FO_ASSERT_STRING_EQUAL(buffer, COMMIT_HASH);
163 
164  write_con("CLOSE\n");
165 }
166 
167 /* ************************************************************************** */
168 /* *** tests **************************************************************** */
169 /* ************************************************************************** */
170 
180 {
181  int argc = 2;
182  char* argv[] = {"./testlibs", "--config=./scheddata"};
183 
184  fo_scheduler_connect(&argc, argv, NULL);
185 
186  FO_ASSERT_FALSE(sscheduler);
187  FO_ASSERT_EQUAL(items_processed, 0);
188  FO_ASSERT_FALSE(valid);
189  FO_ASSERT_FALSE(agent_verbose);
190 
191  /* make sure that fo_scheduler_connect didn't write anything to stdout */
192  fprintf(stdout, FROM_UNIT);
193  FO_ASSERT_PTR_NOT_NULL(fgets(buffer, sizeof(buffer), read_from));
194  FO_ASSERT_STRING_EQUAL(buffer, FROM_UNIT);
195 
196  /* reset stdout for the next test */
197  while (strcmp(buffer, FROM_UNIT) != 0)
198  FO_ASSERT_PTR_NOT_NULL(fgets(buffer, sizeof(buffer), read_from));
199 }
200 
212 {
213  int argc = 2;
214  char* argv[] = {"./testlibs", "--config=./scheddata", "--scheduler_start"};
215  char* tmp;
216 
217  fo_scheduler_connect(&argc, argv, NULL);
218 
219  FO_ASSERT_TRUE(sscheduler);
220  FO_ASSERT_EQUAL(items_processed, 0);
221  FO_ASSERT_FALSE(valid);
222  FO_ASSERT_FALSE(agent_verbose);
223 
224  /* check that the correct stuff was written to stdout */
225  memset(buffer, '\0', sizeof(buffer));
226  tmp = fgets(buffer, sizeof(buffer), read_from);
227  FO_ASSERT_PTR_NOT_NULL(tmp);
228  FO_ASSERT_STRING_EQUAL(buffer, COMMIT_HASH);
229 
230  tmp = fgets(buffer, sizeof(buffer), read_from);
231  FO_ASSERT_PTR_NOT_NULL(tmp);
232  FO_ASSERT_STRING_EQUAL(tmp, "OK\n");
233 
234  ualarm(10, 0);
235  usleep(20);
236 
237  FO_ASSERT_STRING_EQUAL(
238  fgets(buffer, sizeof(buffer), read_from),
239  "HEART: 0\n");
240 }
241 
251 {
252  write_con("CLOSE\n");
253 
254  FO_ASSERT_PTR_NULL(fo_scheduler_next());
255  FO_ASSERT_FALSE(valid);
256 }
257 
268 {
269  write_con("END\n");
270 
271  signal(SIGALRM, signal_connect_end);
272  ualarm(10, 0);
273 
274  FO_ASSERT_PTR_NULL(fo_scheduler_next());
275  FO_ASSERT_FALSE(valid);
276 }
277 
289 {
290  write_con("VERBOSE %d\n", VERBOSE_TEST);
291 
292  signal(SIGALRM, signal_connect_verbose);
293  ualarm(10, 0);
294 
295  FO_ASSERT_PTR_NULL(fo_scheduler_next());
296  FO_ASSERT_FALSE(valid);
297 }
298 
310 {
311  write_con("VERSION\n");
312 
313  signal(SIGALRM, signal_connect_version);
314  ualarm(10, 0);
315 
316  FO_ASSERT_PTR_NULL(fo_scheduler_next());
317  FO_ASSERT_FALSE(valid);
318 }
319 
330 {
331  char* ret;
332 
333  write_con(NC_TEST);
334 
335  FO_ASSERT_PTR_NOT_NULL((ret = fo_scheduler_next()));
336  FO_ASSERT_STRING_EQUAL(ret, NC_TEST);
337  FO_ASSERT_TRUE(valid);
338 }
339 
348 {
349  FO_ASSERT_STRING_EQUAL(fo_scheduler_current(), NC_TEST);
350 
351  write_con("CLOSE\n");
352 
353  FO_ASSERT_PTR_NULL(fo_scheduler_next());
354  FO_ASSERT_PTR_NULL(fo_scheduler_current());
355 }
356 
365 {
366  sscheduler = 1;
367 
369  FO_ASSERT_STRING_EQUAL(fgets(buffer, sizeof(buffer), read_from), "BYE 2\n");
370  FO_ASSERT_FALSE(valid);
371  FO_ASSERT_FALSE(sscheduler);
372 }
373 
386 {
387  FO_ASSERT_EQUAL(items_processed, 0);
389  FO_ASSERT_EQUAL(items_processed, 1);
390  fo_scheduler_heart(10);
391  FO_ASSERT_EQUAL(items_processed, 11);
392 
393  signal(SIGALRM, fo_heartbeat);
394  ualarm(10, 0);
395  usleep(20);
396 
397  FO_ASSERT_STRING_EQUAL(
398  fgets(buffer, sizeof(buffer), read_from),
399  "HEART: 11\n");
400 }
401 
402 /* ************************************************************************** */
403 /* *** cunit test info ****************************************************** */
404 /* ************************************************************************** */
405 
406 CU_TestInfo fossscheduler_testcases[] =
407  {
408  {"fossscheduler set up", set_up},
409  {"fossscheduler no connect", test_scheduler_no_connect},
410  {"fossscheduler connect", test_scheduler_connect},
411  {"fossscheduler next close", test_scheduler_next_close},
412  {"fossscheduler next end", test_scheduler_next_end},
413  {"fossscheduler next verbose", test_scheduler_next_verbose},
414  {"fossscheduler next version", test_scheduler_next_version},
415  {"fossscheduler next oth", test_scheduler_next_oth},
416  {"fossscheduler current", test_scheduler_current},
417  {"fossscheduler disconnect", test_scheduler_disconnect},
418  {"fossscheduler heat", test_scheduler_heart},
419  {"fossscheduler tear down", tear_down},
420  CU_TEST_INFO_NULL
421  };
422 
423 
void test_scheduler_next_end()
Tests sending "END\\n" to the stdin for the scheduler next function.
int valid
If the information stored in buffer is valid.
void signal_connect_version()
Test for version string.
int items_processed
The number of items processed by the agent.
void test_scheduler_no_connect()
Test for fo_scheduler_connect() with no new connection.
void test_scheduler_next_close()
Tests sending "CLOSE\\n" to 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...
char * fo_scheduler_current()
Get the last read string from the scheduler.
void test_scheduler_next_verbose()
Tests sending "VERBOSE #\\n" to the stdin for the scheduler next function.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
void test_scheduler_next_oth()
Tests scheduler for non commands.
void signal_connect_end()
Test for fo_scheduler_next() blocking.
char buffer[]
The last thing received from the scheduler.
void test_scheduler_next_version()
Tests sending "VERSION\\n" to the stdin for the scheduler next function.
void test_scheduler_connect()
Test for fo_scheduler_connect() with a new connection.
void fo_heartbeat()
Internal function to send a heartbeat to the scheduler along with the number of items processed...
void test_scheduler_current()
Tests the scheduler current function.
void set_up(void)
Since the fossscheduler library depends on reading and writing very specific data to stdin and stdout...
int sscheduler
Whether the agent was started by the scheduler.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
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...
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
void test_scheduler_disconnect()
Tests the scheduler disconnection function.
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...