#! /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 CondorTest;
use CondorUtils;
use Check::SimpleJob;

$cmd      = 'job_filexfer_minus1_van.cmd';
$testdesc =  'Base file transfer short one output file - vanilla U';
$testname = "job_filexfer_minus1_van";
my $sawshadow = "";

# truly const variables in perl
sub IDLE{1};
sub HELD{5};
sub RUNNING{2};

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	NUM_CPUS = 2
	MachineMaxVacateTime = 10
	NEGOTIATOR_INTERVAL = 5
CONDOR_TESTREQ_CONFIG
#endtestreq


my $job = $$;
my $baseoutfile = "submit_filetrans_output" . "$job";
my $outd = "$baseoutfile"."d.txt";
my $oute = "$baseoutfile"."e.txt";
my $outf = "$baseoutfile"."f.txt";
my $outg = "$baseoutfile"."g.txt";
my $outh = "$baseoutfile"."h.txt";
my $outi = "$baseoutfile"."i.txt";
my $outj = "$baseoutfile"."j.txt";

my $alreadydone=0;

$execute = sub {
	my %args = @_;
	my $cluster = $args{"cluster"};

	CondorTest::debug("Running $cluster\n",1);

};

$shadow = sub
{
       my %args = @_;
       my $cluster = $args{"cluster"};
       my $error = $args{"shadowerror"};

       CondorTest::debug("Shadow Error: Cluster $cluster: $error\n",1);
		my @adarray;
		my $status = 1;
		my $cmd = "condor_rm $cluster";
		$sawshadow = "yes";
		print "great, we saw a shadow exception from a mising output file\n";
		$status = CondorTest::runCondorTool($cmd,\@adarray,2);
		if(!$status)
		{
			CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
			exit(1);
		}
};

$success = sub
{
	die "Job shouldn't succeed!\n";
};

$abort = sub {
	print "In abort handler: we removed the job which failed\n";
	if($sawshadow eq "yes") {
    	CondorTest::debug("$testname: SUCCESS\n",1);
    	RegisterResult(1,"test_name","$testname");
	} else {
		print "ABORT without having seen a shadow exception\n";
    	RegisterResult(0,"test_name","$testname");
	}
};

$held = sub {
	print "Its is OK to seea hold now\n";
};

# make some needed files. All 0 sized and xxxxxx.txt for
# easy cleanup

my $job = $$;
CondorTest::debug("Process Id for this script is  $job\n",1);
my $basefile = "submit_filetrans_input" . "$job";
my $in = "$basefile".".txt";
my $ina = "$basefile"."a.txt";
my $inb = "$basefile"."b.txt";
my $inc = "$basefile"."c.txt";

my $inputdir = "job_"."$job"."_dir";

CreateDir("-p $inputdir");
CreateEmptyFile("$inputdir/$in");
CreateEmptyFile("$inputdir/$ina");
CreateEmptyFile("$inputdir/$inb");
CreateEmptyFile("$inputdir/$inc");

my $line = "";
my $args = "--job=$job --onesetout ";

SimpleJob::RunCheck(
    runthis=>"x_job_filexfer_testjob.pl",
    duration=>"$args",
    transfer_output_files=>"$outd,$oute,$outf,$outg",
    should_transfer_files=>"YES",
    when_to_transfer_output=>"ON_EXIT",
    on_success=>$success,
    on_execute=>$execute,
    on_abort=>$abort,
	on_hold=>$held,
    on_shadow=>$shadow,
);


CondorTest::EndTest();
print scalar localtime() . "\n";

if( $result != 0 )
{
    exit(1);
}

CondorTest::debug("$testname SUCCESS\n",1);
exit(0);
