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

my $append_condor_config_B = '
  # reduce latency in test
	NEGOTIATOR_INTERVAL = 5
	NEGOTIATOR_MIN_INTERVAL = 5
	NEGOTIATOR_CYCLE_DELAY = 5
';

# This is the "remote" pool where the job will run.
my $poolB = CondorTest::StartCondorWithParams(
    condor_name => "poolB",
    daemon_list => "MASTER, COLLECTOR, SCHEDD, STARTD, NEGOTIATOR",
    append_condor_config => $append_condor_config_B,
);


my $poolB_collector = $poolB->GetCollectorAddress();
# my $poolB_schedd_name =
#    `condor_status -pool '$poolB_collector' -schedd -af Name`
#    || die "Failed to query schedd name.";
my $rv = runCommandCarefully( undef, qw( condor_status -pool ),
	$poolB_collector, qw( -schedd -af Name ) );
if( $rv->{ "exitcode" } ) { die( "Failed to query schedd name." ); }
my $poolB_schedd_name = $rv->{ "stdout" }->[0];
CondorUtils::fullchomp $poolB_schedd_name;

my $append_condor_config_A = '
  # reduce latency in test
CONDOR_JOB_POLL_INTERVAL = 10
GRIDMANAGER_JOB_PROBE_INTERVAL = 10
# Gittrac Ticket 2889
# This is ok when tests are running as a slot user but if/when we start running tests as root we will
# need to put these into a directory with permissions 1777.
# Relocate C_GAHP files to prevent collision from multiple tests (running on multiple slots) writing into /tmp\n";
# If we are running tests as root we might need to relocate these to a directory with permissions 1777\n";
C_GAHP_LOG = $(LOG)/CGAHPLog.$(USERNAME)
C_GAHP_LOCK = $(LOCK)/CGAHPLock.$(USERNAME)
C_GAHP_WORKER_THREAD_LOG = $(LOG)/CGAHPWorkerLog.$(USERNAME)
C_GAHP_WORKER_THREAD_LOCK = $(LOCK)/CGAHPWorkerLock.$(USERNAME)
';

# This is the "local" pool where the job is submitted.
# Since it is started last, CONDOR_CONFIG points to it.
CondorTest::StartCondorWithParams(
    condor_name => "poolA",
    daemon_list => "MASTER, COLLECTOR, SCHEDD",
    append_condor_config => $append_condor_config_A,
);


# verify that a job runs
SimpleJob::RunCheck(
    universe => "grid",
	timeout => 480,
    grid_resource => "condor $poolB_schedd_name $poolB_collector"
);

CondorTest::EndTest();
