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

# This test tests the anonymous metrics reporting for Pegasus.

use CondorTest;
use CondorUtils;

$cmd = 'job_dagman_metrics-A-upper.dag';
$testdesc =  'Condor submit dag - basic metrics test A - scheduler U';
$testname = "job_dagman_metrics-A";
$dagman_args = "-verbose";
$ENV{'PEGASUS_METRICS'} = 'true';
$ENV{'PEGASUS_BRAINDUMP_FILE'} = 'job_dagman_metrics-A-braindump.txt';

@outfiles = ("job_dagman_metrics-A-upper.dag.dagman.out",
	"job_dagman_metrics-A-splice-lower.dag.dagman.out",
	"job_dagman_metrics-A-lower.dag.dagman.out",
	"job_dagman_metrics-A-upper.dag.metrics.out",
	"job_dagman_metrics-A-splice-lower.dag.metrics.out",
	"job_dagman_metrics-A-lower.dag.metrics.out");

@metricsfiles = ("job_dagman_metrics-A-upper.dag.metrics",
	"job_dagman_metrics-A-splice-lower.dag.metrics",
	"job_dagman_metrics-A-lower.dag.metrics");

my $killedchosen = 0;

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

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

# Get rid of any existing metrics files.
foreach $file (@metricsfiles) {
	if (-e $file) {
		runcmd("rm -f $file");
	}
}

$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
{
	die "Error: DAG should fail!\n";
};

$failure = sub
{
	CondorTest::debug("DAG failed as expected\n",1);
	CondorTest::debug("Verifying output\n",1);
	$diditpass = 1;

	# Note:  these are not complete checks!!!!

	# Check metrics files
	foreach $file (@metricsfiles) {
		if (! -e $file) {
			CondorTest::debug("Error: file $file does not exist!\n",1);
			$diditpass = 0;
		}
	}

	# Check dagman.out files
	foreach $file (@outfiles) {
		if (! -e $file) {
			CondorTest::debug("Error: file $file does not exist!\n",1);
			$diditpass = 0;
		}
	}

	if ($diditpass == 0) {
		die "All expected files and correct output failed\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 $outfiles[0]) {
		die "$testname: failed -- no $outfiles[0] file!  DAG not run?\n";
	}   
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}

