#! /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 check availability and usage contol of the new
## local, resources in both a static configuration and in psolts.
## It is model after what we did to test concurrency limits
## as we are checking the same sorts of things. bt
##
##**************************************************************

use CondorTest;
use CondorUtils;
use Check::SimpleJob;
use Check::CondorLog;
use strict;
use warnings;

my $testname = "cmd_submit_massive";
my @submitfiles = ();

my $pid = $$;
my $submitfile = "$testname$pid.sub"; 
my $ClusterId = 0;

# ================ start a personal condor ===============
#
my $firstappend_condor_config = '
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	WANT_SUSPEND = FALSE
	ALL_DEBUG = D_ALWAYS
	MACHINE_RESOURCE_Boats = 30
';

my $configfile = CondorTest::CreateLocalConfig($firstappend_condor_config,"cmdsubmitmassive");

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

# ========= submit a small job first to test the node ==================

TLOG("Submitting small job to test the pool\n");
my $result = SimpleJob::RunCheck();
Condor::EventReset();

# ========= create a massive submit file ==================

# this will get it submitted later after we build it
push @submitfiles, $submitfile;

my @basesubmit = "universe = vanilla
executable = x_sleep.pl
log = $submitfile.log
hold = False
arguments = 1
notification = never
";

TLOG("Building massive submit file '$submitfile'\n");

open(SF,">$submitfile") or die "Failed to open:$submitfile :$!\n";
print SF @basesubmit;
GenerateMassive(1000);
print SF "queue\n";
close(SF);

my $on_execute = sub {
	TLOG("Saw Execute event in userlog\n");
};

my $on_evictedwithoutcheckpoint = sub {
	TLOG("Saw Evicted Without Checkpoint in userlog. This can happen\n");
};

my $submitted = sub {
	TLOG("Saw Submit event in userlog. removing job $ClusterId\n");
	runToolNTimes("condor_rm $ClusterId",1,0);
};

my $aborted = sub {
	TLOG("Saw Abort event in userlog. this was expected\n");
};

# this gets passed in to RunTest to get the cluster id back.
my $GetClusterId = sub {
	my $cid = shift;
	$ClusterId = $cid;
	TLOG("Got Cluster $ClusterId from condor_submit\n");
};

# ========= submit the massive submit file ==================

CondorTest::RegisterSubmit( $testname, $submitted );
CondorTest::RegisterAbort( $testname, $aborted );
CondorTest::RegisterEvictedWithoutCheckpoint( $testname, $on_evictedwithoutcheckpoint );

my $res = 0;
foreach my $file (@submitfiles) {
	TLOG("Running submit_massive test on file $file:\n");
	$res = CondorTest::RunTest($testname, $file, 0, $GetClusterId);
	CondorTest::RegisterResult($res, test_name=>$testname);
	if($res == 1) {
		CondorTest::debug("$testname: SUCCESS\n",1);
	} else {
		CondorTest::debug("$testname: FAILED\n",1);
	}
}

CondorTest::EndTest();

## Subroutines here
##
sub GenerateMassive {
	my $iterations = shift;
	my $counter = 0;
	my $pattern = "x";
	my $unique = "+unique";
	my $genunique = "";
	while($counter < $iterations) {
		$genunique = "$unique$counter = $pattern";
		print SF "$genunique\n";
		$counter++;
		$pattern = $pattern . "xxxxxxxxxxxxxxxx";
	}
};

