#! /usr/bin/env perl
#
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
##
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
##
##    http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************
use File::Copy;
use CondorTest;
use CondorUtils;
use Check::SimpleJob;
use CheckOutputFormats;
use strict;
use warnings;

my $testname = "cmd_q_format_file";

# needs to specify every time before a check
my $command_arg;
my $ClusterId;
my $pid;
my $executable = "x_sleep.pl";
my $arguments;
my $submit_content;
my $submitfile;

# other variables, updated through each check
my %Attr_RAW;
my %Attr;
my @fields;
my %other;
my %data;
my @summary;
my $other_ref;
my $data_ref;
my $summary_ref;
my %cnt_num;
my %cluster_batch;
my %user_batch;
my $cluster_batch_ref;
my $user_batch_ref;
my $option;
my @table;

# elements of the format file
my $format_file;
my $format_content;

###############################################
$command_arg = "-pr";
$option = "normal";
$ClusterId = 100;
$pid = $$;
$arguments = 600;
$submit_content = 
"executable = $executable
arguments = $arguments
queue 8";
$submitfile = "$testname$pid.sub";
emit_dag_files($testname,$submit_content,$pid);

%Attr_RAW = dry_run($testname,$pid,$ClusterId,$executable);
for my $i (0..3){
	$Attr_RAW{$i*2}{JobStatus} = 5;
	$Attr_RAW{$i*2+1}{JobStatus} = 2;
}

%Attr = various_hold_reasons(\%Attr_RAW);
%Attr = add_status_ads(\%Attr);
%Attr = change_clusterid(\%Attr,$ClusterId);
%Attr = multi_owners(\%Attr);

$format_file = "$option.cpf";
$format_content = 
"SELECT
   ClusterId     AS \" ID\"  NOSUFFIX WIDTH 4
   ProcId        AS \" \"    NOPREFIX             PRINTF \".%-3d\"
   Owner         AS \"OWNER\"         WIDTH -14   PRINTAS OWNER
   QDate         AS \"  SUBMITTED\"   WIDTH 11    PRINTAS QDATE
   {RemoteUserCpu,RemoteWallClockTime,ShadowBday,ServerTime}[0] AS \"    RUN_TIME\"  WIDTH 12    PRINTAS CPU_TIME
   JobStatus     AS ST                          PRINTAS JOB_STATUS
   JobPrio       AS PRI
   ImageSize     AS SIZE              WIDTH 4     PRINTAS MEMORY_USAGE
   Cmd           AS CMD               WIDTH -18   PRINTAS JOB_DESCRIPTION
SUMMARY STANDARD";

open(FH,">$format_file") || print "FAILED opening file $format_file\n";
print FH $format_content;
close(FH);

($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg,$format_file);
%other = %{$other_ref};
%data = %{$data_ref};
#for my $i (0..(scalar keys %data) -1){print $data{$i},"\n";}
@summary = @{$summary_ref};

@fields = split_fields(\%data);
%cnt_num = count_job_states(\%Attr);
($cluster_batch_ref,$user_batch_ref) = make_batch(\%Attr);
%cluster_batch = %{$cluster_batch_ref};
%user_batch = %{$user_batch_ref};
if (check_pr(\%data,$option) && check_data(\@fields,\%Attr,$command_arg,\%cluster_batch,\%user_batch)){
	RegisterResult(check_summary('have_sum',\@summary,\%cnt_num),check_name => $option, test_name=>$testname);
} else {	
	RegisterResult(0,check_name => $option, test_name=>$testname);
}

###################################################
$option = "q_totals";
$format_file = "$option.cpf";
$format_content = 
"SELECT NOHEADER NOTITLE
SUMMARY STANDARD";

open(FH,">$format_file") || print "FAILED opening file $format_file\n";
print FH $format_content;
close(FH);

($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg,$format_file);
#%other = %{$other_ref};
#%data = %{$data_ref};
#for my $i (0..(scalar keys %data) -1){print $data{$i},"\n";}
@summary = @{$summary_ref};

RegisterResult(check_summary('have_sum',\@summary,\%cnt_num),check_name => $option, test_name=>$testname);

###################################################
$option = "printf";
$format_file = "$option.cpf";
$format_content = 
"SELECT
ClusterId AS ID PRINTF %4d
NumJobStarts AS SIZE PRINTF %-3d
ProcId AS PID PRINTF %5.2f";

