HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Automation of application utilities on Linux

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
utilitieslinuxautomationapplication

Problem

I am working on a project to automate utilities of our application on Linux.
The utilities are basically chunks of programs with specific options for specific tasks. They are run like UNIX commands. We need to validate command outputs and at times the files generated by them.

The automation needs to cover all options for each utility. As per the plan each utility will have its own .pl script and each option will be in a subroutine.
The common functions like checking command output and verifying output files are in a separate module.

Please comment on how this can be made better.

This is a function to test one of the utility options:

``
sub exportMeasUtil01 {
my $test = 'exportMeasUtil01';
my $test_name = "exportMeasure meas, intx intxstring, out";
begin_log( $test, $test_name );

#use the copy function from the File::Copy module
copy ("$INPUT/exportMeasure/DESSTEPS_32138_salesv.csv.rpl", "$DOMAINCOPY/input/salesv.csv.rpl") or die "Copy failed: $!";

#remove all in processed dir
unlink glob "$DOMAINCOPY/input/processed/*";

my $command1 = "loadmeasure -d $DOMAINCOPY -measure salesv";
my $output1 =
$command1;
my $pattern1 = "loadmeasure completed successfully";
my $check1 = check_output( $output1, $pattern1, $test, $command1 );
do { check_teststatus( $test, $test_name ); return; } if $check1 != 0;

my $check2 = checkFilePattern($test, "$DOMAINCOPY/input/processed", "salesv");
do { check_teststatus( $test, $test_name ); return; } if $check2 != 0;

my $command2 = "exportMeasure -d $DOMAINCOPY -out $OUTPUT/exportMeasure/$test.out -meas salesv -intx clssweek";
my $output2 =
$command2`;
my $pattern2 = "exportMeasure completed successfully";
my $check3 = check_output( $output2, $pattern2, $test, $command2 );
do { check_teststatus( $test, $test_name ); return; } if $check3 != 0;

checkFilePattern($test, "$OUTPUT/exportMeasure", "$test");
check_teststatus( $test, $test

Solution

Aside from various style issues which are almost trivial to fix, the big issue is the command that you run in backticks. You construct a single string with variables from outside the scope. You can use a module such as Capture::Tiny to handle all the security and cross platform issues. Since you look like you might be passing data between external sources, taint checking may be appropriate. I cover some of this in Mastering Perl.

You can also reduce some print statements with here docs:

print $FH <<"HERE"
     TEST COMMAND :  $command
     Test Command passed
     $HRLINE
     Command Output :
     $output
     $HRLINE
     HERE

Code Snippets

print $FH <<"HERE"
     TEST COMMAND :  $command
     Test Command passed
     $HRLINE
     Command Output :
     $output
     $HRLINE
     HERE

Context

StackExchange Code Review Q#138692, answer score: 4

Revisions (0)

No revisions yet.