From a088af7d3eace4ea53ac8aa9ab5886f71ebb4ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 7 Apr 2011 15:37:03 +0200 Subject: [PATCH] Feature #487, #488: Templates now can't repeat name --- src/oca/java/test/all_tests.sh | 17 ++++++++++++ src/vm_template/VMTemplatePool.cc | 31 +++++++++++++++++++++- src/vm_template/test/VMTemplatePoolTest.cc | 25 +++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/oca/java/test/all_tests.sh b/src/oca/java/test/all_tests.sh index 3507e8b911..4b01299520 100755 --- a/src/oca/java/test/all_tests.sh +++ b/src/oca/java/test/all_tests.sh @@ -1,5 +1,22 @@ #!/bin/bash +if [ -z $ONE_LOCATION ]; then + echo "ONE_LOCATION not defined." + exit -1 +fi + +ONEDCONF_LOCATION="$ONE_LOCATION/etc/oned.conf" + +if [ -f $ONEDCONF_LOCATION ]; then + echo "$ONEDCONF_LOCATION has to be overwritten, move it to a safe place." + exit -1 +fi + +cp oned.conf $ONEDCONF_LOCATION + +export ONE_XMLRPC=http://localhost:2666/RPC2 + + ./test.sh ClusterTest ./test.sh HostTest ./test.sh ImageTest diff --git a/src/vm_template/VMTemplatePool.cc b/src/vm_template/VMTemplatePool.cc index aaa1feeb8e..a8ebe75179 100644 --- a/src/vm_template/VMTemplatePool.cc +++ b/src/vm_template/VMTemplatePool.cc @@ -30,13 +30,30 @@ int VMTemplatePool::allocate ( int * oid, string& error_str) { - VMTemplate * vm_template; + VMTemplate * vm_template; + VMTemplate * vm_template_aux = 0; + string name; + ostringstream oss; // ------------------------------------------------------------------------ // Build a new VMTemplate object // ------------------------------------------------------------------------ vm_template = new VMTemplate(-1, uid, user_name, template_contents); + // Check name + vm_template->get_template_attribute("NAME", name); + + if ( !name.empty() ) + { + // Check for duplicates + vm_template_aux = get(name,uid,false); + + if( vm_template_aux != 0 ) + { + goto error_duplicated; + } + } + // ------------------------------------------------------------------------ // Insert the Object in the pool // ------------------------------------------------------------------------ @@ -44,4 +61,16 @@ int VMTemplatePool::allocate ( *oid = PoolSQL::allocate(vm_template, error_str); return *oid; + + +error_duplicated: + oss << "NAME is already taken by TEMPLATE " + << vm_template_aux->get_oid() << "."; + + delete vm_template; + + *oid = -1; + error_str = oss.str(); + + return *oid; } diff --git a/src/vm_template/test/VMTemplatePoolTest.cc b/src/vm_template/test/VMTemplatePoolTest.cc index a1065fbc94..1e41967af2 100644 --- a/src/vm_template/test/VMTemplatePoolTest.cc +++ b/src/vm_template/test/VMTemplatePoolTest.cc @@ -109,6 +109,7 @@ class VMTemplatePoolTest : public PoolTest CPPUNIT_TEST ( update ); CPPUNIT_TEST ( get_using_name ); CPPUNIT_TEST ( wrong_get_name ); + CPPUNIT_TEST ( duplicates ); CPPUNIT_TEST ( public_attribute ); CPPUNIT_TEST ( dump ); CPPUNIT_TEST ( dump_where ); @@ -313,6 +314,30 @@ public: CPPUNIT_ASSERT( obj == 0 ); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + + void duplicates() + { + int rc, oid; + VMTemplatePoolFriend * tpool = static_cast(pool); + + // Allocate a template + rc = tpool->allocate(uids[0], templates[0], &oid); + CPPUNIT_ASSERT( oid == 0 ); + CPPUNIT_ASSERT( oid == rc ); + + // Try to allocate twice the same template, should fail + rc = tpool->allocate(uids[0], templates[0], &oid); + CPPUNIT_ASSERT( rc == -1 ); + CPPUNIT_ASSERT( oid == rc ); + + // Try again, this time with different uid. Should be allowed + rc = tpool->allocate(uids[1], templates[0], &oid); + CPPUNIT_ASSERT( rc >= 0 ); + CPPUNIT_ASSERT( oid == rc ); + } + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */