From 6b03c3ffab558773590b664d18cbaac9f384dc0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 14 May 2010 15:41:43 +0200 Subject: [PATCH] feature #212 #206: Tests for Scheduler's Host --- src/scheduler/src/pool/test/HostXMLTest.cc | 299 +++++++++++++++++++++ src/scheduler/src/pool/test/SConstruct | 1 + 2 files changed, 300 insertions(+) create mode 100644 src/scheduler/src/pool/test/HostXMLTest.cc diff --git a/src/scheduler/src/pool/test/HostXMLTest.cc b/src/scheduler/src/pool/test/HostXMLTest.cc new file mode 100644 index 0000000000..2c8a62e57e --- /dev/null +++ b/src/scheduler/src/pool/test/HostXMLTest.cc @@ -0,0 +1,299 @@ +/* -------------------------------------------------------------------------- */ +/* 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 + + +#include +#include +#include +#include +#include +#include +#include + +#include "ObjectXML.h" +#include "HostPoolXML.h" + +/* ************************************************************************* */ +/* ************************************************************************* */ + +class FriendHostPool : public HostPoolXML +{ +public: + FriendHostPool(Client* client):HostPoolXML(client){}; + + friend class HostXMLTest; + + static const string host_dump; + +protected: + int load_info(xmlrpc_c::value &result) + { + vector arrayData; + arrayData.push_back(xmlrpc_c::value_boolean(true)); + arrayData.push_back(xmlrpc_c::value_string(host_dump)); + + // Make an XML-RPC array out of it + xmlrpc_c::value_array array(arrayData); + result = array; + + return 0; + }; +}; + + +/* ************************************************************************* */ +/* ************************************************************************* */ + +class HostXMLTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( HostXMLTest ); + + CPPUNIT_TEST( initialization ); + CPPUNIT_TEST( get_capacity ); + CPPUNIT_TEST( test_capacity ); + CPPUNIT_TEST( add_capacity ); + + CPPUNIT_TEST_SUITE_END (); + +private: + FriendHostPool * hp; + +public: + void setUp() + { + xmlInitParser(); + + try + { + hp = new FriendHostPool(NULL); + } + catch(runtime_error& re) + { + cerr << re.what() << endl; + } + }; + + void tearDown() + { + xmlCleanupParser(); + + if (hp != 0) + { + delete hp; + } + }; + + HostXMLTest(){}; + + ~HostXMLTest(){}; + + /* ********************************************************************* */ + + void initialization() + { + int rc; + + + CPPUNIT_ASSERT( hp != 0 ); + + rc = hp->set_up(); + CPPUNIT_ASSERT( rc == 0 ); + + CPPUNIT_ASSERT( hp->objects.size() == 4 ); + CPPUNIT_ASSERT( hp->objects.count(0) == 1); + CPPUNIT_ASSERT( hp->objects.count(1) == 1); + CPPUNIT_ASSERT( hp->objects.count(2) == 0); + CPPUNIT_ASSERT( hp->objects.count(3) == 1); + CPPUNIT_ASSERT( hp->objects.count(4) == 1); + }; + + void get_capacity() + { + int rc; + + + CPPUNIT_ASSERT( hp != 0 ); + + rc = hp->set_up(); + CPPUNIT_ASSERT( rc == 0 ); + + CPPUNIT_ASSERT( hp->objects.size() == 4 ); + + // TODO QUÉ ES THRESHOLD? + int cpu, mem, threshold; + threshold = 100; + + HostXML* host = (HostXML*) hp->objects[4]; + CPPUNIT_ASSERT(host != 0); + + host->get_capacity(cpu, mem, threshold); + + CPPUNIT_ASSERT(mem == 384); + CPPUNIT_ASSERT(cpu == 180); + }; + + void test_capacity() + { + int rc; + + + CPPUNIT_ASSERT( hp != 0 ); + + rc = hp->set_up(); + CPPUNIT_ASSERT( rc == 0 ); + + CPPUNIT_ASSERT( hp->objects.size() == 4 ); + + HostXML* host = (HostXML*) hp->objects[4]; + CPPUNIT_ASSERT(host != 0); + + /* + 256" + 128" + 20" + 512" + 512" + 200" + + 256" + 384" + 180" + */ + + int n_test = 4; + + int disk[] = {256, 260, 0, 100}; + int mem[] = {384, 384, 0, 100}; + int cpu[] = {180, 180, 0, 200}; + bool result[] = {true, false, true, false}; + + for(int i = 0; i < n_test; i++) + { + bool test = host->test_capacity(cpu[i], mem[i], disk[i]); + CPPUNIT_ASSERT(test == result[i]); + } + + }; + + void add_capacity() + { + int rc; + + + CPPUNIT_ASSERT( hp != 0 ); + + rc = hp->set_up(); + CPPUNIT_ASSERT( rc == 0 ); + + CPPUNIT_ASSERT( hp->objects.size() == 4 ); + + HostXML* host = (HostXML*) hp->objects[4]; + CPPUNIT_ASSERT(host != 0); + + // add cpu, mem, disk + host->add_capacity(100, 128, 128); + + + int n_test = 2; + + int disk[] = {256, 128}; + int mem[] = {384, 256}; + int cpu[] = {180, 80}; + bool result[] = {false, true}; + + for(int i = 0; i < n_test; i++) + { + bool test = host->test_capacity(cpu[i], mem[i], disk[i]); + CPPUNIT_ASSERT(test == result[i]); + } + }; +}; + +/* ************************************************************************* */ +/* ************************************************************************* */ + +int main(int argc, char ** argv) +{ + CppUnit::TextUi::TestRunner runner; + + runner.addTest(HostXMLTest::suite()); + runner.run(); + + return 0; +} + +// ---------------------------------------------------------------------------- + +const string FriendHostPool::host_dump = + "0a0im_madvmm_madtm_mad000000" + "000000000000000000" + "1a name1im_madvmm_madtm_mad0000000000100000" + "000000002a_name3im_madvmm_madtm_mad0000000000200000000000003another " + "name2im_madvmm_mad" + "tm_mad000000000030000000000000" + "" +" 4" +" host" +" 0" +" im_mad" +" vmm_mad" +" tm_mad" +" 0000000000" +" " +" 4" +" 256" +" 128" +" 20" +" 512" +" 512" +" 200" +" 256" +" 384" +" 180" +" 256" +" 128" +" 20" +" 0" +" " +" " +""; diff --git a/src/scheduler/src/pool/test/SConstruct b/src/scheduler/src/pool/test/SConstruct index 93f9a95ded..5b4b99ab15 100644 --- a/src/scheduler/src/pool/test/SConstruct +++ b/src/scheduler/src/pool/test/SConstruct @@ -55,6 +55,7 @@ main_env.Append(LIBS=[ ]) main_env.Program('test_vm','VirtualMachineXMLTest.cc') +main_env.Program('test_host','HostXMLTest.cc') ################################################################################ # EXTRA CONFIGURATION