FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testHost.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011, 2012 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 #include <host.h>
14 
15 /* ************************************************************************** */
16 /* **** host function tests ************************************************* */
17 /* ************************************************************************** */
18 
29 {
30  host_t* host;
31 
32  host = host_init("local", "localhost", "directory", 10);
33  FO_ASSERT_PTR_NOT_NULL(host);
34  FO_ASSERT_STRING_EQUAL(host->name, "local");
35  FO_ASSERT_STRING_EQUAL(host->address, "localhost");
36  FO_ASSERT_STRING_EQUAL(host->agent_dir, "directory");
37  FO_ASSERT_EQUAL(host->max, 10);
38  FO_ASSERT_EQUAL(host->running, 0);
39 
40  host_destroy(host);
41 }
42 
54 {
55  scheduler_t* scheduler;
56  gint list_size;
57  gint queue_size;
58  uint32_t i;
59  GList* iter;
60  gchar* name = g_strdup(" _local");
61 
62  scheduler = scheduler_init(testdb, NULL);
63 
64  /* add 10 hosts to the scheduler */
65  for(i = 0; i < 9; i++)
66  {
67  name[0] = (char)('1' + i);
68  host_insert(host_init(name, "localhost", "directory", i), scheduler);
69 
70  list_size = g_tree_nnodes(scheduler->host_list);
71  queue_size = g_list_length(scheduler->host_queue);
72  FO_ASSERT_EQUAL(list_size, i + 1);
73  FO_ASSERT_EQUAL(queue_size, i + 1);
74  }
75 
76  list_size = g_tree_nnodes(scheduler->host_list);
77  queue_size = g_list_length(scheduler->host_queue);
78  FO_ASSERT_EQUAL(list_size, 9);
79  FO_ASSERT_EQUAL(queue_size, 9);
80 
81  /* make sure they are in the correct order */
82  for(iter = scheduler->host_queue, i = 0; iter != NULL; iter = iter->next, i++)
83  FO_ASSERT_EQUAL(((host_t*)iter->data)->max, i);
84 
85  scheduler_destroy(scheduler);
86  g_free(name);
87 }
88 
98 {
99  host_t* host = host_init("local", "localhost", "directory", 10);
100 
101  FO_ASSERT_EQUAL(host->running, 0);
102  host_increase_load(host);
103  FO_ASSERT_EQUAL(host->running, 1);
104  host_increase_load(host);
105  FO_ASSERT_EQUAL(host->running, 2);
106 
107  host_destroy(host);
108 }
109 
119 {
120  host_t* host = host_init("local", "localhost", "directory", 10);
121  host->running = 2;
122 
123  FO_ASSERT_EQUAL(host->running, 2);
124  host_decrease_load(host);
125  FO_ASSERT_EQUAL(host->running, 1);
126  host_decrease_load(host);
127  FO_ASSERT_EQUAL(host->running, 0);
128 
129  host_destroy(host);
130 }
131 
141 {
142  host_t* host;
143  scheduler_t* scheduler;
144  uint32_t i;
145  char* name = g_strdup(" _local");
146 
147  scheduler = scheduler_init(testdb, NULL);
148 
149  for(i = 0; i < 9; i++)
150  {
151  name[0] = (char)('1' + i);
152  host_insert(host_init(name, "localhost", "directory", i + 1), scheduler);
153  }
154 
155  for(i = 0; i < 9; i++)
156  {
157  host = get_host(&scheduler->host_queue, i + 1);
158  name[0] = (char)('1' + i);
159 
160  FO_ASSERT_PTR_EQUAL(host, g_tree_lookup(scheduler->host_list, name));
161  FO_ASSERT_EQUAL(host->max, i + 1);
162  }
163 
164  host = get_host(&scheduler->host_queue, 3);
165  FO_ASSERT_STRING_EQUAL(host->name, "3_local");
166  FO_ASSERT_EQUAL(host->max, 3);
167  host = get_host(&scheduler->host_queue, 1);
168  FO_ASSERT_STRING_EQUAL(host->name, "1_local");
169  FO_ASSERT_EQUAL(host->max, 1);
170  host = get_host(&scheduler->host_queue, 9);
171  FO_ASSERT_STRING_EQUAL(host->name, "9_local");
172  FO_ASSERT_EQUAL(host->max, 9);
173  host = get_host(&scheduler->host_queue, 3);
174  FO_ASSERT_STRING_EQUAL(host->name, "4_local");
175  FO_ASSERT_EQUAL(host->max, 4);
176 
177  scheduler_destroy(scheduler);
178  g_free(name);
179 }
180 
181 /* ************************************************************************** */
182 /* *** suite declaration **************************************************** */
183 /* ************************************************************************** */
184 
185 CU_TestInfo tests_host[] =
186 {
187  {"Test host_init", test_host_init },
188  {"Test host_insert", test_host_insert },
189  {"Test host_increase_load", test_host_increase_load },
190  {"Test host_decrease_load", test_host_decrease_load },
191  {"Test host_get_host", test_get_host },
192  CU_TEST_INFO_NULL
193 };
194 
void host_insert(host_t *host, scheduler_t *scheduler)
Inserts a new host into the scheduler structure.
Definition: host.c:91
host_t * host_init(char *name, char *address, char *agent_dir, int max)
Creates a new host, and adds it to the host list.
Definition: host.c:48
void host_decrease_load(host_t *host)
Decrease the number of running agents on a host by 1.
Definition: host.c:113
void host_destroy(host_t *host)
Frees and uninitializes any memory associated with the host struct.
Definition: host.c:66
host_t * get_host(GList **queue, uint8_t num)
Definition: host.c:144
void host_increase_load(host_t *host)
Increase the number of running agents on a host by 1.
Definition: host.c:102
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
Definition: host.h:26
char * agent_dir
The location on the host machine where the executables are.
Definition: host.h:29
int running
The number of agents currently running on this host.
Definition: host.h:31
char * address
The address of the host, used by ssh when starting a new agent.
Definition: host.h:28
char * name
The name of the host, used to store host internally to scheduler.
Definition: host.h:27
int max
The max number of agents that can run on this host.
Definition: host.h:30
GList * host_queue
Round-robin queue for choosing which host use next.
Definition: scheduler.h:161
GTree * host_list
List of all hosts available to the scheduler.
Definition: scheduler.h:160
void test_host_increase_load()
Test for host_increase_load()
Definition: testHost.c:97
void test_host_init()
Test for host_init()
Definition: testHost.c:28
void test_host_insert()
Test for host_insert()
Definition: testHost.c:53
void test_host_decrease_load()
Test for host_decrease_load()
Definition: testHost.c:118
void test_get_host()
Test for get_host()
Definition: testHost.c:140