2008-07-10 15:24:48 +04:00
#!/usr/bin/env ruby
require 'pp'
2008-07-22 20:15:38 +04:00
require 'digest/md5'
2008-07-14 16:56:37 +04:00
require 'ftools'
2008-07-10 15:24:48 +04:00
2008-07-14 16:56:37 +04:00
###########################################
# Code to test compilation/linkning flags #
###########################################
TestCodeServer="""
2008-07-10 15:24:48 +04:00
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
int main(int argc, char *argv[])
{
xmlrpc_c::registry RequestManagerRegistry;
return 0;
2008-07-14 16:56:37 +04:00
}"""
TestCodeClient=<<-EOT
2008-07-10 15:24:48 +04:00
#include <cstdlib>
#include <string>
#include <iostream>
#include <xmlrpc-c/girerr.hpp>
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/client_simple.hpp>
#include <map>
using namespace std;
int
main(int argc, char **) {
if (argc-1 > 0) {
cerr << "This program has no arguments" << endl;
exit(1);
}
try {
xmlrpc_env env;
string const serverUrl("http://localhost:8080/RPC2");
string const methodAllocate("one.vmallocate");
xmlrpc_c::clientSimple myClient;
xmlrpc_c::value resultSUBMIT;
xmlrpc_env_init(&env);
myClient.call(serverUrl, methodAllocate, "ss",
&resultSUBMIT,"SESSION-GOLA&4H9109KVFSG",
"MEMORY=345 CPU=4 DISK=[FILE=\\"img\\",TYPE=cd]"
"DISK=[FILE=\\"../f\\"]");
xmlrpc_c::value_array resultArray = xmlrpc_c::value_array(resultSUBMIT);
vector<xmlrpc_c::value> const paramArrayValue(resultArray.vectorValueValue());
//check posible Errors:
xmlrpc_c::value * firstvalue;
firstvalue = &(static_cast<xmlrpc_c::value>(paramArrayValue[0]));
xmlrpc_c::value_boolean * status = &(static_cast<xmlrpc_c::value_boolean>(*firstvalue));
xmlrpc_c::value * secondvalue;
secondvalue = &(static_cast<xmlrpc_c::value>(paramArrayValue[1]));
xmlrpc_c::value_string * valueS = &(static_cast<xmlrpc_c::value_string>(*secondvalue));
if(static_cast<bool>(*status)) {
//Success, returns the id assigned to the VM:
cout << "vmid returned: " << static_cast<string>(*valueS) << endl;
return 0;
}
else{ //Failure:
string error_value=static_cast<string>(*valueS);
if (error_value.find("Error inserting",0)!=string::npos ) cout << "Error inserting VM in the database" << endl;
else if (error_value.find("Error parsing",0)!=string::npos ) cout << "Error parsing VM template" << endl;
else cout << "Unknown error " << static_cast<string>(*valueS) << endl;
};
} catch (girerr::error const error) {
cerr << "Client threw error: " << error.what() << endl;
//"Client threw error:"
return 20;
} catch (std::exception const e) {
cerr << "Client threw unexpected error." << endl;
//Unexpected error:
return 999;
}
return 0;
}
EOT
2008-07-14 16:56:37 +04:00
TestCode={
:client => TestCodeClient,
:server => TestCodeServer
}
2008-07-10 15:24:48 +04:00
2008-07-14 16:56:37 +04:00
# Executes a command and discards stderr
2008-07-10 15:24:48 +04:00
def exec_command(text)
lambda {
2008-07-14 16:56:37 +04:00
#STDERR.puts text
2008-07-10 15:24:48 +04:00
text=`#{text} 2>/dev/null`
if $?!=0
nil
else
text
end
}
end
2008-07-14 16:56:37 +04:00
# Adds fixed flags and libraries
2008-07-10 15:24:48 +04:00
def flags_and_libs_array(flags, libs)
lambda {
libs_text=libs.collect {|lib| "-l"+lib }.join(' ')
text=flags+" "+libs_text
2008-07-14 16:56:37 +04:00
#STDERR.puts text
2008-07-10 15:24:48 +04:00
text
}
end
2008-07-14 16:56:37 +04:00
# Array with flags/libs to test
2008-07-10 15:24:48 +04:00
Configs=[
2008-07-14 16:56:37 +04:00
# Configuration for fedora
2008-07-10 15:24:48 +04:00
{
2008-09-11 21:41:02 +04:00
:description => "pkg-config",
2008-07-10 15:24:48 +04:00
:client => exec_command("pkg-config xmlrpc_client++ xmlrpc++ --libs"),
:server => exec_command("pkg-config xmlrpc_server_abyss++ --static --libs")
},
2008-09-11 21:41:02 +04:00
# Configuration using xmlrpc-c-config
{
:description => "xmlrpc-c-config",
:client => exec_command("xmlrpc-c-config c++2 client --libs"),
:server => exec_command("xmlrpc-c-config c++2 abyss-server --libs")
},
# Debian lenny
{
:description => "mixed hardcoded libraries and xmlrpc-c-config (debian lenny)",
:client => flags_and_libs_array("-I/usr/include -L/usr/lib", [
'xmlrpc_client', 'xmlrpc_client', 'xmlrpc', 'xmlrpc_util',
'xmlrpc_xmlparse', 'xmlrpc_xmltok', 'xmlrpc_client++', 'xmlrpc++'
]),
:server => exec_command("xmlrpc-c-config c++2 abyss-server --libs"),
},
2008-07-14 16:56:37 +04:00
# Configuration for Mac OS X and Debian
2008-07-10 15:24:48 +04:00
{
2008-09-11 21:41:02 +04:00
:description => "hardcoded libraries for Mac OS X (installed using port)",
2008-07-10 15:24:48 +04:00
:server => flags_and_libs_array("-I/opt/local/include -L/opt/local/lib", [
2008-07-14 16:56:37 +04:00
'wwwxml', 'xmltok', 'xmlparse', 'wwwzip', 'wwwinit', 'wwwapp',
'wwwtelnet', 'wwwhtml', 'wwwnews', 'wwwhttp', 'wwwmime', 'wwwgopher',
'wwwftp', 'wwwfile', 'wwwdir', 'wwwcache', 'wwwstream', 'wwwmux',
'wwwtrans', 'wwwcore', 'wwwutils', 'md5', 'dl', 'z', 'pthread',
'xmlrpc_client++', 'xmlrpc_client', 'xmlrpc++', 'xmlrpc',
'xmlrpc_util', 'xmlrpc_xmlparse', 'xmlrpc_xmltok',
'xmlrpc_server_abyss++', 'xmlrpc_server++', 'xmlrpc_server_abyss',
'xmlrpc_server', 'xmlrpc_abyss',
2008-07-10 15:24:48 +04:00
]),
:client => flags_and_libs_array("-I/opt/local/include -L/opt/local/lib", [
2008-07-14 16:56:37 +04:00
'curl', 'xmlrpc_client++', 'xmlrpc_client', 'xmlrpc++', 'xmlrpc',
'xmlrpc_util', 'xmlrpc_xmlparse', 'xmlrpc_xmltok', 'wwwxml',
'xmltok', 'xmlparse', 'wwwzip', 'wwwinit', 'wwwapp', 'wwwtelnet',
'wwwhtml', 'wwwnews', 'wwwhttp', 'wwwmime', 'wwwgopher', 'wwwftp',
'wwwfile', 'wwwdir', 'wwwcache', 'wwwstream', 'wwwmux', 'wwwtrans',
'wwwcore', 'wwwutils', 'm', 'md5'
2008-07-10 15:24:48 +04:00
])
2008-07-14 16:56:37 +04:00
},
# Example adding more custom libraries
# {
# :client => exec_command("xmlrpc-c-config client c++ --cflags --libs"),
# :server => exec_command("xmlrpc-c-config abyss-server c++ --cflags --libs | tr '\\n' ' ' ; echo -lxmlrpc_server++")
# },
2008-07-10 15:24:48 +04:00
]
2008-07-14 16:56:37 +04:00
# Compiles test code with given arguments
2008-07-10 15:24:48 +04:00
def compile(file, args)
command="g++ #{file} -o #{file}.out #{args} 1>/dev/null 2>/dev/null"
2008-09-11 21:41:02 +04:00
STDERR.puts command
STDERR.puts ""
2008-07-10 15:24:48 +04:00
out=system(command)
end
2008-07-14 16:56:37 +04:00
def gen_test_file(kind)
dir='.xmlrpc_test'
hash=Digest::MD5.hexdigest(Time.now.to_s+kind.to_s)[0..5]
fname="xmlrpc_test."+hash+".cc"
#fname="xmlrpc_test.cc"
full_path=dir+"/"+fname
File.makedirs(dir)
f=open(full_path, "w")
2008-07-10 15:24:48 +04:00
f.write(TestCode[kind])
f.close
2008-07-14 16:56:37 +04:00
full_path
end
# Tests a configuration and writes build parameters if it is successful
def test_config(kind, config)
2008-09-11 21:41:02 +04:00
STDERR.puts "Testing recipe: "+config[:description].to_s
2008-07-10 15:24:48 +04:00
args=config[kind.to_sym].call
return nil if !args
args.strip!
2008-07-14 16:56:37 +04:00
name=gen_test_file(kind)
2008-07-10 15:24:48 +04:00
exit_code=compile(name, args)
2008-07-14 16:56:37 +04:00
File.delete(name)
2008-08-19 17:52:54 +04:00
File.delete(name+".out") if File.exists?(name+".out")
2008-07-10 15:24:48 +04:00
return nil if !exit_code
puts args
true
end
2008-07-14 16:56:37 +04:00
# Test each of possible configurations and exits when it finds one working
2008-07-10 15:24:48 +04:00
def search_config(kind)
Configs.each {|config|
found=test_config(kind,config)
exit(0) if found
}
exit(1)
end
2008-07-14 16:56:37 +04:00
if !%w{client server}.include?ARGV[0]
puts "You need to specify client or server"
exit -1
end
2008-07-10 15:24:48 +04:00
search_config(ARGV[0].to_sym)