Statistics
| Branch: | Tag: | Revision:

one / src / cluster / test / ClusterPoolTest.cc @ 6329d8b3

History | View | Annotate | Download (11.5 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org)             */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#include <string>
18
#include <iostream>
19
#include <stdlib.h>
20

    
21
#include "ClusterPool.h"
22
#include "PoolTest.h"
23

    
24
using namespace std;
25

    
26
const string names[] = {"cluster_a", "Second cluster"};
27

    
28
const string xmls[] =
29
{
30
    "<CLUSTER><ID>1</ID><NAME>cluster_a</NAME></CLUSTER>",
31
    "<CLUSTER><ID>2</ID><NAME>Second cluster</NAME></CLUSTER>"
32
};
33

    
34
const string cluster_default =
35
    "<CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER>";
36

    
37
const string cluster_xml_dump =
38
    "<CLUSTER_POOL><CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER><CLUSTER><ID>1</ID><NAME>cluster_a</NAME></CLUSTER><CLUSTER><ID>3</ID><NAME>cluster_c</NAME></CLUSTER><CLUSTER><ID>4</ID><NAME>cluster_d</NAME></CLUSTER></CLUSTER_POOL>";
39

    
40
const string host_0_cluster =
41
    "<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CID>1</CID><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
42

    
43
const string host_0_default =
44
    "<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE>"
45
    "<IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD>"
46
    "<LAST_MON_TIME>0</LAST_MON_TIME><CID>0</CID><HOST_SHARE>"
47
    "<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
48
    "<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU>"
49
    "<FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU>"
50
    "<USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU>"
51
    "<RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
