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

my $pid = $$;
$name = "job_filexfer_streamerr_van$pid.err";
$testdesc =  'Stream error works - vanilla U';
$testname = "job_filexfer_streamerr_van";

$submitted = sub
{
	my %info = @_;
	my $name = $info{"error"};
};

$aborted = sub 
{
	my %info = @_;
	my $done;
	CondorTest::debug("Abort event expected as we clear job\n",1);
};

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

$execute = sub
{
	my %info = @_;
	my $job = "good";
	my $cluster = $info{"cluster"};
	my $name = $info{"error"};
	CondorTest::RegisterTimed($testname, $timed, 300);

	#job sleeps forever and will be there for removal
	sleep(60);
	if( -f $name )
	{
		CondorTest::debug("YEAH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n",1);
	}
	else
	{
		$job = "bad";
	}

	CondorTest::debug("OK remove the job!\n",1);
	my @adarray;
	my $status = 1;
	my $cmd = "condor_rm $cluster";
	$status = CondorTest::runCondorTool($cmd,\@adarray,2,{emit_output=>0});

	if($job eq "bad")
	{
		die "Test failed to stream error!\n";
	}

	if(!$status)
	{
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
		exit(0)
	}
};

$ExitSuccess = sub {
	my %info = @_;
};


# before the test let's throw some weird crap into the environment

my $on_evictedwithoutcheckpoint = sub {
	CondorTest::debug("Evicted Without Checkpoint from removing jobs.\n",1);
};

#empty local environment and add only a few things that way......
my $path = $ENV{"PATH"};

my $streamcommand = '
	stream_error = true
	getenv=false
';


SimpleJob::RunCheck(
    runthis=>"x_job_filexfer_stream.pl",
    duration=>"stderr 100 0",
    should_transfer_files=>"YES",
    when_to_transfer_output=>"ON_EXIT_OR_EVICT",
	append_submit_commands=>$streamcommand,
	error=>$name,
    on_success=>$ExitSuccess,
    on_execute=>$execute,
    on_abort=>$aborted,
	on_submit=>$submitted,
	on_evictedwithoutcheckpoint=>$on_evictedwithoutcheckpoint,
);

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

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

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