package PITA::Image::Test;

use strict;
use base 'PITA::Image::Task';
use Params::Util '_POSINT',
                 '_CLASS',
                 '_HASH0';

use vars qw{$VERSION};
BEGIN {
	$VERSION = '0.20';
}

sub new {
	my $class = shift;
	my $self  = bless { @_ }, $class;

	# Got somewhere to report this to?
	unless ( $self->job_id ) {
		Carp::croak("Task does not have a job_id");
	}

	# Resolve the specific schema class for this test run
	my $scheme = $self->scheme; # A convenience
	unless ( $scheme ) {
		Carp::croak("Missing option 'task.scheme' in image.conf");
	}
	my $driver = join( '::', 'PITA', 'Scheme', map { ucfirst $_ } split /\./, lc $scheme );
	unless ( _CLASS($driver) ) {
		Carp::croak("Invalid scheme '$scheme' for task.scheme in in image.conf");
	}

	# Load the scheme class
	eval "require $driver;";
	if ( $@ =~ /^Can\'t locate PITA/ ) {
		Carp::croak("Scheme '$scheme' is unsupported on this Guest");
	} elsif ( $@ ) {
		Carp::croak("Error loading scheme '$scheme' driver $driver: $@");
	}

	# Did we get a path
	unless ( defined $self->path ) {
		Carp::croak("Missing option task.path in image.conf");
	}

	# Did we get a config file
	unless ( $self->config ) {
		Carp::croak("Missing option task.config in image.conf");
	}

	# Did we get a job_id?
	unless ( _POSINT($self->job_id) ) {
		Carp::croak("Missing option task.job_id in image.conf");
	}

	# Did we get a request?
	# Create the task object from it
	$self->{driver} = $driver->new(
		injector    => $self->{injector},
		workarea    => $self->{workarea},
		scheme      => $self->scheme,
		path        => $self->path,
		request_xml => $self->config,
		request_id  => $self->job_id,
		);

	$self;
}

sub job_id {
	$_[0]->{job_id};
}

sub scheme {
	$_[0]->{scheme};
}

sub path {
	$_[0]->{path};
}

sub config {
	$_[0]->{config};
}

sub driver {
	$_[0]->{driver};
}





#####################################################################
# Run the test

sub run {
	my $self = shift;
	$self->driver->prepare_all;
	$self->driver->execute_all;
	1;
}

sub result {
	$_[0]->report;
}





#####################################################################
# Return the resulting data

sub report {
	$_[0]->driver->report;
}

sub install {
	$_[0]->driver->install;
}





#####################################################################
# Support Methods

#sub DESTROY {
#	# Clean up our driver early
#	if ( $_[0]->{driver} ) {
#		undef $_[0]->{driver};
#	}
#}

1;
