From 466d5f0a9f4347fa7ca05c067ac752b6f4285cc4 Mon Sep 17 00:00:00 2001 From: Bruce Potter Date: Fri, 31 Jan 2014 08:26:55 -0500 Subject: [PATCH] test scripts for the rest api --- xCAT-server/xCAT-wsapi/xcatws-test.pl | 103 ++++++++++++++++++++++++++ xCAT-server/xCAT-wsapi/xcatws-test.sh | 25 +++++++ 2 files changed, 128 insertions(+) create mode 100755 xCAT-server/xCAT-wsapi/xcatws-test.pl create mode 100644 xCAT-server/xCAT-wsapi/xcatws-test.sh diff --git a/xCAT-server/xCAT-wsapi/xcatws-test.pl b/xCAT-server/xCAT-wsapi/xcatws-test.pl new file mode 100755 index 000000000..5c5c8e0bd --- /dev/null +++ b/xCAT-server/xCAT-wsapi/xcatws-test.pl @@ -0,0 +1,103 @@ +#!/usr/bin/env perl +# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html + +# This is a command which can be used to access the rest api of xCAT + +use strict; +use Getopt::Long; +use LWP; +use JSON; +require HTTP::Request; + +# set the usage message +my $usage_string = "Usage: + restapi -u url -m GET|PUT|POST|DELETE [-f html|json|xml] [-V] + -u The url of the action. .e.g to get the power status of node \'nodename\' https://httpserver/xcatws/nodes/nodename/power + -m The method of the request + -h The hostname of the xCAT web server + -o Target object + -f The output format of the requested action + -V Display the verbose message\n"; + +# Parse the argument +$Getopt::Long::ignorecase = 0; +Getopt::Long::Configure( "bundling" ); +if (!GetOptions( 'u=s' => \$::URL, + 'f=s' => \$::FORMAT, + 'h=s' => \$::HOST, + 'o=s' => \$::OBJ, + 'm=s' => \$::METHOD, + 'V' => \$::VERBOSE )) { + print $usage_string; + exit 1; +} + +if (defined($::FORMAT)) { + if ($::FORMAT eq "") { + $::FORMAT = "html"; + } elsif ($::FORMAT !~ /^(html|json|xml)$/) { + print $usage_string; + exit 1; + } +} +else{ + $::FORMAT = "html"; +} + +if (defined($::METHOD)){ + if ($::METHOD !~/^(GET|PUT|POST|DELETE)$/){ + print $usage_string; + exit 1; + } +} +else{ + print $usage_string; + exit 1; +} + +if (!$::URL) { + if ($::HOST && $::OBJ) { + $::URL = "https://$::HOST/xcatws/$::OBJ?format=$::FORMAT"; + } else { + print $usage_string; + exit 1; + } +} else { + if ($::URL =~ /\?/){ + $::URL .= "&format=$::FORMAT"; + } + else{ + $::URL .= "?format=$::FORMAT"; + } +} + +my @updatearray; +my $fieldname; +my $fieldvalue; +if (scalar(@ARGV) > 0){ + foreach my $tempstr (@ARGV){ + push @updatearray, $tempstr; + } +} + +my $request; + +my $ua = LWP::UserAgent->new(); +my $response; +if (($::METHOD eq 'PUT') || ($::METHOD eq 'POST')){ + my $tempstr = encode_json \@updatearray; + $request = HTTP::Request->new($::METHOD => $::URL); + $request->header('content-type' => 'text/plain'); + $request->header('content-length' => length($tempstr)); + $request->content($tempstr); +} +elsif(($::METHOD eq 'GET'|| ($::METHOD eq 'DELETE'))){ + $request = HTTP::Request->new($::METHOD=>$::URL); +} + +my $response = $ua->request($request); + +print $response->content . "\n"; +print $response->code . "\n"; +print $response->message . "\n"; + diff --git a/xCAT-server/xCAT-wsapi/xcatws-test.sh b/xCAT-server/xCAT-wsapi/xcatws-test.sh new file mode 100644 index 000000000..f8a592984 --- /dev/null +++ b/xCAT-server/xCAT-wsapi/xcatws-test.sh @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +# test & doc all current calls +# finish test driver (including calling it natively from perl) +# restructure & comment code +# add debugging and fix bugs: +# - all put and post calls +# - and allow put data in url args too +# change structure of json and add Returns lines to doc +# add missing functionality +# - nodeset stat +# - osimage create and change and delete and copycds +# do perf test and optimize code + +curl -X GET -k 'http://127.0.0.1/xcatws/nodes?userName=bp&password=and9ew88&format=xml' +curl -X GET -k 'https://9.114.34.210/xcatws/nodes?userName=bp&password=bryan1&format=xml' +curl -X GET -k 'https://9.114.34.210/xcatws/nodes?userName=bp&password=bryan1&format=xml&field=mac' +curl -X GET -k 'https://9.114.34.210/xcatws/nodes/test001-test006?userName=bp&password=bryan1&format=xml' +curl -X GET -k 'https://9.114.34.210/xcatws/nodes/test001-test006?userName=bp&password=bryan1&format=xml&field=mac' +#curl -X PUT -k --data '{"room":"foo"}' 'https://127.0.0.1/xcatws/nodes/test001?userName=bp&password=bryan1' +#curl -X POST -k --data '{"groups":"compute,all"}' 'https://127.0.0.1/xcatws/nodes/test001?userName=bp&password=bryan1' +curl -X DELETE -k 'http://127.0.0.1/xcatws/nodes/test001?userName=bp&password=and9ew88' + +./restapi -u "https://127.0.0.1/xcatws/nodes/test001?userName=bp&password=bryan1" -m GET +./restapi -u "https://10.1.0.210/xcatws/nodes/test001?userName=bp&password=bryan1" -m PUT "nodepos.room=foo" \ No newline at end of file