mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-19 03:12:55 +09:00
More tests for the Template & TemplateSQL
git-svn-id: http://svn.opennebula.org/one/branches/DEVELOPMENT/Testing@767 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
1 parent
147a0c287e
commit
83bf1a6cbb
3 files changed
+401
-29
No files matched your search
@@ -49,7 +49,8 @@ main_env.Append(LIBS=[
|
||||
'nebula_common',
|
||||
'cppunit',
|
||||
'dl',
|
||||
'pthread'
|
||||
'pthread',
|
||||
'sqlite3'
|
||||
])
|
||||
|
||||
# Compile flags
|
||||
@@ -59,8 +60,8 @@ main_env.Append(CPPFLAGS=[
|
||||
])
|
||||
|
||||
# Linking flags
|
||||
main_env.Append(LDFLAGS=["-g"])
|
||||
main_env.Append(LDFLAGS=["-g "])
|
||||
|
||||
main_env.Program('template.cc')
|
||||
#main_env.Program('template_sql.cc')
|
||||
main_env.Program('template_sql.cc')
|
||||
#
|
||||
+177
-26
@@ -11,21 +11,27 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
class TemplateTest : public CppUnit::TestFixture
|
||||
{
|
||||
private:
|
||||
Template *t, *tr;
|
||||
Template *t, *tr, *t1;
|
||||
|
||||
string test_ok;
|
||||
string test_ok_marshall;
|
||||
string test_ok_xml;
|
||||
string test_ok_str;
|
||||
|
||||
public:
|
||||
void setUp()
|
||||
{
|
||||
t = new Template();
|
||||
tr= new Template(true);
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
TemplateTest()
|
||||
{
|
||||
test_ok =
|
||||
"#This line is a comment\n"
|
||||
" # Other comment\n"
|
||||
@@ -64,12 +70,34 @@ public:
|
||||
"\n\tREQUIREMENTS=HOSTNAME = \"host*.com\"";
|
||||
}
|
||||
|
||||
~TemplateTest(){};
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void setUp()
|
||||
{
|
||||
char *error = 0;
|
||||
|
||||
t = new Template();
|
||||
|
||||
tr= new Template(true);
|
||||
tr->parse(test_ok,&error);
|
||||
|
||||
t1= new Template();
|
||||
t1->parse(test_ok,&error);
|
||||
}
|
||||
|
||||
void tearDown()
|
||||
{
|
||||
delete t;
|
||||
delete tr;
|
||||
delete t1;
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
/* ********************************************************************* */
|
||||
|
||||
void test_parser()
|
||||
{
|
||||
char * error = 0;
|
||||
@@ -87,52 +115,160 @@ public:
|
||||
CPPUNIT_ASSERT( rc == 0);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_marshall()
|
||||
{
|
||||
char * error = 0;
|
||||
int rc;
|
||||
string tmp;
|
||||
|
||||
rc = t->parse(test_ok,&error);
|
||||
|
||||
CPPUNIT_ASSERT( rc == 0);
|
||||
|
||||
t->marshall(tmp);
|
||||
t1->marshall(tmp);
|
||||
|
||||
CPPUNIT_ASSERT(test_ok_marshall == tmp);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_xml()
|
||||
{
|
||||
char * error = 0;
|
||||
int rc;
|
||||
string tmp;
|
||||
|
||||
rc = t->parse(test_ok,&error);
|
||||
|
||||
CPPUNIT_ASSERT( rc == 0);
|
||||
|
||||
t->to_xml(tmp);
|
||||
t1->to_xml(tmp);
|
||||
|
||||
CPPUNIT_ASSERT(test_ok_xml == tmp);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_str()
|
||||
{
|
||||
char * error = 0;
|
||||
int rc;
|
||||
string tmp;
|
||||
|
||||
rc = t->parse(test_ok,&error);
|
||||
|
||||
CPPUNIT_ASSERT( rc == 0);
|
||||
|
||||
t->to_str(tmp);
|
||||
t1->to_str(tmp);
|
||||
|
||||
CPPUNIT_ASSERT(test_ok_str == tmp);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_get()
|
||||
{
|
||||
vector<Attribute*> attrs;
|
||||
string *tmp;
|
||||
|
||||
CPPUNIT_ASSERT(t1->get("DISK",attrs) == 2 );
|
||||
|
||||
CPPUNIT_ASSERT(attrs[0]->type() == Attribute::VECTOR);
|
||||
|
||||
tmp = attrs[0]->to_xml();
|
||||
CPPUNIT_ASSERT( *tmp ==
|
||||
"<DISK><EXTRA>disk attribute </EXTRA><FILE>path1</FILE></DISK>");
|
||||
delete tmp;
|
||||
|
||||
CPPUNIT_ASSERT(attrs[1]->type() == Attribute::VECTOR);
|
||||
|
||||
tmp = attrs[1]->to_xml();
|
||||
CPPUNIT_ASSERT( *tmp ==
|
||||
"<DISK><EXTRA>str</EXTRA><FILE>path2</FILE><TYPE>disk</TYPE></DISK>");
|
||||
delete tmp;
|
||||
|
||||
CPPUNIT_ASSERT(t1->get("CPU",attrs) == 1 );
|
||||
|
||||
CPPUNIT_ASSERT(attrs[2]->type() == Attribute::SIMPLE);
|
||||
|
||||
tmp = attrs[2]->to_xml();
|
||||
CPPUNIT_ASSERT( *tmp == "<CPU>4</CPU>");
|
||||
delete tmp;
|
||||
|
||||
string sval;
|
||||
string sname = "REQUIREMENTS";
|
||||
|
||||
string iname = "MEMORY";
|
||||
int ival;
|
||||
|
||||
t1->get(iname,ival);
|
||||
t1->get(sname,sval);
|
||||
|
||||
CPPUNIT_ASSERT ( ival == 345 );
|
||||
CPPUNIT_ASSERT ( sval == "HOSTNAME = \"host*.com\"");
|
||||
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_remove()
|
||||
{
|
||||
vector<Attribute*> attrs;
|
||||
|
||||
string t1_xml;
|
||||
string rm_xml="<TEMPLATE><CPU>4</CPU><EMPTY_VAR></EMPTY_VAR><GRAPHICS>"
|
||||
"<PORT>12</PORT><VNC>127.0.0.1</VNC></GRAPHICS><MEMORY>345</MEMORY>"
|
||||
"<REQUIREMENTS>HOSTNAME = \"host*.com\"</REQUIREMENTS></TEMPLATE>";
|
||||
|
||||
t1->remove("DISK",attrs);
|
||||
t1->to_xml(t1_xml);
|
||||
|
||||
CPPUNIT_ASSERT(t1_xml == rm_xml);
|
||||
|
||||
delete attrs[0];
|
||||
delete attrs[1];
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_set()
|
||||
{
|
||||
string t1_xml="<TEMPLATE><CPU>4</CPU><DISK><EXTRA>disk attribute </EXTRA>"
|
||||
"<FILE>path1</FILE></DISK><DISK><EXTRA>str</EXTRA><FILE>path2</FILE>"
|
||||
"<TYPE>disk</TYPE></DISK><EMPTY_VAR></EMPTY_VAR><GRAPHICS>"
|
||||
"<PORT>12</PORT><VNC>127.0.0.1</VNC></GRAPHICS><MEMORY>345</MEMORY>"
|
||||
"<REQUIREMENTS>HOSTNAME = \"host*.com\"</REQUIREMENTS>"
|
||||
"<XTRA>44</XTRA></TEMPLATE>";
|
||||
string xml;
|
||||
|
||||
string nattr = "XTRA";
|
||||
string vattr = "44";
|
||||
|
||||
SingleAttribute *a = new SingleAttribute(nattr,vattr);
|
||||
|
||||
t1->set(a);
|
||||
t1->to_xml(xml);
|
||||
|
||||
CPPUNIT_ASSERT(t1_xml == xml);
|
||||
|
||||
nattr = "CPU";
|
||||
vattr = "5";
|
||||
|
||||
t1_xml="<TEMPLATE><CPU>4</CPU><CPU>5</CPU>"
|
||||
"<DISK><EXTRA>disk attribute </EXTRA>"
|
||||
"<FILE>path1</FILE></DISK><DISK><EXTRA>str</EXTRA><FILE>path2</FILE>"
|
||||
"<TYPE>disk</TYPE></DISK><EMPTY_VAR></EMPTY_VAR><GRAPHICS>"
|
||||
"<PORT>12</PORT><VNC>127.0.0.1</VNC></GRAPHICS><MEMORY>345</MEMORY>"
|
||||
"<REQUIREMENTS>HOSTNAME = \"host*.com\"</REQUIREMENTS>"
|
||||
"<XTRA>44</XTRA></TEMPLATE>";
|
||||
SingleAttribute *b = new SingleAttribute(nattr,vattr);
|
||||
|
||||
t1->set(b);
|
||||
t1->to_xml(xml);
|
||||
|
||||
CPPUNIT_ASSERT(t1_xml == xml);
|
||||
|
||||
string tr_xml="<TEMPLATE><CPU>5</CPU>"
|
||||
"<DISK><EXTRA>str</EXTRA><FILE>path2</FILE>"
|
||||
"<TYPE>disk</TYPE></DISK><EMPTY_VAR></EMPTY_VAR><GRAPHICS>"
|
||||
"<PORT>12</PORT><VNC>127.0.0.1</VNC></GRAPHICS><MEMORY>345</MEMORY>"
|
||||
"<REQUIREMENTS>HOSTNAME = \"host*.com\"</REQUIREMENTS></TEMPLATE>";
|
||||
SingleAttribute *c = new SingleAttribute(nattr,vattr);
|
||||
|
||||
tr->set(c);
|
||||
tr->to_xml(xml);
|
||||
|
||||
CPPUNIT_ASSERT(tr_xml == xml);
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
/* ********************************************************************* */
|
||||
|
||||
static CppUnit::TestSuite * suite()
|
||||
{
|
||||
CppUnit::TestSuite *ts=new CppUnit::TestSuite("Template Tests");
|
||||
@@ -153,10 +289,25 @@ public:
|
||||
"str() Test",
|
||||
&TemplateTest::test_str));
|
||||
|
||||
ts->addTest(new CppUnit::TestCaller<TemplateTest>(
|
||||
"get() Test",
|
||||
&TemplateTest::test_get));
|
||||
|
||||
ts->addTest(new CppUnit::TestCaller<TemplateTest>(
|
||||
"remove() Test",
|
||||
&TemplateTest::test_remove));
|
||||
|
||||
ts->addTest(new CppUnit::TestCaller<TemplateTest>(
|
||||
"set() Test",
|
||||
&TemplateTest::test_set));
|
||||
return ts;
|
||||
}
|
||||
};
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
CppUnit::TextUi::TestRunner tr;
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
#include "TemplateSQL.h"
|
||||
#include "SqliteDB.h"
|
||||
#include "Log.h"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <TestFixture.h>
|
||||
#include <TestAssert.h>
|
||||
#include <TestSuite.h>
|
||||
#include <TestCaller.h>
|
||||
#include <ui/text/TestRunner.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
/* --------------------------------------- */
|
||||
/* WRAPPER TO ACCESS TEMPLATESQL CLASS */
|
||||
/* --------------------------------------- */
|
||||
|
||||
class TSQL : public TemplateSQL
|
||||
{
|
||||
public:
|
||||
TSQL(const char * _table,
|
||||
int template_id = -1,
|
||||
bool replace = false,
|
||||
const char separator = '=',
|
||||
const char * xml_root = "TEMPLATE"):
|
||||
TemplateSQL(_table,template_id,replace,separator,xml_root){}
|
||||
|
||||
~TSQL(){}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
int insert(SqliteDB * db){return TemplateSQL::insert(db);}
|
||||
int update(SqliteDB * db){return TemplateSQL::update(db);}
|
||||
int select(SqliteDB * db){return TemplateSQL::select(db);}
|
||||
int drop (SqliteDB * db){return TemplateSQL::drop(db);}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
int replace_attribute(SqliteDB * db, Attribute * attr)
|
||||
{return TemplateSQL::replace_attribute(db,attr);}
|
||||
int insert_attribute(SqliteDB * db, Attribute * attr)
|
||||
{return TemplateSQL::insert_attribute(db,attr);}
|
||||
/* --------------------------------------------------------------------- */
|
||||
int id(){return TemplateSQL::id;};
|
||||
};
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
class TemplateSQLTest : public CppUnit::TestFixture
|
||||
{
|
||||
private:
|
||||
SqliteDB *db;
|
||||
TSQL *t;
|
||||
|
||||
string filename;
|
||||
|
||||
string template_ok;
|
||||
string template_xml;
|
||||
|
||||
static void log(
|
||||
const char * module,
|
||||
const Log::MessageType type,
|
||||
const ostringstream& message,
|
||||
const char * filename = 0,
|
||||
Log::MessageType clevel = Log::ERROR)
|
||||
{
|
||||
cerr << message.str() << endl;
|
||||
};
|
||||
|
||||
public:
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
TemplateSQLTest()
|
||||
{
|
||||
filename = "template_sql.db";
|
||||
|
||||
template_ok =
|
||||
"#This line is a comment\n"
|
||||
" # Other comment\n"
|
||||
"MEMORY=345 # more comments behind an attribute\n"
|
||||
" CPU = 4\n"
|
||||
"#------------------------------\n"
|
||||
"#Comments in the middle\n"
|
||||
"#------------------------------\n"
|
||||
" empty_var = \n"
|
||||
"REQUIREMENTS = \"HOSTNAME = \\\"host*.com\\\"\"\n"
|
||||
" DISK= [file = path1, extra = \"disk attribute \"]\n"
|
||||
"DISK =[ FILE = \"path2\", EXTRA = str, TYPE=disk]\n"
|
||||
" graphics = [\n"
|
||||
" VNC = \"127.0.0.1\",\n"
|
||||
" PORT = 12\n"
|
||||
" ]\n";
|
||||
|
||||
template_xml =
|
||||
"<TEMPLATE><CPU>4</CPU><DISK><EXTRA>disk "
|
||||
"attribute </EXTRA><FILE>path1</FILE></DISK><DISK><EXTRA>str"
|
||||
"</EXTRA><FILE>path2</FILE>"
|
||||
"<TYPE>disk</TYPE></DISK><EMPTY_VAR></EMPTY_VAR><GRAPHICS>"
|
||||
"<PORT>12</PORT><VNC>127.0.0.1</VNC></GRAPHICS><MEMORY>345</MEMORY>"
|
||||
"<REQUIREMENTS>HOSTNAME = \"host*.com\"</REQUIREMENTS></TEMPLATE>";
|
||||
}
|
||||
|
||||
~TemplateSQLTest()
|
||||
{
|
||||
unlink("template_sql.db");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void setUp()
|
||||
{
|
||||
db = new SqliteDB (filename , TemplateSQLTest::log);
|
||||
t = new TSQL("template");
|
||||
}
|
||||
|
||||
void tearDown()
|
||||
{
|
||||
delete db;
|
||||
delete t;
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
/* ********************************************************************* */
|
||||
|
||||
void test_bootstrap()
|
||||
{
|
||||
const char * db_bs = "CREATE TABLE template"
|
||||
" (id INTEGER, name TEXT, type INTEGER, value TEXT)";
|
||||
|
||||
CPPUNIT_ASSERT( db->exec(db_bs) == 0);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_insert()
|
||||
{
|
||||
char * error = 0;
|
||||
int rc;
|
||||
string tmp;
|
||||
|
||||
rc = t->parse(template_ok,&error);
|
||||
|
||||
if ( error != 0 )
|
||||
{
|
||||
cerr << error << endl;
|
||||
free(error);
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT( rc == 0);
|
||||
|
||||
CPPUNIT_ASSERT( t->insert(db) == 0 );
|
||||
CPPUNIT_ASSERT( t->id() == 0 );
|
||||
|
||||
CPPUNIT_ASSERT( t->insert(db) == 0 );
|
||||
CPPUNIT_ASSERT( t->id() == 1 );
|
||||
|
||||
CPPUNIT_ASSERT( t->insert(db) == 0 );
|
||||
CPPUNIT_ASSERT( t->id() == 2 );
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_select()
|
||||
{
|
||||
TSQL t2("template",1);
|
||||
string t2_xml;
|
||||
|
||||
CPPUNIT_ASSERT( t2.select(db) == 0 );
|
||||
|
||||
t2.to_xml(t2_xml);
|
||||
|
||||
CPPUNIT_ASSERT( t2_xml == template_xml );
|
||||
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
/* ********************************************************************* */
|
||||
|
||||
static CppUnit::TestSuite * suite()
|
||||
{
|
||||
CppUnit::TestSuite *ts=new CppUnit::TestSuite("TemplateSQL Tests");
|
||||
|
||||
ts->addTest(new CppUnit::TestCaller<TemplateSQLTest>(
|
||||
"db() Test",
|
||||
&TemplateSQLTest::test_bootstrap));
|
||||
|
||||
ts->addTest(new CppUnit::TestCaller<TemplateSQLTest>(
|
||||
"insert() Test",
|
||||
&TemplateSQLTest::test_insert));
|
||||
|
||||
ts->addTest(new CppUnit::TestCaller<TemplateSQLTest>(
|
||||
"update() Test",
|
||||
&TemplateSQLTest::test_select));
|
||||
|
||||
return ts;
|
||||
}
|
||||
};
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
CppUnit::TextUi::TestRunner tr;
|
||||
|
||||
tr.addTest(TemplateSQLTest::suite());
|
||||
tr.run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user