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

## Tests the ALL_NODES feature (gittrac #5729).

use CondorTest;
use CondorUtils;

$cmd = 'job_dagman_all_nodes.dag';
$testdesc =  'Condor submit dag - all_nodes test - scheduler U';
$testname = "job_dagman_all_nodes";
$dagman_args = "-verbose -dumprescue";

@logfiles = ("job_dagman_all_nodes.dag.dagman.out");

$rescuedag = "job_dagman_all_nodes.dag.rescue001";

my $killedchosen = 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");
	}
}

if (-e $rescuedag) {
	runcmd("rm -f $rescuedag");
}

$abnormal = sub 
{
	die "Want to see only submit, execute and successful 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
{
	CondorTest::debug("executed successfully\n",1);
	CondorTest::debug("Verifying output\n",1);
	$diditpass = 1;

	foreach $name (@logfiles) {
		$line = "";
		$passed = 0;
		$canopen = open(OUT,"<$name");
		if( ! (defined $canopen)) {
			CondorTest::debug("Can not open logfile<$name>:$!\n",1);
			$diditpass = 0;
		}
		while(<OUT>) {
			chomp;
			if( /^*.EXITING WITH STATUS 0*.$/ ) {
				$passed = 1;
				CondorTest::debug("$name: $_\n",1);
			}
		}
		close(OUT);
		if($passed == 1 ) {
			;
		} else {
			CondorTest::debug("failed $name: $line\n",1);
			$diditpass = 0;
		}
	}

	# Check for a bunch of lines in the rescue DAG.
	@checklines = ("JOB NodeA /dev/null NOOP ",
		"SCRIPT PRE NodeA /dev/null A \$JOB \$RETRY",
		"PRE_SKIP NodeA 4",
		"SCRIPT POST NodeA /dev/null A \$JOB \$RETURN",
		"VARS NodeA nodename=\"\$(JOB)\"",
		"ABORT-DAG-ON NodeA 3 RETURN 4",
		"CATEGORY NodeA upper",
		"RETRY NodeA 2 UNLESS-EXIT 5",

		"JOB NodeB /dev/null NOOP ",
		"SCRIPT PRE NodeB /dev/null A barfoo \$RETRY",
		"PRE_SKIP NodeB 20",
		"SCRIPT POST NodeB /dev/null A barfoo \$RETURN",
		"VARS NodeB nodename=\"barfoo\"",
		"ABORT-DAG-ON NodeB 30 RETURN 40",
		"CATEGORY NodeB barfoo",
		"RETRY NodeB 20 UNLESS-EXIT 50",

		"JOB NodeC /dev/null NOOP ",
		"SCRIPT PRE NodeC /dev/null A \$JOB \$RETRY",
		"PRE_SKIP NodeC 4",
		"SCRIPT POST NodeC /dev/null A \$JOB \$RETURN",
		"VARS NodeC nodename=\"\$(JOB)\"",
		"ABORT-DAG-ON NodeC 3 RETURN 4",
		"CATEGORY NodeC upper",
		"RETRY NodeC 2 UNLESS-EXIT 5",

		"FINAL NodeF /dev/null NOOP ",
		"SCRIPT PRE NodeF /dev/null F \$JOB \$RETRY",
		"SCRIPT POST NodeF /dev/null F \$JOB \$RETURN",
		"VARS NodeF nodename=\"final\"",

		"JOB SpliceA+NodeX /dev/null NOOP ",
		"SCRIPT PRE SpliceA+NodeX /dev/null B \$JOB \$RETRY",
		"PRE_SKIP SpliceA+NodeX 5",
		"SCRIPT POST SpliceA+NodeX /dev/null B \$JOB \$RETURN",
		"VARS SpliceA+NodeX nodename=\"\$(JOB)\"",
		"ABORT-DAG-ON SpliceA+NodeX 4 RETURN 5",
		"CATEGORY SpliceA+NodeX SpliceA+lower",
		"RETRY SpliceA+NodeX 3 UNLESS-EXIT 6",

		"JOB SpliceA+NodeY /dev/null NOOP ",
		"SCRIPT PRE SpliceA+NodeY /dev/null B \$JOB \$RETRY",
		"PRE_SKIP SpliceA+NodeY 5",
		"SCRIPT POST SpliceA+NodeY /dev/null B \$JOB \$RETURN",
		"VARS SpliceA+NodeY nodename=\"\$(JOB)\"",
		"ABORT-DAG-ON SpliceA+NodeY 4 RETURN 5",
		"CATEGORY SpliceA+NodeY SpliceA+lower",
		"RETRY SpliceA+NodeY 3 UNLESS-EXIT 6",

		"JOB SpliceA+NodeZ /dev/null NOOP ",
		"SCRIPT PRE SpliceA+NodeZ /dev/null B \$JOB \$RETRY",
		"PRE_SKIP SpliceA+NodeZ 5",
		"SCRIPT POST SpliceA+NodeZ /dev/null B \$JOB \$RETURN",
		"VARS SpliceA+NodeZ nodename=\"\$(JOB)\"",
		"ABORT-DAG-ON SpliceA+NodeZ 4 RETURN 5",
		"CATEGORY SpliceA+NodeZ SpliceA+lower",
		"RETRY SpliceA+NodeZ 3 UNLESS-EXIT 6",

		"MAXJOBS SpliceA+lower 2",
		"MAXJOBS upper 1",
	);

	foreach $check (@checklines) {
		$line = "";
		$passed = 0;

		$canopen = open(OUT,"<$rescuedag");
		if( ! (defined $canopen)) {
			CondorTest::debug("Can not open rescue DAG <$rescuedag>:$!\n",1);
			$diditpass = 0;
		}
		while(<OUT>) {
			chomp();
			$line = $_;
			if( $line eq $check ) {
				$passed = 1;
				CondorTest::debug("$rescuedag: $line\n",1);
			}
		}
		close(OUT);
	
		if($passed) {
			;
		} else {
			CondorTest::debug("ERROR did not find $rescuedag: $check\n",1);
				$diditpass = 0;
		}
	}

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

CondorTest::RegisterExitedSuccess( $testname, $success);
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";
}

