#! /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.
##
##**************************************************************
##
## This is a very basic test of condor_Ping to show it is outputing 
## a line per check.
##
##**************************************************************

use CondorTest;
use CondorUtils;
use Check::SimpleJob;
use Check::CondorLog;

my $test_name = "cmd_condor_ping_basic";

#
# Make our expectations about ALLOW vs DENY explicit.  Not sure where the
# 'ALLOW' line comes from.
#
#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	ALLOW_READ = *
	ALLOW_WRITE = *
	ALLOW_NEGOTIATOR = *
	ALLOW_ADMINISTRATOR = *
	ALLOW_OWNER = *
	ALLOW_CONFIG = -
	ALLOW_DAEMON = *
	ALLOW_ADVERTISE_STARTD = *
	ALLOW_ADVERTISE_SCHEDD = *
	ALLOW_ADVERTISE_MASTER = *
CONDOR_TESTREQ_CONFIG
#endtestreq

my $result = 1;


#Do a job before setting tesing exit codes
print "First test basic job\n";
$result = SimpleJob::RunCheck(); # jobid 1

my @pingoutput = `condor_ping -table ALL`;
my @foundlines = ();
my $tablesize = 0;

$instruction = 0;
$allow = 0;
$read = 0;
$writte = 0;
$negotiator = 0;
$administrator = 0;
$owner = 0;
$config = 0;
$daemon = 0;
$adstartd = 0;
$adschedd = 0;
$admaster = 0;

foreach my $line (@pingoutput) {
	chomp($line);
	print "$line\n";
	if($line =~ /^\s*Instruction\s+Authentication\s+Encryption\s+Integrity\s+Decision\s+Identity\s*$/) {
		push @foundlines, $line;
		$instruction = 1;
	}elsif($line =~ /^\s*ALLOW\s+none\s+none\s+none\s+ALLOW\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$allow = 1;
	}elsif($line =~ /^\s*READ\s+none\s+none\s+none\s+ALLOW\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$read = 1;
	}elsif($line =~ /^\s*WRITE\s+none\s+none\s+none\s+ALLOW\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$writte = 1;
	}elsif($line =~ /^\s*NEGOTIATOR\s+none\s+none\s+none\s+ALLOW\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$negotiator = 1;
	}elsif($line =~ /^\s*ADMINISTRATOR\s+none\s+none\s+none\s+ALLOW\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$administrator = 1;
	}elsif($line =~ /^\s*OWNER\s+none\s+none\s+none\s+ALLOW\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$owner = 1
	}elsif($line =~ /^\s*CONFIG\s+none\s+none\s+none\s+DENY\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$config = 1;
	}elsif($line =~ /^\s*DAEMON\s+none\s+none\s+none\s+ALLOW\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$daemon = 1;
	}elsif($line =~ /^\s*ADVERTISE_STARTD\s+none\s+none\s+none\s+ALLOW\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$adstartd = 1
	}elsif($line =~ /^\s*ADVERTISE_SCHEDD\s+none\s+none\s+none\s+ALLOW\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$adschedd = 1;
	}elsif($line =~ /^\s*ADVERTISE_MASTER\s+none\s+none\s+none\s+ALLOW\s+unauthenticated.*$/) {
		push @foundlines, $line;
		$admaster = 1;
	}
}

if($instruction == 0) {
	print "Table Header line either changed or missing\n";
}

if($allow == 0) {
	print "ALLOW line either changed or missing\n";
}

if($read == 0) {
	print "READ line either changed or missing\n";
}

if($writte == 0) {
	print "WRITE line either changed or missing\n";
}

if($negotiator == 0) {
	print "NEGOTIATOR line either changed or missing\n";
}

if($administrator == 0) {
	print "ADMINISTRATOR line either changed or missing\n";
}

if($owner == 0) {
	print "OWNER line either changed or missing\n";
}

if($config == 0) {
	print "CONFIG line either changed or missing\n";
}

if($daemon == 0) {
	print "DAEMON line either changed or missing\n";
}

if($adstartd == 0) {
	print "ADVERTISE_STARTD line either changed or missing\n";
}

if($adschedd == 0) {
	print "ADVERTISE_SCHEDD line either changed or missing\n";
}

if($admaster == 0) {
	print "ADVERTISE_MASTER line either changed or missing\n";
}

$tablesize = @foundlines;
CheckExpectations("condor_ping -table ALL",$tablesize,12);

CondorTest::EndTest();


sub CheckExpectations
{
	my $ad = shift;
	my $got = shift;
	my $expected = shift;

	CondorTest::debug("\n\nCheckExpectations: $ad Got,$got Expected,$expected\n\n",1);
	if($got != $expected) {
		die "$ad: Expected <$expected> but only got <$got>\n";
	} 
}
