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

# configure the personal condor that we need
#
#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	use FEATURE : PartitionableSlot
CONDOR_TESTREQ_CONFIG
#endtestreq



# needs to specify every time before a check
my $command_arg;
my $pid;
my $executable = "x_sleep.pl";
my $arguments;
my $submit_content;
my $submitfile;

# other variables, updated through each check
my %Attr_old;
my %Attr_new;
my @fields;
my @machine_info;
my @summary;
my %cnt_num;
my $option;
my %table;
my %table1;
my %table2;
my $table1_ref;
my $table2_ref;
my @content;

# elements of the format file
my $format_file;
my $format_content;
my $rules_file;
my $rules_content;
my $orig_ads;
my $new_ads;


print "############################################################################\n";
$option = "compact";
$pid = $$;
$arguments = 600;
$orig_ads = "$testname"."_orig.ads";
$new_ads = "$testname"."_new.ads";
$submit_content =
"executable = $executable
arguments = $arguments
queue";
$submitfile = "$testname$pid.sub";
emit_dag_files($testname,$submit_content,$pid);

$rules_content = 
"DOMAIN=cs.wisc.edu
SharedFileSystem=true
NAME Simulate multiple STARTD platforms
REQUIREMENTS MyType == \"Machine\" || MyType == \"Slot\"

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 Arch           = \"\$(ARCH)\"
SET OpSys          = \"\$(OS)\"
SET OpSysAndVer    = \"\$(OSAndVer)\"
SET OpSysVer       = \"\$(OSVer)\"
SET OpSysName      = \"\$(OSName)\"
SET OpSysMajorVer  = \"\$(OSMajor)\"
SET OpSysShortName = \"\$(OSShort)\"
SET OpSysLegacy    = \"\$(OSLegacy)\"
SET NumDynamicSlots = 0
SET TotalGpus = 0
SET Mips = 0
SET KFlops = 0

TRANSFORM Arch,OS,OSAndVer,OSVer,OSName,OSMajor,OSShort,OSLegacy from (
  X86_64 LINUX   Debian7      700 Debian       7 Debian7  LINUX
  X86_64 LINUX   Debian8      800 Debian       8 Debian8  LINUX
  X86_64 LINUX   SL6          602 SL           6 SL6      LINUX
  X86_64 LINUX   SL7          702 SL           7 SL7      LINUX
  X86_64 LINUX   RedHat6      607 RedHat       6 RedHat6  LINUX
  X86_64 LINUX   RedHat6      607 RedHat       6 RedHat6  LINUX
  INTEL  LINUX   RedHat6      607 RedHat       6 RedHat6  LINUX
  X86_64 LINUX   RedHat7      702 RedHat       7 RedHat7  LINUX
  X86_64 WINDOWS WINDOWS601   601 Windows7   601 Win7    WINNT61
  X86_64 WINDOWS WINDOWS602   602 Windows8   602 Win8    WINNT62
  X86_64 WINDOWS WINDOWS1000 1000 Windows10 1000 Win10   WINNT100
  X86_64 OSX     MacOSX7      705 Lion         7 MacOSX7  OSX
  X86_64 OSX     MacOSX8      805 MountainLion 8 MacOSX8  OSX
  X86_64 OSX     MacOSX10    1001 ElCapitan   10 MacOSX10  OSX
  X86_64 OSX     MacOSX10    1005 ElCapitan   10 MacOSX10  OSX
  X86_64 LINUX   Ubuntu12    1204 Ubuntu      12 Ubuntu12  LINUX
  X86_64 LINUX   Ubuntu14    1404 Ubuntu      14 Ubuntu14  LINUX
  X86_64 LINUX   Fedora23    2300 Fedora      23 Fedora23  LINUX
  X86_64 LINUX   Fedora24    2400 Fedora      24 Fedora24  LINUX
  X86_64 LINUX   Fedora25    2500 Fedora      25 Fedora25  LINUX
  X86_64 FREEBSD FreeBSD10   1003 FreeBSD     10 FreeBSD10 FREEBSD
)";
$rules_file = "$option.rules";
open(FH,">$rules_file") || print "FAILED opening file $rules_file\n";
print FH $rules_content;
close(FH);

my $counter = 100;
while ($counter ne 0){
	if ($counter == 0){
		print "Took too long!\n";
		exit(1);
	} else {
		`condor_status -long > $orig_ads`;
		%Attr_old = read_attr($orig_ads);
		if (scalar keys %Attr_old == 0){
			sleep(1);
		} else {
			last;
		}
		$counter --;
	}
}
`condor_status -long > $orig_ads`;
%Attr_old = read_attr($orig_ads);
`condor_transform_ads -rules $rules_file -out $new_ads $orig_ads`;

