Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/BdbPud/testBdbPud.cc

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: testBdbPud.cc,v 1.3 2002/01/18 00:40:41 becla Exp $
00004 //
00005 // Description:
00006 //      Test for BdbPud
00007 //
00008 // Environment:
00009 //      Software developed for the BaBar Detector at the SLAC B-Factory
00010 //
00011 // Author List:
00012 //      Jacek Becla                Original Author
00013 //
00014 // Copyright Information:
00015 //      Copyright (C) 2001      Stanford Linear Accelerator Center
00016 //
00017 //------------------------------------------------------------------------------
00018 
00019 
00020 #include "BdbPud/BdbPud.hh"
00021 
00022 #include <assert.h>
00023 #include <iostream.h>
00024 #include <strstream.h>
00025 #include <unistd.h> /* unlink */
00026 #include <time.h>
00027 
00028 const char* host    = 0;
00029 int         port    = 0;
00030 BdbPud      thePud;
00031 
00032 
00033 int test_checkExist()
00034 {
00035     const char* fn = "test_checkExist: ";
00036 
00037     const char* path = tempnam("/tmp", "tstR");
00038     // check if ok when file does not exist
00039     cout << fn << "checking if " << path << " exist (it does not)" << endl;
00040     if ( BdbcError != thePud.checkExist(path, host, port) ) {
00041         return 1;
00042     }
00043 
00044     cout << fn << "creating a temp file " << path << endl;
00045     if ( BdbcSuccess != thePud.createFile(path, host, port) ) {
00046         return 2;
00047     }
00048 
00049     // check if ok when file exists
00050     cout << fn << "checking if " << path << " exist (it does)" << endl;
00051     if ( BdbcSuccess != thePud.checkExist(path, host, port) ) {
00052         return 3;
00053     }
00054 
00055     cout << fn << "finished ok" << endl;
00056     return 0;   
00057 }
00058 
00059 
00060 
00061 
00062 int test_createFile()
00063 {
00064     const char* fn = "test_createFile: ";
00065 
00066     const char* path = tempnam("/tmp", "tstR");
00067     cout << fn << "creating a file " << path << endl;
00068     if ( BdbcSuccess != thePud.createFile(path, host, port) ) {
00069         return 1;
00070     }
00071 
00072     // this should fail, we have just created the file
00073     cout << fn << "creating a file " << path << " this file should already exists, it should fail" << endl;
00074     if ( BdbcError != thePud.createFile(path, host, port) ) {
00075         return 2;
00076     }
00077 
00078     cout << fn << "finished ok" << endl;
00079     return 0;
00080 }
00081 
00082 
00083 
00084 
00085 int test_rmFile()
00086 {
00087     const char* fn = "test_rmFile: ";
00088 
00089     const char* path = tempnam("/tmp", "tstR");
00090 
00091     cout << fn << "creating a file " << path << endl;
00092     if ( BdbcSuccess != thePud.createFile(path, host, port) ) {
00093         return 1;
00094     }
00095     cout << fn << "removing a file " << path << " (file exists)" << endl;
00096     if ( BdbcSuccess != thePud.rmFile(path, host, port) ) {
00097         return 2;
00098     }
00099 
00100     // this should fail, we have just removed that file
00101     cout << fn << "removing a file " << path << " (it should fail, file does not exist)" << endl;
00102     if ( BdbcError != thePud.rmFile(path, host, port) ) {
00103         return 3;
00104     }
00105 
00106     cout << fn << "finished ok" << endl;
00107     return 0;
00108 }
00109 
00110 
00111 
00112 int test_chmodRead()
00113 {
00114     const char* fn = "test_chmodRead: ";
00115 
00116     const char* path = tempnam("/tmp", "tstR");
00117 
00118     cout << fn << "creating a file " << path << endl;
00119     if ( BdbcSuccess != thePud.createFile(path, host, port) ) {
00120         return 1;
00121     }
00122 
00123     cout << fn << "changing mode of " << path << " to readonly" << endl;
00124     if ( BdbcSuccess != thePud.chmodRead(path, host, port) ) {
00125         return 2;
00126     }
00127     cout << "check mode of the file " << path << ", should be -r--r--r-- ok....(y/n) " << endl;
00128     char c; cin >> c;
00129     int ret = 0;
00130     if ( c != 'y' ) {
00131         ret = 1;
00132     }
00133     thePud.rmFile(path, host, port);
00134 
00135     if ( ret == 0 ) {
00136         cout << fn << "finished ok" << endl;
00137     }
00138     
00139     return ret;    
00140 }
00141 
00142 
00143 int test_chmodWrite()
00144 {
00145     const char* fn = "test_chmodWrite: ";
00146 
00147     const char* path = tempnam("/tmp", "tstR");
00148 
00149     cout << fn << "creating a file " << path << endl;    
00150     if ( BdbcSuccess != thePud.createFile(path, host, port) ) {
00151         return 1;
00152     }
00153 
00154     cout << fn << "changing mode of " << path << " to readwrite" << endl;
00155     if ( BdbcSuccess != thePud.chmodWrite(path, host, port) ) {
00156         return 2;
00157     }
00158     cout << "check mode of the file " << path << ", should be -rw-rw-r-- ok...(y/n)" << endl;
00159     char c; cin >> c;
00160     int ret = 0;
00161     if ( c != 'y' ) {
00162         ret = 1;
00163     }    
00164 
00165     thePud.rmFile(path, host, port);
00166     if ( ret == 0 ) {
00167         cout << fn << "finished ok" << endl;
00168     }
00169     return ret;
00170 }
00171 
00172 
00173 int test_copyFile()
00174 {
00175     const char* fn = "test_copyFile: ";
00176 
00177     const char* source = tempnam("/tmp", "tstR");
00178     const char* dest   = tempnam("/tmp", "tstR");
00179 
00180     cout << fn << "creating a file " << source << endl;
00181     if ( BdbcSuccess != thePud.createFile(source, host, port) ) {
00182         return 1;
00183     }
00184     cout << fn << "copying a file from " << source << " to " << dest << endl;    
00185     if ( BdbcSuccess != thePud.copyFile(source, dest, host, port) ) {
00186         return 2;
00187     }
00188 
00189     cout << fn << "removing a file " << source << endl;    
00190     if ( BdbcSuccess != thePud.rmFile(source, host, port) ) {
00191         return 3;
00192     }
00193 
00194     cout << fn << "removing a file " << dest << endl;    
00195     if ( BdbcSuccess != thePud.rmFile(dest, host, port) ) {
00196         return 4;
00197     }
00198     
00199     cout << fn << "finished ok" << endl;
00200     return 0;
00201 }
00202 
00203 
00204 
00205 int test_moveFile()
00206 {
00207     const char* fn = "test_moveFile: ";
00208 
00209     const char* source = tempnam("/tmp", "tstR");
00210     const char* dest   = tempnam("/tmp", "tstR");
00211 
00212     cout << fn << "creating a file " << source << endl;
00213     if ( BdbcSuccess != thePud.createFile(source, host, port) ) {
00214         return 1;
00215     }
00216     cout << fn << "moving a file from " << source << " to " << dest << endl;    
00217     if ( BdbcSuccess != thePud.moveFile(source, dest, host, port) ) {
00218         return 2;
00219     }
00220 
00221     cout << fn << "removing a file " << source << "(this should not exist)" << endl;    
00222     if ( BdbcError != thePud.rmFile(source, host, port) ) {
00223         return 3;
00224     }
00225 
00226     cout << fn << "removing a file " << dest << endl;    
00227     if ( BdbcSuccess != thePud.rmFile(dest, host, port) ) {
00228         return 4;
00229     }
00230     
00231     cout << fn << "finished ok" << endl;
00232     return 0;
00233 }
00234 
00235 
00236 
00237 int test_getFileSize()
00238 {
00239     const char* fn = "test_getFileSize: ";
00240 
00241     const char* path = tempnam("/tmp", "tstL");
00242     off_t size = 0;
00243     cout << fn << "checking size of notexistent file" << endl;
00244     if ( BdbcError != thePud.getFileSize(path, size, host, port) ) {
00245         return 1;
00246     }
00247 
00248     path = "/tmp/testSize";
00249     cout << "test_getFileSize: this test expects a file: " << path << " (size=7)" << endl;
00250     size = 0;
00251     if ( BdbcSuccess != thePud.getFileSize(path, size, host, port) ) {
00252         return 2;
00253     }
00254     if ( size != 7 ) {
00255         return 3;
00256     }
00257     
00258     cout << fn << "finished ok" << endl;
00259     return 0;
00260 }
00261 
00262 
00263 int test_getFDFilePath()
00264 {
00265     const char* fn = "test_getFDFilePath: ";
00266 
00267     char buf[256]; char* fdPath = buf;
00268     const char* path = "/tmp/BaBar.BOOT";
00269     cout << "test_getFDFilePath: This test expects a valid boot file: " << path << endl;
00270     if ( BdbcSuccess != thePud.getFDFilePath(path, fdPath, host, port) ) {
00271         return 1;
00272     }
00273     cout << "test_getFDFilePath: got: " << fdPath << endl;
00274     char c;
00275     cout << "Is this correct? (y/n)";
00276     cin >> c;
00277     if ( c != 'y' ) {
00278         return 2;
00279     }
00280 
00281     cout << fn << "finished ok" << endl;
00282     return 0;
00283 }
00284 
00285 
00286 
00287 int test_getFDHost()
00288 {
00289     const char* fn = "test_getFDHost: ";
00290 
00291     char buf[256]; char* fdHost = buf;
00292     const char* path = "/tmp/BaBar.BOOT";
00293     cout << "test_getFDHost: This test expects a file: " << path << endl;
00294     if ( BdbcSuccess != thePud.getFDHost(path, fdHost, host, port) ) {
00295         return 1;
00296     }
00297     cout << "test_getFDHost: got: " << fdHost << endl;
00298     char c;
00299     cout << "Is this correct? (y/n)";
00300     cin >> c;
00301     if ( c != 'y' ) {
00302         return 2;
00303     }
00304     cout << fn << "finished ok" << endl;
00305     return 0;
00306 }
00307 
00308 
00309 int test_getModificationDate()
00310 {
00311     const char* fn = "test_getModificationDate: ";
00312 
00313     const char* path = tempnam("/tmp", "tstR");
00314     time_t stime = 0;
00315 
00316     cout << fn << "checking mode date of nonexisting file" << endl;
00317     if ( BdbcError != thePud.getModificationDate(path, stime, host, port) ) {
00318         return 1;
00319     }
00320 
00321     cout << fn << "creating a temp file " << path << endl;
00322     if ( BdbcSuccess != thePud.createFile(path, host, port) ) {
00323         return 2;
00324     }
00325     stime = 0;
00326     cout << fn << "checking mode date of just created file" << endl;
00327     if ( BdbcSuccess != thePud.getModificationDate(path, stime, host, port) ) {
00328         return 3;
00329     }
00330     thePud.rmFile(path, host, port);
00331     
00332     struct tm* ttt = localtime(&stime);
00333     cout << "test_getModificationDate: " << ttt->tm_hour 
00334          << ":" << ttt->tm_min << ":" << ttt->tm_sec << endl;
00335 
00336     cout << fn << "finished ok" << endl;
00337     return 0;
00338 }
00339 
00340 
00341 int test_copyDb()
00342 {
00343     const char* fn = "test_copyDb: ";
00344 
00345     thePud.copyDb("/tmp/empty.bdb", "/tmp/destinFile.bdb", "/tmp/dummy.Boot", host, port);
00346     cout << "check pud log file. ok? (y/n)" << endl;
00347     char c; cin >> c;
00348     if ( c != 'y' ) {
00349         return 1;
00350     }
00351     cout << fn << "finished ok" << endl;
00352     return 0;
00353 }
00354 
00355 
00356 
00357 
00358 int test_mkDir()
00359 {
00360     const char* fn = "test_mkDir: ";
00361 
00362     const char* path = tempnam("/tmp", "tstL");
00363     cout << fn << "creating directory " << path << endl;
00364     if ( BdbcSuccess != thePud.mkDir(path, host, port) ) {
00365         return 1;
00366     }
00367 
00368     cout << fn << "finished ok" << endl;
00369     return 0;
00370 }
00371 
00372 
00373 
00374 int test_mkDirP()
00375 {
00376     const char* fn = "test_mkDirP: ";
00377 
00378     const char* p = tempnam("/tmp", "tstL");
00379     char path[256];
00380     ostrstream b(path, 256);
00381     b << p << "/one/two/three/four/five" << ends;
00382     cout << fn << "creating directory " << path << endl;
00383     if ( BdbcSuccess != thePud.mkDirP(path, host, port) ) {
00384         return 1;
00385     }
00386 
00387     cout << fn << "finished ok" << endl;
00388     return 0;
00389 }
00390 
00391 
00392 
00393 
00394 int test_rmDir()
00395 {
00396     const char* fn = "test_rmDir: ";
00397 
00398     const char* path = tempnam("/tmp", "tstL");
00399 
00400     cout << fn << "removing nonexistent directory " << path << endl;
00401     if ( BdbcError != thePud.rmDir(path, host, port) ) {
00402         return 1;
00403     }
00404 
00405     cout << fn << "creating directory " << path << endl;
00406     if ( BdbcSuccess != thePud.mkDir(path, host, port) ) {
00407         return 2;
00408     }
00409 
00410     cout << fn << "removing directory " << path << endl;
00411     if ( BdbcSuccess != thePud.rmDir(path, host, port) ) {
00412         return 3;
00413     }
00414 
00415     cout << fn << "finished ok" << endl;
00416     return 0;
00417 }
00418 
00419 
00420 int test_purgeMigrateFile()
00421 {
00422     const char* fn = "test_purgeMigrateFile: ";
00423 
00424     const char* path = tempnam("/tmp", "tstR");
00425     // the file that is being purged should exist, now it does not, so
00426     // it should fail
00427     cout << fn << "trying to purge nonexisting file" << endl;
00428     if ( BdbcSuccess == thePud.purgeMigrateFile(path, "mp", host, port) ) {
00429         return 1;
00430     }
00431 
00432     cout << fn << "creating a temp file " << path << endl;
00433     if ( BdbcSuccess != thePud.createFile(path, host, port) ) {
00434         return 2;
00435     }
00436 
00437     cout << fn << "trying to purge file" << path << endl;
00438     if ( BdbcSuccess != thePud.purgeMigrateFile(path, "mp", host, port) ) {
00439         return 3;
00440     }
00441     // this should fail, argument is not valid
00442     cout << fn << "trying to purge file, wrong argument" << path << endl;
00443     if ( BdbcSuccess == thePud.purgeMigrateFile(path, "blabla", host, port) ) {
00444         return 4;
00445     }
00446     thePud.rmFile(path, host, port);
00447 
00448     cout << fn << "finished ok" << endl;
00449     return 0;
00450 }
00451 
00452 
00453 
00454 int test_precrActiveDb()
00455 {
00456     const char* fn = "test_precrActiveDb: ";
00457 
00458     const char* dbName     = "testDB";
00459     const char* dbHost     = "objyserv02";
00460     const char* dbDirPath  = "/tmp";
00461     const char* dbFileName = "testDB.bdb";
00462     d_ULong dbid           = 1234;
00463     d_ULong nrConts        = 20;
00464     d_ULong hashed         = 0;
00465     d_ULong initPg         = 10;
00466     d_ULong percGr         = 1;
00467     const char* bootF      = "/nfs/objyserv02/objy/databases/users/becla/testA/BaBar.BOOT";
00468     char* responseMsg      = 0;
00469     cout << fn << "testing precreation on objyserv02, "
00470          << "/nfs/objyserv02/objy/databases/users/becla/testA/BaBar.BOOT must be valid, run or skip the test? (s-skip) ";
00471     char c;
00472     cin >> c;
00473     if ( c == 's') {
00474         return 0;
00475     }
00476     
00477     if ( BdbcSuccess != thePud.precreateActiveDb(dbName, dbHost, 
00478                         dbDirPath, dbFileName, dbid, nrConts, 
00479                         hashed, initPg, percGr, bootF, responseMsg, host, port) ) {
00480         return 1;
00481     }
00482     cout << "response was: " << responseMsg << endl;
00483     cout << fn << "finished ok" << endl;
00484     return 0;
00485 }
00486 
00487 
00488 int test_stageInFile()
00489 {
00490     const char* fn = "test_stageInFile: ";
00491     
00492     const char* path = tempnam("/tmp", "tstL");
00493     cout << "calling stage in " << path << ", see pud log file" << endl;
00494     if ( BdbcSuccess != thePud.stageInFile("token user", path,  'r',  1, host, port) ) {
00495         return 1;
00496     }
00497     cout << fn << "finished ok" << endl;
00498     return 0;
00499 }
00500 
00501 
00502 int test_stageInFiles()
00503 {
00504     const char* fn = "test_stageInFiles: ";
00505     
00506     char** paths = new char* [3];
00507     *(paths+0) = tempnam("/tmp", "tstL");
00508     *(paths+1) = tempnam("/tmp", "tstL");
00509     *(paths+2) = tempnam("/tmp", "tstL");
00510 
00511     cout << "calling stage in paths[0] = " << *(paths+0) << "\n"
00512          << "                 paths[1] = " << *(paths+1) << "\n"
00513          << "                 paths[2] = " << *(paths+2) << "\n"
00514          << " see pud log file" << endl;
00515     if ( BdbcSuccess != thePud.stageInFiles((const char**)paths,  3, "memo", 'r',  1, host, port) ) {
00516         return 1;
00517     }
00518     cout << fn << "finished ok" << endl;
00519     return 0;
00520 }
00521 
00522 
00523 int test_checkAvailSpace()
00524 {
00525     const char* fn = "test_checkAvailSpace: ";
00526     
00527     d_Boolean isAvail = d_False;
00528     cout << fn << "checking if enough space in /tmp, minSpace = 100" << endl;
00529     if ( BdbcSuccess != thePud.checkAvailSpace("/tmp", 100, &isAvail, host, port) ) {
00530         return 1;
00531     }
00532     if ( ! isAvail ) {
00533         return 2;
00534     }
00535     cout << fn << "checking if enough space in /tmp, minSpace = 10000000" << endl;
00536     if ( BdbcSuccess != thePud.checkAvailSpace("/tmp", 10000000, &isAvail, host, port) ) {
00537         return 3;
00538     }
00539     if ( isAvail ) {
00540         return 4;
00541     }
00542     cout << fn << "checking if enough space in /garbage" << endl;
00543     if ( BdbcError != thePud.checkAvailSpace("/garbage", 100, &isAvail, host, port) ) {
00544         return 5;
00545     }
00546     cout << fn << "finished ok" << endl;
00547     return 0;
00548 }
00549 
00550 
00551 
00552 void runAllTests()
00553 {
00554     assert( 0 == test_checkExist()          );
00555     assert( 0 == test_createFile()          );
00556     assert( 0 == test_rmFile()              );
00557     assert( 0 == test_chmodRead()           );
00558     assert( 0 == test_chmodWrite()          );
00559     assert( 0 == test_copyFile()            );
00560     assert( 0 == test_moveFile()            );
00561     assert( 0 == test_getFileSize()         );
00562     assert( 0 == test_getFDFilePath()       );
00563     assert( 0 == test_getFDHost()           );
00564     assert( 0 == test_getModificationDate() );
00565     assert( 0 == test_copyDb()              );
00566     assert( 0 == test_mkDir()               );
00567     assert( 0 == test_mkDirP()              );
00568     assert( 0 == test_rmDir()               );
00569     assert( 0 == test_purgeMigrateFile()    );
00570     assert( 0 == test_precrActiveDb()       );
00571     assert( 0 == test_stageInFile()         );
00572     assert( 0 == test_stageInFiles()        );
00573     assert( 0 == test_checkAvailSpace()     );
00574 }
00575 
00576 int main(int, char**)
00577 {
00578     host = "objyserv02.slac.stanford.edu";
00579     port = 3333;
00580 
00581     runAllTests();
00582 
00583     //assert( 0 == test_stageInFiles()        );
00584     //assert( 0 == test_stageInFiles()        );
00585 
00586     //cout << "\nSecond part, using predefined host/port, talking to port = 3334" << endl;
00587     //thePud.set(host, 3334);
00588     //host = 0;
00589     //port = 0;
00590     //runAllTests();
00591     
00592     return 0;  
00593 }
00594 

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002