// WsClientGenericSelectionCatalog_t.cxx #include "dial_ws_selection/WsClientGenericSelectionCatalog.h" #include #include #include #include #include #include "dataset_util/FileStatus.h" #include "dataset_util/getcwd.h" #include "dataset_util/mkdir.h" #include "dataset_util/ThreadCredential.h" #include "dataset_catalog/ConnectionResolver.h" #include "dataset_credential/CredentialSelectionCatalog.h" #include "dataset_credential/GssCredentialManager.h" #include "dial_ws/dial_ws_flags.h" using std::string; using std::cout; using std::endl; using std::ostream; using std::ifstream; using dset::SqlQuery; using dset::ConnectionResolver; using dset::GenericSelectionCatalog; using dset::CredentialSelectionCatalog; using dset::GssCredentialManager; using dial::WsClientGenericSelectionCatalog; typedef GenericSelectionCatalog::Name Name; typedef GenericSelectionCatalog::NameList NameList; typedef GenericSelectionCatalog::Attributes Attributes; #ifdef WITH_GSI #include "gsi.h" #endif void msg(string txt) { cout << "*** "; cout << txt; cout << " ***" << endl; } void sigpipe_handler(int) { cout << "Another broken pipe" << endl; } #ifdef WITH_GSI int client_authorization(struct soap* psoap) { struct gsi_plugin_data* pgsiplug = (struct gsi_plugin_data *) soap_lookup_plugin(psoap, GSI_PLUGIN_ID); //string client_identity = pgsiplug->client_identity; string service_identity = pgsiplug->server_identity; //cout << "GSI Client ID: " << client_identity << endl; cout << "GSI Service ID: " << service_identity << endl; return 0; } #endif ostream& operator<<(ostream& str, const NameList& names) { str << "Name list has " << names.size() << " entries:"; for ( NameList::const_iterator iid=names.begin(); iid!=names.end(); ++iid ) { str << " \n" << *iid; } return str; } int WsClientGenericSelectionCatalog_t() { signal(SIGPIPE, sigpipe_handler); msg("Define service connection resolver"); string confdir = getcwd(); unlink("resolver.dat"); Text txt("resolver.dat"); txt.append("DatasetSelectionCatalog"); txt.append("SQLRESULT:" + confdir + "/dsc.dat"); txt.write(); msg("Construct file description of dsc"); system("rm -f dsc.dat"); Text tdsc("dsc.dat"); tdsc.append("SQLRESULT"); tdsc.append("name,uid,owner,myprop"); tdsc.append(".TYPE:DatasetSelectionCatalog,0-0,,"); tdsc.append(".ID:uid,0-0,,"); tdsc.append("dst1,101-1,me,myval1"); tdsc.append("dst2,101-2,me,myval2"); tdsc.append("dst3,101-3,me,myval1"); tdsc.write(); msg("Create CSC"); ThreadCredential tc; CredentialSelectionCatalog::create_default_instance(); CredentialSelectionCatalog& csc = CredentialSelectionCatalog::default_instance(); cout << csc << endl; assert( csc.is_valid() ); msg("Fetch credential"); GssCredentialManager::set_default(); string credname = GssCredentialManager::name(); assert( credname.size() ); cout << credname << endl; msg("Insert owner into CSC"); assert( GssCredentialManager::set_owner("this_is_me") == 0 ); int pid = -1; if ( FileStatus("server/pid").exists() ) { msg("Clean up from earlier test"); ifstream pidfile("serverpid"); pidfile >> pid; if ( pid > 0 ) { cout << "Killing process " << pid << endl; kill(pid, SIGKILL); } unlink("server/pid"); pid = -1; sleep(1); // Give system time to free up port } msg("Create and change to server directory"); system("rm -rf server"); assert( mkdir("server") == 0 ); assert( chdir("server") == 0 ); #ifdef WITH_GSI // Create auth file. string globus_version = "3"; string gcom = "rm -f authorized_dn"; gcom += "; "; if ( globus_version == "2" ) { gcom += "grid-proxy-info -subject"; gcom += " | sed 's#/CN=proxy##g'"; } else { gcom += "grid-proxy-info -identity"; } gcom += " > authorized_dn"; system(gcom.c_str()); #endif msg("Start server"); assert( ! FileStatus("pid").exists() ); string com = "rm -rf server.log; "; com += "DIAL_CATALOG_CONF=" + confdir + "/resolver.dat; "; com += "export DIAL_CATALOG_CONF; "; string libs; libs += "dataset_id:dataset_sql:dataset_catalog:dataset_base"; libs += ":dial_ws_selection"; com += "dialws " + libs + ":selection"; com += " 8080 server.log"; com += " gsi service"; com += " >service.log 2>&1 &"; cout << com << endl; int sysstat = system(com.c_str()); assert( sysstat == 0 ); for ( int i=0; i<35; ++i ) { cout << "."; if ( FileStatus("pid").exists() ) break; sleep(1); } cout << endl; assert( FileStatus("pid").exists() ); msg("Set server URL"); #ifdef WITH_GSI string server = "http://127.0.0.1:8080"; #else string server = "http://127.0.0.1:8080"; #endif msg("Check server process"); int nloop = 0; while ( nloop < 10 ) { if ( FileStatus("pid").exists() ) break; sleep(1); ++nloop; } assert( nloop < 10 ); { ifstream pidfile("pid"); pidfile >> pid; } cout << "Server process ID is " << pid << endl; assert( pid > 0 ); msg("Change back to client directory"); assert( chdir("..") == 0 ); msg("Create bad client"); string type = "DatasetSelectionCatalog"; string conn = "SQLRESULT"; WsClientGenericSelectionCatalog badclient(server, type, "nosuchconn"); assert( ! badclient.is_valid() ); msg("Create client"); WsClientGenericSelectionCatalog client(server, type, conn); assert( client.is_valid() ); msg("Check catalog type"); string newtype = client.catalog_type(); assert( newtype.size() ); cout << newtype << endl; assert( newtype == type ); msg("Check ID encoding"); string iden = client.id_encoding(); assert( iden.size() ); cout << iden << endl; assert( iden == "uid" ); msg("Check size"); int sz = client.size(); cout << sz << endl; assert( sz == 3 ); msg("Schema"); NameList sch = client.schema(); assert( sch.size() == 4 ); assert( sch[0] == "name" ); assert( sch[1] == "uid" ); assert( sch[2] == "owner" ); assert( sch[3] == "myprop" ); msg("Has name"); assert( client.has_name("dst1") ); assert( client.has_name("dst2") ); assert( client.has_name("dst3") ); assert( ! client.has_name("dst4") ); msg("Attributes"); Attributes atts = client.attributes("dst2"); cout << atts << endl; assert( atts.size() == 4 ); assert( atts.find("name")->second == "dst2" ); assert( atts.find("uid")->second == "101-2" ); assert( atts.find("myprop")->second == "myval2" ); assert( atts.find("owner") != atts.end() ); msg("ID"); assert( client.id("dst3") == "101-3" ); msg("Size"); assert( client.size() == 3 ); msg("Query for 0"); SqlQuery qry("myprop = nosuchvalue"); NameList names = client.query(qry, 10); assert( names.size() == 0 ); msg("Query for 1"); names = client.query(SqlQuery("myprop = myval2"), 10); assert( names.size() == 1 ); assert( names[0] == "dst2" ); msg("Query for 2"); names = client.query(SqlQuery("myprop = myval1"), 10); assert( names.size() == 2 ); assert( names[0] == "dst1" ); assert( names[1] == "dst3" ); msg("Query for 1 of 2"); names = client.query(SqlQuery("myprop = myval1"), 1); assert( names.size() == 1 ); msg("Set thread credential owner"); ThreadCredential().set_default_owner("Annette"); cout << ThreadCredential() << endl; msg("Insert"); Attributes insatts; insatts["uid"] = "101-4"; insatts["myprop"] = "myval4"; assert( client.insert("dst4", insatts) == 0 ); assert( client.size() == 4 ); assert( client.has_name("dst4") ); assert( client.attributes("dst4").find("myprop")->second == "myval4" ); assert( client.insert("dst4", insatts) != 0 ); msg("Update"); Attributes updatts; updatts["myprop"] = "myval5"; assert( client.update("dst4", updatts) == 0 ); assert( client.size() == 4 ); assert( client.has_name("dst4") ); assert( client.attributes("dst4").find("myprop")->second == "myval5" ); msg("Remove"); assert( client.remove("dst4") == 0 ); assert( client.size() == 3 ); assert( ! client.has_name("dst4") ); msg("Terminate service"); client.terminate_service(); assert( ! client.is_valid() ); while ( nloop < 10 ) { cout << "."; if ( ! FileStatus("pid").exists() ) break; sleep(1); ++nloop; } cout << endl; return 0; } #ifdef CTEST_MAIN int main() { return WsClientGenericSelectionCatalog_t(); } #endif