blib/lib/TAP/Harness/Remote/EC2.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 105 CC 11 SCAR 35.6
CC
11
Cov
0%
load_remote_config
72.0
hosts
12.0
wait_pending
6.0
CRAP 35.2
1 package TAP::Harness::Remote::EC2;
2  
3 our $VERSION = '1.00';
4  
5 use warnings;
6 use strict;
7  
8 use base 'TAP::Harness::Remote';
9 use constant config_path => "$ENV{HOME}/.remote_test_ec2";
10 use Net::Amazon::EC2;
11  
12 =head1 NAME
13  
14 TAP::Harness::Remote::EC2 - Run tests on EC2 servers
15  
16 =head1 SYNOPSIS
17  
18 prove --harness TAP::Harness::Remote::EC2 t/*.t
19  
20 =head1 DESCRIPTION
21  
22 Based on L<TAP::Harness::Remote>, this module uses your running Amazon
23 EC2 instances (L<http://amazon.com/ec2>) to run tests against, instead
24 of a preconfigured list of hosts.
25  
26 =head1 USAGE
27  
28 Configuration is much the same as L<TAP::Harness::Remote>, except the
29 configuration file lives in C</.remote_test_ec2> -- see
30 L</"CONFIGURATION AND ENVIRONMENT">.
31  
32 =head1 METHODS
33  
34 =head2 load_remote_config
35  
36 Loads and canonicalizes the configuration. Writes and uses the
37 default configuration (L</default_config>) if the file does not exist.
38  
39 =cut
40  
41 sub load_remote_config {
42 my $self = shift;
43  
44 $self->SUPER::load_remote_config;
45 warn
46 "Useless 'host' configuration parameter set for TAP::Harness::Remote::EC2\n"
47 if grep {defined} @{ $self->remote_config("host") };
48 delete $self->{remote_config}{host};
49  
50 die "Configuration failed to include required 'access_key' parameter\n"
51 unless $self->remote_config("access_key");
52  
53 die
54 "Configuration failed to include required 'secret_access_key' parameter\n"
55 unless $self->remote_config("secret_access_key");
56  
57 my $ami = $self->remote_config("ami");
58 warn "No value set for 'ami', assuming you mean all running instances\n"
59 unless defined $ami;
60  
61 my $ec2 = Net::Amazon::EC2->new(
62 AWSAccessKeyId => $self->remote_config("access_key"),
63 SecretAccessKey => $self->remote_config("secret_access_key"),
64 );
65  
66 my @hosts = $self->hosts( $ec2 => qr/running|pending/ );
67 my $run = $self->remote_config("instances") || 1;
68 my $need = $run - @hosts;
69 if ( $need > 0 ) {
70 die "Need to run $need new instances, but no AMI specified!\n"
71 unless $ami;
72  
73 warn "Starting $need new instances of AMI $ami\n";
74 my $reservation = $ec2->run_instances(
75 ImageId => $ami,
76 MinCount => $need,
77 MaxCount => $need,
78 InstanceType => $self->remote_config("instance_type")
79 || "m1.xlarge",
80 );
81 die join( "", map { $_->message } @{ $reservation->errors } )
82 . "\n"
83 if $reservation->isa("Net::Amazon::EC2::Errors");
84 }
85  
86 # Wait for starting instances
87 $self->wait_pending( $ec2 );
88  
89 $self->{remote_config}{host}
90 = [ map { $_->dns_name } $self->hosts( $ec2 ) ];
91 return $self;
92 }
93  
94 =head2 hosts EC2 [, STATUS]
95  
96 Returns an array of L<Net::Amazon::EC2::RunningInstances> objects
97 which match the C<ami> type specified in the config file, and whose
98 status matches the given C<STATUS>. C<STATUS> may be either a string,
99 or a regex; it defaults to "running".
100  
101 C<EC2> should be a valid L<Net::Amazon::EC2> object.
102  
103 =cut
104  
105 sub hosts {
106 my $self = shift;
107 my ( $ec2, $test ) = @_;
108 my $ami = $self->remote_config("ami");
109 $test ||= 'running';
110 $test = qr/$test/ unless ref $test;
111  
112 my $running_instances = $ec2->describe_instances;
113 return grep { defined $ami ? ( $_->image_id eq $ami ) : 1 }
114 grep { $_->instance_state->name =~ $test }
115 map { @{ $_->instances_set } } @{$running_instances};
116 }
117  
118 =head2 wait_pending EC2.
119  
120 Waits until all hosts in the "pending" state have started. C<EC2>
121 should be a valid L<Net::Amazon::EC2> object.
122  
123 =cut
124  
125 sub wait_pending {
126 my $self = shift;
127 my ( $ec2 ) = @_;
128 my @hosts = $self->hosts( $ec2 => "pending" );
129 return unless @hosts;
130  
131 my $delay = 60;
132 while (@hosts) {
133 warn "Waiting for @{[@hosts + 0]} starting instances..\n";
134 sleep $delay;
135 $delay = 20;
136 @hosts = $self->hosts( $ec2 => "pending" );
137 }
138  
139 # Even after they report as "started," they still need time to
140 # start up sshd, etc.
141 sleep 30;
142 }
143  
144 =head1 CONFIGURATION AND ENVIRONMENT
145  
146 The configuration is stored in F<~/.remote_test_ec2>, and is mostly
147 identical to the configuration of L<TAP::Harness::Remote> (see
148 L<TAP::Harness::Remote/"CONFIGURATION AND ENVIRONMENT">), with the
149 following differences:
150  
151 =over
152  
153 =item *
154  
155 The C<hosts> parameter is ignored, and produces a warning if present.
156  
157 =item *
158  
159 The C<access_key> amd C<secret_access_key> parameters are required, to
160 be able to query the Amazon EC2 interface and determine the running
161 EC2 hosts. You can find your access keys at
162 L<https://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key>
163  
164 =item *
165  
166 There is an optional C<ami> parameter, which limits which AMIs will be
167 used to run tests; it also lets the harness start the instances if
168 there are not enough running.
169  
170 =item *
171  
172 There is an optional C<instances> parameter, which specifies the
173 number of AMI instances to run tests on. Defaults to one.
174  
175 =item *
176  
177 There is an optional C<instance_type> parameter, which controls the
178 type of instances to create; possible arguments are C<m1.small>,
179 C<m1.large>, and C<m1.xlarge>. The default is C<m1.xlarge>.
180  
181 =back
182  
183 =head1 DEPENDENCIES
184  
185 L<Net::Amazon::EC2>, L<TAP::Harness::Remote>
186  
187 =head1 BUGS AND LIMITATIONS
188  
189 The default perl installed Amazon's provided EC2 images is
190 B<extremely> slow (about 4x slower than a clean, optimized build). We
191 thus strongly suggest you compile your own.
192  
193 =head1 AUTHOR
194  
195 Alex Vandiver C<< <alexmv@bestpractical.com> >>
196  
197 =head1 LICENCE AND COPYRIGHT
198  
199 Copyright (c) 2007-2008, Best Practical Solutions, LLC. All rights
200 reserved.
201  
202 This module is free software; you can redistribute it and/or
203 modify it under the same terms as Perl itself. See L<perlartistic>.
204  
205 =head1 DISCLAIMER OF WARRANTY
206  
207 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
208 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
209 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
210 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
211 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
212 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
213 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
214 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
215 NECESSARY SERVICING, REPAIR, OR CORRECTION.
216  
217 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
218 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
219 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
220 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
221 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
222 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
223 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
224 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
225 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
226 SUCH DAMAGES.
227  
228 =cut
229  
230 1;