@content = `condor_status -ads $new_ads -compact`;
%Attr_new = read_attr($new_ads);
print @content;
($table1_ref,$table2_ref) = read_status_output(\@content);
%table1 = %{$table1_ref};
%table2 = %{$table2_ref};

@machine_info = split_fields(\%table1);
@summary = split_fields(\%table2);
if (check_heading('status_compact_machine',\%table1) && check_heading('status_summary',\%table2)){
	RegisterResult(check_status(\@machine_info,\@summary,$option,\%Attr_old,\%Attr_new,20),check_name =>$option,test_name =>$testname);
} else {
	RegisterResult(0, check_name => $option, test_name => $testname);
}
unlink($new_ads);
unlink($orig_ads);
print "############################################################################\n";
`condor_submit $submitfile`;
$option = "run";
$counter = 100;
while ($counter ne 0){
	if ($counter == 0){
		print "Took too long!\n";
		exit(1);
	} else {
		`condor_status -schedd -long > $orig_ads`;
		%Attr_old = read_attr($orig_ads);
#		if (scalar keys %Attr_old == 0){
		if ($Attr_old{0}{ShadowsRunning} == 0){
			sleep(1);
		} else {
			last;
		}
		$counter --;
	}
}
`condor_status -schedd -long > "${orig_ads}.drain"`;
%Attr_old = read_attr("${orig_ads}.drain");
print "attr_old is\n";
my $k;
foreach $k (sort keys %Attr_old) {
    print "$k => $Attr_old{$k}\n";
}
print "attr_old was\n";
`condor_transform_ads -rules $rules_file -out "${new_ads}.drain" "${orig_ads}.drain"`;
print "new ads are\n";
system("/bin/cat ${new_ads}.drain");
print "new ads were\n";
@content = `condor_status -ads "${new_ads}.drain" -run`;
`condor_rm -all`;
%Attr_new = read_attr("${new_ads}.drain");
print @content;
my $index = 0;
for my $i (0.. scalar @content -1){
	if ($content[$i] =~ /\S/){
		$table{$index} = $content[$i];
		$index++;
	}
}
@machine_info = split_fields(\%table);
#@summary = split_fields(\%table2);
if (check_heading('status_run_machine',\%table)){
	RegisterResult(check_status(\@machine_info,\@summary,$option,\%Attr_old,\%Attr_new,20),check_name =>$option,test_name =>$testname);
} else {
	RegisterResult(0, check_name => $option, test_name => $testname);
}

print "############################################################################\n";
$option = "schedd";
`condor_status -schedd -long > $orig_ads`;
%Attr_old = read_attr($orig_ads);
`condor_transform_ads -rules $rules_file -out $new_ads $orig_ads`;

@content = `condor_status -ads $new_ads -schedd`;
%Attr_new = read_attr($new_ads);
print @content;
($table1_ref,$table2_ref) = read_status_output(\@content);
%table1 = %{$table1_ref};
%table2 = %{$table2_ref};

@machine_info = split_fields(\%table1);
@summary = split_fields(\%table2);
if (check_heading('status_schedd_machine',\%table1) && check_heading('status_schedd_summary',\%table2)){
	RegisterResult(check_status(\@machine_info,\@summary,$option,\%Attr_old,\%Attr_new,20),check_name =>$option,test_name =>$testname);
} else {
	RegisterResult(0, check_name => $option, test_name => $testname);
}

print "############################################################################\n";
$option = "negotiator";
`condor_status -negotiator -long > $orig_ads`;
%Attr_old = read_attr($orig_ads);
`condor_transform_ads -rules $rules_file -out $new_ads $orig_ads`;

@content = `condor_status -ads $new_ads -negotiator`;
%Attr_new = read_attr($new_ads);
print @content;
$index = 0;
for my $i (0.. scalar @content -1){
	if ($content[$i] =~ /\S/){
		$table{$index} = $content[$i];
		$index++;
	}
}
my @chars = split("",$table{0});
$index = 0;
for my $i (0..scalar @chars-1){
	if (!($chars[$i] =~ /M/)){
		$index++;
	}
	else {last;}
}
@machine_info = split_fields(\%table);
#for my $i (0..(scalar keys %table)-1){
#	@chars = split("",$table{$i});
#	$machine_info[0][$i] = trim(join("",@chars[0..$index-1]));
#	$machine_info[1][$i] = trim(join("",@chars[$index..(scalar @chars -1)]));
#}

if (check_heading('status_negotiator',\%table)){
	RegisterResult(check_status(\@machine_info,\@summary,$option,\%Attr_old,\%Attr_new,20),check_name =>$option,test_name =>$testname);
} else {
	RegisterResult(0, check_name => $option, test_name => $testname);
}

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