#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## 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.
##
##**************************************************************

# new test now is a single file and does both local and vanilla universe
# bt 2/13/15

use CondorUtils;
use CondorTest;
use Cwd;
use POSIX;
use Check::SimpleJob;

$topdir = getcwd();
print scalar localtime() . "\n";

$testname = "job_basic_submit-queueitemfrom-1Line";

# where am I running
$currenthost = CondorTest::getFqdnHost();

$mypid = $$;

my $counter = 0;
my $datafileswanted = 3;
my $datafilename = "filepart-macros";
my $extension = ".Fnxmacro";
my $targetname = "";
my @results = ();
my @extraction = ();
my $extractionsize = 0;

my $submitfile = "
	executable = x_print-args.pl
	should_transfer_files = yes
	when_to_transfer_output = ON_EXIT
	output = $testname.out
	log = $testname$mypid.log
	Arguments = \$(ACTION) \$(ITEM)
	queue ACTION,ITEM from (Cook Beans)
";

my $onelinepattern = "Cook Beans";

my $submitfilename = "$testname$mypid.cmd";
open(SF,">$submitfilename") or die "failed submit file write:$submitfilename:$!\n";
print SF "$submitfile";
close(SF);

my $datafilename = CondorTest::TempFileName("$testname");
my $cmdtorun = "condor_submit -dry-run $datafilename $submitfilename";
my $res = system("$cmdtorun");
LoadResults(\@results,$datafilename);

#runCondorTool("condor_submit -dry-run $submitfilename",\@results,2,{emit_output=>1});

DryExtract(\@results, \@extraction, "Args");

$extractionsize = @extraction;

#foreach my $item (@extraction) {
	#print "found:$item:\n";
#}

print "Size of argument list:$extractionsize\n";

if($extractionsize != 1) {
	print "A one line queue from can only spawn one job - $extractionsize is Bad!\n";
	RegisterResult(0,"test_name","$testname");
} else {
	print "Good! We got 1 job from a one line queue from.\n";
	RegisterResult(1,"test_name","$testname");
}
my %seenargs = ();

foreach my $arg (@extraction){
	if($arg =~ /Args=\"(.*?)\"/) {
		print "Captured:$1:\n";
		$seenargs{$1} = 1;
	}
}

if(exists $seenargs{$onelinepattern}) {
	print "Expected args passed to job - Good\n";
	RegisterResult(1,"test_name","$testname");
} else {
	print "We did not get the expected args from this one line queue from - Bad\n";
	RegisterResult(0,"test_name","$testname");
}

my $submitfile2 = "
	executable = x_print-args.pl
	should_transfer_files = yes
	when_to_transfer_output = ON_EXIT
	log = $testname$mypid.log
	Arguments = \$(PROCESS) \$(ACTION) \$(ITEM)
	queue 4 ACTION,ITEM from (Cook Beans)
";
my $submitfilename2 = "$testname2$mypid.cmd";
open(SF,">$submitfilename2") or die "failed submit file write:$submitfilename:$!\n";
print SF "$submitfile2";
close(SF);


@extraction = ();
$extractionsize = 0;
@results = ();

$datafilename = CondorTest::TempFileName("$testname");
$cmdtorun = "condor_submit -dry-run $datafilename $submitfilename2";
$res = system("$cmdtorun");
LoadResults(\@results,$datafilename);

#runCondorTool("condor_submit -dry-run $submitfilename2",\@results,2,{emit_output=>1});

DryExtract(\@results, \@extraction, "Args");

$extractionsize = @extraction;


#foreach my $item (@extraction) {
	#print "found:$item:\n";
#}

print "Size of argument list:$extractionsize\n";

if($extractionsize != 4) {
	print "A one line queue from can only spawn one job - $extractionsize is Bad!\n";
	RegisterResult(0,"test_name","$testname");
	print "Dumping results\n";
	foreach my $item (@results) {
		print "result:$item\n";
	}
} else {
	print "Good! We got 4 job from a one line queue 4 from.\n";
	RegisterResult(1,"test_name","$testname");
}
my %seenargs = ();

foreach my $arg (@extraction){
	if($arg =~ /Args=\"\d+\s+(.*?)\"/) {
		print "Captured:$1:\n";
		if(exists $seenargs{$1}) {
			$seenargs{$1} += 1;
		} else {
			$seenargs{$1} = 1;
		}
	}
}

if(exists $seenargs{$onelinepattern}) {
	print "Expected args passed to job - Good\n";
	print "Quantity == $seenargs{$onelinepattern}\n";
	RegisterResult(1,"test_name","$testname");
} else {
	print "We did not get the expected args from this one line queue from - Bad\n";
	RegisterResult(0,"test_name","$testname");
}

CondorTest::EndTest();
print scalar localtime() . "\n";
my $result = 0;

if( $result != 0 )
{
	exit(1);
}

CondorTest::debug("$testname SUCCESS\n",1);
exit(0);

