#! /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;
use Check::SimpleJob;
#use Check::CondorLog;
use Check::ProcInfo;

my $testname =  "job_basic_start_test";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	NUM_CPUS = 5
	use FEATURE : StaticSlots
	SLOT1_CPU_AFFINITY = 0
	SLOT2_CPU_AFFINITY = 1
	SLOT3_CPU_AFFINITY = 2
	SLOT4_CPU_AFFINITY = 3
	SLOT5_CPU_AFFINITY = 4
	SLOT5_IsGeneralPurposeSlot = False
	START =  ((TARGET.RequiresTypeB =?= TRUE && MY.SlotID == 5) \
		  || (TARGET.RequiresTypeB =!= TRUE && MY.SlotID != 5))
	ALL_DEBUG = 
CONDOR_TESTREQ_CONFIG
#endtestreq

my $sleepforever = '
	universe = vanilla
	executable = x_sleep.pl
	log = $sleepforeverlog
	notification = never
	arguments = 3600
	queue
';

TLOG "First test basic job\n";
SimpleJob::RunCheck();


TLOG "Starting sleep forever job\n";

my $sleepf1 = $$ . "sf1";
open(SF1,">$sleepf1") or die "Can not create <$sleepf1>\n";
print SF1 "$sleepforever";

system("cat $sleepf1");

print "Permanent sleep job started\n";
my $plain = 0;
$plain = SubmitReturnCluster($sleepf1);

my $sleepf2 = $$ . "sf2";
open(SF2,">$sleepf2") or die "Can not create <$sleepf2>\n";
print SF2 "+RequiresTypeB = True\n";
print SF2 "$sleepforever";

system("cat $sleepf2");
print "submitting RequiresTypeB job now\n";
my $special =  0;
$special = SubmitReturnCluster($sleepf2);

my $numberinstate = 0;
my $result = 0;
CondorTest::VerifyNumJobsInState("running",2,30);
system(" condor_q -run -nobatch");

my @cmdout = `condor_q -run -nobatch`;

my %jobs = ();
foreach my $line (@cmdout) {
	chomp($line);
	if($line =~ /^\s*(\d+)\.0.*\s+slot(\d+).*$/) {
		#print "Match <$line> job $1 slot$2\n";
		$jobs{$1} = $2;
	} else {
		#print "No match <$line>\n";
	}
}

# if this worked, special cluster should be 5 and the plain
# should be some number less.
if($jobs{$special} == 5){
    print "Special job $special running on dedicated slot $jobs{$special}\n";
    CondorTest::RegisterResult(1,"test_name","$testname");
} else {
    print "Special job $special NOT running on dedicated slot 5 <$jobs{$special}>\n";
    CondorTest::RegisterResult(0,"test_name","$testname");
}

if($jobs{$plain} < 5){
    print "Regular job $plain running on geneneral purpose slot $jobs{$plain}\n";
    CondorTest::RegisterResult(1,"test_name","$testname");
} else {
    print "Regular job $plain running on dedicated slot 5 <$jobs{$plain}>\n";
    CondorTest::RegisterResult(0,"test_name","$testname");
}

my @tooloutput = ();
print "Clearing jobs before taking personal down\n";
runCondorTool("condor_rm -all",\@tooloutput,2,{emit_output=>1});

CondorTest::EndTest();


sub SubmitReturnCluster
{
	my $submitfile = shift;
	my $cluster = 0;

	my @output = `condor_submit $submitfile`;

	foreach my $line (@output) {
		chomp($line);
		if($line =~ /^.*cluster (\d+).*$/) {
			#print "$line\n";
			#print "returning $1\n";
			$cluster = $1;
			return($cluster);
		}
	}
	return(0);
}
