#! /usr/bin/env perl
#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
  SCHEDD_DEBUG = D_FULLDEBUG
CONDOR_TESTREQ_CONFIG
#endtestreq
##**************************************************************
##
## 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 test checks that node jobs are removed, and PRE and POST scripts
# are killed, when a DAGMan is condor_rm'ed.

use CondorTest;
use CondorUtils;

$cmd = 'job_dagman_rm.dag';
$testdesc =  'Condor submit dag - basic condor_rm test - scheduler U';
$testname = "job_dagman_rm";
$dagman_args = "-verbose";

@tmpfiles = ("job_dagman_rm.dag.dagman.out",
	"job_dagman_rm-nodeA.log",
	"job_dagman_rm-nodeD.log",
	"job_dagman_rm-nodeE.log",
	"job_dagman_rm-nodeF.log",
	"job_dagman_rm-nodeZ.log",
	"job_dagman_rm.dag.rescue001");

my $killedchosen = 0;

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

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

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

$aborted = sub 
{
	CondorTest::debug("DAG aborted as expected\n",1);

	CondorTest::debug("Making sure node jobs have exited from queue\n",1);
	runcmd("condor_wait job_dagman_rm-nodeA.log");
	CondorTest::debug("NodeA exited\n",1);
	runcmd("condor_wait job_dagman_rm-nodeD.log");
	CondorTest::debug("NodeD exited\n",1);
	runcmd("condor_wait job_dagman_rm-nodeE.log");
	CondorTest::debug("NodeE exited\n",1);
	runcmd("condor_wait job_dagman_rm-nodeF.log");
	CondorTest::debug("NodeF exited\n",1);
	runcmd("condor_wait job_dagman_rm-nodeZ.log");
	CondorTest::debug("NodeZ exited\n",1);

	CondorTest::debug("Verifying output\n",1);
	$diditpass = 1;

	if (-e "job_dagman_rm-NodeB-pre.finished" ) {
		CondorTest::debug("Error: file NodeB-pre.finished exists; NodeB PRE script should have been killed\n",1);
		$diditpass = 0;
	}

	if (-e "job_dagman_rm-NodeC-post.finished") {
		CondorTest::debug("Error: file NodeC-post.finished exists; NodeC POST script should have been killed\n",1);
		$diditpass = 0;
	}

	if (-e "job_dagman_rm-NodeD-job.finished") {
		CondorTest::debug("Error: file NodeD-job.finished exists; NodeD job should have been condor_rm'ed\n",1);
		$diditpass = 0;
	}

	if (-e "job_dagman_rm-NodeF-job.finished") {
		CondorTest::debug("Error: file NodeF-job.finished exists; NodeF job should have been condor_rm'ed\n",1);
		$diditpass = 0;
	}

	@Zfiles = glob("job_dagman_rm-NodeZ-job.*.finished");
	if ($#Zfiles >= 0) {
		CondorTest::debug("Error: found @Zfiles; NodeZ jobs should have been condor_rm'ed\n",1);
		$diditpass = 0;
	}

	$rescuedag = "job_dagman_rm.dag.rescue001";
	if (! -e $rescuedag) {
		CondorTest::debug("Error: rescue DAG file $rescuedag doesn't exist\n",1);
	}

	# Maybe check the node status file here, too.

	if($diditpass == 0) { 
		die "Error: All expected files and correct output failed\n";
	}
};

$held = sub 
{
	die "Error: 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
{
	die "Error: DAG should fail!\n";
};

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

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

CondorTest::RegisterExitedSuccess( $testname, $success);
CondorTest::RegisterEvictedWithoutCheckpoint( $testname, $on_evictedwithoutcheckpoint );
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 "Error: $testname: CondorTest::RunTest() failed\n";
}

