FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testAgent.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 *********************************************************************/
22 /* include functions to test */
23 #include <testRun.h>
24 
25 /* scheduler includes */
26 #include <agent.h>
27 #include <job.h>
28 #include <scheduler.h>
29 
30 /* ************************************************************************** */
31 /* **** local declarations ************************************************** */
32 /* ************************************************************************** */
33 /*
34 int agent_init_suite(void)
35 {
36  meta_agents = g_tree_new_full(string_compare, NULL, NULL, (GDestroyNotify)meta_agent_destroy);
37  job_list = g_tree_new((GCompareFunc)int_compare);
38  agents = g_tree_new((GCompareFunc)int_compare);
39 
40  return init_suite();
41 }
42 
43 int agent_clean_suite(void)
44 {
45  g_tree_destroy(job_list);
46  g_tree_destroy(agents);
47 
48  return clean_suite();
49 }
50 */
51 
59 void create_pipe(int* int_dst, int* int_src, FILE** file_dst, FILE** file_src)
60 {
61  int a_to_b[2];
62 
63  if(pipe(a_to_b) != 0)
64  return;
65 
66  if(int_dst) *int_dst = a_to_b[0];
67  if(int_src) *int_src = a_to_b[1];
68 
69  if(file_dst) *file_dst = fdopen(a_to_b[0], "r");
70  if(file_src) *file_src = fdopen(a_to_b[1], "w");
71 }
72 
73 /* ************************************************************************** */
74 /* **** meta agent function tests ******************************************* */
75 /* ************************************************************************** */
76 
88 {
89  char* name = "copyright";
90  char* cmmd = name;
91  int max = 11;
92  int spc = 0;
93 
94  meta_agent_t* ma = meta_agent_init(name, cmmd, max, spc);
95 
96  FO_ASSERT_PTR_NOT_NULL_FATAL(ma);
97  FO_ASSERT_STRING_EQUAL(ma->name, "copyright");
98  FO_ASSERT_STRING_EQUAL(ma->raw_cmd, "copyright --scheduler_start");
99  FO_ASSERT_EQUAL(ma->max_run, max);
100  FO_ASSERT_EQUAL(ma->special, spc);
101  FO_ASSERT_PTR_NULL(ma->version);
102  FO_ASSERT_TRUE(ma->valid);
103 
104  FO_ASSERT_PTR_NULL(meta_agent_init(NULL, cmmd, max, spc));
105  FO_ASSERT_PTR_NULL(meta_agent_init(name, NULL, max, spc));
106 }
107 
116 {
117  scheduler_t* scheduler;
118  meta_agent_t* ma;
119 
120  scheduler = scheduler_init(testdb, NULL);
121  scheduler_foss_config(scheduler);
122 
123  FO_ASSERT_TRUE(add_meta_agent(scheduler->meta_agents, "name", "cmd", 11, 1));
124  FO_ASSERT_FALSE(add_meta_agent(scheduler->meta_agents, NULL, "cmd", 11, 1));
125 
126  ma = g_tree_lookup(scheduler->meta_agents, "name");
127  FO_ASSERT_EQUAL(g_tree_nnodes(scheduler->meta_agents), 1);
128  FO_ASSERT_PTR_NOT_NULL(ma);
129  FO_ASSERT_STRING_EQUAL(ma->name, "name");
130  FO_ASSERT_STRING_EQUAL(ma->raw_cmd, "cmd --scheduler_start");
131  FO_ASSERT_EQUAL(ma->max_run, 11);
132  FO_ASSERT_EQUAL(ma->special, 1);
133  FO_ASSERT_PTR_NULL(ma->version);
134  FO_ASSERT_TRUE(ma->valid);
135 
136  g_tree_remove(scheduler->meta_agents, "name");
137  scheduler_destroy(scheduler);
138 }
139 
140 /*
141 void test_agent_list_init()
142 {
143  FO_ASSERT_PTR_NULL(meta_agents);
144  FO_ASSERT_PTR_NULL(agents);
145  FO_ASSERT_PTR_NULL(heart_regex);
146 
147  agent_list_init();
148 
149  FO_ASSERT_PTR_NOT_NULL(meta_agents);
150  FO_ASSERT_PTR_NOT_NULL(agents);
151  FO_ASSERT_PTR_NOT_NULL(heart_regex);
152 }
153 
154 void test_agent_list_clear()
155 {
156  agent_list_clean();
157 
158  FO_ASSERT_PTR_NOT_NULL(meta_agents);
159  FO_ASSERT_PTR_NOT_NULL(agents);
160  FO_ASSERT_PTR_NOT_NULL(heart_regex);
161  FO_ASSERT_EQUAL(g_tree_nnodes(meta_agents), 0);
162  FO_ASSERT_EQUAL(g_tree_nnodes(agents), 0);
163 }
164 */
165 
166 /* ************************************************************************** */
167 /* **** agent function tests ************************************************ */
168 /* ************************************************************************** */
169 
179 {
180  scheduler_t* scheduler;
181  agent_t fagent;
182  job_t fjob;
183  int* pid_set = NULL;
184  agent_t* a1;
185 
186  scheduler = scheduler_init(testdb, NULL);
187  scheduler_foss_config(scheduler);
188  //meta_agent_t* ma = meta_agent_init("sample", "test_binary", 0, 0);
189 
190  fagent.pid = 10;
191  fagent.owner = &fjob;
192  fagent.status = AG_CREATED;
193  //fagent.thread = g_thread_create(fake_thread, NULL, TRUE, NULL);
194 
195  fjob.id = 1;
196  fjob.status = JB_STARTED;
197  fjob.failed_agents = NULL;
198 
199  create_pipe(&fagent.from_child, &fagent.to_parent, NULL, &fagent.write);
200 
201  pid_set = g_new0(int, 2);
202  pid_set[0] = fagent.pid;
203  pid_set[1] = 0;
204  fagent.return_code = 0;
205 
206  agent_death_event(scheduler, pid_set);
207  a1 = g_tree_lookup(scheduler->agents, &fagent.pid);
208 
209  FO_ASSERT_EQUAL(fagent.status, AG_CREATED);
210  FO_ASSERT_PTR_NULL(a1);
211 
212  close(fagent.from_child);
213  close(fagent.to_parent);
214  fclose(fagent.write);
215  scheduler_destroy(scheduler);
216 }
217 
231 {
232  scheduler_t* scheduler;
233  agent_t* fagent = NULL;
234  job_t* fjob = NULL;
235  agent_t* ag = NULL;
236  GList* gl = NULL;
237  int* pid_set = NULL;
238 
239  static int32_t id_gen = -1;
240  GList* iter;
241  host_t* host;
242 
243  scheduler = scheduler_init(testdb, NULL);
244  scheduler_config_event(scheduler, NULL);
245 
246  meta_agent_t* ma = g_tree_lookup(scheduler->meta_agents, "copyright");
247  for(iter = scheduler->host_queue; iter != NULL; iter = iter->next)
248  {
249  host = (host_t*)iter->data;
250  fjob = job_init(scheduler->job_list, scheduler->job_queue, ma->name,
251  host->name, id_gen--, 0, 0, 0, 0, NULL);
252  fagent = agent_init(scheduler, host, fjob);
253  }
254  fagent->pid = 10;
255  fagent->owner = fjob;
256  fagent->status = AG_CREATED;
257 
258  /* test agent_create_event */
259  agent_create_event(scheduler, fagent);
260 
261  ag = g_tree_lookup(scheduler->agents, &fagent->pid);
262  gl = g_list_find(fjob->running_agents, fagent);
263 
264  FO_ASSERT_PTR_NOT_NULL(ag);
265  FO_ASSERT_PTR_NOT_NULL(gl);
266  FO_ASSERT_EQUAL(fagent->status, AG_SPAWNED);
267  FO_ASSERT_PTR_EQUAL(ag, gl->data);
268 
269  agent_pause(fagent);
270  FO_ASSERT_EQUAL(fagent->status, AG_PAUSED);
271  agent_unpause(fagent);
272  FO_ASSERT_EQUAL(fagent->status, AG_RUNNING);
273 
274  //agent_print_status(fagent, stdout);
275 
276  /* test agent_ready_event */
277  agent_ready_event(scheduler, fagent);
278  ag = g_tree_lookup(scheduler->agents, &fagent->pid);
279 
280  FO_ASSERT_PTR_NOT_NULL(ag);
281  FO_ASSERT_EQUAL(fagent->status, AG_PAUSED);
282 
283  /* test agent fail event */
284  agent_fail_event(scheduler, fagent);
285  ag = g_tree_lookup(scheduler->agents, &fagent->pid);
286 
287  FO_ASSERT_PTR_NOT_NULL(ag);
288  FO_ASSERT_EQUAL(fagent->status, AG_FAILED);
289 
290  /* test agent update event */
291  agent_update_event(scheduler, NULL);
292  ag = g_tree_lookup(scheduler->agents, &fagent->pid);
293  FO_ASSERT_PTR_NOT_NULL(ag);
294  FO_ASSERT_EQUAL(fagent->status, AG_FAILED);
295 
296  pid_set = g_new0(int, 2);
297  pid_set[0] = fagent->pid;
298  pid_set[1] = 0;
299  fagent->return_code = 0;
300 
301  /* test agent death event */
302  agent_death_event(scheduler, pid_set);
303  ag = g_tree_lookup(scheduler->agents, &fagent->pid);
304 
305  FO_ASSERT_EQUAL(fagent->status, AG_FAILED);
306  FO_ASSERT_PTR_NULL(ag);
307 
308  scheduler_close_event(scheduler, (void*)1);
309  scheduler_destroy(scheduler);
310 }
311 
317 {
318  scheduler_t* scheduler;
319  agent_t* fagent;
320  job_t* fjob;
321 
322  static int32_t id_gen = -1;
323  GList* iter;
324  host_t* host;
325 
326  scheduler = scheduler_init(testdb, NULL);
327  scheduler_agent_config(scheduler);
328 
329  meta_agent_t* ma = g_tree_lookup(scheduler->meta_agents, "copyright");
330  for(iter = scheduler->host_queue; iter != NULL; iter = iter->next)
331  {
332  host = (host_t*)iter->data;
333  fjob = job_init(scheduler->job_list, scheduler->job_queue, ma->name,
334  host->name, id_gen--, 0, 0, 0, 0, NULL);
335  fagent = agent_init(scheduler, host, fjob);
336  FO_ASSERT_PTR_NOT_NULL(fagent);
337  }
338  /*
339  FO_ASSERT_EQUAL(g_tree_nnodes(scheduler->meta_agents), 9);
340  FO_ASSERT_PTR_NOT_NULL(ma);
341  FO_ASSERT_STRING_EQUAL(ma->name, "copyright");
342  FO_ASSERT_STRING_EQUAL(ma->raw_cmd, "copyright --scheduler_start");
343  FO_ASSERT_EQUAL(ma->max_run, 255);
344  FO_ASSERT_EQUAL(ma->special, 0);
345  FO_ASSERT_PTR_NULL(ma->version);
346  FO_ASSERT_TRUE(ma->valid);
347  */
348 
349  scheduler_destroy(scheduler);
350  // TODO finish
351 }
352 /* ************************************************************************** */
353 /* **** suite declaration *************************************************** */
354 /* ************************************************************************** */
355 
356 CU_TestInfo tests_meta_agent[] =
357 {
358 
359  {"Test meta_agent_init", test_meta_agent_init },
360  {"Test add_meta_agent", test_add_meta_agent },
361  CU_TEST_INFO_NULL
362 };
363 
364 CU_TestInfo tests_agent[] =
365 {
366  {"Test agent_init", test_agent_init },
367  //{"Test agent_death_event", test_agent_death_event },
368  {"Test agent_create_event", test_agent_create_event },
369  CU_TEST_INFO_NULL
370 };
371 
char name[256]
the name associated with this agent i.e. nomos, copyright...
Definition: agent.h:94
void scheduler_close_event(scheduler_t *scheduler, void *killed)
Sets the closing flag and possibly kills all currently running agents.
Definition: scheduler.c:1025
meta_agent_t * meta_agent_init(char *name, char *cmd, int max, int spc)
Creates a new meta agent.
Definition: agent.c:755
GTree * meta_agents
List of all meta agents available to the scheduler.
Definition: scheduler.h:167
int add_meta_agent(GTree *meta_agents, char *name, char *cmd, int max, int spc)
Definition: agent.c:1327
void create_pipe(int *int_dst, int *int_src, FILE **file_dst, FILE **file_src)
Creates 2 pipes and initialize parameters.
Definition: testAgent.c:59
void scheduler_destroy(scheduler_t *scheduler)
Free any memory associated with a scheduler_t.
Definition: scheduler.c:373
void agent_death_event(scheduler_t *scheduler, pid_t *pid)
Definition: agent.c:963
GList * host_queue
Round-robin queue for choosing which host use next.
Definition: scheduler.h:172
int max_run
the maximum number that can run at once -1 if no limit
Definition: agent.h:96
GSequence * job_queue
heap of jobs that still need to be started
Definition: scheduler.h:184
void agent_fail_event(scheduler_t *scheduler, agent_t *agent)
Fails an agent.
Definition: agent.c:1113
char * name
The name of the host, used to store host internally to scheduler.
Definition: host.h:39
int to_parent
file identifier to print to the parent (child stdout)
Definition: agent.h:127
GTree * job_list
List of jobs that have been created.
Definition: scheduler.h:183
agent_t * agent_init(scheduler_t *scheduler, host_t *host, job_t *job)
Allocate and spawn a new agent.
Definition: agent.c:813
void agent_ready_event(scheduler_t *scheduler, agent_t *agent)
Event created when an agent is ready for more data.
Definition: agent.c:1053
uint8_t return_code
what was returned by the agent when it disconnected
Definition: agent.h:137
FILE * write
FILE* that abstracts the use of the to_child socket.
Definition: agent.h:129
agent_status status
the state of execution the agent is currently in
Definition: agent.h:117
void test_agent_init()
Test for agent_init()
Definition: testAgent.c:316
void test_agent_death_event()
Test for agent_death_event()
Definition: testAgent.c:178
void test_add_meta_agent()
Test for add_meta_agent()
Definition: testAgent.c:115
void agent_create_event(scheduler_t *scheduler, agent_t *agent)
Event created when a new agent has been created.
Definition: agent.c:1032
GList * running_agents
The list of agents assigned to this job that are still working.
Definition: job.h:66
void scheduler_agent_config(scheduler_t *scheduler)
Loads a particular agents configuration file.
Definition: scheduler.c:754
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
Definition: libfossagent.c:309
void scheduler_foss_config(scheduler_t *scheduler)
Loads the configuration data from fossology.conf.
Definition: scheduler.c:864
void test_meta_agent_init()
Test for meta_agent_init()
Definition: testAgent.c:87
void agent_unpause(agent_t *agent)
Definition: agent.c:1188
void agent_pause(agent_t *agent)
Definition: agent.c:1175
GTree * agents
List of any currently running agents.
Definition: scheduler.h:168
scheduler_t * scheduler_init(gchar *sysconfigdir, log_t *log)
Create a new scheduler object.
Definition: scheduler.c:260
Definition: agent.h:110
int from_child
file identifier to read from child
Definition: agent.h:126
char raw_cmd[MAX_CMD+1]
the raw command that will start the agent, used for ssh
Definition: agent.h:95
void agent_update_event(scheduler_t *scheduler, void *unused)
Definition: agent.c:1098
pid_t pid
the pid of the process this agent is running in
Definition: agent.h:121
int valid
flag indicating if the meta_agent is valid
Definition: agent.h:100
Header file with agent related operations.
int special
any special condition associated with the agent
Definition: agent.h:97
Definition: host.h:38
void scheduler_config_event(scheduler_t *scheduler, void *unused)
Load both the fossology configuration and all the agent configurations.
Definition: scheduler.c:1002
The job structure.
Definition: job.h:61
void test_agent_create_event()
Test for agent_create_event()
Definition: testAgent.c:230
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
gchar * data
the data that has been sent to the agent for analysis
Definition: agent.h:133
job_status status
The current status for the job.
Definition: job.h:72
GList * failed_agents
The list of agents that failed while working.
Definition: job.h:68
char * version
the version of the agent that is running on all hosts
Definition: agent.h:99
job_t * owner
the job that this agent is assigned to
Definition: agent.h:132
int32_t id
The identifier for this job.
Definition: job.h:84