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

# This test should run the DAG.  It should see multiple hold events for the same
# jobid.  DAGMan should report that it saw multiple hold events, and not
# increment its counters on the second or subsequent hold events 

$cmd = 'job_dagman_repeat_holds.dag';
$testdesc =  'Condor submit dag - Repeated hold events in log - scheduler U';
$testname = "job_dagman_repeat_holds";
$dagman_args = "-verbose";
$out = "${cmd}.dagman.out";

my $killedchosen = 0;

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

$abnormal = sub 
{
	die "Want to see only submit, execute and failed completion\n";
};

$aborted = sub 
{
	die "Abort event NOT expected\n";
};

$held = sub 
{
	die "Held event NOT expected\n";
};

$executed = sub
{
	my %info = @_;

	CondorTest::debug("Good. We need the dag to run<$info{cluster}>\n",1);
};

$submitted = sub
{
	CondorTest::debug("submitted: This test will see submit, executing and successful completion\n",1);
};

$success = sub
{
	my $diditpass = 0;
	runcmd ("cat job_dagman_repeat_holds_watch.out");
	open OUT, "<$out" || die "Could not open $out\n";	
	while(<OUT>) {
		chomp;
		CondorTest::debug("$_\n",1) if(/ULOG_JOB_HELD/);
		if(/is already on hold/) {
			CondorTest::debug("$_\n",1);
			$diditpass = 1;
			last;
		}
	}
	close OUT;
	return $diditpass;
};

$failure = sub
{
	die "Error: DAG is not expected to fail!\n";
};

CondorTest::RegisterExitedSuccess( $testname, $success );
CondorTest::RegisterExitedFailure( $testname, $failure );
CondorTest::RegisterExecute($testname, $executed );
CondorTest::RegisterExitedAbnormal( $testname, $abnormal );
CondorTest::RegisterAbort( $testname, $aborted );
CondorTest::RegisterHold( $testname, $held );
CondorTest::RegisterSubmit( $testname, $submitted );

if( CondorTest::RunDagTest($testname, $cmd, 0, $dagman_args) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}
