#! /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 DAGMan VARS feature with the new ALL_NODES option.

use CondorTest;
use CondorUtils;

$cmd = 'job_dagman_vars-B.dag';
$testdesc =  'Condor submit dag - VARS ALL_NODES test - scheduler U';
$testname = "job_dagman_vars-B";
$dagman_args = "-verbose";

@outfiles = ("job_dagman_vars-B.dag.dagman.out",
	"job_dagman_vars-B-XNodeA.out",
	"job_dagman_vars-B-XNodeB.out",
	"job_dagman_vars-B-XNodeC.out",
	"job_dagman_vars-B-XNodeD.out",
	"job_dagman_vars-B-XNodeZ+NodeZ1.out",
	"job_dagman_vars-B-XNodeZ+NodeZ2.out");

@outlines = ('<XNodeA> <Cadel_Evans> <Romain_Bardet> <Nairo_Quintana> <!@#$%^&*()_-=+=[]{}?> ',
	'<XNodeB> <Chris_Froome> <Andy_Schleck> <Nairo_Quintana> <!@#$%^&*()_-=+=[]{}?> ',
	'<XNodeC> <Chris_Froome> <Romain_Bardet> <Frank_Schleck> <!@#$%^&*()_-=+=[]{}?> ',
	'<XNodeD> <Chris_Froome> <Romain_Bardet> <Nairo_Quintana> <that\'s_all> ',
	'<XNodeZ+NodeZ1> <Vincenzo_Nibali> <J-C_Peraud> <Thibaut_Pinot> <!@#$%^&*()_-=+=[]{}?> ',
	'<XNodeZ+NodeZ2> <Vincenzo_Nibali> <J-C_Peraud> <Bryan_Coquard> <!@#$%^&*()_-=+=[]{}?> ');

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

# Get rid of any existing output files.
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;

	# Check the dagman.out file for successful completion.
	$file = shift @outfiles;
	$line = "";
	$passed = 0;
	$canopen = open(OUT,"<$file");
	if( ! (defined $canopen)) {
		CondorTest::debug("Can not open logfile<$name>:$!\n",1);
		$diditpass = 0;
	}
	while(<OUT>) {
		chomp();
		$line = $_;

		if( $line =~ /EXITING WITH STATUS 0/ ) {
			CondorTest::debug("Found $name: <$line>\n",1);
			$passed = 1;
		}
	}
	close(OUT);
	if($passed == 1 ) {
		;
	} else {
		CondorTest::debug("FAILED $name: $expected\n",1);
		$diditpass = 0;
	}

	# Check the node job output files for proper output.
	foreach $file (@outfiles) {
		$contents = "";
		$expected = shift @outlines;
		$passed = 0;
		$canopen = open(OUT,"<$file");
		if( !(defined $canopen) ) {
			CondorTest::debug("Can not open $file: $!\n",1);
			$diditpass = 0;
		} else {
			$contents = do { local $/; <OUT>; };
			chomp $contents;
			# Using eq here to avoid having to escape regexp special chars.
			if( $contents eq $expected ) {
				CondorTest::debug("Found $name: <$contents>\n",1);
				$passed = 1;
			}
			close(OUT);
		}
		if( $passed == 1 ) {
			;
		} else {
			CondorTest::debug("FAILED $name: <$contents>\n",1);
			CondorTest::debug("  Expected <$expected>\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::RunDagTest() FAILED\n";
}

