7 =head1 NAME $RCSfile: testclearquest.pl,v $
9 Test the Clearquest libary
11 This script tests various functions of the Clearquest library
19 Andrew DeFaria <Andrew@ClearSCM.com>
27 Mon Nov 12 16:50:44 PST 2012
31 $Date: 2013/03/14 23:39:39 $
37 Usage: testclearquest.pl [-u|sage] [-v|erbose] [-d|ebug]
38 [-get] [-add] [-modify] [-change] [-delete]
39 [-username <username>] [-password <password>]
40 [-database <dbname>] [-dbset <dbset>]
41 [-module] [-server <server>] [-port <port>]
44 -usa|ge: Displays usage
46 -de|bug: Output debug messages
54 -use|rname: Username to open database with (Default: from config file)
55 -p|assword: Password to open database with (Default: from config file)
56 -da|tabase: Database to open (Default: from config file)
57 -db|set: Database Set to use (Default: from config file)
58 -m|odule: Type of Clearquest module to use. Must be one of 'api',
59 'client', or 'rest'. The 'api' module can only be used if
60 Clearquest is installed locally. The 'client' module can only
61 be successful if a corresponding server is running. And the
62 'rest' module can only be used if a CQ Web server has been set
63 up and configured (Default: rest)
64 -s|erver: For module = client or rest this is the name of the server that
65 will be providing the service
66 -p|ort: For module = client, this is the point on the server to talk
72 Options are keep in the cq.conf file in etc. They specify the default options
73 listed below. Or you can export the option name to the env(1) to override the
74 defaults in cq.conf. Finally you can programmatically set the options when you
75 call new by passing in a %parms hash. To specify the %parms hash key remove the
76 CQ_ portion and lc the rest.
78 =for html <blockquote>
84 Clearquest server to talk to (Default: From cq.conf)
88 Port to connect to (Default: From cq.conf)
92 The web host to contact with leading http:// (Default: From cq.conf)
96 Name of database to connect to (Default: From cq.conf)
100 User name to connect as (Default: From cq.conf)
104 Password for CQREST_USERNAME (Default: From cq.conf)
108 Database Set name (Default: From cq.conf)
117 use lib "$FindBin::Bin/../lib";
125 my ($cq, %opts, $log);
127 sub displayRecord (%) {
130 $log->msg ('-' x 79);
133 $log->msg ("$_: ", 1);
135 if (ref $record{$_} eq 'ARRAY') {
136 $log->msg (join ", ", @{$record{$_}});
137 } elsif ($record{$_}) {
138 $log->msg ($record{$_});
140 $log->msg ('<undef>');
147 sub displayResults (@) {
151 displayRecord %$_ foreach (@records);
153 $log->msg ('Did not find any records');
159 sub testGetRecord ($$;@) {
160 my ($table, $key, @fields) = @_;
162 my $startTime = time;
164 $log->msg ("Testing get table: $table key: $key");
166 displayRecord $cq->get ($table, $key, @fields);
168 display_duration $startTime, $log;
173 sub testFindRecord ($$;@) {
174 my ($table, $condition, @fields) = @_;
176 my $startTime = time;
178 $log->msg ("Testing find table: $table condition: $condition");
180 my ($result, $nbrRecs) = $cq->find ($table, $condition, @fields);
182 $log->msg ("$nbrRecs records qualified");
184 while (my %record = $cq->getNext($result)) {
185 displayRecord %record;
188 display_duration $startTime, $log;
193 sub testModifyRecord ($$;%) {
194 my ($table, $key, %update) = @_;
196 my $startTime = time;
198 $log->msg ("Testing modify table: $table key: $key");
200 $cq->modify ($table, $key, undef, \%update);
204 display_duration $startTime, $log;
209 sub testChangeState ($$) {
210 my ($table, $key) = @_;
212 my $startTime = time;
214 my %record = $cq->get ($table, $key, ('State'));
216 $cq->checkErr ("Unable to find $table where key = $key");
218 return if $cq->error;
220 my ($action, %update);
222 if ($record{State} eq 'Assigned') {
223 $action = 'AdminAssignToSubmit';
224 $update{Stability_Issue} = 'User Fault';
227 $update{Stability_Issue} = 'Assert';
230 $log->msg ("Testing change state table: $table key: $key action: $action");
232 $cq->modify ($table, $key, $action, \%update);
236 display_duration $startTime, $log;
241 sub testAddRecord ($%) {
242 my ($table, %record) = @_;
244 my $startTime = time;
246 $log->msg ("Testing adding table: $table");
248 $cq->add ($table, \%record);
252 display_duration $startTime, $log;
257 sub testDeleteRecord ($$) {
258 my ($table, $key) = @_;
260 my $startTime = time;
262 $log->msg ("Testing deleting table: $table key: $key");
264 $cq->delete ($table, $key);
268 display_duration $startTime, $log;
276 usage => sub { Usage },
277 verbose => sub { set_verbose },
278 debug => sub { set_debug },
293 my $processStartTime = time;
297 # Translate any options to ones that the lib understands
298 $opts{CQ_USERNAME} = delete $opts{username};
299 $opts{CQ_PASSWORD} = delete $opts{password};
300 $opts{CQ_DATABASE} = delete $opts{database};
301 $opts{CQ_DBSET} = delete $opts{dbset};
302 $opts{CQ_SERVER} = delete $opts{server};
303 $opts{CQ_PORT} = delete $opts{port};
304 $opts{CQ_MODULE} = delete $opts{module};
306 # If nothing is set then do everything
307 unless ($opts{get} or
313 $opts{get} = $opts{add} = $opts{modify} = $opts{change} = 1;
316 # If we are testing add or delete then toggle on the other one
317 $opts{delete} = 1 if $opts{add};
318 $opts{add} = 1 if $opts{delete};
320 my $startTime = time;
324 $cq = Clearquest->new (%opts);
326 $log->msg ('Connecting to Clearquest database ' . $cq->connection, 1);
328 unless ($cq->connect) {
329 $cq->checkErr ('Unable to connect to database ' . $cq->connection, undef, $log);
331 if ($cq->module eq 'client') {
332 $log->msg ('Unable to connect to server ' . $cq->server () . ':' . $cq->port ());
338 display_duration $startTime, $log;
341 $cq->setOpts (emptyStringForUndef => 1);
345 testGetRecord 'WOR', 'XTST100000019';
347 # Get record by condition
348 testFindRecord 'WOR', 'Owner = "ccadm"';
350 # Get record by key with field list
351 testFindRecord 'WOR', 'Owner = "ccadm"', ('id', 'Headline', 'Owner');
353 # Get record by condition with field list
354 testFindRecord 'WOR', 'Owner = "ccadm"', ('id', 'Headline', 'Owner');
359 testAddRecord 'Component', (
360 Name => $FindBin::Script,
361 Description => 'This is a test component',
367 my $newDescription = 'This is a modified test component';
369 testModifyRecord ('Component', $FindBin::Script, (Description => $newDescription));
371 # Make sure the modification happened
372 my %component = $cq->get ('Component', $FindBin::Script, ('Description'));
374 if ($component{Description} ne $newDescription) {
375 $log->err ('Modification of Component.Description failed!');
381 testChangeState 'Defect', 'apd00000034';
386 testDeleteRecord 'Component', $FindBin::Script;
389 $log->msg ('Total process time ', 1);
391 display_duration $processStartTime, $log;