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

$cmd = 'lib_procapi_pidtracking-snapshot.cmd';
$testdesc =  'We cleanup correctly after our kids. - vanilla U';
$testname = "lib_procapi_pidtracking-snapshot";
$datafile = "lib_procapi_pidtracking-snapshot.data";
$kiddatafile = "lib_procapi_pidtracking-snapshot.data.kids";
$worked = "yes";

$execute = sub
{
	CondorTest::debug( "Goood, job is running so we'll start the timer....\n",1);
	CondorTest::RegisterTimed($testname, $timed, 300); # give it 5 minutes
};

$timed = sub
{
        print scalar localtime() . "\n";
        CondorTest::debug("lib_procapi_pidtracking-snapshot HUNG !!!!!!!!!!!!!!!!!!\n",1);
        exit(1);
};

$ExitSuccess = sub {
	my %info = @_;
	my $line;
	my $pidline;
	my @pids;
	my @kidpids;

	open(PIN, "<$datafile") or die "Could not open data file '$datafile': $?";
	while(<PIN>)
	{
		CondorUtils::fullchomp();
		$line = $_;
		if( $line =~ /\s*Relationship:\s*(\d+)\s*created\s*(\d+).*$/ )
		{
			CondorTest::debug( "Process $1 created $2\n",1);
			push @pids, $2;
		}
		CondorTest::debug( "$line\n",1);
	}
	close(PIN);
	open(PIN, "<$kiddatafile") or die "Could not open data file '$kiddatafile': $?";
	while(<PIN>)
	{
		CondorUtils::fullchomp();
		$line = $_;
		if( $line =~ /\s*Relationship:\s*(\d+)\s*child\s+created\s*(\d+).*$/ )
		{
			CondorTest::debug( "Process $1 created $2\n",1);
			push @kidpids, $2;
		}
		CondorTest::debug( "$line\n",1);
	}
	close(PIN);

	# determine correct ps by expecting a certain number of lines of text

	my $pscmd = "ps -ef";
	runcmd("which ps");
	open(PULL, "$pscmd | wc -l 2>&1 |") || die "ps -eo pid failed: $!\n";
	while(<PULL>)
	{
		my $outputlines;
		CondorUtils::fullchomp();
		$outputlines = $_;
		my $linesoftext = 0;
		$linesoftext = $outputlines;
		if($linesoftext == 0)
		{
			CondorTest::debug("Wrong ps options!\n",1);
			$pscmd = "ps -ax";
		}
		else
		{
			CondorTest::debug("Ps delivered $linesoftext too check!\n",1);
		}
	}

	# move ahead with a valid ps command to parse output from

	open(PULL, "$pscmd 2>&1 |") || die "$pscmd failed: $!\n";
	while(<PULL>)
	{
		my $thispid;
		CondorUtils::fullchomp();
		$pidline = $_;
		if( $pscmd eq "ps -ax") # with pid first
		{
			if($pidline =~ /\s*(\d+)\s*.*$/ )
			{
				$thispid = $1;
				CondorTest::debug("Checking againt list of removed pids --$thispid--\n",1);
				foreach my $pidcheck (@pids)
				{
					if($thispid eq $pidcheck)
					{
						CondorTest::debug("Test fails because PARENT pid $pidcheck should have been removed\n",1);
						$worked = "no";
						runcmd("kill -9 $pidcheck");
					}
				}
				foreach my $pidcheck (@kidpids)
				{
					if($thispid eq $pidcheck)
					{
						CondorTest::debug("Test fails because CHILD pid $pidcheck should have been removed\n",1);
						$worked = "no";
						runcmd("kill -9 $pidcheck");
					}
				}
			}
		}
		else # ps -ef with uid first
		{
			if($pidline =~ /\s*[A-Za-z]+\s*(\d+).*$/ )
			{
				$thispid = $1;
				CondorTest::debug("Checking againt list of removed pids --$thispid--\n",1);
				foreach my $pidcheck (@pids)
				{
					if($thispid eq $pidcheck)
					{
						CondorTest::debug("Test fails because PARENT pid $pidcheck should have been removed\n",1);
						$worked = "no";
						runcmd("kill -9 $pidcheck");
					}
				}
				foreach my $pidcheck (@kidpids)
				{
					if($thispid eq $pidcheck)
					{
						CondorTest::debug("Test fails because CHILD pid $pidcheck should have been removed\n",1);
						$worked = "no";
						runcmd("kill -9 $pidcheck");
					}
				}
			}
		}
		#print "$pidline\n";
	}
	close(PULL);
	if($worked eq "no")
	{
		CondorTest::debug("$testname: FAILED. We created parent processes with children and inserted this list into testname.data \n",1);
		CondorTest::debug("and testname.data.kids. When we went through these files some processes were not cleaned up. They were\n",1);
		CondorTest::debug("labeled in the error as either PARENT or CHILD created by x_pid_tracking.pl. We removed them during the checking.\n",1);
		exit(1);
	}
};

CondorTest::RegisterExitedSuccess( $testname, $ExitSuccess );
CondorTest::RegisterExecute($testname, $execute);

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

