#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## 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 CondorTest;
use CondorUtils;
use Check::SimpleJob;
use CheckOutputFormats;
use strict;
use warnings;

my $testname = "cmd_q_formats";

# 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;

################################################
########### test -nobatch (all run) ############
################################################
$command_arg = '-nobatch';
$arguments = 60;
$ClusterId = 100;
$pid = $$;
$submit_content = 
"executable = $executable
arguments = $arguments;
queue 4";
$submitfile = "$testname$pid.sub";

emit_dag_files($testname,$submit_content,$pid);

%Attr_RAW = dry_run($testname,$pid,$ClusterId,$executable);

# custom job status
for my $i (0..(scalar keys %Attr_RAW)-1){
	$Attr_RAW{$i}{JobStatus} = 2;
}

%Attr = add_status_ads(\%Attr_RAW);
%Attr = change_clusterid(\%Attr,$ClusterId);
%Attr = multi_owners(\%Attr);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg);
%other = %{$other_ref};
%data = %{$data_ref};
@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_heading($command_arg,\%data) && check_data(\@fields,\%Attr,$command_arg,\%cluster_batch,\%user_batch)){
	RegisterResult(check_summary($command_arg,\@summary,\%cnt_num),check_name => '-nobatch (run)',test_name=>$testname);
} else {
	RegisterResult(0,check_name => '-nobatch (run)', test_name => $testname);
}

#############################################################
######## test -hold (different hold reasons) #############
#############################################################
$command_arg = '-hold';
$ClusterId = 100;
$arguments = 60;
$pid = $$;
$submit_content = 
"executable = $executable
arguments = $arguments
queue 4";
$submitfile = "$testname$pid.sub";

emit_dag_files($testname,$submit_content,$pid);

%Attr_RAW = dry_run($testname,$pid,$ClusterId,$executable);

# custom job status
for my $i (0..(scalar keys %Attr_RAW)-1){
	$Attr_RAW{$i}{JobStatus} = 5;
}

%Attr = various_hold_reasons(\%Attr_RAW);
%Attr = add_status_ads(\%Attr);
%Attr = change_clusterid(\%Attr,$ClusterId);
%Attr = multi_owners(\%Attr);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg);
%other = %{$other_ref};
%data = %{$data_ref};
@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_heading($command_arg,\%data) && check_data(\@fields,\%Attr,$command_arg,\%cluster_batch,\%user_batch)){
	RegisterResult(check_summary($command_arg,\@summary,\%cnt_num),check_name => '-nobatch (run)',test_name=>$testname);
} else {
	RegisterResult(0,check_name => '-hold (different hold reasons)', test_name => $testname);
}

#################################################################
########################## test -batch ##########################
#################################################################
$command_arg = '-batch';
$ClusterId = 100;
$pid = $$;
$arguments = 60;
$submit_content = 
"executable = $executable
arguments = $arguments
queue";
$submitfile = "$testname$pid.sub";
emit_dag_files($testname,$submit_content,$pid);
my %Attr_RAW1 = dry_run($testname,$pid,$ClusterId,$executable);
$Attr_RAW1{0}{ClusterId} = $ClusterId;

$ClusterId = 101;
$submit_content = 
"executable = $executable
arguments = $arguments
queue";
emit_dag_files($testname,$submit_content,$pid);
my %Attr_RAW2 = dry_run($testname,$pid,$ClusterId,$executable);
$Attr_RAW2{0}{ClusterId} = $ClusterId;

$submit_content = 
"executable = $executable
arguments = $arguments
queue";
$ClusterId = 102;
emit_dag_files($testname,$submit_content,$pid);
my %Attr_RAW3 = dry_run($testname,$pid,$ClusterId,$executable);
$Attr_RAW3{0}{ClusterId} = $ClusterId;

$submit_content =
"executable = $executable
arguments = $arguments
queue";
$ClusterId = 103;
emit_dag_files($testname,$submit_content,$pid);
my %Attr_RAW4 = dry_run($testname,$pid,$ClusterId,$executable);
$Attr_RAW4{0}{ClusterId} = $ClusterId;

# custom job status
$Attr_RAW1{0}{JobStatus} = 1;
$Attr_RAW2{0}{JobStatus} = 2;
$Attr_RAW3{0}{JobStatus} = 2;
$Attr_RAW4{0}{JobStatus} = 5;

%{$Attr_RAW{0}} = %{$Attr_RAW1{0}};
%{$Attr_RAW{1}} = %{$Attr_RAW2{0}};
%{$Attr_RAW{2}} = %{$Attr_RAW3{0}};
%{$Attr_RAW{3}} = %{$Attr_RAW4{0}};

