#! /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_transform_ads";

my $ClusterId = 100;
my $pid = $$;
my $executable = "x_sleep.pl";
my $arguments = 600;
my $submit_content;
my $submitfile;
my $rules_content;
my $rules_file;

my %Attr_RAW;
my %Attr;
my $option;
my $ads_file;
my $out_file;

#######################################################################
$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);
$ads_file = write_ads(\%Attr,$testname);

TLOG("$testname : Created $ads_file to use as input for transform tests\n");


#######################################################################
$option = "general";
$out_file = "$testname"."_$option.out";
$rules_file = "$option.rules";
$rules_content = 
"SET ClusterId 200
DEFAULT Fooo \"test\"
COPY ExitBySignal TransferIn
RENAME NumCkpts NumCheckPoints
DELETE TransferInputSizeMB
DELETE /(.+)Err/
TRANSFORM 1";

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

`condor_transform_ads -rules $rules_file -out $out_file $ads_file`;

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

###########################################################################
$option = 'EVAL';
$out_file = "$testname"."_$option.out";
$rules_file = "$option.rules";
$rules_content = 
"EVALSET MemoryUsage ( ResidentSetSize / 100 )
EVALMACRO WantDisk = ( DiskUsage * 2 )
SET RequestDisk ( \$(WantDisk) / 1024 )
TRANSFORM 1";

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

`condor_transform_ads -rules $rules_file -out $out_file $ads_file`;

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

###########################################################################
$option = "transform_num";
$out_file = "$testname"."_$option.out";
$rules_file = "$option.rules";
$rules_content = 
"TRANSFORM 5";

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

`condor_transform_ads -rules $rules_file -out $out_file $ads_file`;

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

###########################################################################
$option = "transform_in1";
$out_file = "$testname"."_$option.out";
$rules_file = "$option.rules";
$rules_content = 
"SET ARG1 \$(argument)
TRANSFORM argument in (2.0, 100, \"test\")";

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

`condor_transform_ads -rules $rules_file -out $out_file $ads_file`;

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

############################################################################
$option = "transform_in2";
$out_file = "$testname"."_$option.out";
$rules_file = "$option.rules";
$rules_content = 
"SET ARG1 \$(argument)
TRANSFORM 2 argument in (2.0, 100, \"test\")";

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

`condor_transform_ads -rules $rules_file -out $out_file $ads_file`;

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

##########################################################################
$option = "transform_from1";
$out_file = "$testname"."_$option.out";
$rules_file = "$option.rules";
$rules_content = 
"SET SIZE_IMAGE \$(macro1)
SET SIZE_DISK \$(macro2)
TRANSFORM macro1, macro2 from (
1000 120.0
2000 128.0
)";

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

`condor_transform_ads -rules $rules_file -out $out_file $ads_file`;

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

###########################################################################
$option = "transform_from2";
$out_file = "$testname"."_$option.out";
open(FH, ">$option.txt") || print "FAILED opening file $option.txt";
print FH "1000 120.0\n2000 128.0";
close(FH);
$rules_file = "$option.rules";
$rules_content =
"SET SIZE_IMAGE \$(macro1)
SET SIZE_DISK \$(macro2)
TRANSFORM macro1, macro2 from $option.txt";

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

`condor_transform_ads -rules $rules_file -out $out_file $ads_file`;

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

############################################################################
$option = "transform_from3";
$out_file = "$testname"."_$option.out";
open(FH, ">$option.txt") || print "FAILED opening file $option.txt";
print FH "1000 120.0\n2000 128.0";
close(FH);
$rules_file = "$option.rules";
$rules_content =
"SET SIZE_IMAGE \$(macro1)
SET SIZE_DISK \$(macro2)
TRANSFORM 2 macro1, macro2 from (
1000 120.0
2000 128.0
)";

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

`condor_transform_ads -rules $rules_file -out $out_file $ads_file`;

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

##########################################################################
$option = "regex";
$out_file = "$testname"."_$option.out";
$rules_file = "$option.rules";
$rules_content = 
"COPY /^Max(.+)/  TotalSuspensions
RENAME /(.+)User/ Test\\0
RENAME /^Request(.+)/ Time\\1
TRANSFORM";

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

`condor_transform_ads -verbose -rules $rules_file -out $out_file $ads_file`;

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


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