diff --git a/src/vm/test/SConstruct b/src/vm/test/SConstruct new file mode 100644 index 0000000000..ee8c037638 --- /dev/null +++ b/src/vm/test/SConstruct @@ -0,0 +1,87 @@ +# -------------------------------------------------------------------------- +# 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/vm', + cwd + '/src/hm', + cwd + '/src/mad', + cwd + '/src/vnm', + cwd + '/src/um', + '/usr/include/openssl/', +]) + +main_env.Append(LIBS=[ + 'nebula_um', + 'nebula_vm', + 'nebula_mad', + 'nebula_hm', + 'nebula_vnm', + 'nebula_template', + 'nebula_pool', + 'nebula_common', + 'nebula_core', + 'cppunit', + 'dl', + 'pthread', + 'sqlite3', + 'crypto', + '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('VirtualMachinePoolTest.cc') + diff --git a/src/vm/test/VirtualMachinePoolTest.cc b/src/vm/test/VirtualMachinePoolTest.cc new file mode 100644 index 0000000000..dcce617a21 --- /dev/null +++ b/src/vm/test/VirtualMachinePoolTest.cc @@ -0,0 +1,286 @@ +/* -------------------------------------------------------------------------- */ +/* 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 "VirtualMachinePool.h" +#include "PoolTest.h" + +using namespace std; + +const int uids[] = {123, 261, 123}; + +const string names[] = {"VM one", "Second VM", "VM one"}; + +const string templates[] = +{ + "NAME = \"VM one\"\n" + "MEMORY = 128\n" + "CPU = 1", + + "NAME = \"Second VM\"\n" + "MEMORY = 256\n" + "CPU = 2", + + "NAME = \"VM one\"\n" + "MEMORY = 1024\n" + "CPU = 1" +}; + + +const string xmls[] = +{ + "0123VM one010000000000000000", + + "1261Second VM0" + "1000000000000<" + "/ETIME>0000", + + "0123VM one010000000000000000" +}; + + +// This xml dump result has the STIMEs modified to 0000000000 +const string xml_dump = + "01A userVM one" + "01000000000000000012B userSecond VM0200000000000" + "00000"; + +const string xml_dump_where = + "01A userVM one" + "010000000000000000"; + +const string replacement = "0000000000"; + + +/* ************************************************************************* */ +/* ************************************************************************* */ + +class VirtualMachinePoolTest : public PoolTest +{ + CPPUNIT_TEST_SUITE (VirtualMachinePoolTest); + + // Not all PoolTest tests can be used. Drop method isn't defined for + // the VirtualMachinePool. + CPPUNIT_TEST (oid_assignment); + CPPUNIT_TEST (get_from_cache); + CPPUNIT_TEST (get_from_db); + CPPUNIT_TEST (wrong_get); + + CPPUNIT_TEST (update); + CPPUNIT_TEST (dump); + CPPUNIT_TEST (dump_where); + CPPUNIT_TEST (history); + + CPPUNIT_TEST_SUITE_END (); + +protected: + + virtual string database_name() + { + return "vmachine_pool_test"; + }; + + virtual void bootstrap(SqlDB* db) + { + VirtualMachinePool::bootstrap(db); + }; + + virtual PoolSQL* create_pool(SqlDB* db) + { + // The VM pool needs a vector containing the vm hooks + vector vm_hooks; + return new VirtualMachinePool(db, vm_hooks); + }; + + virtual int allocate(int index) + { + int oid; + ((VirtualMachinePool*)pool)->allocate(uids[index], + templates[index], &oid, false); + return oid; + }; + + void check(int index, PoolObjectSQL* obj) + { + CPPUNIT_ASSERT( obj != 0 ); + + string xml_str = ""; + + // Get the xml and replace the STIME to 0, so we can compare it + ((VirtualMachine*)obj)->to_xml(xml_str); + xml_str.replace( xml_str.find("")+7, 10, replacement); + + CPPUNIT_ASSERT( ((VirtualMachine*)obj)->get_name() == names[index] ); + CPPUNIT_ASSERT( xml_str == xmls[index]); + }; + +public: + VirtualMachinePoolTest(){}; + + ~VirtualMachinePoolTest(){}; + + + /* ********************************************************************* */ + /* ********************************************************************* */ + + void update() + { + VirtualMachinePool* vmp = static_cast(pool); + VirtualMachine* vm; + int oid; + + string hostname = "hostname"; + string vm_dir = "vm_dir"; + string vmm_mad = "vm_mad"; + string tm_mad = "tm_mad"; + + // Allocate two VMs + oid = allocate(0); + CPPUNIT_ASSERT( oid != -1 ); + + // Get the first one, and change one of the templates attributes + vm = vmp->get(oid, true); + + CPPUNIT_ASSERT( vm != 0 ); + + string attribute = "MEMORY"; + string value = "1024"; + + vm->set_state(VirtualMachine::ACTIVE); + + // VirtualMachine object should be cached. Let's update the DB + vmp->update_template_attribute(vm, attribute, value); + + vmp->update(vm); + + //In memory (cache) check + string new_mem; + + vm->get_template_attribute("MEMORY",new_mem); + + CPPUNIT_ASSERT( new_mem == "1024" ); + CPPUNIT_ASSERT( vm->get_state() == VirtualMachine::ACTIVE ); + + vm->unlock(); + + //Now force access to DB + + pool->clean(); + vm = vmp->get(oid,false); + + new_mem.clear(); + + vm->get_template_attribute("MEMORY",new_mem); + + CPPUNIT_ASSERT( new_mem == "1024" ); + CPPUNIT_ASSERT( vm->get_state() == VirtualMachine::ACTIVE ); + }; + + void dump() + { + // TBD + } + + void dump_where() + { + //TBD + } + + void history() + { + VirtualMachine * vm; + VirtualMachinePool * vmp = static_cast(pool); + + int rc, oid; + + string hostname = "hostname"; + string new_hostname = "new_hostname"; + string vm_dir = "vm_dir"; + string vmm_mad = "vm_mad"; + string tm_mad = "tm_mad"; + + // Allocate a VM + oid = allocate(0); + CPPUNIT_ASSERT( oid != -1 ); + + vm = vmp->get(oid, true); + CPPUNIT_ASSERT( vm != 0 ); + + // Add a history item + vm->add_history(0, hostname, vm_dir, vmm_mad, tm_mad); + + rc = vmp->update_history(vm); + CPPUNIT_ASSERT( rc == 0 ); + + vm->add_history(0, new_hostname, vm_dir, vmm_mad, tm_mad); + + vm->set_reason(History::USER); + vm->set_previous_reason(History::ERROR); + + rc = vmp->update_history(vm); + rc = vmp->update_previous_history(vm); + + CPPUNIT_ASSERT( rc == 0 ); + + vm->unlock(); + + // Clean the DB cache + pool->clean(); + + vm = vmp->get(oid, false); + + CPPUNIT_ASSERT( vm != 0 ); + CPPUNIT_ASSERT( vm->hasHistory() == true ); + CPPUNIT_ASSERT( vm->hasPreviousHistory() == true ); + + CPPUNIT_ASSERT( vm->get_hostname() == new_hostname ); + CPPUNIT_ASSERT( vm->get_previous_hostname() == hostname ); + + CPPUNIT_ASSERT( vm->get_vmm_mad() == vmm_mad ); + CPPUNIT_ASSERT( vm->get_previous_vmm_mad() == vmm_mad ); + + CPPUNIT_ASSERT( vm->get_previous_reason() == History::ERROR ); + } +}; + + +/* ************************************************************************* */ +/* ************************************************************************* */ + +int main(int argc, char ** argv) +{ + return PoolTest::main(argc, argv, VirtualMachinePoolTest::suite()); +}