mirror of
https://github.com/OpenNebula/one.git
synced 2025-02-26 09:57:23 +03:00
Feature #407: Tests for clusters and some minor host-cluster dependencies moved
This commit is contained in:
parent
829a48e50e
commit
c61186bc3a
@ -218,8 +218,7 @@ if testing=='yes':
|
||||
'src/authm/test/SConstruct',
|
||||
'src/common/test/SConstruct',
|
||||
'src/host/test/SConstruct',
|
||||
# TODO
|
||||
# 'src/cluster/test/SConstruct',
|
||||
'src/cluster/test/SConstruct',
|
||||
'src/image/test/SConstruct',
|
||||
'src/lcm/test/SConstruct',
|
||||
'src/pool/test/SConstruct',
|
||||
|
@ -414,7 +414,8 @@ private:
|
||||
string _hostname="",
|
||||
string _im_mad_name="",
|
||||
string _vmm_mad_name="",
|
||||
string _tm_mad_name="");
|
||||
string _tm_mad_name="",
|
||||
string _cluster="");
|
||||
|
||||
virtual ~Host();
|
||||
|
||||
|
@ -140,6 +140,19 @@ public:
|
||||
return PoolSQL::dump(oss, "HOST_POOL", Host::table, where);
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds a set objects that satisfies a given condition
|
||||
* @param oids a vector with the oids of the objects.
|
||||
* @param the name of the DB table.
|
||||
* @param where condition in SQL format.
|
||||
*
|
||||
* @return 0 on success
|
||||
*/
|
||||
int search(vector<int>& oids, const string& where)
|
||||
{
|
||||
return PoolSQL::search(oids, Host::table, where);
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
|
@ -117,7 +117,7 @@ int ClusterPool::drop(Cluster * cluster)
|
||||
// Move the hosts assigned to the deleted cluster to the default one
|
||||
if( rc == 0 )
|
||||
{
|
||||
hpool->search(hids, Host::table, where);
|
||||
hpool->search(hids, where);
|
||||
|
||||
for ( hid_it=hids.begin() ; hid_it < hids.end(); hid_it++ )
|
||||
{
|
||||
|
@ -1,4 +1,4 @@
|
||||
# SConstruct for src/vm
|
||||
# SConstruct for src/cluster
|
||||
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) #
|
||||
|
366
src/cluster/test/ClusterPoolTest.cc
Normal file
366
src/cluster/test/ClusterPoolTest.cc
Normal file
@ -0,0 +1,366 @@
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* 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 "ClusterPool.h"
|
||||
#include "PoolTest.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
const string names[] = {"cluster_a", "Second cluster"};
|
||||
|
||||
const string xmls[] =
|
||||
{
|
||||
"<CLUSTER><ID>1</ID><NAME>cluster_a</NAME></CLUSTER>",
|
||||
"<CLUSTER><ID>2</ID><NAME>Second cluster</NAME></CLUSTER>"
|
||||
};
|
||||
|
||||
const string cluster_default =
|
||||
"<CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER>";
|
||||
|
||||
const string cluster_xml_dump =
|
||||
"<CLUSTER_POOL><CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER><CLUSTER><ID>1</ID><NAME>cluster_a</NAME></CLUSTER><CLUSTER><ID>3</ID><NAME>cluster_c</NAME></CLUSTER><CLUSTER><ID>4</ID><NAME>cluster_d</NAME></CLUSTER></CLUSTER_POOL>";
|
||||
|
||||
const string host_0_cluster =
|
||||
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>cluster_a</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
|
||||
|
||||
const string host_0_default =
|
||||
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE>"
|
||||
"<IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD>"
|
||||
"<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE>"
|
||||
"<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
|
||||
"<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU>"
|
||||
"<FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU>"
|
||||
"<USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU>"
|
||||
"<RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
|
||||
#include "NebulaTest.h"
|
||||
|
||||
class NebulaTestCluster: public NebulaTest
|
||||
{
|
||||
public:
|
||||
NebulaTestCluster():NebulaTest()
|
||||
{
|
||||
NebulaTest::the_tester = this;
|
||||
|
||||
need_host_pool = true;
|
||||
need_cluster_pool= true;
|
||||
}
|
||||
};
|
||||
|
||||
class ClusterPoolTest : public PoolTest
|
||||
{
|
||||
CPPUNIT_TEST_SUITE (ClusterPoolTest);
|
||||
|
||||
// Not all tests from PoolTest can be used. Because
|
||||
// of the initial default cluster added to the DB, the
|
||||
// oid_assignment would fail.
|
||||
CPPUNIT_TEST (get_from_cache);
|
||||
CPPUNIT_TEST (get_from_db);
|
||||
CPPUNIT_TEST (wrong_get);
|
||||
CPPUNIT_TEST (drop_and_get);
|
||||
|
||||
CPPUNIT_TEST (duplicates);
|
||||
CPPUNIT_TEST (set_cluster);
|
||||
CPPUNIT_TEST (remove_cluster);
|
||||
CPPUNIT_TEST (delete_cluster);
|
||||
CPPUNIT_TEST (dump);
|
||||
|
||||
CPPUNIT_TEST_SUITE_END ();
|
||||
|
||||
protected:
|
||||
|
||||
NebulaTestCluster * tester;
|
||||
HostPool * hpool;
|
||||
ClusterPool * cpool;
|
||||
|
||||
|
||||
|
||||
void bootstrap(SqlDB* db)
|
||||
{
|
||||
// setUp overwritten
|
||||
};
|
||||
|
||||
PoolSQL* create_pool(SqlDB* db)
|
||||
{
|
||||
// setUp overwritten
|
||||
return cpool;
|
||||
};
|
||||
|
||||
int allocate(int index)
|
||||
{
|
||||
int oid;
|
||||
string err;
|
||||
|
||||
return cpool->allocate(&oid, names[index], err);
|
||||
};
|
||||
|
||||
void check(int index, PoolObjectSQL* obj)
|
||||
{
|
||||
Cluster * cluster = static_cast<Cluster *>(obj);
|
||||
|
||||
CPPUNIT_ASSERT( obj != 0 );
|
||||
|
||||
string xml_str = "";
|
||||
string name = cluster->get_name();
|
||||
|
||||
CPPUNIT_ASSERT( name == names[index] );
|
||||
|
||||
// Get the xml
|
||||
cluster->to_xml(xml_str);
|
||||
|
||||
// A little help for debugging
|
||||
/*
|
||||
if( xml_str != xmls[index] )
|
||||
{
|
||||
cout << endl << xml_str << endl << "========"
|
||||
<< endl << xmls[index];
|
||||
}
|
||||
//*/
|
||||
CPPUNIT_ASSERT( xml_str == xmls[index]);
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Allocates a Host.
|
||||
*/
|
||||
void init_hp()
|
||||
{
|
||||
int oid;
|
||||
string err;
|
||||
|
||||
// Allocate a host
|
||||
oid = hpool->allocate(&oid, "Host one","im_mad","vmm_mad", "tm_mad", err);
|
||||
CPPUNIT_ASSERT(oid == 0);
|
||||
|
||||
hpool->clean();
|
||||
};
|
||||
|
||||
public:
|
||||
ClusterPoolTest()
|
||||
{
|
||||
xmlInitParser();
|
||||
};
|
||||
|
||||
~ClusterPoolTest()
|
||||
{
|
||||
xmlCleanupParser();
|
||||
};
|
||||
|
||||
void setUp()
|
||||
{
|
||||
create_db();
|
||||
|
||||
tester = new NebulaTestCluster();
|
||||
|
||||
Nebula& neb = Nebula::instance();
|
||||
neb.start();
|
||||
|
||||
hpool = neb.get_hpool();
|
||||
cpool = neb.get_cpool();
|
||||
|
||||
pool = cpool;
|
||||
};
|
||||
|
||||
void tearDown()
|
||||
{
|
||||
delete_db();
|
||||
|
||||
delete tester;
|
||||
};
|
||||
|
||||
/* ********************************************************************* */
|
||||
/* ********************************************************************* */
|
||||
|
||||
void duplicates()
|
||||
{
|
||||
int rc, oid;
|
||||
string err;
|
||||
|
||||
// Allocate a cluster.
|
||||
rc = cpool->allocate(&oid, names[1], err);
|
||||
CPPUNIT_ASSERT( oid == 1 );
|
||||
CPPUNIT_ASSERT( oid == rc );
|
||||
|
||||
// Try to allocate twice the same cluster, should fail
|
||||
rc = cpool->allocate(&oid, names[1], err);
|
||||
CPPUNIT_ASSERT( rc == -1 );
|
||||
CPPUNIT_ASSERT( oid == rc );
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
|
||||
void set_cluster()
|
||||
{
|
||||
Host* host;
|
||||
int rc;
|
||||
string xml_str, err;
|
||||
|
||||
init_hp();
|
||||
|
||||
host = hpool->get(0, false);
|
||||
CPPUNIT_ASSERT(host != 0);
|
||||
|
||||
rc = host->set_cluster("cluster_a");
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
hpool->update(host);
|
||||
|
||||
host->to_xml(xml_str);
|
||||
CPPUNIT_ASSERT( xml_str == host_0_cluster);
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
|
||||
void remove_cluster()
|
||||
{
|
||||
Host* host;
|
||||
|
||||
int rc;
|
||||
string xml_str;
|
||||
|
||||
init_hp();
|
||||
|
||||
host = hpool->get(0, false);
|
||||
CPPUNIT_ASSERT(host != 0);
|
||||
|
||||
// Set cluster
|
||||
rc = host->set_cluster("cluster_a");
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
hpool->update(host);
|
||||
|
||||
host->to_xml(xml_str);
|
||||
CPPUNIT_ASSERT( xml_str == host_0_cluster);
|
||||
|
||||
// Remove from the cluster
|
||||
rc = cpool->set_default_cluster(host);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
hpool->update(host);
|
||||
|
||||
// The host should have been moved to the default cluster
|
||||
|
||||
host = hpool->get(0, false);
|
||||
CPPUNIT_ASSERT(host != 0);
|
||||
|
||||
host->to_xml(xml_str);
|
||||
CPPUNIT_ASSERT( xml_str == host_0_default);
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
|
||||
void delete_cluster()
|
||||
{
|
||||
Host * host;
|
||||
Cluster * cluster;
|
||||
|
||||
int rc, oid;
|
||||
string xml_str;
|
||||
|
||||
init_hp();
|
||||
host = hpool->get(0, false);
|
||||
CPPUNIT_ASSERT(host != 0);
|
||||
|
||||
// Allocate a cluster
|
||||
oid = allocate(0);
|
||||
CPPUNIT_ASSERT(oid == 1);
|
||||
|
||||
cluster = cpool->get(1, false);
|
||||
|
||||
// Set cluster
|
||||
rc = host->set_cluster(cluster->get_name());
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
hpool->update(host);
|
||||
|
||||
host->to_xml(xml_str);
|
||||
CPPUNIT_ASSERT( xml_str == host_0_cluster);
|
||||
|
||||
// Delete the cluster
|
||||
rc = cpool->drop(cluster);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
// The host should have been moved to the default cluster
|
||||
host = hpool->get(0, false);
|
||||
host->to_xml(xml_str);
|
||||
/*
|
||||
if( xml_str != host_0_default )
|
||||
{
|
||||
cout << endl << xml_str << endl << "========"
|
||||
<< endl << host_0_default;
|
||||
}
|
||||
//*/
|
||||
CPPUNIT_ASSERT( xml_str == host_0_default);
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
|
||||
void dump()
|
||||
{
|
||||
Cluster * cluster;
|
||||
int oid, rc;
|
||||
ostringstream oss;
|
||||
string err;
|
||||
|
||||
// Allocate some clusters
|
||||
rc = cpool->allocate(&oid, "cluster_a", err);
|
||||
CPPUNIT_ASSERT( rc == 1 );
|
||||
|
||||
rc = cpool->allocate(&oid, "cluster_b", err);
|
||||
CPPUNIT_ASSERT( rc == 2 );
|
||||
|
||||
rc = cpool->allocate(&oid, "cluster_c", err);
|
||||
CPPUNIT_ASSERT( rc == 3 );
|
||||
|
||||
rc = cpool->allocate(&oid, "cluster_d", err);
|
||||
CPPUNIT_ASSERT( rc == 4 );
|
||||
|
||||
// Drop one of them
|
||||
cluster = cpool->get(2, false);
|
||||
CPPUNIT_ASSERT( cluster != 0 );
|
||||
|
||||
rc = cpool->drop(cluster);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
// dump the pool
|
||||
rc = cpool->dump(oss,"");
|
||||
/*
|
||||
if( oss.str() != cluster_xml_dump )
|
||||
{
|
||||
cout << endl << oss.str() << endl << "========"
|
||||
<< endl << cluster_xml_dump;
|
||||
}
|
||||
//*/
|
||||
CPPUNIT_ASSERT( oss.str() == cluster_xml_dump );
|
||||
}
|
||||
};
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
return PoolTest::main(argc, argv, ClusterPoolTest::suite());
|
||||
}
|
56
src/cluster/test/SConstruct
Normal file
56
src/cluster/test/SConstruct
Normal file
@ -0,0 +1,56 @@
|
||||
# --------------------------------------------------------------------------
|
||||
# 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('env')
|
||||
|
||||
env.Prepend(LIBS=[
|
||||
'nebula_cluster',
|
||||
'nebula_host',
|
||||
'nebula_pool',
|
||||
'nebula_template',
|
||||
'nebula_xml',
|
||||
'nebula_log',
|
||||
'nebula_common',
|
||||
'nebula_sql',
|
||||
|
||||
|
||||
### TODO: delete not needed
|
||||
'nebula_core_test',
|
||||
'nebula_host',
|
||||
'nebula_cluster',
|
||||
'nebula_xml',
|
||||
'nebula_vmm',
|
||||
'nebula_im',
|
||||
'nebula_rm',
|
||||
'nebula_tm',
|
||||
'nebula_um',
|
||||
'nebula_mad',
|
||||
'nebula_template',
|
||||
'nebula_vm',
|
||||
'nebula_vnm',
|
||||
'nebula_image',
|
||||
'nebula_pool',
|
||||
'nebula_hm',
|
||||
'nebula_authm',
|
||||
'nebula_common',
|
||||
'nebula_lcm',
|
||||
'nebula_dm',
|
||||
'nebula_sql',
|
||||
'nebula_log',
|
||||
'crypto'
|
||||
])
|
||||
|
||||
env.Program('test','ClusterPoolTest.cc')
|
@ -21,7 +21,6 @@
|
||||
#include <sstream>
|
||||
|
||||
#include "Host.h"
|
||||
#include "ClusterPool.h"
|
||||
#include "NebulaLog.h"
|
||||
|
||||
/* ************************************************************************ */
|
||||
@ -33,14 +32,15 @@ Host::Host(
|
||||
string _hostname,
|
||||
string _im_mad_name,
|
||||
string _vmm_mad_name,
|
||||
string _tm_mad_name):
|
||||
string _tm_mad_name,
|
||||
string _cluster):
|
||||
PoolObjectSQL(id,_hostname,-1,table),
|
||||
state(INIT),
|
||||
im_mad_name(_im_mad_name),
|
||||
vmm_mad_name(_vmm_mad_name),
|
||||
tm_mad_name(_tm_mad_name),
|
||||
last_monitored(0),
|
||||
cluster(ClusterPool::DEFAULT_CLUSTER_NAME),
|
||||
cluster(_cluster),
|
||||
host_template()
|
||||
{}
|
||||
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "HostPool.h"
|
||||
#include "HostHook.h"
|
||||
#include "NebulaLog.h"
|
||||
#include "ClusterPool.h"
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
@ -149,7 +150,8 @@ int HostPool::allocate (
|
||||
hostname,
|
||||
im_mad_name,
|
||||
vmm_mad_name,
|
||||
tm_mad_name);
|
||||
tm_mad_name,
|
||||
ClusterPool::DEFAULT_CLUSTER_NAME);
|
||||
|
||||
// Insert the Object in the pool
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user