#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## 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 CheckOutputFormats;
use strict;
use warnings;

my $testname = "max_jobs_per_owner_test_group";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	MAX_JOBS_PER_OWNER = 10
CONDOR_TESTREQ_CONFIG
#endtestreq

my $pid = $$;
my $executable = "x_sleep.pl";
my $arguments = 600;
my $submit_content;
my $submitfile;
my @content;

print "############################# test with accounting group #############################\n";
# submit 12 jobs
$submit_content =
"executable = $executable
arguments = $arguments
+accounting_group = foo
queue 12";
$submitfile = "$testname$pid.sub";
emit_dag_files($testname, $submit_content, $pid);
`condor_submit hold=true $submitfile`;
@content = `condor_q -nobatch`;

print @content;

if (how_many_entries(\@content) eq 0){
	RegisterResult(1, check_name => "submit 12 jobs", test_name => $testname);
} else {
	print "        ERROR: there shouldn't be any jobs in the queue\n";
	RegisterResult(0, check_name => "submit 12 jobs", test_name => $testname);
}

`condor_rm -all`;

# submit 8 jobs then submit 4 jobs
$submit_content = 
"executable = $executable
arguments = $arguments
+accounting_group = foo
queue 8";
$submitfile = "$testname$pid.sub";
emit_dag_files($testname, $submit_content, $pid);
`condor_submit hold=true $submitfile`;
@content = `condor_q -nobatch`;

print @content;

if (how_many_entries(\@content) eq 8){
	RegisterResult(1, check_name => "submit 8 jobs", test_name => $testname);
} else {
	print "        ERROR: there should be 8 jobs in the queue\n";
	RegisterResult(0, check_name => "submit 8 jobs", test_name => $testname);
}

# add another 4 jobs
$submit_content = 
"executable = $executable
arguments = $arguments
+accounting_group = foo
queue 4";
$submitfile = "$testname$pid.sub";
emit_dag_files($testname, $submit_content, $pid);
`condor_submit hold=true $submitfile`;
@content = `condor_q -nobatch`;

print @content;

if (how_many_entries(\@content) eq 8){
	RegisterResult(1, check_name => "another 4 jobs", test_name => $testname);
} else {
	print "        ERROR: there should be 8 jobs in the queue\n";
	RegisterResult(0, check_name => "another 4 jobs", test_name => $testname);
}

CondorTest::EndTest();
exit(0);
