#! /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 Cwd;
use File::Copy;
use CondorTest;
use Check::CondorLog;

my $cwd = getcwd();
my $configd = $cwd . '/x_ganglia_d';
if( ! -d $configd ) {
    mkdir $configd or die "Failed to create $configd";
}
if( -e "../gangliad/ganglia_default_metrics" ) {
    # testing in the source tree
    copy("../gangliad/ganglia_default_metrics",$configd . "/ganglia_default_metrics") or die "Failed to copy ganglia_default_metrics";
}
elsif( -e "../condor/etc/condor/ganglia.d/00_default_metrics" ) {
    # nightly test
    copy("../condor/etc/condor/ganglia.d/00_default_metrics",$configd . "/ganglia_default_metrics") or die "Failed to copy ganglia_default_metrics";
}
elsif( -e "ganglia_default_metrics" ) {
    # ctest
    copy("ganglia_default_metrics", $configd . "/ganglia_default_metrics") or die "Failed to copy ganglia_default_metrics";
}
else {
    die "Failed to find ganglia_default_metrics";
}

my $append_condor_config = '
  DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD,GANGLIAD

  # NOOP is a special hack for testing; no actual interaction with ganglia happens
  GANGLIA_LIB = NOOP
  GANGLIA_CONFIG = ' . $cwd . '/x_ganglia_config
  GANGLIA_SEND_DATA_FOR_ALL_HOSTS = true
  GANGLIAD_DEBUG = D_FULLDEBUG
  GANGLIAD_METRICS_CONFIG_DIR = ' . $configd . '
';

CondorTest::StartCondorWithParams(
    append_condor_config => $append_condor_config
);

# sleep for a while
sleep(10);

# Verify that at least one metric was successfully parsed and
# attempted to be published.  The actual publication will not happen,
# because we have not set up ganglia for real.
CondorLog::RunCheck(
    daemon => "GANGLIAD",
    match_regexp => "publishing Jobs in Pool",
	num_retries => 20,
);

CondorTest::EndTest();
