#! /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;
use Check::SimpleJob;
use CheckOutputFormats;
use strict;
use warnings;

my $testname = "cmd_status-af";

# other variables, updated through each check
my %Attr_old;
my %Attr_new;
my $option;
my @content;

# elements of the format file
my $rules_file;
my $rules_content;
my $orig_ads_first_test;
my $orig_ads_second_test;
my $new_ads;

my $sel0;
my $sel1;
my $sel2;
my $sel3;

print "############################################################################\n";
$orig_ads_first_test  = "$testname"."_orig_first_test.ads";
$orig_ads_second_test = "$testname"."_orig_second_test.ads";
$new_ads = "$testname"."_new.ads";

$option = "af";
$rules_content = 
"NAME Simulate multiple STARTD platforms
REQUIREMENTS MyType == \"Machine\"

EVALMACRO SLOT = splitslotname(Name)[0]
INDEX = \$(ROW)+1

SET UidDomain = \"\$(DOMAIN:cs.wisc.edu)\"
SET Machine = \"exec\$INT(INDEX,%02d).\$(MY.UidDomain)\"
if defined SLOT
  SET  Name = \"\$(SLOT)@\$(My.Machine)\"
else
  COPY Machine Name
endif
COPY Machine PrivateNetworkName

if \$(SharedFileSystem)
  COPY UidDomain FileSystemDomain
else
  COPY Machine FileSystemDomain
endif

SET State =  \"\$(STATE)\"
SET Activity = \"\$(ACTIVITY)\"

TRANSFORM STATE,ACTIVITY from (
Unclaimed Idle
Claimed Retiring
Matched Busy
Preempting Busy
Backfill Busy
)";

$rules_file = "$option.rules";
open(FH,">$rules_file") || print "FAILED opening file $rules_file\n";
print FH $rules_content;
close(FH);

%Attr_old = read_attr($orig_ads_first_test);
`condor_transform_ads -rules $rules_file -out $new_ads $orig_ads_first_test`;

%Attr_new = read_attr($new_ads);

# remove classads that causes complications
delete $Attr_new{0}{CpuBusy};
delete $Attr_new{0}{IsValidCheckpointPlatform};
delete $Attr_new{0}{Requirements};
delete $Attr_new{0}{SlotWeight};
delete $Attr_new{0}{Unhibernate};
delete $Attr_new{0}{MachineMaxVacateTime};
delete $Attr_new{0}{CondorVersion};
delete $Attr_new{0}{AddressV1};
delete $Attr_new{0}{TotalTimeClaimedIdle};
for my $key (sort keys %{$Attr_new{0}}) {
	if ($Attr_new{0}{$key} =~ /,/) { delete $Attr_new{0}{$key}; }
}
$sel0 = "TotalSlotMemory";
$sel1 = "Arch";
$sel2 = "LastHeardFrom";
$sel3 = "MaxJobRetirementTime";
@content = `condor_status -ads $new_ads -af $sel0 $sel1 $sel2 $sel3 -sort Name`;
print @content;

if (check_af(\%Attr_new,\@content,"",$sel0,$sel1,$sel2,$sel3)){
	RegisterResult(1,check_name=>"$option",test_name=>$testname);
} else {
	RegisterResult(0,check_name=>"$option",test_name=>$testname);
} 

$sel0 = "UpdatesSequenced";
$sel1 = "HasJobDeferral";
$sel2 = "Activity";
$sel3 = "DaemonLastReconfigTime";
@content = `condor_status -ads $new_ads -af:, $sel0 $sel1 $sel2 $sel3 -sort Name`;
print @content;

if (check_af(\%Attr_new,\@content,":,",$sel0,$sel1,$sel2,$sel3)){
	RegisterResult(1,check_name=>"$option:,",test_name=>$testname);
} else {
	RegisterResult(0,check_name=>"$option:,",test_name=>$testname);
}

$sel0 = "TotalSlotCpus";
$sel1 = "has_ssse3";
$sel2 = "MonitorSelfRegisteredSocketCount";
$sel3 = "TimeToLive";
@content = `condor_status -ads $new_ads -af:h $sel0 $sel1 $sel2 $sel3 -sort Name`;
print @content;

if (check_af(\%Attr_new,\@content,":h",$sel0,$sel1,$sel2,$sel3)){
	RegisterResult(1,check_name=>"$option:h",test_name=>$testname);
} else {
	RegisterResult(0,check_name=>"$option:h",test_name=>$testname);
}

$sel0 = "HasSingularity";
$sel1 = "WakeOnLanEnabledFlags";
$sel2 = "DetectedMemory";
$sel3 = "UidDomain";
@content = `condor_status -ads $new_ads -af:ln $sel0 $sel1 $sel2 $sel3 -sort Name`;
print @content;

