1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

feature #192: Tests for VirtualMachinePool

This commit is contained in:
Carlos Martin and Ruben S. Montero 2010-04-24 17:17:52 +02:00 committed by Ruben S. Montero
parent d5eca2da00
commit 04e8a402a9
2 changed files with 373 additions and 0 deletions

87
src/vm/test/SConstruct Normal file
View File

@ -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')

View File

@ -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 <string>
#include <iostream>
#include <stdlib.h>
#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[] =
{
"<VM><ID>0</ID><UID>123</UID><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><ST"
"ATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ET"
"IME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX"
"><NET_RX>0</NET_RX><TEMPLATE><CPU>1</CPU><MEMORY>128</MEMORY><NAME>VM one<"
"/NAME><VMID>0</VMID></TEMPLATE></VM>",
"<VM><ID>1</ID><UID>261</UID><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL>"
"<STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0<"
"/ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET"
"_TX><NET_RX>0</NET_RX><TEMPLATE><CPU>2</CPU><MEMORY>256</MEMORY><NAME>Seco"
"nd VM</NAME><VMID>1</VMID></TEMPLATE></VM>",
"<VM><ID>0</ID><UID>123</UID><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><ST"
"ATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ET"
"IME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX"
"><NET_RX>0</NET_RX><TEMPLATE><CPU>1</CPU><MEMORY>1024</MEMORY><NAME>VM one"
"</NAME><VMID>0</VMID></TEMPLATE></VM>"
};
// This xml dump result has the STIMEs modified to 0000000000
const string xml_dump =
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one"
"</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><ST"
"IME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</ME"
"MORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX></VM><VM><ID>1</ID><U"
"ID>2</UID><USERNAME>B user</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</L"
"AST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME>"
"<ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_"
"TX>0</NET_TX><NET_RX>0</NET_RX></VM></VM_POOL>";
const string xml_dump_where =
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one"
"</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><ST"
"IME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</ME"
"MORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX></VM></VM_POOL>";
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<const Attribute *> 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("<STIME>")+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<VirtualMachinePool*>(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<VirtualMachinePool*>(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());
}