#! /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.
##
##**************************************************************

## This tests recovery mode in combination with PRE_SKIP (see gittrac
## #2966).

## Note: this is a bit of a tricky test -- the node C job actually
## puts DAGMan on hold and then releases it to force it into recovery
## mode.

## TO DO: add some stuff to verify that each node really only got
## successfully run once.

use CondorTest;
use CondorUtils;
use Data::Dumper;

$cmd = 'job_dagman_recovery_preskip.dag';
$testdesc =  'Condor submit dag - preskip recovery checking test - scheduler U';
$testname = "job_dagman_recovery_preskip";
$dagman_args = "-verbose";

@logfiles = ("job_dagman_recovery_preskip.dag.dagman.out",
		"job_dagman_recovery_preskip.dag.lock");
$rescue_file = "job_dagman_recovery_preskip.dag.rescue001";

my $dag_was_held = 0;
my $dag_was_released = 0;

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

# Get rid of any existing log files.
foreach $name (@logfiles) {
	if (-e $name) {
		runcmd("rm -f $name");
	}
}

# Get rid of the first rescue DAG files if it exists.  (Note: the test
# should not produce a rescue DAG.)
if (-e $rescue_file) {
	runcmd("rm -f $rescue_file");
}

$abnormal = sub 
{
	my %info = @_;
	CondorTest::debug("Got Abnormal job exit:\n");
	print Dumper($info);
	die "Abnormal exit was NOT expected - aborting test\n";
};

$failure = sub 
{
	die "Job exit with failure was NOT expected - aborting test\n";
};

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

$held = sub 
{
	CondorTest::debug("DAGMan was held -- good!\n");
	$dag_was_held = 1;
};

$released = sub 
{
	CondorTest::debug("DAGMan was released -- good!\n");
	$dag_was_released = 1;
};

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

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

$submitted = sub
{
	my %info = @_;
	CondorTest::debug("DAG $info{cluster} submitted:\n\tWe should see submit, executing, held, released, and successful completion\n",1);
};

$success = sub
{
	CondorTest::debug("DAG succeeded as expected.\n");
};

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

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

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