if (check_af(\%Attr_new,\@content,":ln",$sel0,$sel1,$sel2,$sel3)){
	RegisterResult(1,check_name=>"$option:ln",test_name=>$testname);
} else {
	RegisterResult(0,check_name=>"$option:ln",test_name=>$testname);
}

$sel0 = "ClockMin";
$sel1 = "Memory";
$sel2 = "UtsnameSysname";
$sel3 = "HibernationLevel";
@content = `condor_status -ads $new_ads -af:lrng $sel0 $sel1 $sel2 $sel3 -sort Name`;
print @content;

if (check_af(\%Attr_new,\@content,":lrng",$sel0,$sel1,$sel2,$sel3)){
	RegisterResult(1,check_name=>"$option:lrng",test_name=>$testname);
} else {
	RegisterResult(0,check_name=>"$option:lrng",test_name=>$testname);
}

$sel0 = "HibernationState";
$sel1 = "ExecutableSize";
$sel2 = "IsWakeAble";
$sel3 = "State";
@content = `condor_status -ads $new_ads -af:t $sel0 $sel1 $sel2 $sel3 -sort Name`;
print @content;

if (check_af(\%Attr_new,\@content,":t",$sel0,$sel1,$sel2,$sel3)){
	RegisterResult(1,check_name=>"$option:t",test_name=>$testname);
} else {
	RegisterResult(0,check_name=>"$option:t",test_name=>$testname);
}

print "############################################################################\n";
$option = "format";
my $flag;
$rules_content = 
"NAME Simulate multiple STARTD platforms
REQUIREMENTS MyType == \"Machine\"

EVALMACRO SLOT = splitslotname(Name)[0]
INDEX = \$(ROW)+1

SET UidDomain = \"\$(DOMAIN:cs.wisc.edu)\"
SET Machine = \"exec\$INT(INDEX,%02d).\$(MY.UidDomain)\"
if defined SLOT
  SET  Name = \"\$(SLOT)@\$(My.Machine)\"
else
  COPY Machine Name
endif
COPY Machine PrivateNetworkName

if \$(SharedFileSystem)
  COPY UidDomain FileSystemDomain
else
  COPY Machine FileSystemDomain
endif

SET State =  \"\$(STATE)\"
SET Activity = \"\$(ACTIVITY)\"

TRANSFORM STATE,ACTIVITY from (
Unclaimed Idle
Claimed Retiring
Matched Busy
Preempting Busy
Backfill Busy
)";

$rules_file = "$option.rules";
open(FH,">$rules_file") || print "FAILED opening file $rules_file\n";
print FH $rules_content;
close(FH);

%Attr_old = read_attr($orig_ads_second_test);
`condor_transform_ads -rules $rules_file -out $new_ads $orig_ads_second_test`;

%Attr_new = read_attr($new_ads);
delete $Attr_new{0}{CpuBusy};
delete $Attr_new{0}{IsValidCheckpointPlatform};
delete $Attr_new{0}{Requirements};
delete $Attr_new{0}{SlotWeight};
delete $Attr_new{0}{Unhibernate};
delete $Attr_new{0}{MachineMaxVacateTime};
delete $Attr_new{0}{CondorVersion};
delete $Attr_new{0}{AddressV1};
delete $Attr_new{0}{RecentDaemonCoreDutyCycle};
delete $Attr_new{0}{DaemonCoreDutyCycle};
delete $Attr_new{0}{TotalTimeClaimedIdle};

#$sel0 = "OpSys";
#$sel1 = "CpuIsBusy";
#$sel2 = "Memory";
#$sel3 = "Rank";
$flag = " %v";
#print "$sel0\n$sel1\n$sel2\n$sel3\n";

my $result0;
my $result1;
my $result2;
my $result3;

$sel0 = "EnteredCurrentState";
@content = `condor_status -ads $new_ads -format$flag $sel0 -sort Name`;
$result0 = check_type(\%Attr_new,$flag,\@content,$sel0);

$sel1 = "SlotType";
@content = `condor_status -ads $new_ads -format$flag $sel1 -sort Name`;
$result1 = check_type(\%Attr_new,$flag,\@content,$sel1);

$sel2 = "RecentJobUserPrioPreemptions";
@content = `condor_status -ads $new_ads -format$flag $sel2 -sort Name`;
$result2 = check_type(\%Attr_new,$flag,\@content,$sel2);

$sel3 = "RecentJobUserPrioPreemptions";
@content = `condor_status -ads $new_ads -format$flag $sel3 -sort Name`;
$result3 = check_type(\%Attr_new,$flag,\@content,$sel3);

RegisterResult(($result0 && $result1 && $result2 && $result2 && $result3),check_name=> "$option$flag", testname=>$testname);


$flag = " %V";
$sel0 = "HibernationLevel";
@content = `condor_status -ads $new_ads -format$flag $sel0 -sort Name`;
$result0 = check_type(\%Attr_new,$flag,\@content,$sel0);

