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

use CondorTest;
use CondorUtils;

$cmd = 'job_dagman_splice-G.dag';
$testdesc =  'Condor submit dag - basic splice test - scheduler U';
$testname = "job_dagman_splice-G";
$dagman_args = "-verbose";

@outfiles = (
		"G_S1+A",
		"G_S1+B",
		"G_S1+C",
		"G_S1+D",
		"G_S1+E",
		"G_S1+F",
		"G_S1+G",
		"G_S1+H",
		"G_S1+I",
		"G_S2+A",
		"G_S2+B",
		"G_S2+C",
		"G_S2+D",
		"G_S2+E",
		"G_S2+F",
		"G_S2+G",
		"G_S2+H",
		"G_S2+I",
		"G_S3+A",
		"G_S3+B",
		"G_S3+C",
		"G_S3+D",
		"G_S3+E",
		"G_S3+F",
		"G_S3+G",
		"G_S3+H",
		"G_S3+I",
		"G_S4+A",
		"G_S4+B",
		"G_S4+C",
		"G_S4+D",
		"G_S4+E",
		"G_S4+F",
		"G_S4+G",
		"G_S4+H",
		"G_S4+I",
			);

my $killedchosen = 0;

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

$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 (@outfiles) {
		$line = "";
		$passed = 0;
		$canopen = open(OUT,"<$name.out");
		if( ! (defined $canopen)) {
			CondorTest::debug("Can not open outfile<$name.out>:$!\n",1);
			$diditpass = 0;
		}
		while(<OUT>) {
			chomp();
			$line = $_;
			if( $line =~ /^OK.*$/ ) {
				$passed = 1;
				CondorTest::debug("$name.out: $line\n",1);
			}
		}
		close(OUT);
		if($passed == 1 ) {
			;
		} else {
			CondorTest::debug("failed $name.out: $line\n",1);
			$diditpass = 0;
		}
	}
	# clean out old output files
	foreach $name (@outfiles) {
		runcmd("rm -f $name.out $name.err");
	}
	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";
}

