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 *********************************************************************/
17 /**
18  * \file
19  * \brief Unit test for host operations
20  */
21 
22 /* include functions to test */
23 #include <testRun.h>
24 #include <host.h>
25 
26 /* ************************************************************************** */
27 /* **** host function tests ************************************************* */
28 /* ************************************************************************** */
29 
30 /**
31  * \brief Test for host_init()
32  * \test
33  * -# Create a host using host_init()
34  * -# Check if the host returned is not NULL
35  * -# Check if the host gets name, address, agent_dir and max properly
36  * -# Check if the host has no running agents
37  * -# Destroy the host
38  */
test_host_init()39 void test_host_init()
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 
54 /**
55  * \brief Test for host_insert()
56  * \test
57  * -# Initialize scheduler using scheduler_init()
58  * -# Create a host using host_init()
59  * -# Insert the host to the scheduler using host_insert()
60  * -# Check if scheduler's host list and queue size increases
61  * -# Verify if the hosts are added in the given order to the scheduler's host
62  *    queue
63  */
test_host_insert()64 void test_host_insert()
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 
100 /**
101  * \brief Test for host_increase_load()
102  * \test
103  * -# Initialize host using host_init()
104  * -# Check the running agents on host are 0
105  * -# Call host_increase_load() on the host
106  * -# Check if the running agents on host are increasing
107  */
test_host_increase_load()108 void test_host_increase_load()
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 
121 /**
122  * \brief Test for host_decrease_load()
123  * \test
124  * -# Initialize host using host_init()
125  * -# Set the host load to 2
126  * -# Call host_decrease_load() on the host
127  * -# Check if the running agents on host are decreasing
128  */
test_host_decrease_load()129 void test_host_decrease_load()
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 
143 /**
144  * \brief Test for get_host()
145  * \test
146  * -# Initialize the scheduler using scheduler_init()
147  * -# Add hosts to the scheduler with different capacity using host_insert()
148  * -# Get the hosts from scheduler using get_host.
149  * -# Check the name of the host for a given capacity
150  */
test_get_host()151 void test_get_host()
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 
206