$sel1 = "CpuModelNumber";
@content = `condor_status -ads $new_ads -format$flag $sel1 -sort Name`;
$result1 = check_type(\%Attr_new,$flag,\@content,$sel1);

$sel2 = "UtsnameMachine";
@content = `condor_status -ads $new_ads -format$flag $sel2 -sort Name`;
$result2 = check_type(\%Attr_new,$flag,\@content,$sel2);

$sel3 = "StarterAbilityList";
@content = `condor_status -ads $new_ads -format$flag $sel3 -sort Name`;
$result3 = check_type(\%Attr_new,$flag,\@content,$sel3);

RegisterResult(($result0 && $result1 && $result2 && $result2 && $result3),check_name=>"$option$flag", testname=>$testname);


$flag = " %s";
$sel0 = "UidDomain";
@content = `condor_status -ads $new_ads -format$flag $sel0 -sort Name`;
$result0 = check_type(\%Attr_new,$flag,\@content,$sel0);

$sel1 = "VirtualMemory";
@content = `condor_status -ads $new_ads -format$flag $sel1 -sort Name`;
$result1 = check_type(\%Attr_new,$flag,\@content,$sel1);

$sel2 = "DaemonLastReconfigTime";
@content = `condor_status -ads $new_ads -format$flag $sel2 -sort Name`;
$result2 = check_type(\%Attr_new,$flag,\@content,$sel2);

$sel3 = "TimeToLive";
@content = `condor_status -ads $new_ads -format$flag $sel3 -sort Name`;
$result3 = check_type(\%Attr_new,$flag,\@content,$sel3);

RegisterResult(($result0 && $result1 && $result2 && $result2 && $result3),check_name=>"$option$flag", testname=>$testname);


$flag = " %d";
$sel0 = "Activity";
@content = `condor_status -ads $new_ads -format$flag $sel0 -sort Name`;
$result0 = check_type(\%Attr_new,$flag,\@content,$sel0);

$sel1 = "DetectedCpus";
@content = `condor_status -ads $new_ads -format$flag $sel1 -sort Name`;
$result1 = check_type(\%Attr_new,$flag,\@content,$sel1);

$sel2 = "SingularityVersion";
@content = `condor_status -ads $new_ads -format$flag $sel2 -sort Name`;
$result2 = check_type(\%Attr_new,$flag,\@content,$sel2);

$sel3 = "HasJICLocalStdin";
@content = `condor_status -ads $new_ads -format$flag $sel3 -sort Name`;
$result3 = check_type(\%Attr_new,$flag,\@content,$sel3);

RegisterResult(($result0 && $result1 && $result2 && $result2 && $result3),check_name=>"$option$flag", testname=>$testname);


$flag = " %f";
$sel0 = "HasReconnect";
@content = `condor_status -ads $new_ads -format$flag $sel0 -sort Name`;
$result0 = check_type(\%Attr_new,$flag,\@content,$sel0);

$sel1 = "SubnetMask";
@content = `condor_status -ads $new_ads -format$flag $sel1 -sort Name`;
$result1 = check_type(\%Attr_new,$flag,\@content,$sel1);

$sel2 = "HasJICLocalStdin";
@content = `condor_status -ads $new_ads -format$flag $sel2 -sort Name`;
$result2 = check_type(\%Attr_new,$flag,\@content,$sel2);

$sel3 = "TotalSlotCpus";
@content = `condor_status -ads $new_ads -format$flag $sel3 -sort Name`;
$result3 = check_type(\%Attr_new,$flag,\@content,$sel3);

RegisterResult(($result0 && $result1 && $result2 && $result2 && $result3),check_name=>"$option$flag", testname=>$testname);


$flag = " %.2f";
$sel0 = "SubnetMask";
@content = `condor_status -ads $new_ads -format$flag $sel0 -sort Name`;
$result0 = check_type(\%Attr_new,$flag,\@content,$sel0);

$sel1 = "Memory";
@content = `condor_status -ads $new_ads -format$flag $sel1 -sort Name`;
$result1 = check_type(\%Attr_new,$flag,\@content,$sel1);

$sel2 = "HasReconnect";
@content = `condor_status -ads $new_ads -format$flag $sel2 -sort Name`;
$result2 = check_type(\%Attr_new,$flag,\@content,$sel2);

$sel3 = "HasJICLocalStdin";
@content = `condor_status -ads $new_ads -format$flag $sel3 -sort Name`;
$result3 = check_type(\%Attr_new,$flag,\@content,$sel3);

RegisterResult(($result0 && $result1 && $result2 && $result2 && $result3),check_name=>"$option$flag", testname=>$testname);

$flag = " \"%6.2f;\"";
$sel0 = "Rank";
@content = `condor_status -ads $new_ads -format$flag $sel0 -sort Name`;
#print @content,"\n";
$result0 = check_type(\%Attr_new, $flag, \@content, $sel0);
RegisterResult($result0, check_name => "$option$flag", testname => $testname);

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