diff --git a/src/host/test/HostPoolTest.cc b/src/host/test/HostPoolTest.cc new file mode 100755 index 0000000000..a3d0634903 --- /dev/null +++ b/src/host/test/HostPoolTest.cc @@ -0,0 +1,318 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2010, 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. */ +/* -------------------------------------------------------------------------- */ + +#include +#include +#include + +#include "HostPool.h" +#include "PoolTest.h" + +using namespace std; + +const string im_mad = "im_mad"; +const string vmm_mad = "vmm_mad"; +const string tm_mad = "tm_mad"; + +const string names[] = {"Host one", "Second host"}; + +const string xmls[] = +{ + "0Host one0" + "im_madvmm_madtm_mad" + "00000000000" + "000" + "000" + "000" + "000" + "0", + + "1Second host0" + "im_madvmm_madtm_mad" + "00000000001" + "000" + "000" + "000" + "000" + "0" +}; + +// This xml dump result has the LAST_MON_TIMEs modified to 0000000000 +const string xml_dump = + "0a0im_madvmm_madtm_mad000000" + "000000000000000000" + "1a name0im_madvmm_madtm_mad0000000000100000" + "000000002a_name0im_madvmm_madtm_mad0000000000200000000000003another " + "name0im_madvmm_mad" + "tm_mad0000000000300000000000004host0im_madvmm_madtm_mad" + "00000000004" + "000" + "0000" + "000000"; + +const string xml_dump_like_a = + "0a0im_madvmm_madtm_mad000000" + "000000000000000000" + "1a name0im_madvmm_madtm_mad0000000000100000" + "000000002a_name0im_madvmm_madtm_mad0000000000200000000000003another " + "name0im_madvmm_mad" + "tm_mad000000000030000000000000"; + +const string replacement = "0000000000"; + + +/* ************************************************************************* */ +/* ************************************************************************* */ + +class HostPoolTest : public PoolTest +{ + CPPUNIT_TEST_SUITE (HostPoolTest); + + ALL_POOLTEST_CPPUNIT_TESTS(); + + CPPUNIT_TEST (update); + CPPUNIT_TEST (dump); + CPPUNIT_TEST (dump_where); + CPPUNIT_TEST (discover); + + CPPUNIT_TEST_SUITE_END (); + +protected: + + string database_name() + { + return "host_pool_test"; + }; + + void bootstrap(SqlDB* db) + { + HostPool::bootstrap(db); + }; + + PoolSQL* create_pool(SqlDB* db) + { + return new HostPool(db); + }; + + int allocate(int index) + { + int oid; + ((HostPool*)pool)->allocate(&oid, names[index], im_mad, vmm_mad, tm_mad); + return oid; + }; + + void check(int index, PoolObjectSQL* obj) + { + Host * host = static_cast(obj); + + CPPUNIT_ASSERT( obj != 0 ); + + string xml_str = ""; + string name = host->get_hostname(); + + CPPUNIT_ASSERT( name == names[index] ); + + // Get the xml and replace the LAST_MON_TIME to 0, so we can compare + // it with a prepared string. + host->to_xml(xml_str); + xml_str.replace( xml_str.find("")+15, 10, "0000000000"); + + CPPUNIT_ASSERT( xml_str == xmls[index]); + + }; + + +public: + HostPoolTest(){}; + + ~HostPoolTest(){}; + + + /* ********************************************************************* */ + /* ********************************************************************* */ + + + void update() + { + int oid_1 = allocate(0); + + Host* host = ((HostPool*)pool)->get(oid_1, true); + + // Host object should be cached. Let's update its status + host->set_state(Host::DISABLED); + pool->update(host); + + host->unlock(); + + host = ((HostPool*)pool)->get(oid_1,false); + CPPUNIT_ASSERT( host->get_state() == Host::DISABLED ); + + //Now force access to DB + + pool->clean(); + host = ((HostPool*)pool)->get(oid_1,false); + + CPPUNIT_ASSERT( host->get_state() == Host::DISABLED ); + }; + + void dump() + { + string names[] = {"a", "a name", "a_name", "another name", "host"}; + int rc, oid; + + for(int i=0; i<5; i++) + { + ((HostPool*)pool)->allocate(&oid, names[i], im_mad, vmm_mad, tm_mad); + } + + ostringstream oss; + + rc = ((HostPool*)pool)->dump(oss, ""); + CPPUNIT_ASSERT(rc == 0); + + // To be able to compare one string to another, the monitoring times + // have to be changed + string result = oss.str(); + result.replace( 142, 10, replacement); + result.replace( 648, 10, replacement); + result.replace(1154, 10, replacement); + result.replace(1666, 10, replacement); + result.replace(2170, 10, replacement); + + CPPUNIT_ASSERT( result == xml_dump ); + } + + void dump_where() + { + string names[] = {"a", "a name", "a_name", "another name", "host"}; + int rc, oid; + + for(int i=0; i<5; i++) + { + ((HostPool*)pool)->allocate(&oid, names[i], im_mad, vmm_mad, tm_mad); + } + + + ostringstream oss; + rc = ((HostPool*)pool)->dump(oss, "host_name LIKE 'a%'"); + CPPUNIT_ASSERT(rc == 0); + + // To be able to compare one string to another, the monitoring times + // have to be changed + string result = oss.str(); + result.replace( 142, 10, replacement); + result.replace( 648, 10, replacement); + result.replace(1154, 10, replacement); + result.replace(1666, 10, replacement); + + + CPPUNIT_ASSERT( result == xml_dump_like_a ); + } + + void discover() + { + int rc, oid, i; + + map dh; + map::iterator it; + + Host * host; + HostPool * hp = static_cast(pool); + ostringstream oss; + + for(i=0, oss.str(""); i<20; i++,oss.str("")) + { + oss << "host" << i; + + hp->allocate(&oid, oss.str().c_str(), im_mad, vmm_mad, tm_mad); + CPPUNIT_ASSERT(oid == i); + + if (i >=8 ) + { + host = hp->get(oid, false); + host->disable(); + hp->update(host); + } + } + + // Discover the enabled hosts + rc = hp->discover(&dh); + CPPUNIT_ASSERT(rc == 0); + CPPUNIT_ASSERT(dh.size() == 8); + + for(i=0,it=dh.begin(),oss.str("");it!=dh.end();it++,i++,oss.str("")) + { + CPPUNIT_ASSERT(it->first == i); + CPPUNIT_ASSERT(it->second == im_mad); + } + } +}; + +/* ************************************************************************* */ +/* ************************************************************************* */ +/* ************************************************************************* */ + +int main(int argc, char ** argv) +{ + return PoolTest::main(argc, argv, HostPoolTest::suite()); +} diff --git a/src/host/test/SConstruct b/src/host/test/SConstruct new file mode 100755 index 0000000000..d290f4a63e --- /dev/null +++ b/src/host/test/SConstruct @@ -0,0 +1,77 @@ +# -------------------------------------------------------------------------- +# Copyright 2002-2010, 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. +# -------------------------------------------------------------------------- + +import os +import sys +import shutil +sys.path.append("../../../share/scons") +from lex_bison import * + +# This is the absolute path where the project is located +cwd="../../../" + +# Environment that will be applied to each scons child +main_env=Environment() +main_env['ENV']['PATH']=os.environ['PATH'] + +# Add builders for flex and bison +add_lex(main_env) +add_bison(main_env) + +# Include dirs +main_env.Append(CPPPATH=[ + cwd + '/include', + cwd + '/include/test', + '/usr/include/cppunit/', +]) + +# Library dirs +main_env.Append(LIBPATH=[ + cwd + '/src/common', + cwd + '/src/nebula', + cwd + '/src/pool', + cwd + '/src/template', + cwd + '/src/host', +]) + +main_env.Append(LIBS=[ + 'nebula_host', + 'nebula_template', + 'nebula_pool', + 'nebula_common', + 'nebula_core', + 'cppunit', + 'dl', + 'pthread', + 'sqlite3', + 'mysqlclient', +]) + +# MYSQL, puesto a mano +main_env.Append(LIBPATH=["/usr/lib/mysql"]) +main_env.Append(CPPPATH=["/usr/include/mysql"]) + +# Compile flags +main_env.Append(CPPFLAGS=[ + "-g", + "-Wall" +]) + +# Linking flags +main_env.Append(LDFLAGS=["-g "]) + +main_env.Program('HostPoolTest.cc') +