52

    
53
/* ************************************************************************* */
54
/* ************************************************************************* */
55

    
56

    
57
#include "NebulaTest.h"
58

    
59
class NebulaTestCluster: public NebulaTest
60
{
61
public:
62
    NebulaTestCluster():NebulaTest()
63
    {
64
        NebulaTest::the_tester = this;
65

    
66
        need_host_pool = true;    
67
        need_cluster_pool= true;
68
    }
69
};
70

    
71
class ClusterPoolTest : public PoolTest
72
{
73
    CPPUNIT_TEST_SUITE (ClusterPoolTest);
74

    
75
    // Not all tests from PoolTest can be used. Because
76
    // of the initial default cluster added to the DB, the
77
    // oid_assignment would fail.
78
    CPPUNIT_TEST (get_from_cache);
79
    CPPUNIT_TEST (get_from_db);
80
    CPPUNIT_TEST (wrong_get);
81
    CPPUNIT_TEST (drop_and_get);
82

    
83
    CPPUNIT_TEST (name_pool);
84

    
85
    CPPUNIT_TEST (duplicates);
86
    CPPUNIT_TEST (set_cluster);
87
    CPPUNIT_TEST (remove_cluster);
88
    CPPUNIT_TEST (delete_cluster);
89
    CPPUNIT_TEST (dump);
90

    
91
    CPPUNIT_TEST_SUITE_END ();
92

    
93
protected:
94

    
95
    NebulaTestCluster * tester;
96
    HostPool *          hpool;
97
    ClusterPool *       cpool;
98

    
99

    
100

    
101
    void bootstrap(SqlDB* db)
102
    {
103
        // setUp overwritten
104
    };
105

    
106
    PoolSQL* create_pool(SqlDB* db)
107
    {
108
        // setUp overwritten
109
        return cpool;
110
    };
111

    
112
    int allocate(int index)
113
    {
114
        int    oid;
115
        string err;
116

    
117
        return cpool->allocate(&oid, names[index], err);
118
    };
119

    
120
    void check(int index, PoolObjectSQL* obj)
121
    {
122
        Cluster * cluster = static_cast<Cluster *>(obj);
123

    
124
        CPPUNIT_ASSERT( obj != 0 );
125

    
126
        string xml_str = "";
127
        string name = cluster->get_name();
128

    
129
        CPPUNIT_ASSERT( name == names[index] );
130

    
131
        // Get the xml
132
        cluster->to_xml(xml_str);
133

    
134
//  A little help for debugging
135
/*
136
        if( xml_str != xmls[index] )
137
        {
138
            cout << endl << xml_str << endl << "========"
139
                 << endl << xmls[index];
140
        }
141
//*/
142
        CPPUNIT_ASSERT( xml_str == xmls[index]);
143
    };
144

    
145
private:
146

    
147
    /**
148
     *  Allocates a Host.
149
     */
150
    void init_hp()
151
    {
152
        int    oid;
153
        string err;
154

    
155
        // Allocate a host
156
        oid = hpool->allocate(&oid, "Host one","im_mad","vmm_mad", "tm_mad", err);
157
        CPPUNIT_ASSERT(oid == 0);
158

    
159
        hpool->clean();
160
    };
161

    
162
public:
163
    ClusterPoolTest()
164
    {
165
        xmlInitParser();
166
    };
167

    
168
    ~ClusterPoolTest()
169
    {
170
        xmlCleanupParser();
171
    };
172

    
173
    void setUp()
174
    {
175
        create_db();
176

    
177
        tester = new NebulaTestCluster();
178

    
179
        Nebula& neb = Nebula::instance();
180
        neb.start();
181

    
182
        hpool   = neb.get_hpool();
183
        cpool   = neb.get_cpool();
184

    
185
        pool    = cpool;
186
    };
187

    
188
    void tearDown()
189
    {
190
        delete_db();
191

    
192
        delete tester;
193
    };
194

    
195
    /* ********************************************************************* */
196
    /* ********************************************************************* */
197

    
198
    // Test intended to check the PoolSQL name index functionallity
199
    void name_pool()
200
    {
201
        Cluster *   cluster;
202
        int         oid;
203
        string      err;
204

    
205

    
206
        // Allocate some clusters
207
        cpool->allocate(&oid, "name_1", err);
208
        CPPUNIT_ASSERT( oid == 1 );
209

    
210
        cpool->allocate(&oid, "name_2", err);
211
        CPPUNIT_ASSERT( oid == 2 );
212

    
213
        cpool->allocate(&oid, "name_3", err);
214
        CPPUNIT_ASSERT( oid == 3 );
215

    
216
        // Clean the cache
217
        cpool->clean();
218

    
219
        cpool->allocate(&oid, "name_4", err);
220
        CPPUNIT_ASSERT( oid == 4 );
221

    
222
        cpool->allocate(&oid, "name_5", err);
223
        CPPUNIT_ASSERT( oid == 5 );
224

    
225
        // Cluster names 0-3 should be unknown, and 4-5 cached
226
        // Ask for a cached object
227
        cluster = cpool->get("name_5", false);
228
        CPPUNIT_ASSERT( cluster != 0 );
229
        CPPUNIT_ASSERT( cluster->get_oid() == 5 );
230
        CPPUNIT_ASSERT( cluster->get_name() == "name_5" );
231

    
232
        // Ask for non-cached object
233
        cluster = cpool->get("name_2", false);
234
        CPPUNIT_ASSERT( cluster != 0 );
235
        CPPUNIT_ASSERT( cluster->get_oid() == 2 );
236
        CPPUNIT_ASSERT( cluster->get_name() == "name_2" );
237

    
238
        // Ask for non-existing object
239
        cluster = cpool->get("name_X", false);
240
        CPPUNIT_ASSERT( cluster == 0 );
241
    };
242

    
243
    /* ********************************************************************* */
244

    
245
    void duplicates()
246
    {
247
        int rc, oid;
248
        string err;
249

    
250
        // Allocate a cluster.
251
        rc = cpool->allocate(&oid, names[1], err);
252
        CPPUNIT_ASSERT( oid == 1 );
253
        CPPUNIT_ASSERT( oid == rc );
254

    
255
        // Try to allocate twice the same cluster, should fail
256
        rc = cpool->allocate(&oid, names[1], err);
257
        CPPUNIT_ASSERT( rc  == -1 );
258
        CPPUNIT_ASSERT( oid == rc );
259
    }
260

    
261
    /* ********************************************************************* */
262

    
263
    void set_cluster()
264
    {
265
        Host*       host;
266
        int         rc;
267
        string      xml_str, err;
268

    
269
        init_hp();
270

    
271
        host = hpool->get(0, false);
272
        CPPUNIT_ASSERT(host != 0);
273

    
274
        rc = host->set_cluster(1);
275
        CPPUNIT_ASSERT( rc == 0 );
276

    
277
        hpool->update(host);
278

    
279
        host->to_xml(xml_str);
280
        CPPUNIT_ASSERT( xml_str == host_0_cluster);
281
    }
282

    
283
    /* ********************************************************************* */
284

    
285
    void remove_cluster()
286
    {
287
        Host*           host;
288

    
289
        int             rc;
290
        string          xml_str;
291

    
292
        init_hp();
293

    
294
        host = hpool->get(0, false);
295
        CPPUNIT_ASSERT(host != 0);
296

    
297
        // Set cluster
298
        rc = host->set_cluster(1);
299
        CPPUNIT_ASSERT( rc == 0 );
300

    
301
        hpool->update(host);
302

    
303
        host->to_xml(xml_str);
304
        CPPUNIT_ASSERT( xml_str == host_0_cluster);
305

    
306
        // Remove from the cluster
307
        rc = cpool->set_default_cluster(host);
308
        CPPUNIT_ASSERT( rc == 0 );
309

    
310
        hpool->update(host);
311

    
312
        // The host should have been moved to the default cluster
313

    
314
        host = hpool->get(0, false);
315
        CPPUNIT_ASSERT(host != 0);
316

    
317
        host->to_xml(xml_str);
318
        CPPUNIT_ASSERT( xml_str == host_0_default);
319
    }
320

    
321
    /* ********************************************************************* */
322

    
323
    void delete_cluster()
324
    {
325
        Host *          host;
326
        Cluster *       cluster;
327

    
328
        int             rc, oid;
329
        string          xml_str;
330

    
331
        init_hp();
332
        host = hpool->get(0, false);
333
        CPPUNIT_ASSERT(host != 0);
334

    
335
        // Allocate a cluster
336
        oid = allocate(0);
337
        CPPUNIT_ASSERT(oid == 1);
338

    
339
        cluster = cpool->get(1, false);
340

    
341
        // Set cluster
342
        rc = host->set_cluster(cluster->get_oid());
343
        CPPUNIT_ASSERT( rc == 0 );
344

    
345
        hpool->update(host);
346

    
347
        host->to_xml(xml_str);
348
        CPPUNIT_ASSERT( xml_str == host_0_cluster);
349

    
350
        // Delete the cluster
351
        rc = cpool->drop(cluster);
352
        CPPUNIT_ASSERT( rc == 0 );
353

    
354
        // The host should have been moved to the default cluster
355
        host = hpool->get(0, false);
356
        host->to_xml(xml_str);
357
/*
358
        if( xml_str != host_0_default )
359
        {
360
            cout << endl << xml_str << endl << "========"
361
                 << endl << host_0_default;
362
        }
363
//*/
364
        CPPUNIT_ASSERT( xml_str == host_0_default);
365
    }
366

    
367
    /* ********************************************************************* */
368

    
369
    void dump()
370
    {
371
        Cluster *       cluster;
372
        int             oid, rc;
373
        ostringstream   oss;
374
        string          err;
375

    
376
        // Allocate some clusters
377
        rc = cpool->allocate(&oid, "cluster_a", err);
378
        CPPUNIT_ASSERT( rc == 1 );
379

    
380
        rc = cpool->allocate(&oid, "cluster_b", err);
381
        CPPUNIT_ASSERT( rc == 2 );
382

    
383
        rc = cpool->allocate(&oid, "cluster_c", err);
384
        CPPUNIT_ASSERT( rc == 3 );
385

    
386
        rc = cpool->allocate(&oid, "cluster_d", err);
387
        CPPUNIT_ASSERT( rc == 4 );
388

    
389
        // Drop one of them
390
        cluster = cpool->get(2, false);
391
        CPPUNIT_ASSERT( cluster != 0 );
392

    
393
        rc = cpool->drop(cluster);
394
        CPPUNIT_ASSERT( rc == 0 );
395

    
396
        // dump the pool
397
        rc = cpool->dump(oss,"");
398
/*
399
        if( oss.str() != cluster_xml_dump )
400
        {
401
            cout << endl << oss.str() << endl << "========"
402
                 << endl << cluster_xml_dump;
403
        }
404
//*/
405
        CPPUNIT_ASSERT( oss.str() == cluster_xml_dump );
406
    }
407
};
408

    
409
/* ************************************************************************* */
410
/* ************************************************************************* */
411

    
412
int main(int argc, char ** argv)
413
{
414
    return PoolTest::main(argc, argv, ClusterPoolTest::suite());
415
}