#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## Copyright (C) 1990-2012, 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.
##
##**************************************************************

# Condor submit dag - splice test for gittrac #1751 - scheduler U

use strict;
use warnings;

use CondorTest;
use CondorUtils;
use DagmanTest;

my $cmd = 'job_dagman_splice-Q-0.dag';
my $testname = "job_dagman_splice-Q";
my $dagman_args = "-verbose";

DagmanTest::register_event_handlers($testname);

my @outfiles = ("0_Q2");

my $success = sub {
    CondorTest::debug("executed successfully\n",1);
    CondorTest::debug("Verifying output\n",1);
    my $passed = 1;
    FILE: foreach my $file (@outfiles) {
	if(!open(OUT, '<', "$file.out")) {
	    CondorTest::debug("ERROR: Can not open output file '$file.out': $!\n", 1);
	    $passed = 0;
	    next;
	}

	while(<OUT>) {
	    if(/^OK/) {
		next FILE;
	    }
	}

	CondorTest::debug("ERROR - $file.out did not contain OK\n", 1);
	$passed = 0;
    }

    my $save_dir = "$testname.output_files";
    runcmd("mkdir -p $save_dir");
    foreach my $name (@outfiles) {
	runcmd("mv $name.out $name.err $save_dir");
    }

    if($passed == 0) {
	die "All expected files and correct output failed\n";
    }
};

CondorTest::RegisterExitedSuccess( $testname, $success);

if( CondorTest::RunDagTest($testname, $cmd, 0, $dagman_args) ) {
    CondorTest::debug("$testname: SUCCESS\n",1);
    exit(0);
}
else {
    die "$testname: CondorTest::RunTest() failed\n";
}

