Changed cvs_man.php -> scm_man.php.
[clearscm.git] / cc / testcc.pl
1 #!/bin/bin/perl
2
3 =pod
4
5 =head1 NAME $RCSfile: testcc.pl,v $
6
7 Test Clearcase
8
9 =head1 VERSION
10
11 =over
12
13 =item Author
14
15 Andrew DeFaria <Andrew@ClearSCM.com>
16
17 =item Revision
18
19 $Revision: 1.6 $
20
21 =item Created:
22
23 Tue Apr 10 13:14:15 CDT 2007
24
25 =item Modified:
26
27 $Date: 2011/01/09 01:01:32 $
28
29 =back
30
31 =head1 SYNOPSIS
32
33  Usage testcc.pl: [-u|sage] [-ve|rbose] [-d|ebug]
34                   [-c|onfig <file>] [-vi|ewstore <viewstore>] 
35                   [-vo|bstore <vobstore>]
36
37  Where:
38    -u|sage:     Displays usage
39  
40    -ve|rbose:   Be verbose
41    -d|ebug:     Output debug messages
42
43    -c|onfig <file>: Config file (Default: testcc.conf)
44    -vi|ewstore:     Path to view storage area
45    -vo|bstore:      Path to vob storage area
46
47 =head1 DESCRIPTION  
48
49 Clearcase smoke tests. Perform simple Clearcase operations to validate that
50 Clearcase minimally works
51
52 =cut
53
54 use strict;
55 use warnings;
56
57 use FindBin;
58 use Getopt::Long;
59 use Cwd;
60 use Term::ANSIColor qw(:constants);
61
62 my $libs;
63
64 BEGIN {
65   $libs = $ENV{SITE_PERLLIB} ? $ENV{SITE_PERLLIB} : "$FindBin::Bin/../lib";
66
67   die "Unable to find libraries\n" 
68     unless -d $libs;
69 } # BEGIN
70
71 use lib $libs;
72
73 use Clearcase;
74 use Clearcase::Element;
75 use Clearcase::View;
76 use Clearcase::Views;
77 use Clearcase::Vob;
78 use Clearcase::Vobs;
79 use DateUtils;
80 use Display;
81 use GetConfig;
82 use Logger;
83 use OSDep;
84 use Utils;
85
86 # Globals
87 my $VERSION = '2.0';
88
89 my ($vbs, $vws, %default_opts, %opts);
90
91 my $log      = Logger->new;
92 my $view     = $Clearcase::VIEWTAG_PREFIX;
93 my $view_tag = $FindBin::Script;
94 my $vob      = $ENV{TMP} ? $ENV{TMP} : "/tmp"; # Private vob - mount to /tmp!
95 my $vob_tag  = $view_tag;
96
97 my ($test_view, $test_vob);
98
99 # LogOpts: Log the %opts has to the log file so we can tell the options used for
100 # this run.
101 sub LogOpts () {
102   $log->msg (
103     "$FindBin::Script v$VERSION run at " 
104   . YMDHM
105   . " with the following options:"
106   );
107
108   foreach (sort keys %opts) {
109     if (ref $opts{$_} eq "ARRAY") {
110       my $name = $_;
111       $log->msg ("$name:\t$_") foreach (@{$opts{$_}});
112     } else {
113       $log->msg ("$_:\t$opts{$_}");
114     }  # if
115   } # foreach
116   
117   return;
118 } # LogOpts
119
120 sub CreateVob () {
121   $log->msg ("Creating vob $vob/$vob_tag");
122
123   $test_vob = Clearcase::Vob->new ("$vob/$vob_tag");
124
125   my ($status, @output) = $test_vob->create ($opts{vobhost}, $vbs);
126
127   $log->log ($_) foreach (@output);
128
129   if ($status != 0) {
130     if ($output[0] =~ /already exists/) {
131       $log->warn ("Vob " . $test_vob->tag . " already exists");
132       return 0;
133     } # if
134   } # if
135
136   return $status;
137 } # CreateVob
138
139 sub MountVob () {
140   $log->msg ("Mounting vob " . $test_vob->tag);
141
142   # Create mount directory
143   my ($status, @output) = Execute "mkdir -p " . $test_vob->tag . " 2>&1";
144
145   $log->log ($_) foreach (@output);
146
147   ($status, @output) = $test_vob->mount;
148
149   $log->log ($_) foreach (@output);
150
151   return $status;
152 } # MountVob
153
154 sub DestroyVob () {
155   my ($status, @output);
156
157   ($status, @output) = $Clearcase::CC->execute ("cd");
158
159   $log->msg ("Unmounting vob " . $test_vob->tag);
160
161   ($status, @output) = $test_vob->umount;
162
163   $log->msg ("Removing vob " . $test_vob->tag);
164
165   ($status, @output) = $test_vob->remove;
166
167   $log->log ($_) foreach (@output);
168
169   ($status, @output) = Execute "rmdir " . $test_vob->tag;
170
171   $log->log ($_)
172     foreach (@output);
173
174   return $status;
175 } # DestroyVob
176
177 sub CreateView () {
178   $log->msg ("Creating view $view_tag");
179
180   $test_view = Clearcase::View->new ($view_tag);
181
182   my ($status, @output) = $test_view->create ($opts{viewhost}, $vws);
183
184   $log->log ($_) foreach (@output);
185
186   if ($status != 0) {
187     if ($output[0] =~ /already exists/) {
188       $log->warn ("View " . $test_view->tag . " already exists");
189       return 0;
190     } # if
191   } # if
192
193   return $status;
194 } # CreateView
195
196 sub SetView () {
197   $log->msg ("Setting view $test_view->tag");
198
199   my ($status, @output) = $test_view->set;
200
201   $log->log ($_) foreach (@output);
202
203   return $status;
204 } # SetView
205
206 sub DestroyView () {
207   $log->msg ("Removing view " . $test_view->tag);
208
209   my ($status, @output) = $Clearcase::CC->execute ("cd");
210
211   $log->log ($_) foreach (@output);
212
213   chdir $ENV{HOME}
214     or $log->err ("Unable to chdir $ENV{HOME}", 1);
215
216   ($status, @output) = $test_view->remove;
217
218   $log->log ($_) foreach (@output);
219
220   return $status;
221 } # DestroyView
222
223 sub CreateViewPrivateFiles (@) {
224   my (@elements) = @_;
225
226   $log->msg ("Creating test files");
227
228   foreach (@elements) {
229     my $file;
230
231     $log->msg ("Creating $_");
232
233     open $file, ">>", $_
234       or $log->err ("Unable to open $_ for writing - $!", 1);
235
236     print $file "This is file $_\n";
237
238     close $file;
239   } # foreach
240   
241   return;
242 } # CreateViewPrivateFiles
243
244 sub CheckOut ($) {
245   my ($element) = @_;
246
247   my ($status, @output);
248
249   if (ref $element eq "ARRAY") {
250     foreach (@{$element}) {
251       $log->msg ("Checking out $_");
252
253       my $newElement = Clearcase::Element->new ($_);
254
255       ($status, @output) = $newElement->checkout;
256
257       $log->log ($_) foreach (@output);
258       $log->err ("Unable to check out $_", $status) if $status;
259     } # foreach
260   } else {
261     $log->msg ("Checking out $element");
262
263     my $newElement = Clearcase::Element->new ($element);
264
265     ($status, @output) = $newElement->checkout;
266
267     $log->log ($_) foreach (@output);
268     $log->err ("Unable to check out $element", $status) if $status;
269   } # if
270   
271   return;
272 } # CheckOut
273
274 sub CheckIn ($) {
275   my ($element) = @_;
276
277   my ($status, @output);
278
279   if (ref $element eq "ARRAY") {
280     foreach (@{$element}) {
281       $log->msg ("Checking in $_");
282
283       my $newElement = Clearcase::Element->new ($_);
284
285       ($status, @output) = $newElement->checkin;
286
287       $log->log ($_) foreach (@output);
288       $log->err ("Unable to check in $_", $status) if $status;
289     } # foreach
290   } else {
291     $log->msg ("Checking in $element");
292
293     my $newElement = Clearcase::Element->new ($element);
294
295     ($status, @output) = $newElement->checkin;
296
297     $log->log ($_) foreach (@output);
298     $log->err ("Unable to check in $element", $status) if $status;
299   } # if
300   
301   return;
302 } # CheckIn
303
304 sub ComparingFiles (@) {
305   my (@elements) = @_;
306
307   foreach (@elements) {
308     my @lines = ReadFile $_;
309
310     $log->err ("Element $_ should contain only two lines", 2) if scalar @lines != 2;
311   } # foreach
312   
313   return;
314 } # ComparingFiles
315
316 sub MakeElements (@) {
317   my (@elements) = @_;
318
319   foreach (@elements) {
320     $log->msg ("Mkelem $_");
321
322     my $newElement = Clearcase::Element->new ($_);
323
324     my ($status, @output) = $newElement->mkelem;
325
326     $log->log ($_) foreach (@output);
327     $log->err ("Unable to make $_ an element", $status) if $status;
328   } # foreach
329   
330   return;
331 } # MakeElements
332
333 sub RunTests () {
334   # Simple tests:
335   #
336   #   . Create a few elements
337   #   . Check them in
338   #   . Check them out
339   #   . Modify them
340   #   . Check them in
341   #
342   # Assumptions:
343   #
344   #   . $vob_tag is already created
345   #   . $view_tag is already created
346   #   . View is set and we are in the vob
347   #   . There are no vob elements for @elements
348   my @elements = (
349     "cctest.h",
350     "ccsetup.c",
351     "cctest.c",
352     "Makefile",
353   );
354
355   $log->msg ("Removing test files");
356
357   unlink $_ foreach (@elements);
358
359   $log->msg ("Creating view private files");
360
361   CreateViewPrivateFiles        $log, @elements;
362
363   $log->msg ("Making elements");
364
365   CheckOut      '.';
366   MakeElements  @elements;
367   CheckIn       \@elements;
368   CheckIn       '.';
369
370   $log->msg ("Checking out files");
371
372   CheckOut \@elements;
373
374   $log->msg ("Modifying files");
375
376   CreateViewPrivateFiles @elements;
377
378   $log->msg ("Checking in files");
379
380   CheckIn \@elements;
381
382   $log->msg ("Comparing files");
383
384   ComparingFiles @elements;
385
386   $log->msg ("$FindBin::Script: End Tests");
387
388   return 0;
389 } # RunTests
390
391 sub Cleanup () {
392   my $status = 0;
393
394   $log->msg ("Cleaning up");
395
396   if ($test_view && $test_view->exists) {
397     $status += DestroyView;
398   } # if
399
400   if ($test_vob && $test_vob->exists) {
401     $status += DestroyVob;
402   } # if
403
404   return $status;
405 } # Cleanup
406
407 sub SetupTest () {
408   $log->msg ("Setup test environment");
409
410   my $status += CreateVob;
411
412   return $status if $status != 0;
413
414   $status += MountVob;
415
416   return $status if $status != 0;
417
418   $status += CreateView;
419
420   return $status if $status != 0;
421
422   $status += $test_view->start;
423
424   my $dir = $Clearcase::VIEWTAG_PREFIX . $test_view->tag . $test_vob->tag;
425
426   chdir $dir
427     or $log->err ("Unable to chdir to $dir", $status++);
428
429   my @output;
430   
431   ($status, @output) = $Clearcase::CC->execute ("cd $dir");
432
433   if ($status != 0) {
434     $log->log ($_) foreach (@output);
435     $log->err ("Unable to chdir to $dir", $status);
436   } # if
437
438   return $status;
439 } # SetupTest
440
441 my $conf_file = "$FindBin::Script.conf";
442
443 GetOptions (
444   \%opts,
445   "v|verbose"           => sub { set_verbose },
446   "u|usage"             => sub { Usage },
447   "c|onfig=s",
448   "n|etpath=s",
449   "viewstore=s",
450   "vobstore=s",
451 ) or Usage;
452
453 # Read the config file
454 if (-f $conf_file) {
455   %default_opts = GetConfig $conf_file;
456 } else {
457   $log->err ("Unable to find config file $conf_file", 1);
458 } # if
459
460 # Overlay default opts if not specified
461 foreach (keys %default_opts) {
462   $opts{$_} = $default_opts{$_} if !$opts{$_};
463 } # foreach
464
465 $vws = "$opts{viewstore}/$view_tag.vws";
466 $vbs = "$opts{vobstore}/$vob_tag.vbs";
467
468 $log->msg ("START: $FindBin::Script (v$VERSION)");
469
470 LogOpts;
471
472 my $status = SetupTest;
473
474 if ($status == 0) {
475   $status += RunTests;
476 } else {
477   $log->err ("Tests not run. Failure occured in SetupTest - check logfile");
478 } # if
479
480 $status += Cleanup;
481
482 if ($status != 0) {
483   $log->err ("$FindBin::Script failed");
484 } else {
485   $log->msg ("$FindBin::Script passed");
486 } # if
487
488 $log->msg ("END: $FindBin::Script (v$VERSION)");
489
490 exit $status;
491
492 =pod
493
494 =head1 CONFIGURATION AND ENVIRONMENT
495
496 DEBUG: If set then $debug is set to this level.
497
498 VERBOSE: If set then $verbose is set to this level.
499
500 TRACE: If set then $trace is set to this level.
501
502 =head1 DEPENDENCIES
503
504 =head2 Perl Modules
505
506 L<Cwd>
507
508 L<FindBin>
509
510 L<Getopt::Long|Getopt::Long>
511
512 L<Term::ANSIColor|Term::ANSIColor>
513
514 =head2 ClearSCM Perl Modules
515
516 =begin man 
517
518  Clearcase
519  Clearcase::Element
520  Clearcase::View
521  Clearcase::Views
522  Clearcase::Vob
523  Clearcase::Vobs
524  DateUtils
525  Display
526  GetConfig
527  Logger
528  OSDep
529  Utils
530
531 =end man
532
533 =begin html
534
535 <blockquote>
536 <a href="http://clearscm.com/php/scm_man.php?file=lib/Clearcase.pm">Clearcase</a><br>
537 <a href="http://clearscm.com/php/scm_man.php?file=lib/Clearcase/Element.pm">Element</a><br>
538 <a href="http://clearscm.com/php/scm_man.php?file=lib/Clearcase/View.pm">View</a><br>
539 <a href="http://clearscm.com/php/scm_man.php?file=lib/Clearcase/Views.pm">Views</a><br>
540 <a href="http://clearscm.com/php/scm_man.php?file=lib/Clearcase/Vob.pm">Vob</a><br>
541 <a href="http://clearscm.com/php/scm_man.php?file=lib/Clearcase/Vobspm">Vobs</a><br>
542 <a href="http://clearscm.com/php/scm_man.php?file=lib/DateUtils.pm">DateUtils</a><br>
543 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
544 <a href="http://clearscm.com/php/scm_man.php?file=lib/GetConfig.pm">GetConfig</a><br>
545 <a href="http://clearscm.com/php/scm_man.php?file=lib/Logger.pm">Logger</a><br>
546 <a href="http://clearscm.com/php/scm_man.php?file=lib/OSDep.pm">OSDep</a><br>
547 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
548 </blockquote>
549
550 =end html
551
552 =head1 BUGS AND LIMITATIONS
553
554 There are no known bugs in this script
555
556 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
557
558 =head1 LICENSE AND COPYRIGHT
559
560 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
561
562 =cut