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