#!/usr/bin/env perl

use strict;
use warnings;
use Cwd;

use CondorCmdStatusWorker2;
use CondorTest;
use CondorUtils;

#
# FIXME: Use runcmd(), and/or refactor into a function the conversion 
# from a condor_status command to a hash of ad names.  [from CondorUtils.pm]
#

my $testname = "cmd_status_shows-absent";
my $top = getcwd();

print "Ad file will be here:$top\n";

my $iswindows = is_windows();

print "Test:$testname considers id windows:$iswindows\n";

#
# This is terrible hack.
#
my $terribleHack = CondorCmdStatusWorker2::SetUp( "cmd_status_shows-absent" );
## my $terribleHack = "x&y";
if( $terribleHack eq "" ) {
    die( "Unable to set up personal condor(s).\n" );
}
my( $personalCondorA, $personalCondorB ) = split( '&', $terribleHack );

my $adfile = CondorTest::TempFileName("$testname.classads");
fullchomp($adfile);

#$adfile = "$top/$adfile";

#print "Windows vs others checking\n";

#print "$adfile is value before checking windows\n";

#if($iswindows) {
	#$_ = $adfile;
	#s/\//\\\\/g;
	#$adfile = $_;
#} else {
#}

#print "Path to tempm file is:$adfile\n";

#
# Advertise both absent and present ads so that we can check for false
# positives and false negatives.
#

my $basead = ' MyType                    = "Machine"
TargetType                = "Job"
OpSys                     = "LINUX"
Arch                      = "X86_64"
State                     = "Unclaimed"
Activity                  = "Idle"
LoadAvg                   = 0.0
Memory                    = 666
MyCurrentTime             = 1666
EnteredCurrentActivity    = 1000 ';

my $absenttype = '
Absent                    = TRUE
ClassAdLifetime           = 1000 ';

my $absent1 = '
Name                      = "slot1@absent-machine"
Machine                   = "absent-machine" ';

my $absent2 = '
Name                      = "slot2@absent-machine"
Machine                   = "absent-machine" ';

my $present1 = '
Name                      = "slot1@present-machine"
Machine                   = "present-machine" ';

my $present2 = '
Name                      = "slot2@present-machine"
Machine                   = "present-machine" ';

my @absentnames = (
	"slot1\@absent-machine",
	"slot2\@absent-machine",
);

my @presentnames = (
	"slot1\@present-machine",
	"slot2\@present-machine",
);

# we need to construct a file with 2 absent classads and two
# present classads. All four have the same base and one unique 
# part per add. Additionally, the absent ads need the absenttype lines
# when doing condor_advertise, -multiple lets an empty line
# be an ad separator.

if(-f $adfile) {
	print "AdFile:$adfile exists before creation\n";
}

print "Temp file for ads:$adfile\n";
open(AF,">$adfile") or die "Temp ad file:$adfile could not be created\n";
print AF "$basead";
print AF "$absenttype";
print AF "$absent1";
print AF "\n";
print AF "\n";
print AF "$basead";
print AF "$absenttype";
print AF "$absent2";
print AF "\n";
print AF "\n";
print AF "$basead";
print AF "$present1";
print AF "\n";
print AF "\n";
print AF "$basead";
print AF "$present2";
close(AF);

my $adfilesize = 0;
if(-f $adfile) {
	print "AdFile:$adfile exists after creation\n";
	$adfilesize = -s $adfile;
	print "file size for ads after creation is:$adfilesize\n";
}

my @adresults = ();
runCondorTool("condor_advertise -debug -multiple UPDATE_STARTD_AD $adfile",\@adresults,2,{emit_output=>1});

#
# Verify that we added the ads succesfully.
#
my %verifiedAdNames;

my @statusres = ();
runCondorTool("condor_status -af Name -const \"(TRUE || Absent)\"",\@statusres,2,{emit_output=>1});
print "verifying ads were advertised\n";
foreach my $line (@statusres ) {
    $line =~ s/[\012\015]+$//;
	print "verifying ads published\n";
	print ":$line:\n";
    #$verifiedAdNames{ $line } = undef;
    $verifiedAdNames{ $line } = 1;
}

# by checking both array presentname and absent names for existence in @verifiedAdNames
# we should know if all ads cound be gotten and what type they are.
foreach my $ad (@presentnames) {
	if(exists $verifiedAdNames{$ad}) {
		RegisterResult(1,"test_name", "$testname");
	} else {
		print "a present ad:$ad is missing\n";
		RegisterResult(0,"test_name", "$testname");
	}
}

foreach my $ad (@absentnames) {
	if(exists $verifiedAdNames{$ad}) {
		RegisterResult(1,"test_name", "$testname");
	} else {
		print "an absent ad:$ad is missing\n";
		RegisterResult(0,"test_name", "$testname");
	}
}

#
# Test the -absent flag: when we run condor_status with it do we get 
# all the absent machines, and only the machines that are absent?
#

@statusres = (); # clear for reuse
my %absentAdNames;
runCondorTool("condor_status -absent -af Name",\@statusres,2,{emit_output=>1});
foreach my $line (@statusres) {
    $line =~ s/[\012\015]+$//;
	print "Absent:$line:\n";
    $absentAdNames{ $line } = undef;
}

# Tolerate other ads, but fail if a present ad appears.

foreach my $ad (@presentnames) {
	if(exists $absentAdNames{$ad}) {
		print "a prsent ad:$ad is in absent list\n";
		RegisterResult(0,"test_name", "$testname");
	} else {
		RegisterResult(1,"test_name", "$testname");
	}
}


RegisterResult(1,"test_name", "$testname");
RemoveAllJobs();
CondorTest::EndTest();
#
# Test(s) completed successfully.
#
#print( "cmd_status_shows-absent SUCCESS\n" );
#exit( 0 );

# -----------------------------------------------------------------------------

sub RemoveAllJobs {
	my $res = 1;
	my @rm1 = ();
	my @rm2 = ();
	$ENV{CONDOR_CONFIG} = $personalCondorA;
	$res = runCondorTool("condor_rm -all",\@rm1,2,{emit_output=>0});
	if(!$res) {
		print "removal from local $personalCondorA failed\n";
	}
	sleep(2);
	$ENV{CONDOR_CONFIG} = $personalCondorB;
	$res = runCondorTool("condor_rm -all",\@rm2,2,{emit_output=>0});
	if(!$res) {
		print "removal from local $personalCondorB failed\n";
	}
	sleep(2);

}
