#! /usr/bin/env perl
##**************************************************************
##
## 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_submit_parse.dag';
$testdesc =  'Condor submit dag - Check parsing of submit file - scheduler U';
$testname = "job_dagman_submit_parse";
$dagman_args = "-verbose";

$outfile = "job_dagman_submit_parse.dag.dagman.out";

my $killedchosen = 0;

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

# We need to turn off shared port for this test if we're testing on Windows.
my $appendCondorConfig = "";
if( is_windows() ) {
    $appendCondorConfig .= "JOB_ROUTER.USE_SHARED_PORT = FALSE\n";
}

# Turn off direct submit. This test only applies when forking condor_submit.
$appendCondorConfig .= "DAGMAN_USE_DIRECT_SUBMIT = False\n";

CondorTest::StartCondorWithParams(
	append_condor_config => $appendCondorConfig
);

# Get rid of dagman.out file if it exists.
if (-e $outfile) {
	runcmd("rm -f $outfile");
}

$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 failed completion\n",1);
};

$success = sub
{
	CondorTest::debug("executed successfully\n",1);
	die "Did not expect success!\n";
};

$failure = sub
{
	CondorTest::debug("DAG failed as expected\n",1);
	CondorTest::debug("Verifying output\n",1);
	my $diditpass = 0;
	open OUT, "<$outfile" || die "Could not find $outfile for reading\n";
	while(<OUT>) {
		print if(/typo/);
		if(/ERROR: node B: job ID in userlog submit event .+ doesn.t match ID reported earlier by submit command/) {
			print "\n\nEXCELLENT! We saw the following (expected) failure: ";
			print;
			print "\n\n";
			$diditpass = 1;
			last;
		}
	}
	close OUT;
	return $diditpass;
};

CondorTest::RegisterExitedFailure( $testname, $failure);
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) ) {
	if (! -e $outfile) {
		die "$testname: failed -- no $outfile file!\n";
	}
	CondorTest::debug("$testname: SUCCESS\n",1);
	CondorTest::EndTest();
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}
