File: OneUnitTest.h

package info (click to toggle)
opennebula 3.4.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,680 kB
  • sloc: cpp: 35,288; ruby: 24,818; sh: 5,212; java: 4,001; xml: 1,163; yacc: 821; sql: 252; lex: 216; ansic: 192; makefile: 91; python: 46
file content (244 lines) | stat: -rw-r--r-- 7,218 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org)             */
/*                                                                            */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
/* not use this file except in compliance with the License. You may obtain    */
/* a copy of the License at                                                   */
/*                                                                            */
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
/*                                                                            */
/* Unless required by applicable law or agreed to in writing, software        */
/* distributed under the License is distributed on an "AS IS" BASIS,          */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/* See the License for the specific language governing permissions and        */
/* limitations under the License.                                             */
/* -------------------------------------------------------------------------- */

#ifndef ONE_UNIT_TEST_H_
#define ONE_UNIT_TEST_H_

#include <string>
#include <iostream>
#include <stdlib.h>
#include <getopt.h>

#include <TestFixture.h>
#include <TestAssert.h>
#include <TestSuite.h>
#include <TestCaller.h>
#include <ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/XmlOutputter.h>
#include <unistd.h>

#include "SqlDB.h"
#include "SqliteDB.h"
#include "MySqlDB.h"

using namespace std;

/* ************************************************************************* */
/* ************************************************************************* */

class OneUnitTest : public CppUnit::TestFixture
{
protected:
    // Global flag to use either Sqlite or MySQL
    static bool mysql;

    static SqlDB * db;
    static string db_name;
    static string xml_name;

public:

    void create_db()
    {
        if (mysql)
        {
            db = new MySqlDB(  "localhost",0,
                                "oneadmin","oneadmin",db_name);

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

            ostringstream   oss;
            oss << "CREATE DATABASE " << db_name;
            db->exec(oss);

            ostringstream   oss2;
            oss2 << "use " << db_name;
            db->exec(oss2);
        }
        else
        {
            unlink(db_name.c_str());

            db = new SqliteDB(db_name);
        }
    };

    void delete_db()
    {
        if (mysql)
        {
            ostringstream   oss;
            oss << "DROP DATABASE IF EXISTS " << db_name;
            db->exec(oss);
        }
        else
        {
            unlink(db_name.c_str());
        }

        if ( db != 0 )
        {
            delete db;
        }
    };

    static SqlDB * get_db()
    {
        return db;
    }

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

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

// *****************************************************************************
// *****************************************************************************


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


    /*
     * Not a true main, but a static method that can be called from the
     * child classes' true main.
     * Options:
     *     s: run sqlite tests
     *     m: run mysql tests
     */
    static int main(int argc,
                    char ** argv,
                    CPPUNIT_NS::TestSuite* suite,
                    string xml_name = "output.xml")
    {

        // Option flags
        bool sqlite_flag = true;
        bool log_flag    = false;
        bool xml_flag    = false;

        // Long options
        const struct option long_opt[] =
        {
            { "sqlite", 0,  NULL,   's'},
            { "mysql",  0,  NULL,   'm'},
            { "log",    0,  NULL,   'l'},
            { "help",   0,  NULL,   'h'},
            { "xml",    0,  NULL,   'x'}
        };

        int c;
        while ((c = getopt_long (argc, argv, "smlhx", long_opt, NULL)) != -1)
            switch (c)
            {
                case 'm':
                    sqlite_flag = false;
                    break;
                case 'l':
                    log_flag = true;
                    break;
                case 'x':
                    xml_flag = true;
                    break;
                case 'h':
                    show_options();
                    return 0;
            }


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

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

        CppUnit::TextUi::TestRunner runner;
        ofstream                    outputFile;

        if( xml_flag )
        {
            outputFile.open(xml_name.c_str());
            CppUnit::XmlOutputter* outputter =
                    new CppUnit::XmlOutputter(&runner.result(), outputFile);

            runner.setOutputter(outputter);
        }

        runner.addTest( suite );

        if (sqlite_flag)
        {
            OneUnitTest::mysql = false;
            NebulaLog::log("Test", Log::INFO, "Running Sqlite tests...");
            cout << "\nRunning Sqlite tests...\n";
        }
        else
        {
            OneUnitTest::mysql = true;
            NebulaLog::log("Test", Log::INFO, "Running MySQL tests...");
            cout << "\nRunning MySQL tests...\n";
        }

        runner.run();

        if( xml_flag )
        {
            outputFile.close();
        }

        if (!log_flag)
            remove("test.log");

        NebulaLog::finalize_log_system();

        return 0;
    }
};


/* -----------------------------------------------------------------------------

int main(int argc, char ** argv)
{
    return OneUnitTest::main(argc, argv, TestClass::suite());
}

----------------------------------------------------------------------------- */


#endif // ONE_UNIT_TEST_H_