Statistics
| Branch: | Tag: | Revision:

one / src / group / test / GroupPoolTest.cc @ ae53d437

History | View | Annotate | Download (6.21 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 "GroupPool.h"
22
#include "PoolTest.h"
23

    
24
using namespace std;
25

    
26
const int uids[] = {0,1,2};
27
const string names[] = {"First name", "Second name"};
28

    
29
const string xmls[] =
30
{
31
    "<GROUP><ID>1</ID><UID>0</UID><NAME>First name</NAME></GROUP>",
32
    "<GROUP><ID>2</ID><UID>1</UID><NAME>Second name</NAME></GROUP>"
33
};
34

    
35
const string group_xml_dump =
36
    "<GROUP_POOL><GROUP><ID>0</ID><UID>0</UID><NAME>default</NAME></GROUP><GROUP><ID>1</ID><UID>5</UID><NAME>group_a</NAME></GROUP><GROUP><ID>3</ID><UID>5</UID><NAME>group_c</NAME></GROUP><GROUP><ID>4</ID><UID>5</UID><NAME>group_d</NAME></GROUP></GROUP_POOL>";
37

    
38
/* ************************************************************************* */
39
/* ************************************************************************* */
40

    
41

    
42
#include "NebulaTest.h"
43

    
44
class NebulaTestGroup: public NebulaTest
45
{
46
public:
47
    NebulaTestGroup():NebulaTest()
48
    {
49
        NebulaTest::the_tester = this;
50

    
51
        need_group_pool= true;
52
    }
53
};
54

    
55
class GroupPoolTest : public PoolTest
56
{
57
    CPPUNIT_TEST_SUITE (GroupPoolTest);
58

    
59
    // Not all tests from PoolTest can be used. Because
60
    // of the initial default group added to the DB, the
61
    // oid_assignment would fail.
62
    CPPUNIT_TEST (get_from_cache);
63
    CPPUNIT_TEST (get_from_db);
64
    CPPUNIT_TEST (wrong_get);
65
    CPPUNIT_TEST (drop_and_get);
66

    
67
    CPPUNIT_TEST (duplicates);
68
    CPPUNIT_TEST (dump);
69

    
70
    CPPUNIT_TEST_SUITE_END ();
71

    
72
protected:
73

    
74
    NebulaTestGroup * tester;
75
    GroupPool *       gpool;
76

    
77

    
78

    
79
    void bootstrap(SqlDB* db)
80
    {
81
        // setUp overwritten
82
    };
83

    
84
    PoolSQL* create_pool(SqlDB* db)
85
    {
86
        // setUp overwritten
87
        return gpool;
88
    };
89

    
90
    int allocate(int index)
91
    {
92
        int    oid;
93
        string err;
94

    
95
        return gpool->allocate(uids[index], names[index], &oid, err);
96
    };
97

    
98
    void check(int index, PoolObjectSQL* obj)
99
    {
100
        Group * group = static_cast<Group *>(obj);
101

    
102
        CPPUNIT_ASSERT( obj != 0 );
103

    
104
        string xml_str = "";
105
        string name = group->get_name();
106

    
107
        CPPUNIT_ASSERT( name == names[index] );
108

    
109
        // Get the xml
110
        group->to_xml(xml_str);
111

    
112
//  A little help for debugging
113
/*
114
        if( xml_str != xmls[index] )
115
        {
116
            cout << endl << xml_str << endl << "========"
117
                 << endl << xmls[index];
118
        }
119
//*/
120
        CPPUNIT_ASSERT( xml_str == xmls[index]);
121
    };
122

    
123
private:
124

    
125

    
126
public:
127
    GroupPoolTest()
128
    {
129
        xmlInitParser();
130
    };
131

    
132
    ~GroupPoolTest()
133
    {
134
        xmlCleanupParser();
135
    };
136

    
137
    void setUp()
138
    {
139
        create_db();
140

    
141
        tester = new NebulaTestGroup();
142

    
143
        Nebula& neb = Nebula::instance();
144
        neb.start();
145

    
146
        gpool   = neb.get_gpool();
147

    
148
        pool    = gpool;
149
    };
150

    
151
    void tearDown()
152
    {
153
        delete_db();
154

    
155
        delete tester;
156
    };
157

    
158
    /* ********************************************************************* */
159
    /* ********************************************************************* */
160

    
161
    void duplicates()
162
    {
163
        int         rc, oid;
164
        string      err;
165
        GroupPool * gpool = static_cast<GroupPool*>(pool);
166

    
167
        // Allocate a group
168
        rc = gpool->allocate(uids[0], names[0], &oid, err);
169
        CPPUNIT_ASSERT( oid == 1 );
170
        CPPUNIT_ASSERT( oid == rc );
171

    
172
        // Try to allocate twice the same group, should fail
173
        rc = gpool->allocate(uids[0], names[0], &oid, err);
174
        CPPUNIT_ASSERT( rc  == -1 );
175
        CPPUNIT_ASSERT( oid == rc );
176

    
177
        // Try again, this time with different uid. Should fail, groups can't
178
        // repeat names
179
        rc = gpool->allocate(uids[1], names[0], &oid, err);
180
        CPPUNIT_ASSERT( rc  == -1 );
181
        CPPUNIT_ASSERT( oid == rc );
182
    }
183

    
184
    /* ********************************************************************* */
185

    
186
    void dump()
187
    {
188
        Group *         group;
189
        int             oid, rc;
190
        ostringstream   oss;
191
        string          err;
192

    
193
        // Allocate some groups
194
        rc = gpool->allocate(5, "group_a", &oid, err);
195
        CPPUNIT_ASSERT( rc == 1 );
196

    
197
        rc = gpool->allocate(5, "group_b", &oid, err);
198
        CPPUNIT_ASSERT( rc == 2 );
199

    
200
        rc = gpool->allocate(5, "group_c", &oid, err);
201
        CPPUNIT_ASSERT( rc == 3 );
202

    
203
        rc = gpool->allocate(5, "group_d", &oid, err);
204
        CPPUNIT_ASSERT( rc == 4 );
205

    
206
        // Drop one of them
207
        group = gpool->get(2, false);
208
        CPPUNIT_ASSERT( group != 0 );
209

    
210
        rc = gpool->drop(group);
211
        CPPUNIT_ASSERT( rc == 0 );
212

    
213
        // dump the pool
214
        rc = gpool->dump(oss,"");
215
/*
216
        if( oss.str() != group_xml_dump )
217
        {
218
            cout << endl << oss.str() << endl << "========"
219
                 << endl << group_xml_dump;
220
        }
221
//*/
222
        CPPUNIT_ASSERT( oss.str() == group_xml_dump );
223

    
224
    }
225
};
226

    
227
/* ************************************************************************* */
228
/* ************************************************************************* */
229

    
230
int main(int argc, char ** argv)
231
{
232
    return PoolTest::main(argc, argv, GroupPoolTest::suite());
233
}