#! /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.
##
##**************************************************************

use CondorTest;
use CondorUtils;
use Check::SimpleJob;
use CheckOutputFormats;
use strict;
use warnings;

my $testname = "cmd_submit-batchname";

my $pid = $$;
my $executable = "x_sleep.pl";
my $arguments = 600;
my $submitfile = "$testname$pid.sub";
my %Attr;
my @content;

print "####################### test 1: submit a job with batch name foo ######################\n";
%Attr = dry_run_with_batchname($testname, $pid, $executable, $arguments, "foo");

if ($Attr{0}{JobBatchName} eq "\"foo\"" && $Attr{1}{JobBatchName} eq "\"foo\"") {
	RegisterResult(1, check_name => "(batch name is foo)", test_name => $testname);
} else {
	print "        Output is $Attr{0}{JobBatchName}, $Attr{1}{JobBatchName}\n        Should be foo, foo\n";
	RegisterResult(0, check_name => "(batch name is foo)", test_name => $testname);
}

print "################## test 2: submit a job with a batch name with spaces #################\n";
%Attr = dry_run_with_batchname($testname, $pid, $executable, $arguments, "\"foo fooo fo1\"");

if ($Attr{0}{JobBatchName} eq "\"foo fooo fo1\"" && $Attr{1}{JobBatchName} eq "\"foo fooo fo1\"") {
	RegisterResult(1, check_name => "(batch name with sapces)", test_name => $testname);
} else {
	print "        Output is $Attr{0}{JobBatchName}, $Attr{1}{JobBatchName}\n        Should be foo fooo fo1, foo fooo fo1\n";
	RegisterResult(0, check_name => "(batch name with spaces)", test_name => $testname);
}

print "########################## test 3: check condor_q column ##############################\n";
`condor_rm -all`;
`condor_submit -batch-name "foo 2 1x" hold=true $submitfile`;
@content = `condor_q`;

my ($other_ref, $data_ref, $summary_ref) = read_array_content_to_table(\@content);	
my %other = %{$other_ref};
my %data = %{$data_ref};
my @summary = @{$summary_ref};
	
my @fields = split_fields(\%data);

if ($fields[1][1] eq "foo 2 1x") {
	RegisterResult(1, check_name => "(condor_q column)", test_name => $testname);
} else {
	print "        Output is $fields[1][1]\n        Should be foo 2 1x\n";
	RegisterResult(0, check_name => "(condor_q column)", test_name => $testname);
}

print "########################## test 4: submit same batch name #############################\n";
`condor_submit -batch-name "foo 2 1x" hold=true $submitfile`;
@content = `condor_q`;
print @content;
my ($other_ref, $data_ref, $summary_ref) = read_array_content_to_table(\@content);	
my %other = %{$other_ref};
my %data = %{$data_ref};
my @summary = @{$summary_ref};
	
my @fields = split_fields(\%data);

if ($fields[6][1] && $fields[1][1] eq "foo 2 1x") {
	RegisterResult(1, check_name => "(same batch name)", test_name => $testname);
} else {
	print "        Output is $fields[1][1], hold number is $fields[7][1]\n        Should be foo 2 1x, hold number should be 4\n";
	RegisterResult(0, check_name => "(same batch name)", test_name => $testname);
}
`condor_rm -all`;

CondorTest::EndTest();
exit(0);
