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

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	DAEMON_LIST = MASTER COLLECTOR STARTD
	use FEATURE:PartitionableSlot(1)
	# prevent the master from taking longer and longer to restart the startd each time
	# constant is minimum time to wait, factor is x^restarts added to that, ceiling is cap
	MASTER_BACKOFF_CONSTANT = 1
	#MASTER_BACKOFF_FACTOR = 1
	#MASTER_RECOVER_FACTOR = 1
	MASTER_BACKOFF_CEILING = 1
	CONFIG_FRAGMENT = $(LOCAL_DIR)/validate_policy.config
	include ifexist : $(CONFIG_FRAGMENT)
CONDOR_TESTREQ_CONFIG
#endtestreq

my $testname = "config_validate_policy_templates";

TLOG("Initialize\n");
my $config_fragment = `condor_config_val CONFIG_FRAGMENT`;

# --- helper functions
# 

# remove \r but preserve \n at the end of each line
sub winchomp {
    foreach my $arg (@_) { $arg =~ s/\012\015$/\015/; }
    return;
}

sub test_policy {
	my $knob = shift;
	my $knob_args = shift;

	print "---------------------------------------------------------------------------------------------------------\n";
	TLOG("Checking Policy $knob$knob_args\n");

	# write the policy into config, overwriting the previous policy
	open (FH, ">$config_fragment") || print "error writing to $config_fragment: $!\n";
	print FH "\nuse POLICY : $knob$knob_args\n";
	close(FH);
	chmod(0755,$config_fragment);

	my $restart = `condor_restart -startd`;
	my @who = ();
	my $result = runCondorTool("condor_who -quick", \@who, 2, {emit_output=>0});
	foreach my $line (@who) { winchomp($line); print "\t$line" if ($line =~ /^STARTD|IsReady/); }
	#TLOG("result is $result\n");

	my @summary = ();
	runCondorTool('condor_config_val -summary',\@summary,2,{emit_output=>0});

	# print the part of config that is defined by our config fragment file
	TLOG("config to validate:\n");
	my $show = 0;
	my $hash = 0;
	foreach my $line (@summary) {
		winchomp($line);
		if ($line =~ /^#.*validate_policy\.config/) { $show = 1; }
		if ($show) {
			if ($line =~ /^#/) { $hash += 1; }
			last if $hash > 2;
			print "\t$line";
		}
	}

	@who = ();
	$result = runCondorTool('condor_who -quick -wait:120 IsReady', \@who,2,{emit_output=>0});
	TLOG("query STARTD state:\n");
	foreach my $line (@who) {
		winchomp($line);
		print "\t$line" if ($line =~ /^STARTD|IsReady/);
		fullchomp($line);
		if ($line =~ /^STARTD = (.*)$/) {
			my $state = $1;
			$result = ($state eq '"Alive"');
		}
	}

	RegisterResult($result, $testname, $knob);
}

# --- Test statements begin here
# 
test_policy("ALWAYS_RUN_JOBS", "");

test_policy("DESKTOP", "");

test_policy("UWCS_DESKTOP", "");

test_policy("PREEMPT_IF_CPUS_EXCEEDED", "");
test_policy("PREEMPT_IF_MEMORY_EXCEEDED", "");
test_policy("PREEMPT_IF_DISK_EXCEEDED", "");

test_policy("HOLD_IF_CPUS_EXCEEDED", "");
test_policy("HOLD_IF_MEMORY_EXCEEDED", "");
test_policy("HOLD_IF_DISK_EXCEEDED", "");

test_policy("LIMIT_JOB_RUNTIMES", "");

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