#! /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 strict;
use warnings;

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

# Request that a personal HTCondor with the following config appending is
# started before running this test
#testreq: personal

my $testname = "job_filexfer_trans-excut-true_van";
my $pid = $$;

my $execute = sub {
	my %args = @_;
	my $cluster = $args{"cluster"};

	TLOG("BAD. Job $cluster is executing.  Submit should have failed.\n");

};

my $abort = sub {
	my %args = @_;
	my $cluster = $args{"cluster"};
	TLOG("Job $cluster is gone now.\n");
};

my $hold = sub {
	my %args = @_;
	my $cluster = $args{"cluster"};

	TLOG("BAD. Job $cluster is held. Submit should have failed.\n");
};

my $release = sub {
	my %args = @_;
	my $cluster = $args{"cluster"};
};

my $success = sub {
	my %args = @_;
	my $cluster = $args{"cluster"};
	TLOG("BAD: Job $cluster clmpleted. Submit should have failed.\n");
};

my $wanterror = sub {
	my %args = @_;
	my $errmsg = $args{"ErrorMessage"};

    if($errmsg =~ /^.*died abnormally.*$/) {
        TLOG("BAD. job terminated abnormally, but submit should have failed with error 1\n");
        return(1);
    } elsif($errmsg =~ /^.*\(\s*returned\s*(\d+)\s*\).*$/) {
        if($1 == 1) {
            TLOG("SUCCESS. Submit should fail when File Transfer is off and input files requested\n");
            return(0);
        } else {
            TLOG("BAD. Submit should fail but with error 1 not <<$1>>\n");
            return(1);
        }
    } else {
        TLOG("BAD. Unexpected failure: $errmsg\n");
        return(1);
    }
};

# submit commands that contradict each other.
my $transfercmds = '
transfer_input = false
transfer_error = false
transfer_output = false
transfer_executable = true
should_transfer_files = NO
when_to_transfer_output = NONE
';

TLOG("Attempting to submit a job with should_..=NO but transfer_...=true. Submit should fail.\n");
SimpleJob::RunCheck(
    runthis=>"x_job_filexfer_testjob.pl",
    duration=>"--noextrainput",
	append_submit_commands=>"$transfercmds",
    on_execute=>$execute,
	on_wanterror=>$wanterror,
    on_abort=>$abort,
	on_hold=>$hold,
	on_release=>$release,
    on_success=>$success,
);

CondorTest::EndTest();

TLOG ("$testname SUCCESS\n");
exit(0);