%Attr = various_hold_reasons(\%Attr_RAW);
%Attr = add_status_ads(\%Attr);
#%Attr = change_clusterid(\%Attr,100);
%Attr = multi_owners(\%Attr);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg);
%other = %{$other_ref};
%data = %{$data_ref};
for my $i (0..3){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_heading($command_arg,\%data) && check_data(\@fields,\%Attr,$command_arg,\%cluster_batch,\%user_batch)){
	RegisterResult(check_summary($command_arg,\@summary,\%cnt_num),check_name => '-batch',test_name=>$testname);
} else {
	RegisterResult(0,check_name => '-batch', test_name => $testname);
}

########################################################
#######  test -tot (multiple job states) ###############
########################################################
$command_arg = '-tot';
$ClusterId = 100;
$pid = $$;
$arguments = 60;
$submit_content = 
"executable = $executable
arguments = $arguments
queue 4";
$submitfile = "$testname$pid.sub";

emit_dag_files($testname,$submit_content,$pid);

%Attr_RAW = dry_run($testname,$pid,$ClusterId,$executable);

# custom job status
$Attr_RAW{0}{JobStatus} = 2;
$Attr_RAW{1}{JobStatus} = 2;
$Attr_RAW{2}{JobStatus} = 1;
$Attr_RAW{3}{JobStatus} = 5;
$Attr_RAW{3}{HoldReasonCode} = 1;

%Attr = add_status_ads(\%Attr_RAW);
%Attr = change_clusterid(\%Attr,$ClusterId);
%Attr = multi_owners(\%Attr);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg);
%other = %{$other_ref};
%data = %{$data_ref};
@summary = @{$summary_ref};

if (scalar keys %data > 0){
	@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_heading($command_arg,\%data) && check_data(\@fields,\%Attr,$command_arg,\%cluster_batch,\%user_batch)){
		RegisterResult(check_summary($command_arg,\@summary,\%cnt_num),check_name => '-tot (multiple states)',test_name=>$testname);
	} else {
		RegisterResult(0,check_name => '-tot (multiple job states)', test_name => $testname);
	}
} else {
	%cnt_num = count_job_states(\%Attr);
	RegisterResult(check_summary($command_arg,\@summary,\%cnt_num),check_name => '-tot (multiple states)', testname=>$testname);
}

########################################################################
##################### test  -nobatch -run ##############################
########################################################################
$command_arg = '-nobatch -run -wide';
$ClusterId = 100;
$pid = $$;
$arguments = 60;
$submit_content = 
"executable = $executable
arguments = $arguments
queue 4";
$submitfile = "$testname$pid.sub";

emit_dag_files($testname,$submit_content,$pid);

%Attr_RAW = dry_run($testname,$pid,$ClusterId,$executable);

# custom job states
for my $i (0..(scalar keys %Attr_RAW)-1){
	$Attr_RAW{$i}{JobStatus} = 2;
}

%Attr = add_status_ads(\%Attr_RAW);
%Attr = change_clusterid(\%Attr,$ClusterId);
%Attr = multi_owners(\%Attr);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg);
%other = %{$other_ref};
%data = %{$data_ref};
@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_heading($command_arg,\%data) && check_data(\@fields,\%Attr,$command_arg,\%cluster_batch,\%user_batch)){
	RegisterResult(check_summary($command_arg,\@summary,\%cnt_num),check_name => '-nobatch -run',test_name=>$testname);
} else {
	RegisterResult(0,check_name => '-nobatch -run', test_name => $testname);
}

##############################################################################
################################## test -io ##################################
##############################################################################
$command_arg = '-io';
$ClusterId = 100;
$pid = $$;
$arguments = "This is a test!";
$submit_content = 
"executable = $executable
arguments = $arguments
queue 4";
$submitfile = "$testname$pid.sub";

emit_dag_files($testname,$submit_content,$pid);

%Attr_RAW = dry_run($testname,$pid,$ClusterId,$executable);

# custom job states
$Attr_RAW{0}{JobStatus} = 2;
$Attr_RAW{1}{JobStatus} = 2;
$Attr_RAW{2}{JobStatus} = 2;
$Attr_RAW{3}{JobStatus} = 2;

