00001
00002
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 #include <unistd.h>
00006 #include <dirent.h>
00007 #include <stdio.h>
00008 #include <errno.h>
00009
00010 #include <iostream>
00011 #include <sstream>
00012
00013 using std::cerr ;
00014 using std::cout ;
00015 using std::endl ;
00016 using std::ostringstream ;
00017
00018 #include "cacheT.h"
00019 #include "BESCache.h"
00020 #include "TheBESKeys.h"
00021 #include "BESException.h"
00022 #include <test_config.h>
00023
00024 void
00025 cacheT::check_cache( const string &cache_dir, map<string,string> &should_be )
00026 {
00027 map<string,string> contents ;
00028 string match_prefix = "bes_cache#" ;
00029 DIR *dip = opendir( cache_dir.c_str() ) ;
00030 if( dip != NULL )
00031 {
00032 struct dirent *dit;
00033 while( ( dit = readdir( dip ) ) != NULL )
00034 {
00035 string dirEntry = dit->d_name ;
00036 if( dirEntry.compare( 0, match_prefix.length(), match_prefix ) == 0)
00037 contents[dirEntry] = dirEntry ;
00038 }
00039 }
00040 closedir( dip ) ;
00041
00042 if( should_be.size() != contents.size() )
00043 {
00044 cerr << "actual number of files is " << contents.size()
00045 << " should be " << should_be.size() << endl ;
00046 }
00047 else
00048 {
00049 map<string,string>::const_iterator ci = contents.begin() ;
00050 map<string,string>::const_iterator ce = contents.end() ;
00051 map<string,string>::const_iterator si = should_be.begin() ;
00052 map<string,string>::const_iterator se = should_be.end() ;
00053 bool good = true ;
00054 for( ; ci != ce; ci++, si++ )
00055 {
00056 if( (*ci).first != (*si).first )
00057 {
00058 cerr << "contents: " << (*ci).first
00059 << " - should be: " << (*si).first << endl ;
00060 good = false ;
00061 }
00062 }
00063 if( good )
00064 {
00065 cout << "contents matches what should be there" << endl ;
00066 }
00067 }
00068 }
00069
00076 void
00077 cacheT::init_cache( const string &cache_dir )
00078 {
00079 string chmod = (string)"chmod a+w " + TEST_SRC_DIR + "/cache" ;
00080 system( chmod.c_str() ) ;
00081
00082 string t_file = cache_dir + "/template.txt" ;
00083 for( int i = 1; i < 9; i++ )
00084 {
00085 ostringstream s ;
00086 s << "cp -f " << t_file << " " << TEST_SRC_DIR << "/cache/bes_cache#usr#local#data#template0" << i << ".txt" ;
00087 cout << s.str() << endl ;
00088 system( s.str().c_str() );
00089
00090 ostringstream m ;
00091 m << "chmod a+w " << TEST_SRC_DIR << "/cache/bes_cache#usr#local#data#template0" << i << ".txt" ;
00092 cout << m.str() << endl ;
00093 system( m.str().c_str() ) ;
00094 }
00095
00096 char *touchers[8] = { "7", "6", "4", "2", "8", "5", "3", "1" } ;
00097 for( int i = 0; i < 8; i++ )
00098 {
00099 sleep(1);
00100 string cmd = (string)"cat " + TEST_SRC_DIR
00101 + "/cache/bes_cache#usr#local#data#template0"
00102 + touchers[i]
00103 + ".txt > /dev/null" ;
00104 cout << cmd << endl ;
00105 system( cmd.c_str() );
00106 }
00107 }
00108
00109 int
00110 cacheT::run(void)
00111 {
00112 cout << endl << "*****************************************" << endl;
00113 cout << "Entered cacheT::run" << endl;
00114 int retVal = 0;
00115
00116 string cache_dir = (string)TEST_SRC_DIR + "/cache" ;
00117
00118 init_cache(cache_dir);
00119
00120 BESKeys *keys = TheBESKeys::TheKeys() ;
00121
00122 string target ;
00123 bool is_it = false ;
00124
00125 cout << endl << "*****************************************" << endl;
00126 try
00127 {
00128 BESCache cache( "", "", 0 ) ;
00129 cerr << "Created cache with empty dir, should not have" << endl ;
00130 return 1 ;
00131 }
00132 catch( BESException &e )
00133 {
00134 cout << "Failed to create cache with empty dir, good" << endl ;
00135 cout << e.get_message() << endl ;
00136 }
00137 catch( ... )
00138 {
00139 cerr << "Failed to create cache with empty dir, unknown exception"
00140 << endl ;
00141 return 1 ;
00142 }
00143
00144 cout << endl << "*****************************************" << endl;
00145 try
00146 {
00147 BESCache cache( "/dummy", "", 0 ) ;
00148 cerr << "Created cache with bad dir, should not have" << endl ;
00149 return 1 ;
00150 }
00151 catch( BESException &e )
00152 {
00153 cout << "Failed to create cache with bad dir, good" << endl ;
00154 cout << e.get_message() << endl ;
00155 }
00156 catch( ... )
00157 {
00158 cerr << "Failed to create cache with bad dir, unknown exception"
00159 << endl ;
00160 return 1 ;
00161 }
00162
00163 cout << endl << "*****************************************" << endl;
00164 try
00165 {
00166 BESCache cache( cache_dir, "", 0 ) ;
00167 cerr << "Created cache with empty prefix, should not have" << endl ;
00168 return 1 ;
00169 }
00170 catch( BESException &e )
00171 {
00172 cout << "Failed to create cache with empty prefix, good" << endl ;
00173 cout << e.get_message() << endl ;
00174 }
00175 catch( ... )
00176 {
00177 cerr << "Failed to create cache with empty prefix, unknown exception"
00178 << endl ;
00179 return 1 ;
00180 }
00181
00182 cout << endl << "*****************************************" << endl;
00183 try
00184 {
00185 BESCache cache( cache_dir, "bes_cache", 0 ) ;
00186 cerr << "Created cache with 0 size, should not have" << endl ;
00187 return 1 ;
00188 }
00189 catch( BESException &e )
00190 {
00191 cout << "Failed to create cache with 0 size, good" << endl ;
00192 cout << e.get_message() << endl ;
00193 }
00194 catch( ... )
00195 {
00196 cerr << "Failed to create cache with 0 size, unknown exception"
00197 << endl ;
00198 return 1 ;
00199 }
00200
00201 cout << endl << "*****************************************" << endl;
00202 try
00203 {
00204 BESCache cache( cache_dir, "bes_cache", 1 ) ;
00205 cout << "Created cache with good params, good" << endl ;
00206 }
00207 catch( BESException &e )
00208 {
00209 cerr << "Failed to create cache with good params" << endl ;
00210 cerr << e.get_message() << endl ;
00211 return 1 ;
00212 }
00213 catch( ... )
00214 {
00215 cerr << "Failed to create cache with good params, unknown exception"
00216 << endl ;
00217 return 1 ;
00218 }
00219
00220 cout << endl << "*****************************************" << endl;
00221 try
00222 {
00223 BESCache cache( *keys, "", "", "" ) ;
00224 cerr << "Created cache with empty dir key, should not have" << endl ;
00225 return 1 ;
00226 }
00227 catch( BESException &e )
00228 {
00229 cout << "Failed to create cache with empty dir key, good" << endl ;
00230 cout << e.get_message() << endl ;
00231 }
00232 catch( ... )
00233 {
00234 cerr << "Failed to create cache with empty dir key, unknown exception"
00235 << endl ;
00236 return 1 ;
00237 }
00238
00239 cout << endl << "*****************************************" << endl;
00240 try
00241 {
00242 BESCache cache( *keys, "BES.CacheDir", "", "" ) ;
00243 cerr << "Created cache with non-exist dir key, should not have" << endl;
00244 return 1 ;
00245 }
00246 catch( BESException &e )
00247 {
00248 cout << "Failed to create cache with non-exist dir key, good" << endl ;
00249 cout << e.get_message() << endl ;
00250 }
00251 catch( ... )
00252 {
00253 cerr << "Failed to create cache with non-exist dir key, unknown exception"
00254 << endl ;
00255 return 1 ;
00256 }
00257
00258 keys->set_key( "BES.CacheDir", "/dummy" ) ;
00259 cout << endl << "*****************************************" << endl;
00260 try
00261 {
00262 BESCache cache( *keys, "BES.CacheDir", "", "" ) ;
00263 cerr << "Created cache with bad dir in conf, should not have" << endl;
00264 return 1 ;
00265 }
00266 catch( BESException &e )
00267 {
00268 cout << "Failed to create cache with bad dir in conf, good" << endl ;
00269 cout << e.get_message() << endl ;
00270 }
00271 catch( ... )
00272 {
00273 cerr << "Failed to create cache with bad dir in conf, unknown exception"
00274 << endl ;
00275 return 1 ;
00276 }
00277
00278 keys->set_key( "BES.CacheDir", cache_dir ) ;
00279 cout << endl << "*****************************************" << endl;
00280 try
00281 {
00282 BESCache cache( *keys, "BES.CacheDir", "", "" ) ;
00283 cerr << "Created cache with empty prefix key, should not have" << endl;
00284 return 1 ;
00285 }
00286 catch( BESException &e )
00287 {
00288 cout << "Failed to create cache with empty prefix key, good" << endl ;
00289 cout << e.get_message() << endl ;
00290 }
00291 catch( ... )
00292 {
00293 cerr << "Failed to create cache with empty prefix key, unknown exception"
00294 << endl ;
00295 return 1 ;
00296 }
00297
00298 cout << endl << "*****************************************" << endl;
00299 try
00300 {
00301 BESCache cache( *keys, "BES.CacheDir", "BES.CachePrefix", "" ) ;
00302 cerr << "Created cache with non-exist prefix key, should not have"
00303 << endl;
00304 return 1 ;
00305 }
00306 catch( BESException &e )
00307 {
00308 cout << "Failed to create cache with non-exist prefix key, good"
00309 << endl ;
00310 cout << e.get_message() << endl ;
00311 }
00312 catch( ... )
00313 {
00314 cerr << "Failed to create cache with non-exist prefix key, "
00315 << "unknown exception" << endl ;
00316 return 1 ;
00317 }
00318
00319 keys->set_key( "BES.CachePrefix", "" ) ;
00320 cout << endl << "*****************************************" << endl;
00321 try
00322 {
00323 BESCache cache( *keys, "BES.CacheDir", "BES.CachePrefix", "" ) ;
00324 cerr << "Created cache with empty prefix in conf, should not have"
00325 << endl;
00326 return 1 ;
00327 }
00328 catch( BESException &e )
00329 {
00330 cout << "Failed to create cache with empty prefix in conf, good"
00331 << endl ;
00332 cout << e.get_message() << endl ;
00333 }
00334 catch( ... )
00335 {
00336 cerr << "Failed to create cache with empty prefix in conf, "
00337 << "unknown exception" << endl ;
00338 return 1 ;
00339 }
00340
00341 keys->set_key( "BES.CachePrefix", "bes_cache" ) ;
00342 cout << endl << "*****************************************" << endl;
00343 try
00344 {
00345 BESCache cache( *keys, "BES.CacheDir", "BES.CachePrefix", "" ) ;
00346 cerr << "Created cache with empty size key, should not have"
00347 << endl;
00348 return 1 ;
00349 }
00350 catch( BESException &e )
00351 {
00352 cout << "Failed to create cache with empty size key, good"
00353 << endl ;
00354 cout << e.get_message() << endl ;
00355 }
00356 catch( ... )
00357 {
00358 cerr << "Failed to create cache with empty size key, "
00359 << "unknown exception" << endl ;
00360 return 1 ;
00361 }
00362
00363 cout << endl << "*****************************************" << endl;
00364 try
00365 {
00366 BESCache cache( *keys, "BES.CacheDir", "BES.CachePrefix", "BES.CacheSize" ) ;
00367 cerr << "Created cache with non-exist size key, should not have"
00368 << endl;
00369 return 1 ;
00370 }
00371 catch( BESException &e )
00372 {
00373 cout << "Failed to create cache with non-exist size key, good"
00374 << endl ;
00375 cout << e.get_message() << endl ;
00376 }
00377 catch( ... )
00378 {
00379 cerr << "Failed to create cache with non-exist size key, "
00380 << "unknown exception" << endl ;
00381 return 1 ;
00382 }
00383
00384 keys->set_key( "BES.CacheSize", "dummy" ) ;
00385 cout << endl << "*****************************************" << endl;
00386 try
00387 {
00388 BESCache cache( *keys, "BES.CacheDir", "BES.CachePrefix", "BES.CacheSize" ) ;
00389 cerr << "Created cache with bad size in conf, should not have"
00390 << endl;
00391 return 1 ;
00392 }
00393 catch( BESException &e )
00394 {
00395 cout << "Failed to create cache with bad size in conf, good"
00396 << endl ;
00397 cout << e.get_message() << endl ;
00398 }
00399 catch( ... )
00400 {
00401 cerr << "Failed to create cache with bad size in conf, "
00402 << "unknown exception" << endl ;
00403 return 1 ;
00404 }
00405
00406 keys->set_key( "BES.CacheSize", "0" ) ;
00407 cout << endl << "*****************************************" << endl;
00408 try
00409 {
00410 BESCache cache( *keys, "BES.CacheDir", "BES.CachePrefix", "BES.CacheSize" ) ;
00411 cerr << "Created cache with 0 size in conf, should not have"
00412 << endl;
00413 return 1 ;
00414 }
00415 catch( BESException &e )
00416 {
00417 cout << "Failed to create cache with 0 size in conf, good"
00418 << endl ;
00419 cout << e.get_message() << endl ;
00420 }
00421 catch( ... )
00422 {
00423 cerr << "Failed to create cache with 0 size in conf, "
00424 << "unknown exception" << endl ;
00425 return 1 ;
00426 }
00427
00428 keys->set_key( "BES.CacheSize", "1" ) ;
00429 cout << endl << "*****************************************" << endl;
00430 try
00431 {
00432 BESCache cache( *keys, "BES.CacheDir", "BES.CachePrefix", "BES.CacheSize" ) ;
00433 cout << "Created cache with good keys" << endl;
00434 }
00435 catch( BESException &e )
00436 {
00437 cerr << "Failed to create cache with good keys" << endl ;
00438 cerr << e.get_message() << endl ;
00439 return 1 ;
00440 }
00441 catch( ... )
00442 {
00443 cerr << "Failed to create cache with good keys unknown exception"
00444 << endl ;
00445 return 1 ;
00446 }
00447
00448 BESCache cache( *keys, "BES.CacheDir", "BES.CachePrefix", "BES.CacheSize" );
00449
00450 cout << endl << "*****************************************" << endl;
00451 try
00452 {
00453 is_it = cache.is_cached( "/dummy/dummy/dummy.nc.gz", target ) ;
00454 if( is_it == true )
00455 {
00456 cerr << "non-exist file is cached" << endl ;
00457 return 1 ;
00458 }
00459 else
00460 {
00461 cout << "non-exist file is not cached, good" << endl ;
00462 }
00463 }
00464 catch( BESException &e )
00465 {
00466 cerr << "Error checking if non-exist file cached" << endl ;
00467 cerr << e.get_message() << endl ;
00468 return 1 ;
00469 }
00470 catch( ... )
00471 {
00472 cerr << "Error checking if non-exist file cached" << endl ;
00473 cerr << "Unknown exception" << endl ;
00474 return 1 ;
00475 }
00476
00477 cout << endl << "*****************************************" << endl;
00478 try
00479 {
00480 is_it = cache.is_cached( "dummy", target ) ;
00481 if( is_it == true )
00482 {
00483 cerr << "bad file is cached" << endl ;
00484 return 1 ;
00485 }
00486 else
00487 {
00488 cout << "bad file is not cached, good" << endl ;
00489 }
00490 }
00491 catch( BESException &e )
00492 {
00493 cerr << "Error checking if non-exist file cached" << endl ;
00494 cerr << e.get_message() << endl ;
00495 return 1 ;
00496 }
00497 catch( ... )
00498 {
00499 cerr << "Error checking if non-exist file cached" << endl ;
00500 cerr << "Unknown exception" << endl ;
00501 return 1 ;
00502 }
00503
00504 cout << endl << "*****************************************" << endl;
00505 try
00506 {
00507 string should_be = cache_dir
00508 + "/bes_cache#usr#local#data#template01.txt" ;
00509 is_it = cache.is_cached( "/usr/local/data/template01.txt.gz", target ) ;
00510 if( is_it == true )
00511 {
00512 cout << "file is cached, good" << endl ;
00513 if( target != should_be )
00514 {
00515 cerr << "target is " << target
00516 << ", should be " << should_be << endl ;
00517 return 1 ;
00518 }
00519 else
00520 {
00521 cout << "target is good" << endl ;
00522 }
00523 }
00524 else
00525 {
00526 cerr << "file is not cached, should be" << endl ;
00527 cerr << "looking for file " << target << endl ;
00528 return 1 ;
00529 }
00530 }
00531 catch( BESException &e )
00532 {
00533 cerr << "Error checking if good file cached" << endl ;
00534 cerr << e.get_message() << endl ;
00535 return 1 ;
00536 }
00537 catch( ... )
00538 {
00539 cerr << "Error checking if good file cached" << endl ;
00540 cerr << "Unknown exception" << endl ;
00541 return 1 ;
00542 }
00543
00544 map<string,string> should_be ;
00545 should_be["bes_cache#usr#local#data#template01.txt"] = "bes_cache#usr#local#data#template01.txt" ;
00546 should_be["bes_cache#usr#local#data#template03.txt"] = "bes_cache#usr#local#data#template02.txt" ;
00547 should_be["bes_cache#usr#local#data#template05.txt"] = "bes_cache#usr#local#data#template03.txt" ;
00548 should_be["bes_cache#usr#local#data#template08.txt"] = "bes_cache#usr#local#data#template04.txt" ;
00549
00550 cout << endl << "*****************************************" << endl;
00551 cout << "Test purge, should remove a few" << endl;
00552 try
00553 {
00554 cache.purge() ;
00555 cout << "purge returned with success ... checking" << endl ;
00556 check_cache( cache_dir, should_be ) ;
00557 }
00558 catch( BESException &e )
00559 {
00560 cerr << "purge failed" << endl ;
00561 cerr << e.get_message() << endl ;
00562 return 1 ;
00563 }
00564 catch( ... )
00565 {
00566 cerr << "purge failed with unknown exception" << endl ;
00567 return 1 ;
00568 }
00569
00570 cout << endl << "*****************************************" << endl;
00571 cout << "Test purge, should not remove any" << endl;
00572 try
00573 {
00574 cache.purge() ;
00575 cout << "purge returned with success ... checking" << endl ;
00576 check_cache( cache_dir, should_be ) ;
00577 }
00578 catch( BESException &e )
00579 {
00580 cerr << "purge failed" << endl ;
00581 cerr << e.get_message() << endl ;
00582 return 1 ;
00583 }
00584 catch( ... )
00585 {
00586 cerr << "purge failed with unknown exception" << endl ;
00587 return 1 ;
00588 }
00589
00590 cout << endl << "*****************************************" << endl;
00591 cout << "Returning from cacheT::run" << endl;
00592
00593 return retVal;
00594 }
00595
00596 int
00597 main(int argC, char **argV) {
00598 string env_var = (string)"BES_CONF=" + TEST_SRC_DIR
00599 + "/cache_test.ini" ;
00600 putenv( (char *)env_var.c_str() ) ;
00601 Application *app = new cacheT();
00602 return app->main(argC, argV);
00603 }
00604