#! /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 part of the "allow PRE script to skip node job" feature
# (gittrac #2122).  The node job for node A should get skipped
# because of the PRE script return value.

# TEMP -- to-do: add check of jobstate.log file.

use CondorTest;
use CondorUtils;

$cmd = 'job_dagman_pre_skip-B.dag';
$testdesc =  'Condor submit dag - basic pre_skip node test B - scheduler U';
$testname = "job_dagman_pre_skip-B";
$dagman_args = "-verbose";

$logfile = "job_dagman_pre_skip-B.dag.dagman.out";
@outfiles = ("job_dagman_pre_skip-B_A.out", "job_dagman_pre_skip-B_B.out");

my $killedchosen = 0;

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

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

$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;

	# job_dagman_pre_skip-B_A.out should not exist -- if it does, it
	# means that the job was not correctly skipped.
	if (-e "job_dagman_pre_skip-B_A.out") {
		CondorTest::debug("Node A was not skipped!!\n",1);
		$diditpass = 0;
	} else {
		CondorTest::debug("Node A was correctly skipped\n",1);
	}
	if (! -e "job_dagman_pre_skip-B_B.out") {
		CondorTest::debug("Node B was not run!!\n",1);
		$diditpass = 0;
	} else {
		CondorTest::debug("Node B was run\n",1);
	}

	$canopen = open(LOG,"<$logfile");
	if( ! (defined $canopen)) {
		CondorTest::debug("Could not open logfile<$logfile>:$!\n",1);
		$diditpass = 0;
	}
	my @logoutput = <LOG>;
	$passed = 0;
	foreach (@logoutput) {
		# We need to see this line...
		if(/PRE_SKIP return value \d+ indicates we are done.+A/) {
			CondorTest::debug("Saw PRE_SKIP trip correctly: $_\n",1);
			$passed = 1;
			last;
		}
	}
	foreach (@logoutput) {
		if(/ULOG_EXECUTE for.+Node A/) {
			CondorTest::debug("Job A ran even though PRE_SKIP succeeded!\n",1);
			$passed = 0;
			last;
		}
	}
	close(LOG);
	if($passed != 1) {
		CondorTest::debug("failed to find correct output in $logfile\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";
}