open(FH,">$format_file") || print "FAILED opening file $format_file\n";
print FH $format_content;
close(FH);

($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg,$format_file);
%other = %{$other_ref};
%data = %{$data_ref};
#for my $i (0..(scalar keys %data) -1){print $data{$i},"\n";}

RegisterResult(check_pr(\%data,$option,\%Attr),check_name => $option, test_name => $testname);
###################################################
$option = "random";
$format_file = "$option.cpf";
$format_content = 
"SELECT
Cmd AS V1 WIDTH -15
GlobalJobId AS V2 -35
DiskUsage AS V3 WIDTH -10
NumShadowStarts AS V4 WIDTH -4
WhenToTransferOutput AS V5 -10 
SUMMARY NONE";

open(FH,">$format_file") || print "FAILED opening file $format_file\n";
print FH $format_content;
close(FH);

($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg,$format_file);
%other = %{$other_ref};
%data = %{$data_ref};
#for my $i (0..(scalar keys %data) -1){print $data{$i},"\n";}

@fields = split_fields(\%data);
my $result = 1;
for my $i (1..8){if (!(unquote($Attr{$i-1}{Cmd}) =~ /$fields[0][$i]/)){$result = 0; last;}}
for my $i (1..8){if (!(unquote($Attr{$i-1}{GlobalJobId}) =~/$fields[1][$i]/)){$result = 0; last;}}
for my $i (1..8){if (!($Attr{$i-1}{DiskUsage} =~ /$fields[2][$i]/)){$result = 0; last;}}
for my $i (1..8){if (!($Attr{$i-1}{NumShadowStarts} =~ /$fields[3][$i]/)){$result = 0; last;}}
for my $i (1..8){if (!(unquote($Attr{$i-1}{WhenToTransferOutput}) =~ /$fields[4][$i]/)){$result = 0; last;}}

RegisterResult($result,check_name => $option, test_name=>$testname);

######################################################
$option = "where2";
$format_file = "$option.cpf";
my $sel = "5";
$format_content = 
"SELECT
   ClusterId     AS \" ID\"  NOSUFFIX WIDTH 6
   ProcId        AS \" \"    NOPREFIX             PRINTF \".%-3d\"
   Owner         AS \"OWNER\"         WIDTH -10   PRINTAS OWNER
   QDate         AS \"  SUBMITTED\"   WIDTH 11    PRINTAS QDATE
   {RemoteUserCpu,RemoteWallClockTime,ShadowBday,ServerTime}[0] AS \"    RUN_TIME\"  WIDTH 12    PRINTAS CPU_TIME
   JobStatus     AS ST                          PRINTAS JOB_STATUS
   NumJobStarts  AS RUNS
   ImageSize     AS SIZE            WIDTH 6     PRINTAS MEMORY_USAGE
   Cmd           AS CMD             WIDTH -18   PRINTAS JOB_DESCRIPTION
WHERE JobStatus==$sel
SUMMARY STANDARD";
open(FH,">$format_file") || print "FAILED opening file $format_file\n";
print FH $format_content;
close(FH);

($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg,$format_file);
%other = %{$other_ref};
%data = %{$data_ref};
#for my $i (0..(scalar keys %data) -1){print $data{$i},"\n";}

RegisterResult(check_pr(\%data,$option,\%Attr),check_name=>$option,test_name=>$testname);


######################################################
$option = "short";
$format_file = "$option.cpf";
$format_content = 
"SELECT
Owner AS OWNER WIDTH -5
JobStatus AS ST WIDTH -2
ImageSize AS SIZE WIDTH -4
Cmd AS CMD WIDTH -3";

open(FH,">$format_file") || print "FAILED opening file $format_file\n";
print FH $format_content;
close(FH);

($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg,$format_file);
%other = %{$other_ref};
%data = %{$data_ref};
#for my $i (0..(scalar keys %data) -1){print $data{$i},"\n";}
@summary = @{$summary_ref};

RegisterResult(check_pr(\%data,$option),check_name => $option, test_name => $testname);

