#! /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 the propogation of priorities from DAG specifications to job
# priorities, and from parent nodes to child nodes.  The rules are that a child
# node has priority at least that of its parent; Every node in a subDAG has
# priority at least that of the subDAG's parent, or that priority that was
# assigned in the superDAG's file.

use CondorTest;
use CondorUtils;

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

$logfile = "job_dagman_propogate_priorities.dag.dagman.out";
@outfiles = (
	'job_dagman_propogate_priorities-A.out',
	'job_dagman_propogate_priorities-B.out',
	'job_dagman_propogate_priorities-C.out',
	'job_dagman_propogate_priorities-D.out',
	'job_dagman_propogate_priorities-E.out',
	'job_dagman_propogate_priorities-F.out');

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

# Get rid of any existing log and output files
runcmd("rm -f $logfile") if (-e $logfile);

foreach (@outfiles) {
	runcmd("rm -f $_") if (-e $_);
}

$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("Succeeded as expected\n",1);
	CondorTest::debug("Verifying output\n",1);
	$diditpass = 1;
	foreach (@outfiles){
		if(! -e $_) {
			CondorTest::debug("$_ does not exist!\n");
			$diditpass = 0;
			last;
		}
		$file = $_;
		open OUT, "<$file" || die "Cannot open $file for reading\n";
		while(<OUT>){
			chomp;
			if(/DAG node (.+) has priority (\d+)/){
				$priority{$1} = $2;
				CondorTest::debug("Good. Saw output: $_\n",1);
			}
		}
		close OUT;
	}

	# Check node priorities...
	foreach ('nodeA','nodeB','nodeC','nodeD','nodeE','nodeF') {
		if(! defined $priority{$_}){
			CondorTest::debug("Did not see priority for $_\n");
			$diditpass = 0;
			die;
		}
	}
	if ($priority{'nodeA'} != 67) {
		$diditpass = 0;
		CondorTest::debug("nodeA priority was $priority{'nodeA'}; should be 67\n");
	}
	if ($priority{'nodeB'} != 7) {
		$diditpass = 0;
		CondorTest::debug("nodeB priority was $priority{'nodeB'}; should be 7\n");
	}
	if ($priority{'nodeC'} != 12) {
		$diditpass = 0;
		CondorTest::debug("nodeC priority was $priority{'nodeC'}; should be 12\n");
	}
	if ($priority{'nodeD'} != 117) {
		$diditpass = 0;
		CondorTest::debug("nodeD priority was $priority{'nodeD'}; should be 117\n");
	}
	if ($priority{'nodeE'} != 107) {
		$diditpass = 0;
		CondorTest::debug("nodeE priority was $priority{'nodeE'}; should be 107\n");
	}
	if ($priority{'nodeF'} != 17) {
		$diditpass = 0;
		CondorTest::debug("nodeF priority was $priority{'nodeF'}; should be 17\n");
	}

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

$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) ) {
	if (! -e $logfile) {
		die "$testname: failed -- no $logfile file!  DAG not run?\n";
	}
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}
