#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## Copyright (C) 1990-2012, 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 strict;
use warnings;

use CondorTest;
use CondorUtils;
use Check::SimpleJob;

my $cmd      = 'job_filexfer_output-withvacate_van.cmd';
my $cmdnew   = $cmd . ".modified";
my $testdesc = 'file transfer - look for two sets of output files with a vacate test - vanilla U';
my $testname = "job_filexfer_output-withvacate_van";

my $pid = $$;
CondorTest::debug("Process Id for this script is $pid\n", 1);

my $basefile = "submit_filetrans_output" . "$pid";
my $oute = "$basefile"."e.txt";
my $outf = "$basefile"."f.txt";
my $outg = "$basefile"."g.txt";

my @basefiles = ();
push @basefiles, $oute;
push @basefiles, $outf;
push @basefiles, $outg;


runcmd("rm -f $basefile*");

my $spool = "";
CondorTest::debug("Spool is in --$spool--\n", 1);

my $baseoutfile;
my $haveseenevict = 0;
my $haveseenrun = 0;
my @baseoutfiles = ();

my $execute = sub {
    my %args = @_;
    my $cluster = $args{"cluster"};
    
    $spool = `condor_config_val SPOOL`;
    CondorUtils::fullchomp($spool);
    CondorTest::debug("Spool is in --$spool--\n", 1);
    if($haveseenrun == 0) {
        $haveseenrun = 1;
    }
    else {
        return(0);
    }

    my $shared_spool_dir = $cluster % 10000;
    
    $baseoutfile = $spool . "/${shared_spool_dir}/0/cluster" . $cluster . ".proc0.subproc0/" . "submit_filetrans_output" . "$pid";
    $oute = "$baseoutfile"."e.txt";
    $outf = "$baseoutfile"."f.txt";
    $outg = "$baseoutfile"."g.txt";

	push @baseoutfiles, $oute;
	push @baseoutfiles, $outf;
	push @baseoutfiles, $outg;

    
    runcmd("rm -f $baseoutfile*");
    
    CondorTest::debug("Looking for $oute, $outf, $outg ...\n", 1);
    
    sleep 20; # let output files get created
    CondorTest::debug("Vacate $cluster Now\n", 1);
    my @adarray;
    my $status = 1;
    my $cmd = "condor_vacate_job $cluster";
    $status = CondorTest::runCondorTool($cmd,\@adarray,2);
    if(!$status) {
        CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n", 1);
        exit(1)
    }
    
};

my $aborted = sub {
    my %args = @_;
    my $cluster = $args{"cluster"};
    CondorTest::debug("Aborted job expected from condor_rm, ok.\n", 1);
};

sub CheckOutFiles
{
	foreach my $outfile (@baseoutfiles) {
		if(-f "$outfile") {
		} else {
			print "$outfile not here yet\n";
		}
	}
};

my $evicted = sub {
    my %args = @_;
    my $cluster = $args{"cluster"};
	my @adarray = ();
    
    print "Job was evicted\n";
    if($haveseenevict == 0) {
        $haveseenevict = 1;
    }
    else {
        return(0);
    }
    print "++++++++++++++++++++++++++++++++++++\n";
    print "++++++++++++++++++++++++++++++++++++\n";
    #runCondorTool("ls $baseoutfile*",\@adarray, 2);
	CheckOutFiles();
    print "++++++++++++++++++++++++++++++++++++\n";
    sleep(15);
    #runcmd("ls $baseoutfile*");
    #runCondorTool("ls $baseoutfile*",\@adarray, 2);
	CheckOutFiles();
    print "++++++++++++++++++++++++++++++++++++\n";
    
    my $errors = 0;

    if(!-f $oute) {
        print "$oute failed to arrive\n";
        $errors++;
    }

    if(!-f $outf) {
        print "$outf failed to arrive\n"; 
        $errors++;
    }

    if(!-f $outg) {
        print "$outg failed to arrive\n"; 
        $errors++;
    }

    @adarray = ();
    my $cmd = "condor_rm $cluster";
    my $status = CondorTest::runCondorTool($cmd, \@adarray, 2);
    if(!$status) {
        CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n", 1);
        CondorTest::debug("Command failed: '$cmd'\n", 1);
    }

    if($errors > 0) {
        CondorTest::debug("removing $cluster\n", 1);
        exit(1);
    }
            
    # Cool getting to here means the files all arrived!
    CondorTest::debug("All files arrived as expected.....\n", 1);
};

my $success = sub {
    my $errors = 0;
    CondorTest::debug("Base file transfer job!\n", 1);
    if(!-f $oute) {
        die "$oute failed to arrive\n";
        $errors++;
    }
    if(!-f $outf) {
        die "$outf failed to arrive\n";
        $errors++;
    }
    if(!-f $outg) {
        die "$outg failed to arrive\n";
        $errors++;
    }

    if($errors > 0) {
        exit(1);
    }

    CondorTest::debug("All files arrived as expected.....\n", 1);
};

my $hold = sub {
    print "Job can end up on hold.\n";
};

my $timed = sub {
    die "Job should have ended by now. file transfer broken!\n";
};

my $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";
    $status = CondorTest::runCondorTool($cmd,\@adarray,2);
    if(!$status) {
        CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n", 1);
    }
    CondorTest::debug("$testname: SUCCESS\n", 1);
};

my $args = "--job=$pid --forever ";

SimpleJob::RunCheck(
    runthis=>"x_job_filexfer_testjob.pl",
    duration=>"$args",
	requirements=>"isUndefined(My.NumShadowStarts) || (My.NumShadowStarts < 2)",
    transfer_output_files=>"$oute,$outf,$outg",
    should_transfer_files=>"YES",
    when_to_transfer_output=>"ON_EXIT_OR_EVICT",
    on_success=>$success,
    on_execute=>$execute,
    on_abort=>$aborted,
    on_shadow=>$shadow,
	on_hold=>$hold,
	on_evictedwithoutcheckpoint=>$evicted,
);


CondorTest::EndTest();
print scalar localtime() . "\n";
my $result = 0;

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

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