Statistics
| Branch: | Tag: | Revision:

one / include / test / OneUnitTest.h @ 7aacd86c

History | View | Annotate | Download (7.05 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2011, 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
#ifndef ONE_UNIT_TEST_H_
18
#define ONE_UNIT_TEST_H_
19

    
20
#include <string>
21
#include <iostream>
22
#include <stdlib.h>
23
#include <getopt.h>
24

    
25
#include <TestFixture.h>
26
#include <TestAssert.h>
27
#include <TestSuite.h>
28
#include <TestCaller.h>
29
#include <ui/text/TestRunner.h>
30
#include <cppunit/extensions/HelperMacros.h>
31
#include <cppunit/XmlOutputter.h>
32
#include <unistd.h>
33

    
34
#include "SqlDB.h"
35
#include "SqliteDB.h"
36
#include "MySqlDB.h"
37

    
38
using namespace std;
39

    
40
/* ************************************************************************* */
41
/* ************************************************************************* */
42

    
43
class OneUnitTest : public CppUnit::TestFixture
44
{
45
protected:
46
    // Global flag to use either Sqlite or MySQL
47
    static bool mysql;
48

    
49
    static SqlDB * db;
50
    static string db_name;
51
    static string xml_name;
52

    
53
public:
54

    
55
    void create_db()
56
    {
57
        if (mysql)
58
        {
59
            db = new MySqlDB(  "localhost",0,
60
                                "oneadmin","oneadmin",db_name);
61

    
62
            ostringstream   oss1;
63
            oss1 << "DROP DATABASE IF EXISTS " << db_name;
64
            db->exec(oss1);
65

    
66
            ostringstream   oss;
67
            oss << "CREATE DATABASE " << db_name;
68
            db->exec(oss);
69

    
70
            ostringstream   oss2;
71
            oss2 << "use " << db_name;
72
            db->exec(oss2);
73
        }
74
        else
75
        {
76
            unlink(db_name.c_str());
77

    
78
            db = new SqliteDB(db_name);
79
        }
80
    };
81

    
82
    void delete_db()
83
    {
84
        if (mysql)
85
        {
86
            ostringstream   oss;
87
            oss << "DROP DATABASE IF EXISTS " << db_name;
88
            db->exec(oss);
89
        }
90
        else
91
        {
92
            unlink(db_name.c_str());
93
        }
94

    
95
        if ( db != 0 )
96
        {
97
            delete db;
98
        }
99
    };
100

    
101
    static SqlDB * get_db()
102
    {
103
        return db;
104
    }
105

    
106
    static void set_one_auth(string path = "../../test/one_auth")
107
    {
108
        // The UserPool constructor checks if the DB contains at least
109
        // one user, and adds one automatically from the ONE_AUTH file.
110
        // So the ONE_AUTH environment is forced to point to a test one_auth
111
        // file.
112
        ostringstream oss;
113

    
114
        oss << getenv("PWD") << "/" << path;
115
        setenv("ONE_AUTH", oss.str().c_str(), 1);
116
    }
117

    
118
// *****************************************************************************
119
// *****************************************************************************
120

    
121

    
122
    static void show_options ()
123
    {
124
        cout << "Options:\n";
125
        cout << "    -h  --help         Show this help\n"
126
                "    -s  --sqlite       Run Sqlite tests (default)\n"
127
                "    -m  --mysql        Run MySQL tests\n"
128
                "    -l  --log          Keep the log file, test.log\n"
129
                "    -x  --xml          Create xml output files, for Hudson\n";
130
    }
131

    
132

    
133
    /*
134
     * Not a true main, but a static method that can be called from the
135
     * child classes' true main.
136
     * Options:
137
     *     s: run sqlite tests
138
     *     m: run mysql tests
139
     */
140
    static int main(int argc,
141
                    char ** argv,
142
                    CPPUNIT_NS::TestSuite* suite,
143
                    string xml_name = "output.xml")
144
    {
145

    
146
        // Option flags
147
        bool sqlite_flag = true;
148
        bool log_flag    = false;
149
        bool xml_flag    = false;
150

    
151
        // Long options
152
        const struct option long_opt[] =
153
        {
154
            { "sqlite", 0,  NULL,   's'},
155
            { "mysql",  0,  NULL,   'm'},
156
            { "log",    0,  NULL,   'l'},
157
            { "help",   0,  NULL,   'h'},
158
            { "xml",    0,  NULL,   'x'}
159
        };
160

    
161
        int c;
162
        while ((c = getopt_long (argc, argv, "smlhx", long_opt, NULL)) != -1)
163
            switch (c)
164
            {
165
                case 'm':
166
                    sqlite_flag = false;
167
                    break;
168
                case 'l':
169
                    log_flag = true;
170
                    break;
171
                case 'x':
172
                    xml_flag = true;
173
                    break;
174
                case 'h':
175
                    show_options();
176
                    return 0;
177
            }
178

    
179

    
180
        // When a DB query fails, it tries to log the error.
181
        // We need to set the log file, otherwise it will end in a dead-lock
182
        NebulaLog::init_log_system(NebulaLog::FILE, Log::DEBUG, "test.log");
183
        NebulaLog::log("Test", Log::INFO, "Test started");
184

    
185
        // Set the opennebula install location to be the current dir.
186
        // This will prevent some of the tests from writing the individual
187
        // VM log files in an existing OpenNebula installation
188
        setenv("ONE_LOCATION", ".", 1);
189

    
190
        CppUnit::TextUi::TestRunner runner;
191
        ofstream                    outputFile;
192

    
193
        if( xml_flag )
194
        {
195
            outputFile.open(xml_name.c_str());
196
            CppUnit::XmlOutputter* outputter =
197
                    new CppUnit::XmlOutputter(&runner.result(), outputFile);
198

    
199
            runner.setOutputter(outputter);
200
        }
201

    
202
        runner.addTest( suite );
203

    
204
        if (sqlite_flag)
205
        {
206
            OneUnitTest::mysql = false;
207
            NebulaLog::log("Test", Log::INFO, "Running Sqlite tests...");
208
            cout << "\nRunning Sqlite tests...\n";
209
        }
210
        else
211
        {
212
            OneUnitTest::mysql = true;
213
            NebulaLog::log("Test", Log::INFO, "Running MySQL tests...");
214
            cout << "\nRunning MySQL tests...\n";
215
        }
216

    
217
        runner.run();
218

    
219
        if( xml_flag )
220
        {
221
            outputFile.close();
222
        }
223

    
224
        if (!log_flag)
225
            remove("test.log");
226

    
227
        NebulaLog::finalize_log_system();
228

    
229
        return 0;
230
    }
231
};
232

    
233

    
234
/* -----------------------------------------------------------------------------
235

236
int main(int argc, char ** argv)
237
{
238
    return OneUnitTest::main(argc, argv, TestClass::suite());
239
}
240

241
----------------------------------------------------------------------------- */
242

    
243

    
244
#endif // ONE_UNIT_TEST_H_