FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testHost.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2011, 2012 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 #include <host.h>
25 
26 /* ************************************************************************** */
27 /* **** host function tests ************************************************* */
28 /* ************************************************************************** */
29 
40 {
41  host_t* host;
42 
43  host = host_init("local", "localhost", "directory", 10);
44  FO_ASSERT_PTR_NOT_NULL(host);
45  FO_ASSERT_STRING_EQUAL(host->name, "local");
46  FO_ASSERT_STRING_EQUAL(host->address, "localhost");
47  FO_ASSERT_STRING_EQUAL(host->agent_dir, "directory");
48  FO_ASSERT_EQUAL(host->max, 10);
49  FO_ASSERT_EQUAL(host->running, 0);
50 
51  host_destroy(host);
52 }
53 
65 {
66  scheduler_t* scheduler;
67  gint list_size;
68  gint queue_size;
69  uint32_t i;
70  GList* iter;
71  gchar* name = g_strdup(" _local");
72 
73  scheduler = scheduler_init(testdb, NULL);
74 
75  /* add 10 hosts to the scheduler */
76  for(i = 0; i < 9; i++)
77  {
78  name[0] = (char)('1' + i);
79  host_insert(host_init(name, "localhost", "directory", i), scheduler);
80 
81  list_size = g_tree_nnodes(scheduler->host_list);
82  queue_size = g_list_length(scheduler->host_queue);
83  FO_ASSERT_EQUAL(list_size, i + 1);
84  FO_ASSERT_EQUAL(queue_size, i + 1);
85  }
86 
87  list_size = g_tree_nnodes(scheduler->host_list);
88  queue_size = g_list_length(scheduler->host_queue);
89  FO_ASSERT_EQUAL(list_size, 9);
90  FO_ASSERT_EQUAL(queue_size, 9);
91 
92  /* make sure they are in the correct order */
93  for(iter = scheduler->host_queue, i = 0; iter != NULL; iter = iter->next, i++)
94  FO_ASSERT_EQUAL(((host_t*)iter->data)->max, i);
95 
96  scheduler_destroy(scheduler);
97  g_free(name);
98 }
99 
109 {
110  host_t* host = host_init("local", "localhost", "directory", 10);
111 
112  FO_ASSERT_EQUAL(host->running, 0);
113  host_increase_load(host);
114  FO_ASSERT_EQUAL(host->running, 1);
115  host_increase_load(host);
116  FO_ASSERT_EQUAL(host->running, 2);
117 
118  host_destroy(host);
119 }
120 
130 {
131  host_t* host = host_init("local", "localhost", "directory", 10);
132  host->running = 2;
133 
134  FO_ASSERT_EQUAL(host->running, 2);
135  host_decrease_load(host);
136  FO_ASSERT_EQUAL(host->running, 1);
137  host_decrease_load(host);
138  FO_ASSERT_EQUAL(host->running, 0);
139 
140  host_destroy(host);
141 }
142 
152 {
153  host_t* host;
154  scheduler_t* scheduler;
155  uint32_t i;
156  char* name = g_strdup(" _local");
157 
158  scheduler = scheduler_init(testdb, NULL);
159 
160  for(i = 0; i < 9; i++)
161  {
162  name[0] = (char)('1' + i);
163  host_insert(host_init(name, "localhost", "directory", i + 1), scheduler);
164  }
165 
166  for(i = 0; i < 9; i++)
167  {
168  host = get_host(&scheduler->host_queue, i + 1);
169  name[0] = (char)('1' + i);
170 
171  FO_ASSERT_PTR_EQUAL(host, g_tree_lookup(scheduler->host_list, name));
172  FO_ASSERT_EQUAL(host->max, i + 1);
173  }
174 
175  host = get_host(&scheduler->host_queue, 3);
176  FO_ASSERT_STRING_EQUAL(host->name, "3_local");
177  FO_ASSERT_EQUAL(host->max, 3);
178  host = get_host(&scheduler->host_queue, 1);
179  FO_ASSERT_STRING_EQUAL(host->name, "1_local");
180  FO_ASSERT_EQUAL(host->max, 1);
181  host = get_host(&scheduler->host_queue, 9);
182  FO_ASSERT_STRING_EQUAL(host->name, "9_local");
183  FO_ASSERT_EQUAL(host->max, 9);
184  host = get_host(&scheduler->host_queue, 3);
185  FO_ASSERT_STRING_EQUAL(host->name, "4_local");
186  FO_ASSERT_EQUAL(host->max, 4);
187 
188  scheduler_destroy(scheduler);
189  g_free(name);
190 }
191 
192 /* ************************************************************************** */
193 /* *** suite declaration **************************************************** */
194 /* ************************************************************************** */
195 
196 CU_TestInfo tests_host[] =
197 {
198  {"Test host_init", test_host_init },
199  {"Test host_insert", test_host_insert },
200  {"Test host_increase_load", test_host_increase_load },
201  {"Test host_decrease_load", test_host_decrease_load },
202  {"Test host_get_host", test_get_host },
203  CU_TEST_INFO_NULL
204 };
205 
GTree * host_list
List of all hosts available to the scheduler.
Definition: scheduler.h:171
void test_get_host()
Test for get_host()
Definition: testHost.c:151
void test_host_increase_load()
Test for host_increase_load()
Definition: testHost.c:108
void scheduler_destroy(scheduler_t *scheduler)
Free any memory associated with a scheduler_t.
Definition: scheduler.c:373
void host_destroy(host_t *host)
Frees and uninitializes any memory associated with the host struct.
Definition: host.c:78
void test_host_init()
Test for host_init()
Definition: testHost.c:39
GList * host_queue
Round-robin queue for choosing which host use next.
Definition: scheduler.h:172
int max
The max number of agents that can run on this host.
Definition: host.h:42
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:60
char * address
The address of the host, used by ssh when starting a new agent.
Definition: host.h:40
char * name
The name of the host, used to store host internally to scheduler.
Definition: host.h:39
void host_insert(host_t *host, scheduler_t *scheduler)
Inserts a new host into the scheduler structure.
Definition: host.c:103
void test_host_decrease_load()
Test for host_decrease_load()
Definition: testHost.c:129
void host_increase_load(host_t *host)
Increase the number of running agents on a host by 1.
Definition: host.c:114
scheduler_t * scheduler_init(gchar *sysconfigdir, log_t *log)
Create a new scheduler object.
Definition: scheduler.c:260
host_t * get_host(GList **queue, uint8_t num)
Definition: host.c:156
Definition: host.h:38
char * agent_dir
The location on the host machine where the executables are.
Definition: host.h:41
void test_host_insert()
Test for host_insert()
Definition: testHost.c:64
void host_decrease_load(host_t *host)
Decrease the number of running agents on a host by 1.
Definition: host.c:125
int running
The number of agents currently running on this host.
Definition: host.h:43