diff --git a/src/template/test/SConstruct b/src/template/test/SConstruct index 31b3daa005..77a572f3e1 100644 --- a/src/template/test/SConstruct +++ b/src/template/test/SConstruct @@ -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') # diff --git a/src/template/test/template.cc b/src/template/test/template.cc index 339bfa9ff3..15fdcf9574 100644 --- a/src/template/test/template.cc +++ b/src/template/test/template.cc @@ -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 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 attribute path1"); + delete tmp; + + CPPUNIT_ASSERT(attrs[1]->type() == Attribute::VECTOR); + + tmp = attrs[1]->to_xml(); + CPPUNIT_ASSERT( *tmp == + "strpath2disk"); + delete tmp; + + CPPUNIT_ASSERT(t1->get("CPU",attrs) == 1 ); + + CPPUNIT_ASSERT(attrs[2]->type() == Attribute::SIMPLE); + + tmp = attrs[2]->to_xml(); + CPPUNIT_ASSERT( *tmp == "4"); + 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 attrs; + + string t1_xml; + string rm_xml=""; + + 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=""; + 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=""; + SingleAttribute *b = new SingleAttribute(nattr,vattr); + + t1->set(b); + t1->to_xml(xml); + + CPPUNIT_ASSERT(t1_xml == xml); + + string tr_xml=""; + 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( + "get() Test", + &TemplateTest::test_get)); + + ts->addTest(new CppUnit::TestCaller( + "remove() Test", + &TemplateTest::test_remove)); + + ts->addTest(new CppUnit::TestCaller( + "set() Test", + &TemplateTest::test_set)); return ts; } }; +/* ************************************************************************* */ +/* ************************************************************************* */ +/* ************************************************************************* */ + int main(int argc, char ** argv) { CppUnit::TextUi::TestRunner tr; diff --git a/src/template/test/template_sql.cc b/src/template/test/template_sql.cc new file mode 100644 index 0000000000..008a220502 --- /dev/null +++ b/src/template/test/template_sql.cc @@ -0,0 +1,220 @@ +#include "TemplateSQL.h" +#include "SqliteDB.h" +#include "Log.h" + +#include +#include +#include + +#include +#include +#include +#include +#include + +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 = + ""; + } + + ~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( + "db() Test", + &TemplateSQLTest::test_bootstrap)); + + ts->addTest(new CppUnit::TestCaller( + "insert() Test", + &TemplateSQLTest::test_insert)); + + ts->addTest(new CppUnit::TestCaller( + "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; +}