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

$cmd = 'job_core_shadow-lessthan-memlimit_van.cmd';
$testdesc =  'Make sure shadow process memory stays bellow a limit - vanilla U';
$testname = "job_core_shadow-lessthan-memlimit_van";

$abnormal = sub {
	CondorTest::debug("Normal abort event\n");
};

$aborted = sub {
	CondorTest::debug("Abnormal abort event\n",1);
};

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

	CondorTest::debug("Received execute event: $cluster\n", 1);

	my $line;
	my $shadowPid;
	my $shadowPidFound = 0;
	my $shadowPlatform;
	my $shadowPlatformFound = 0;
	my $privateMem = 1901;
	open(SHADOWLOG, "<TestingPersonalCondor/local/log/ShadowLog") || die "Failed to open Shadow log";

	# find the shadow id from the shadow log
	while(<SHADOWLOG>)
	{
		CondorUtils::fullchomp($_);
		$line = $_;

		# grab the pid from the line that has the cluster id <date time (cluster.0) (pid) message>
		if( $shadowPidFound != 1 && $line =~ /.*\($cluster\.0\)[\s]\(([0-9]*)\).*/)
	    {
			$shadowPid = $1;
			CondorTest::debug("Located shadow pid: $1\n", 1);
			$shadowPidFound = 1;
			if($shadowPidFound == 1 && $shadowPlatformFound == 1)
			{
				last;
			}
		}

		# grab the platform from the CondorPlatform field
		if( $shadowPlatformFound != 1 && $line =~ /.*\$CondorPlatform: (.*) \$.*/ )
		{
			$shadowPlatform = $1;
			CondorTest::debug("Located shadow platform: $1\n", 1);
			$shadowPlatformFound = 1;
			if($shadowPlatformFound == 1 && $shadowPidFound == 1)
			{
				last;		
			}
		}
	}
	close(SHADOWLOG);

	# calc the private memory usage using top
	open(TOPOUT, "pmap -d $shadowPid|grep private|") || die "Failed to run top on shadow log pid: $shadowPid";
	while(<TOPOUT>)
	{
		chomp($_);
		$line = $_;
		CondorTest::debug("pmap: $line\n",1);
		if($line =~ m/private: (.*)K/ )
		{
			$privateMem = $1;
			CondorTest::debug("Private memory calculated as: $privateMem\n", 1);
		}
	}
	close(TOPOUT);

	my $rmcmd = "condor_rm $cluster";
	$status = CondorTest::runCondorTool($rmcmd,\@adarray,2);
	if(!$status)
	{
		CondorTest::debug("Test failure due to Condor Tool Failure<$rmcmd>\n",1);
		exit(1)
	}

	# determin if we are a 64 bit platform	
	my $is64bit = 0;
	if($shadowPlatform =~ /.*64.*/)
	{
		CondorTest::debug("Determined that this is a 64 bit platform\n", 1);
		$is64bit = 1;
	}
	else
	{
		CondorTest::debug("Determined that this is a 32 bit platform\n", 1);
	}

	my $limit32 = 700;
	my $limit64 = 1900;
	my $shadowMemoryLimit = $limit32; # in kilobytes
	if($is64bit == 1)
	{
		$shadowMemoryLimit = $limit64;
	}
	 
	CondorTest::debug("Shadow memory limit: $shadowMemoryLimit -- Private memory: $privateMem\n",1);
	if($privateMem > $shadowMemoryLimit)
	{
		CondorTest::debug("Test failure do to shadow memory usage\n", 1);
		exit(1)
	}
	else
	{
		CondorTest::debug("------------Test PASSED------------\n")
	}

};

CondorTest::RegisterExecute($testname, $executed);
CondorTest::RegisterExitedAbnormal( $testname, $abnormal );
CondorTest::RegisterAbort         ( $testname, $aborted );

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

