#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## Copyright (C) 1990-2012, 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 strict;
use warnings;

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

my $testname = "cmd_q_shows-sub";

my $iswindows = CondorUtils::is_windows();

my $debuglevel = 2;
my $ClusterId = 0;

my $GetClusterId = sub {
	my $cid = shift;
	$ClusterId = $cid;
	print "Resquestd Cluster Got $ClusterId\n";
	##########################
	#
	# Convoluted: This function is paased to RunCheck which uses it in RunTest in the fourth
	# position which is always an undefined variable EXCEPT when it is a call back routine 
	# to pass out the cluster Id.
	#
	##########################
	#runcmd("condor_q",{emit_output => 1});
};


my $whoami;
my $cluster;

my $submit = sub {
	$cluster = $ClusterId;
	print "my cluster is:$cluster\n";
    my @adarray;
    CondorTest::debug("Determining submitter and cluster\n", $debuglevel);


    my $cmd = "";
	if($iswindows) {
    	$cmd = "condor_q -format \"%s\" Owner -format \" ClusterId = %d\\n\" ClusterId";
	} else {
    	$cmd = "condor_q -format \"%s\" Owner -format \" ClusterId = %d\n\" ClusterId";
	}
    if(!CondorTest::runCondorTool($cmd, \@adarray, 2,{emit_output=>0})) {
        CondorTest::debug("ERROR - cannot determine submitted and cluster.", $debuglevel);
        CondorTest::debug("Command failed: $cmd\n", $debuglevel);
		FailAndEndTest();
    }

    foreach (@adarray) {
        if(/^\s*([\w\-]+)\s+ClusterId\s+=\s+$cluster/) {
            CondorTest::debug("Matching line -> $_\n", $debuglevel);
            CondorTest::debug("submitter = $1  -  cluster = $cluster\n", $debuglevel);
            $whoami = $1;

			print "Determined submiter for cluster:$cluster is:$whoami\n";
        }
        else {
            CondorTest::debug("Non-matching line -> $_\n", $debuglevel);
        }
    }

    if(!$whoami) {
        print "Can not find out who I am to use in condor_q -sub <submitter>\n";
		FailAndEndTest();
    }

    # Re-scheduling might get the job running sooner
    CondorTest::runCondorTool("condor_reschedule", \@adarray, 2,{emit_output=>0});
};

my $execute = sub {
    # Once the job is running we know that the collector will have received submitter ads

    my @adarray;
    my $cmd = "";
	if($iswindows) {
    	$cmd = "condor_q -sub $whoami -format \"%d.\" ClusterId -format \"%d \" ProcId -format \" %s\" Owner -format \" %s\\n\" Cmd";
	} else {
    	$cmd = "condor_q -sub $whoami -format \"%d.\" ClusterId -format \"%d \" ProcId -format \" %s\" Owner -format \" %s\n\" Cmd";
	}

    if(!CondorTest::runCondorTool($cmd,\@adarray,2,{emit_output=>0})) {
        CondorTest::debug("ERROR - failed to run command: $cmd\n", $debuglevel);
        system("condor_rm $cluster"); # bail but pull job which never dies...
		FailAndEndTest();
    }

	my $done = 0;
    print "Looking for condor_q with submitter '$whoami' and cluster $cluster - ";
    foreach (@adarray) {
        if(/^\s*(\d+).\d+\s+([\w\-]+)/) {
            if(($cluster eq $1) && ($whoami eq $2)){
                CondorTest::debug("condor_q -sub $whoami found my job matching $cluster\n", $debuglevel);
                CondorTest::runCondorTool("condor_rm $cluster", \@adarray, 8,{emit_output=>0});
                print "ok\n";
				$done = 1;
            }
        }
    }

    CondorTest::runCondorTool("condor_rm $cluster", \@adarray, 8, {expect_result => ANY,emit_output=>0});

	if($done == 0) {
    	print "bad\n";
    	print "We never found ourselves with:condor_q -sub $whoami\n";
		FailAndEndTest();
	}
};

sub FailAndEndTest
{
	CondorTest::RegisterResult(0);
	CondorTest::EndTest();
};

my $abort = sub {
};

my $evictedwithoutchpt = sub {
};

print "Start a long runniing job - ";

my $runresult = SimpleJob::RunCheck(
	duration => 3600,
	on_abort => $abort,
	on_submit => $submit,
	on_execute => $execute,
	on_evictedwithoutcheckpoint => $abort,
	GetClusterId => $GetClusterId,
);

