#!/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 strict;
use warnings;

use CondorTest;
use CondorUtils;
use Check::SimpleJob;

my $mypid = $$;
my $testname = "job_core_basic_par";
my $wantedout = $testname . $mypid . ".out" . "\$(NODE)";

my $submitadditions = '
arguments =  machine $(NODE)
+ParallelShutdownPolicy = "WAIT_FOR_ALL"
machine_count = 4
';

my $debuglevel = 1;

# where am I running
my $currenthost = `condor_config_val FULL_HOSTNAME`;
chomp($currenthost);

print scalar localtime() . "\n";

my $version = "basicpar";
my $mysaveme = $testname . ".saveme";

if($mypid == 0) {
	die "Saveme setup for test failed\n";
}

my $firstappend_condor_config = "
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	ALL_DEBUG = 
	# set up dedicated scheduler
	# Adding Dedicated Scheduler parrallel Universe;
	DedicatedScheduler = \"DedicatedScheduler\@schedd$mypid$version\@$currenthost\"
	STARTD_ATTRS = \$(STARTD_ATTRS), DedicatedScheduler
	SCHEDD_DEBUG = D_FULLDEBUG
	SCHEDD_NAME = schedd$mypid$version
	NUM_CPUS = 8
	use FEATURE : StaticSlots
	SUSPEND = FALSE
";
 
my $configfile = CondorTest::CreateLocalConfig($firstappend_condor_config,"$version");

my $new_condor_instance = CondorTest::StartCondorWithParams(
	condor_name => "$version",
	fresh_local => "TRUE",
	condorlocalsrc => "$configfile",
);

my $collectorAddress = $new_condor_instance->GetCollectorAddress();

print "\n\n\n\n\n\n*************** collector address = $collectorAddress **************\n\n\n\n\n";

runCommandCarefully( undef, 'condor_status', '-pool', $collectorAddress, '-schedd', '-af', 'Name' );

# at this point personal should be up and we have the parallel submit
# file we want.



#my $cmdfinal = $mysubmitnm;
my $pid = $mypid;

my $outputbase = "job_core_basic_par$pid.out";

#CondorTest::debug("Submit file for this test is $cmdfinal\n",$debuglevel);

my $aborted = sub {
	my %info = @_;
	my $done;
	die "Abort event not expected!\n";
};

my $held = sub {
	my %info = @_;
	my $cluster = $info{"cluster"};
	die "Held event not expected.....\n"
};

my $executed = sub
{
	my %args = @_;
	my $cluster = $args{"cluster"};

	CondorTest::debug("Parallel job executed\n",$debuglevel);
};

my $success = sub
{
	my $max = 4;
	my $foundcount = 0;
	my $count = 0;
	my $fullfile = "";

	print "Checking for expected job output - ";

	while ($count < $max) {
		$fullfile = $outputbase . $count;
		my $line = "";
		open(OUT,"<$fullfile") or die "Can not open <$fullfile>:$!\n";
		while(<OUT>) {
			fullchomp();
			$line = $_;
			if($line =~ /^machine $count/) {
				$foundcount += 1;
			}
		}
		close(OUT);
		$count += 1;
	}
	if($foundcount != $max) {
		print "bad\n";
		print "Only correct output in $foundcount output files\n";
		CondorTest::RegisterResult(0, "test_name", $testname);
	} else {
		print "ok\n";
	}
	CondorTest::debug("Success: Parallel Test ok\n",$debuglevel);
	CondorTest::RegisterResult(1, "test_name", $testname);
};

SimpleJob::RunCheck(
	universe=>"parallel",
    runthis=>"x_echostring.pl",
	output=>$wantedout,
    on_execute=>$executed,
    on_success=>$success,
    append_submit_commands=>$submitadditions,
);

CondorTest::EndTest();

exit(0);
