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

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

$mypid = $$;

my $filedata = '
	this is data for an
	input file.
';

my $counter = 0;
my $datafileswanted = 3;
my $datafilename = "filepart-macros";
my $extension = ".Fnxmacro";
my $targetname = "";
my @results = ();
my @extraction = ();
my $extractionsize = 0;
my $arglist1 = "Chicken Fish Bread RasberryPie"; 
my $arglist2 = "Pig,Sky,Floor,House"; 

my @allargs1 = split /\s/, $arglist1;
my @allargs2 = split /,/, $arglist2;

my $ExitedSuccess = sub {
	print "Job ran fine\n";
};

my $submitfile = "
executable = x_print-args.pl
should_transfer_files = yes
when_to_transfer_output = ON_EXIT
output = \$(ITEM)$mypid.out
log = $testname$mypid.log
Arguments = \$(ITEM)
queue ITEM in (
$arglist1
$arglist2
)
";

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

print "Lets see it it runs\n";

CondorTest::RegisterExitedSuccess( $testname, $ExitedSuccess );

if( CondorTest::RunTest($testname, $submitfilename, 0) ) {
    print "Good\n";
    CondorTest::debug("$testname: SUCCESS\n",$debuglevel);
    RegisterResult(1,"test_name","$testname");
} else {
    print "Bad\n";
    RegisterResult(0,"test_name","$testname");
}

print "Lets do some validation\n";
my $expectedresult = 0;

for my $thisarg (@allargs1) {
        $expectedresult = 0;
        $expectedresult = CheckExpectedOutput("$thisarg","$thisarg$mypid.out");
        if($expectedresult == 0) {
                print "Expected $thisarg$mypid.out to contain $thisarg - Good\n";
                RegisterResult(1,"test_name","$testname");
        } else {
                print "Expected $thisarg$mypid.out to contain $thisarg - Bad\n";
                RegisterResult(0,"test_name","$testname");
        }
}

for my $thisarg (@allargs2) {
        $expectedresult = 0;
        $expectedresult = CheckExpectedOutput("$thisarg","$thisarg$mypid.out");
        if($expectedresult == 0) {
                print "Expected $thisarg$mypid.out to contain $thisarg - Good\n";
                RegisterResult(1,"test_name","$testname");
        } else {
                print "Expected $thisarg$mypid.out to contain $thisarg - 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);


sub CheckExpectedOutput {
    my $pattern = shift;
    my $outputfile = shift;
    my $line = "";
    open(OUT,"<$outputfile") or die "Failed to open $outputfile:$!\n";
    while(<OUT>) {
        chomp($_);
        $line = $_;
        if($line =~ /$pattern/) {
            print "Expected to see this:$pattern\n";
            close(OUT);
            return(0);
        }
    }
    close(OUT);
    print "Failed to find $pattern in $outputfile - Bad\n";
    return(1);
}

