TAP::Harness::Remote::EC2 - Run tests on EC2 servers


TAP-Harness-Remote-EC2 documentation Contained in the TAP-Harness-Remote-EC2 distribution.

Index


Code Index:

NAME

Top

TAP::Harness::Remote::EC2 - Run tests on EC2 servers

SYNOPSIS

Top

    prove --harness TAP::Harness::Remote::EC2 t/*.t

DESCRIPTION

Top

Based on TAP::Harness::Remote, this module uses your running Amazon EC2 instances (http://amazon.com/ec2) to run tests against, instead of a preconfigured list of hosts.

USAGE

Top

Configuration is much the same as TAP::Harness::Remote, except the configuration file lives in /.remote_test_ec2 -- see "CONFIGURATION AND ENVIRONMENT".

METHODS

Top

load_remote_config

Loads and canonicalizes the configuration. Writes and uses the default configuration (default_config) if the file does not exist.

hosts EC2 [, STATUS]

Returns an array of Net::Amazon::EC2::RunningInstances objects which match the ami type specified in the config file, and whose status matches the given STATUS. STATUS may be either a string, or a regex; it defaults to "running".

EC2 should be a valid Net::Amazon::EC2 object.

wait_pending EC2.

Waits until all hosts in the "pending" state have started. EC2 should be a valid Net::Amazon::EC2 object.

CONFIGURATION AND ENVIRONMENT

Top

The configuration is stored in ~/.remote_test_ec2, and is mostly identical to the configuration of TAP::Harness::Remote (see "CONFIGURATION AND ENVIRONMENT" in TAP::Harness::Remote), with the following differences:

DEPENDENCIES

Top

Net::Amazon::EC2, TAP::Harness::Remote

BUGS AND LIMITATIONS

Top

The default perl installed Amazon's provided EC2 images is extremely slow (about 4x slower than a clean, optimized build). We thus strongly suggest you compile your own.

AUTHOR

Top

Alex Vandiver <alexmv@bestpractical.com>

LICENCE AND COPYRIGHT

Top

DISCLAIMER OF WARRANTY

Top

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


TAP-Harness-Remote-EC2 documentation Contained in the TAP-Harness-Remote-EC2 distribution.
package TAP::Harness::Remote::EC2;

our $VERSION = '1.00';

use warnings;
use strict;

use base 'TAP::Harness::Remote';
use constant config_path => "$ENV{HOME}/.remote_test_ec2";
use Net::Amazon::EC2;

sub load_remote_config {
    my $self = shift;

    $self->SUPER::load_remote_config;
    warn
        "Useless 'host' configuration parameter set for TAP::Harness::Remote::EC2\n"
        if grep {defined} @{ $self->remote_config("host") };
    delete $self->{remote_config}{host};

    die "Configuration failed to include required 'access_key' parameter\n"
        unless $self->remote_config("access_key");

    die
        "Configuration failed to include required 'secret_access_key' parameter\n"
        unless $self->remote_config("secret_access_key");

    my $ami = $self->remote_config("ami");
    warn "No value set for 'ami', assuming you mean all running instances\n"
        unless defined $ami;

    my $ec2 = Net::Amazon::EC2->new(
        AWSAccessKeyId  => $self->remote_config("access_key"),
        SecretAccessKey => $self->remote_config("secret_access_key"),
    );

    my @hosts = $self->hosts( $ec2 => qr/running|pending/ );
    my $run = $self->remote_config("instances") || 1;
    my $need = $run - @hosts;
    if ( $need > 0 ) {
        die "Need to run $need new instances, but no AMI specified!\n"
            unless $ami;

        warn "Starting $need new instances of AMI $ami\n";
        my $reservation = $ec2->run_instances(
            ImageId      => $ami,
            MinCount     => $need,
            MaxCount     => $need,
            InstanceType => $self->remote_config("instance_type")
                || "m1.xlarge",
        );
        die join( "", map { $_->message } @{ $reservation->errors } )
            . "\n"
            if $reservation->isa("Net::Amazon::EC2::Errors");
    }

    # Wait for starting instances
    $self->wait_pending( $ec2 );

    $self->{remote_config}{host}
        = [ map { $_->dns_name } $self->hosts( $ec2 ) ];
    return $self;
}

sub hosts {
    my $self = shift;
    my ( $ec2, $test ) = @_;
    my $ami = $self->remote_config("ami");
    $test ||= 'running';
    $test = qr/$test/ unless ref $test;

    my $running_instances = $ec2->describe_instances;
    return grep { defined $ami ? ( $_->image_id eq $ami ) : 1 }
        grep { $_->instance_state->name =~ $test }
        map { @{ $_->instances_set } } @{$running_instances};
}

sub wait_pending {
    my $self = shift;
    my ( $ec2 ) = @_;
    my @hosts = $self->hosts( $ec2 => "pending" );
    return unless @hosts;

    my $delay = 60;
    while (@hosts) {
        warn "Waiting for @{[@hosts + 0]} starting instances..\n";
        sleep $delay;
        $delay = 20;
        @hosts = $self->hosts( $ec2 => "pending" );
    }

    # Even after they report as "started," they still need time to
    # start up sshd, etc.
    sleep 30;
}

1;