// Copyright (c) 2006 Ole Christian Weidner (oweidner@cct.lsu.edu) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include #include #include using boost::unit_test_framework::test_suite; #include #include "test_file.hpp" #include "test_directory.hpp" #include "test_directory_bulk.hpp" #include "test_job_description.hpp" #include "test_job_service.hpp" #include "test_cleanup.hpp" #include "test_init.hpp" namespace { /** * utility function: tests if a given section of the config file is a valid * section. a valid section contains at least the following entries: * - enabled (whether to include this section's tests or not) * - adaptor_path (path to the adaptor we want to test with this section) * - helper_path (path to the test helper library of the adaptor) * - adaptor_name (name of the adaptor) * * @param sec * path to the section we want to test * * @return * true if the section is valid, false otherwise */ bool is_valid_section(saga::ini::section const& sec) { bool ret_val = false; if( sec.has_entry("enabled") && sec.has_entry("adaptor_path") && sec.has_entry("adaptor_name") ) { ret_val = true; } return ret_val; } /** * utility function: tests if a given section of the config file is enabled * * @param sec * path to the section we want to test * * @return * true if the section is enabled, false otherwise */ bool is_enabled_section(saga::ini::section const& sec) { bool ret_val = false; std::string is_enabled(sec.get_entry("enabled")); if( is_enabled == "True" || is_enabled == "TRUE" || is_enabled == "true" ) { ret_val = true; } return ret_val; } } // namespace /////////////////////////////////////////////////////////////////////////////// // saga::ini::section * test_init::get_section_custom_properties_(saga::ini::section const& base_sec) { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> Additional properties for adaptor tests present... " << std::flush; } if( base_sec.has_section("properties") ) { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << " YES" << std::endl; } return new saga::ini::section(base_sec.get_section("properties")); } else { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << " NO" << std::endl; } return NULL; } } /////////////////////////////////////////////////////////////////////////////// // bool test_init::lib_has_test_helper_factory_(std::string& lib_path, std::string adaptor_name, bool retry) { bool retval = false; SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> Adaptor supplies test helper functions... " << std::flush; } namespace fs = boost::filesystem; fs::path lib(lib_path, fs::native); try { boost::plugin::dll d(lib.string()); boost::plugin::plugin_factory the_factory (d); TR1::shared_ptr util( the_factory.create ("test_helper_utils")); SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << " YES" << std::endl; } lib_path = lib.string(); retval = true; } catch(std::exception const & e) { #if !defined(BOOST_WINDOWS) std::string libname ("libsaga_adaptor_" + adaptor_name + SAGA_SHARED_LIB_EXTENSION); #elif defined(_DEBUG) std::string libname (adaptor_name + "d" + SAGA_SHARED_LIB_EXTENSION); #else std::string libname (adaptor_name + SAGA_SHARED_LIB_EXTENSION); #endif lib /= fs::path (libname, fs::native); std::string p (lib.string()); if (!retry || !(retval = lib_has_test_helper_factory_(p, adaptor_name, false))) { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << " NO" << std::endl; } std::cerr << e.what() << std::endl << std::flush; retval = false; } else { lib_path = p; } } return retval; } /////////////////////////////////////////////////////////////////////////////// // saga::test::test_helper_utils * test_init::init_helper_object_(std::string lib_path) { boost::plugin::dll d (lib_path); cleanup_handler_->add_library_handle(d); boost::plugin::plugin_factory the_factory (d); saga::test::test_helper_utils * thu (the_factory.create ("test_helper_utils")); cleanup_handler_->add_helper_object(thu); return thu; } /////////////////////////////////////////////////////////////////////////////// // std::string test_init::create_adaptor_ini_file_(std::string base_dir, saga::ini::ini const& sec) { namespace fs = boost::filesystem; std::string adaptor_path(sec.get_entry("adaptor_path")); std::string adaptor_name(sec.get_entry("adaptor_name")); SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> Creating temporary .ini file for " << adaptor_name << "... " << std::flush; } fs::path p (base_dir, fs::native); p /= fs::path(adaptor_name + ".ini", fs::native); std::fstream saga_ini_file(p.string().c_str(), std::ios::out); if( !saga_ini_file.is_open() ) { std::cerr << "FAILED (Can't create file: " << p.string() << ")" << std::endl; } try { saga_ini_file << "[saga.adaptors." << adaptor_name << "]" << std::endl; saga_ini_file << " name = " << adaptor_name << std::endl ; saga_ini_file << " path = " << adaptor_path << std::endl ; saga_ini_file.close(); cleanup_handler_->add_file(p); } catch( std::exception const & e ) { std::cerr << "FAILED ("<< e.what() << ")" << std::endl; exit(-1); } SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "OK" << std::endl; } return p.string(); } /////////////////////////////////////////////////////////////////////////////// // std::string test_init::create_saga_ini_file_(std::string base_dir) { std::fstream saga_ini_file; namespace fs = boost::filesystem; SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] Creating .saga.ini file in " << base_dir << " " << std::flush; } fs::path p (base_dir, fs::native); p /= fs::path(".saga.ini", fs::native); saga_ini_file.open(p.string().c_str(), std::ios::out); if( !saga_ini_file.is_open() ) { std::cerr << "FAILED (Could not open file)" << std::endl; exit(-1); } try { saga_ini_file << "[info]" << std::endl << " ini_path = ." << std::endl ; saga_ini_file.close(); cleanup_handler_->add_file(p); } catch( std::exception const & e ) { std::cerr << "FAILED (" << e.what() << ")" << std::endl; exit(-1); } SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "OK" << std::endl; } return p.string(); } /////////////////////////////////////////////////////////////////////////////// // void test_init::add_adaptor_tests_() { saga::ini::section adaptor_test_section; create_saga_ini_file_("."); ///////////////////////////// // REGISTER THE FILE TESTS // ///////////////////////////// if( config_file_.has_section("file_adaptor_test") ) { adaptor_test_section = config_file_.get_section("file_adaptor_test"); if( is_valid_section(adaptor_test_section) ) { if(is_enabled_section(adaptor_test_section) ) { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT]\n[INIT] Processing file_adaptor_test section..." << std::endl; } std::auto_ptr prop ( get_section_custom_properties_(adaptor_test_section)); std::string adaptor_name(adaptor_test_section.get_entry("adaptor_name")); std::string adaptor_path(adaptor_test_section.get_entry("adaptor_path")); if ( lib_has_test_helper_factory_(adaptor_path, adaptor_name) ) { create_adaptor_ini_file_( ".", adaptor_test_section ); saga::test::test_helper_utils * utils = init_helper_object_(adaptor_path); test_suite_->add (new test_file :: suite (utils)); test_suite_->add (new test_directory :: suite (utils)); //test_suite_->add (new test_directory_bulk :: suite ()); SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> All test cases added." << std::endl; } } else { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> No test cases added." << std::endl; } } } } } //////////////////////////// // REGISTER THE JOB TESTS // //////////////////////////// if( config_file_.has_section("job_adaptor_test") ) { adaptor_test_section = config_file_.get_section("job_adaptor_test"); if( is_valid_section(adaptor_test_section) ) { if(is_enabled_section(adaptor_test_section) ) { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT]\n[INIT] Processing job_adaptor_test section..." << std::endl; } std::auto_ptr prop ( get_section_custom_properties_(adaptor_test_section)); // std::string adaptor_name(adaptor_test_section.get_entry("adaptor_name")); // std::string adaptor_path(adaptor_test_section.get_entry("adaptor_path")); // if ( lib_has_test_helper_factory_(adaptor_path, adaptor_name) ) //{ create_adaptor_ini_file_( ".", adaptor_test_section ); // test_helper_utils * utils = init_helper_object_(adaptor_path); test_suite_->add(new test_job_service :: suite ()); test_suite_->add(new test_job_description :: suite ()); SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> All test cases added." << std::endl; } //} //else //{ // std::cerr << "[INIT] |--> No test cases added." << std::endl; //} } } } /////////////////////////////// // REGISTER THE STREAM TESTS // /////////////////////////////// if( config_file_.has_section("stream_adaptor_test") ) { adaptor_test_section = config_file_.get_section("stream_adaptor_test"); if( is_valid_section(adaptor_test_section) ) { if(is_enabled_section(adaptor_test_section) ) { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT]\n[INIT] Processing stream_adaptor_test section..." << std::endl; } std::auto_ptr prop ( get_section_custom_properties_(adaptor_test_section)); std::string adaptor_name(adaptor_test_section.get_entry("adaptor_name")); std::string adaptor_path(adaptor_test_section.get_entry("adaptor_path")); if ( lib_has_test_helper_factory_(adaptor_path, adaptor_name) ) { create_adaptor_ini_file_( ".", adaptor_test_section ); // saga::test::test_helper_utils * utils = init_helper_object_(adaptor_path); SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> All test cases added." << std::endl; } } else { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> No test cases added." << std::endl; } } } } } //////////////////////////// // REGISTER THE RPC TESTS // //////////////////////////// if( config_file_.has_section("rpc_adaptor_test") ) { adaptor_test_section = config_file_.get_section("rpc_adaptor_test"); if( is_valid_section(adaptor_test_section) ) { if(is_enabled_section(adaptor_test_section) ) { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT]\n[INIT] Processing rpc_adaptor_test section..." << std::endl; } std::auto_ptr prop ( get_section_custom_properties_(adaptor_test_section)); std::string adaptor_name(adaptor_test_section.get_entry("adaptor_name")); std::string adaptor_path(adaptor_test_section.get_entry("adaptor_path")); if ( lib_has_test_helper_factory_(adaptor_path, adaptor_name) ) { create_adaptor_ini_file_( ".", adaptor_test_section ); // saga::test::test_helper_utils * utils = init_helper_object_(adaptor_path); SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> All test cases added." << std::endl; } } else { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> No test cases added." << std::endl; } } } } } //////////////////////////// // REGISTER THE RPC TESTS // //////////////////////////// if( config_file_.has_section("logical_file_adaptor_test") ) { adaptor_test_section = config_file_.get_section("logical_file_adaptor_test"); if( is_valid_section(adaptor_test_section) ) { if(is_enabled_section(adaptor_test_section) ) { SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT]\n[INIT] Processing logical_file_adaptor_test section..." << std::endl; } std::auto_ptr prop ( get_section_custom_properties_(adaptor_test_section)); // std::string adaptor_name(adaptor_test_section.get_entry("adaptor_name")); // std::string adaptor_path(adaptor_test_section.get_entry("adaptor_path")); // if ( lib_has_test_helper_factory_(adaptor_path, adaptor_name) ) // { create_adaptor_ini_file_( ".", adaptor_test_section ); // saga::test::test_helper_utils * utils = init_helper_object_(adaptor_path); SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] |--> All test cases added." << std::endl; } // } // else // { // std::cerr << "[INIT] |--> No test cases added." << std::endl; // } } } } //////////////////////////////////// // REGISTER THE CLEANUP "HANDLER" // //////////////////////////////////// test_suite_->add (cleanup_handler_); } /////////////////////////////////////////////////////////////////////////////// // test_init::test_init(test_suite* suite, std::string config_file_location) : test_suite_(suite) { try { config_file_ = saga::ini::ini(); config_file_.read(config_file_location); } catch( saga::exception const & e ) { std::cerr << "[INIT] Reading config file: ... " << config_file_location; std::cerr << " FAILED:" << std::endl << e.what() << std::endl; exit(-1); } if( !config_file_.has_section("test_preferences") ) { std::cerr << "[INIT] Reading config file: ... " << config_file_location; std::cerr << " FAILED (section \"test_preferences\" missing)" << std::endl; exit(-1); } else { saga::ini::section cfg_sec = config_file_.get_section("test_preferences"); if (cfg_sec.has_entry("verbose")) setenv("SAGA_VERBOSE", cfg_sec.get_entry("verbose").c_str(), 1); SAGA_VERBOSE(SAGA_VERBOSE_LEVEL_INFO) { std::cerr << "[INIT] Reading config file: " << config_file_location << " OK" << std::endl; } } // Create a cleanup_handler instance cleanup_handler_ = new cleanup::suite(); add_adaptor_tests_(); }