diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 6261771fd4..ca384af46b 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -243,9 +243,9 @@ int ImagePool::dump(ostringstream& oss, const string& where) static_cast(&oss)); cmd << "SELECT " << Image::table << ".*, user_pool.user_name FROM " - << Image::table << - " LEFT OUTER JOIN (SELECT oid, user_name FROM user_pool) " - << "AS user_pool ON " << Image::table << ".uid = user_pool.oid"; + << Image::table + << " LEFT OUTER JOIN (SELECT oid, user_name FROM user_pool) " + << "AS user_pool ON " << Image::table << ".uid = user_pool.oid"; if ( !where.empty() ) { diff --git a/src/image/test/ImagePoolTest.cc b/src/image/test/ImagePoolTest.cc index dc2b4de33f..c096dcbe5d 100644 --- a/src/image/test/ImagePoolTest.cc +++ b/src/image/test/ImagePoolTest.cc @@ -23,7 +23,7 @@ using namespace std; -const int uids[] = {122, 262, 127}; +const int uids[] = {0,1,2}; const string names[] = {"Image one", "Second Image", "The third image"}; @@ -48,20 +48,20 @@ const string templates[] = const string xmls[] = { - "0122Image one000000000000source_prefix/7e997f5fdc26712ac64eac8385fc81632b4bf02400", + "00Image one000000000000source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa19800", - "1262Second Image010000000000source_prefix/64c4e548575b3d40274190bad7a92e63b354f83e00", + "11Second Image010000000000source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f300", - "0127The third image000000000000source_prefix/c924d8d760da913871b4c4f1acbe36ba793ae5e500" + "02The third image000000000000source_prefix/e50b0c738be9d431475bf5859629e5580301a7d600" }; // This xml dump result has the STIMEs modified to 0000000000 const string xml_dump = - "0122Image one000000000000source_prefix/7e997f5fdc26712ac64eac8385fc81632b4bf024001262Second Image010000000000source_prefix/64c4e548575b3d40274190bad7a92e63b354f83e002127The third image000000000000source_prefix/c924d8d760da913871b4c4f1acbe36ba793ae5e500"; +"00one_user_testImage one000000000000source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa1980011A userSecond Image010000000000source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f30022B userThe third image000000000000source_prefix/e50b0c738be9d431475bf5859629e5580301a7d600"; const string xml_dump_where = - "1262Second Image010000000000source_prefix/64c4e548575b3d40274190bad7a92e63b354f83e002127The third image000000000000source_prefix/c924d8d760da913871b4c4f1acbe36ba793ae5e500"; +"00one_user_testImage one000000000000source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa1980011A userSecond Image010000000000source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f300"; const string replacement = "0000000000"; @@ -122,10 +122,40 @@ protected: ((Image*)obj)->to_xml(xml_str); xml_str.replace( xml_str.find("")+9, 10, replacement); + //cout << endl << xml_str << endl << xmls[index] << endl; + CPPUNIT_ASSERT( ((Image*)obj)->get_name() == names[index] ); CPPUNIT_ASSERT( xml_str == xmls[index]); }; + void set_up_user_pool() + { + + // The UserPool constructor checks if the DB contains at least + // one user, and adds one automatically from the ONE_AUTH file. + // So the ONE_AUTH environment is forced to point to a test one_auth + // file. + ostringstream oss; + + oss << getenv("PWD") << "/one_auth"; + setenv("ONE_AUTH", oss.str().c_str(), 1); + + UserPool::bootstrap(db); + UserPool * user_pool = new UserPool(db); + int uid_1, uid_2; + + string username_1 = "A user"; + string username_2 = "B user"; + + string pass_1 = "A pass"; + string pass_2 = "B pass"; + + user_pool->allocate(&uid_1, username_1, pass_1, true); + user_pool->allocate(&uid_2, username_2, pass_2, true); + + delete user_pool; + }; + public: ImagePoolTest(){}; @@ -348,21 +378,13 @@ public: "ORIGINAL_PATH = /tmp/image_test\n" "TYPE = WRONG\n", - "NAME = \"name B\"\n" - "TYPE = DATABLOCK\n" - "DESCRIPTION = \"This type doesn't need original_path\"\n", - - "NAME = \"name C\"\n" - "TYPE = CDROM\n" - "DESCRIPTION = \"This type needs original_path\"\n", - "NAME \"PARSE ERROR\"\n" "TYPE = WRONG\n", "END" }; - int results[] = { -1, -1, 0, -1, -2 }; + int results[] = { -1, -1, -2 }; int i = 0; while( templates[i] != "END" ) @@ -370,7 +392,8 @@ public: imp->allocate(0, templates[i], &rc); -//cout << endl << i << " - rc: " << rc << " expected: " << results[i] << endl; + //cout << endl << i << " - rc: " << rc << " expected: " + // << results[i] << endl; CPPUNIT_ASSERT( rc == results[i] ); @@ -389,6 +412,7 @@ public: VectorAttribute * disk; int oid; string value; + int index=0; disk = new VectorAttribute("DISK"); @@ -396,13 +420,14 @@ public: oid = allocate(0); img = imp->get(oid, false); - img->disk_attribute(&disk, 0); + CPPUNIT_ASSERT( img != 0 ); + CPPUNIT_ASSERT( oid == 0 ); + + img->disk_attribute(disk, &index); value = disk->vector_value("TARGET"); CPPUNIT_ASSERT( value == "hda" ); - - // clean up delete disk; value = ""; @@ -417,7 +442,7 @@ public: img = imp->get(oid, false); - img->disk_attribute(&disk, 0); + img->disk_attribute(disk, &index); value = disk->vector_value("TARGET"); CPPUNIT_ASSERT(value == "hdc"); @@ -436,7 +461,7 @@ public: img = imp->get(oid, false); - img->disk_attribute(&disk, 0); + img->disk_attribute(disk, &index); value = disk->vector_value("TARGET"); CPPUNIT_ASSERT(value == "hdd"); @@ -455,10 +480,10 @@ public: img = imp->get(oid, false); - img->disk_attribute(&disk, 2); + img->disk_attribute(disk, &index); value = disk->vector_value("TARGET"); - CPPUNIT_ASSERT(value == "sdf"); + CPPUNIT_ASSERT(value == "sde"); // clean up delete disk; @@ -475,7 +500,7 @@ public: VectorAttribute * disk; int oid; string value; - + int index = 0; // Allocate an OS type image oid = allocate(0); img = imp->get(oid, false); @@ -484,7 +509,7 @@ public: // A disk without a BUS attribute should not have it added. disk = new VectorAttribute("DISK"); - img->disk_attribute(&disk, 0); + img->disk_attribute(disk, &index); value = ""; value = disk->vector_value("BUS"); @@ -493,7 +518,7 @@ public: value = ""; value = disk->vector_value("SOURCE"); CPPUNIT_ASSERT( value == - "source_prefix/7e997f5fdc26712ac64eac8385fc81632b4bf024" ); + "source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198" ); // clean up delete disk; @@ -503,7 +528,7 @@ public: disk = new VectorAttribute("DISK"); disk->replace("BUS", "SCSI"); - img->disk_attribute(&disk, 0); + img->disk_attribute(disk, &index); value = disk->vector_value("BUS"); CPPUNIT_ASSERT( value == "SCSI" ); @@ -511,7 +536,7 @@ public: value = ""; value = disk->vector_value("SOURCE"); CPPUNIT_ASSERT( value == - "source_prefix/7e997f5fdc26712ac64eac8385fc81632b4bf024" ); + "source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198" ); // clean up delete disk; @@ -604,19 +629,22 @@ public: ostringstream oss; int rc; + string nan; + + set_up_user_pool(); allocate(0); allocate(1); allocate(2); - rc = imp->dump(oss, ""); + rc = imp->dump(oss,nan); CPPUNIT_ASSERT(rc == 0); string result = oss.str(); - result.replace(106, 10, replacement); - result.replace(346, 10, replacement); - result.replace(589, 10, replacement); + result.replace(138, 10, replacement); + result.replace(403, 10, replacement); + result.replace(671, 10, replacement); CPPUNIT_ASSERT( result == xml_dump ); } @@ -632,20 +660,21 @@ public: ostringstream oss; ostringstream where; + set_up_user_pool(); allocate(0); allocate(1); allocate(2); - where << "oid > 0"; + where << "uid < 2"; rc = imp->dump(oss, where.str()); CPPUNIT_ASSERT(rc == 0); string result = oss.str(); - result.replace(109, 10, replacement); - result.replace(352, 10, replacement); + result.replace(138, 10, replacement); + result.replace(403, 10, replacement); CPPUNIT_ASSERT( result == xml_dump_where ); }