#############################################################
$option = "ifthenelse";
$format_file = "$option.cpf";
$format_content = 
"SELECT
IfThenElse(Progress=!=undefined,Progress,0.0)  AS V1 PRINTF %5.1f
IfThenElse(Owner isnt undefined,Owner,\"admin\") AS V2 WIDTH 20";
open(FH,">$format_file") || print "FAILED opening file $format_file\n";
print FH $format_content;
close(FH);

($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg,$format_file);
%other = %{$other_ref};
%data = %{$data_ref};
#for my $i (0..(scalar keys %data) -1){print $data{$i},"\n";}
@summary = @{$summary_ref};

RegisterResult(check_pr(\%data,$option,\%Attr),check_name => $option, test_name => $testname);

########################################################
$option = "config -nobatch";
my $local_config = `condor_config_val LOCAL_CONFIG_FILE`;
$local_config =~ s/\n//;
print "local config file is $local_config\n";

my $local_dir = `condor_config_val LOCAL_DIR`;
$local_dir =~ s/\n//;
my $extra_config = "$local_dir/$testname.cfg";
open (FHOUT, ">>$local_config");
print FHOUT "include ifexist : $extra_config\n";
close FHOUT;

print "LOCAL_DIR is $local_dir\n";
print "Creating temporary extra config file: '$extra_config'\n";

open (FHOUT, ">$extra_config");
print FHOUT "Q_DEFAULT_PRINT_FORMAT_FILE=q_totals.cpf\n";
print FHOUT "Q_DEFAULT_RUN_PRINT_FORMAT_FILE=normal.cpf\n";
print FHOUT "Q_DEFAULT_HOLD_PRINT_FORMAT_FILE=q_totals.cpf\n";
print FHOUT "Q_DEFAULT_GOODPUT_PRINT_FORMAT_FILE=normal.cpf\n";
print FHOUT "Q_DEFAULT_GRID_PRINT_FORMAT_FILE=normal.cpf\n";

close FHOUT;

($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,"-run -nobatch");
%data = %{$data_ref};
print $data{0};
$result = check_heading("-nobatch",\%data);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,"-goodput");
%data = %{$data_ref};
$result = $result && check_heading("-nobatch",\%data);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,"-grid");
%data = %{$data_ref};
$result = $result && check_heading("-nobatch",\%data);

my $ads_file = write_ads(\%Attr,$testname);
my @summary1 = `condor_q -job $ads_file -nobatch`;
my @summary2 = `condor_q -job $ads_file -hold`; 
my $totline = $summary1[1];
print "totline is: $totline";
$totline =~ s/^[^:]+:\s+//;
print "totline is now: $totline";
@summary1 = split / /, $totline;
$result = $result && check_summary('have_sum',\@summary1,\%cnt_num) && ($summary2[1] =~ /4 jobs; 0 completed, 0 removed, 0 idle, 0 running, 4 held, 0 suspended/);

RegisterResult($result,check_name=>$option, test_name=>$testname);

############################################################
$option = "config -batch_is_default=false";

open (FHOUT, ">$extra_config");
print FHOUT "CONDOR_Q_DASH_BATCH_IS_DEFAULT=false\n";
print FHOUT "Q_DEFAULT_PRINT_FORMAT_FILE=q_totals.cpf\n";
print FHOUT "Q_DEFAULT_RUN_PRINT_FORMAT_FILE=normal.cpf\n";
print FHOUT "Q_DEFAULT_HOLD_PRINT_FORMAT_FILE=q_totals.cpf\n";
print FHOUT "Q_DEFAULT_GOODPUT_PRINT_FORMAT_FILE=normal.cpf\n";
print FHOUT "Q_DEFAULT_GRID_PRINT_FORMAT_FILE=normal.cpf\n";
close FHOUT;

($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,"-run");
%data = %{$data_ref};
$result = check_heading("-nobatch",\%data);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,"-goodput");
%data = %{$data_ref};
$result = $result && check_heading("-nobatch",\%data);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,"-grid");
%data = %{$data_ref};
$result = $result && check_heading("-nobatch",\%data);

$ads_file = write_ads(\%Attr,$testname);
@summary1 = `condor_q -job $ads_file`;
@summary2 = `condor_q -job $ads_file -hold`; 
$totline = $summary1[1];
print "totline is: $totline";
$totline =~ s/^[^:]+:\s+//;
print "totline is now: $totline";
@summary1 = split / /, $totline;
$result = $result && check_summary('have_sum',\@summary1,\%cnt_num) && ($summary2[1] =~ /4 jobs; 0 completed, 0 removed, 0 idle, 0 running, 4 held, 0 suspended/);

RegisterResult($result,check_name=>$option, test_name=>$testname);

print "deleting extra config file '$extra_config'\n";
unlink ($extra_config);

CondorTest::EndTest();
exit(0);