%Attr = add_status_ads(\%Attr_RAW);
%Attr = add_io_ads(\%Attr,$arguments);
%Attr = change_clusterid(\%Attr,$ClusterId);
%Attr = multi_owners(\%Attr);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg);
%other = %{$other_ref};
%data = %{$data_ref};
@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_heading($command_arg,\%data) && check_data(\@fields,\%Attr,$command_arg,\%cluster_batch,\%user_batch)){
	RegisterResult(check_summary($command_arg,\@summary,\%cnt_num),check_name => '-io',test_name=>$testname);
} else {
	RegisterResult(0,check_name => '-io', test_name => $testname);
}

##############################################################################
########################### test -nobatch -dag ###############################
##############################################################################
$command_arg = '-nobatch -dag -wide:80';
$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);

# custom job states
$Attr_RAW{0}{JobStatus} = 5;
$Attr_RAW{1}{JobStatus} = 2;
$Attr_RAW{2}{JobStatus} = 2;
$Attr_RAW{3}{JobStatus} = 2;
$Attr_RAW{4}{JobStatus} = 5;
$Attr_RAW{5}{JobStatus} = 5;
$Attr_RAW{6}{JobStatus} = 2;
$Attr_RAW{7}{JobStatus} = 5;

%Attr = various_hold_reasons(\%Attr_RAW);
%Attr = add_status_ads(\%Attr);
%Attr = add_dag_ads(\%Attr,$testname);
#%Attr = change_clusterid(\%Attr,$ClusterId);
%Attr = multi_owners(\%Attr);
($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg);
%other = %{$other_ref};
%data = %{$data_ref};
@summary = @{$summary_ref};

@fields = split_fields(\%data);
%cnt_num = count_job_states(\%Attr);
# remove dag nodes from counts, since the totals lines don't count them anymore.
$cnt_num{0} -= 3; $cnt_num{2} -= 3;
($cluster_batch_ref,$user_batch_ref) = make_batch(\%Attr);
%cluster_batch = %{$cluster_batch_ref};
%user_batch = %{$user_batch_ref};

if (check_heading($command_arg,\%data) && check_data(\@fields,\%Attr,$command_arg,\%cluster_batch,\%user_batch)){
	RegisterResult(check_summary($command_arg,\@summary,\%cnt_num), check_name=>$command_arg, test_name=>$testname);
} else {
	RegisterResult(0, check_name=>$command_arg, test_name=>$testname);
}


##############################################################################
############################# test -globus ###################################
##############################################################################
#$command_arg = '-globus';
#$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);
#
## custom job states
#$Attr_RAW{0}{JobStatus} = 5;                                                                      
#$Attr_RAW{1}{JobStatus} = 2;                                                                      
#$Attr_RAW{2}{JobStatus} = 2;                                                                      
#$Attr_RAW{3}{JobStatus} = 2;                                                                      
#$Attr_RAW{4}{JobStatus} = 5;                                                                      
#$Attr_RAW{5}{JobStatus} = 5;                                                                      
#$Attr_RAW{6}{JobStatus} = 2;                                                                      
#$Attr_RAW{7}{JobStatus} = 5;                                                                      

#%Attr = various_hold_reasons(\%Attr_RAW);                                                         
#%Attr = add_status_ads(\%Attr);
#%Attr = add_globus_ads(\%Attr,$testname);
#%Attr = change_clusterid(\%Attr,$ClusterId);                                                      
#%Attr = multi_owners(\%Attr);
#($other_ref,$data_ref,$summary_ref) = create_table(\%Attr,$testname,$command_arg);                
#%other = %{$other_ref};                                                                           
#%data = %{$data_ref};
#for my $k (sort keys %data){print "$data{$k}\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_heading($command_arg,\%data) && check_data(\@fields,\%Attr,$command_arg,\%cluster_batch,\%user_batch)){
#        RegisterResult(check_summary($command_arg,\@summary,\%cnt_num),check_name => '-globus',test_name=>$testname);
#} else {
#        RegisterResult(0,check_name => '-globus', test_name => $testname);                        
#}       

###########################################################################
############################## test -grid #################################
###########################################################################
#$command_arg = '-grid';
#$ClusterId = 100;
#$pid = $$;
#$executable = "/bin/echo";
#$arguements = "This is a test!";
#submit_content =
#"Universe = grid
#grid_resource = condor condorce.example.com condorce.example.com:9619
#arguments = $arguments
#output = $testname$command_arg.out
#error = $testname$comand_arg.err
#log = $testname$command_arg.log
#ShouldTransferFiles = YES
#WhenToTransferOutput = ON_EXIT
#queue";
#$submitfile = "$testname$pid.sub";
#
#emit_dag_files($testname,$submit_content,$pid);
#%Attr_RAW = dry_run($testname,$pid,$ClusterId,$executable);
#
#
CondorTest::EndTest();
exit(0);
