| 1 | package Net::OpenStack::Attack; | ||
| 2 | use v5.10; | ||
| 3 | use Any::Moose; | ||
| 4 | use HTTP::Async; | ||
| 5 | use HTTP::Request; | ||
| 6 | use JSON qw(to_json from_json); | ||
| 7 | |||
| 8 | our $VERSION = '1.0500'; # VERSION | ||
| 9 | |||
| 10 | has compute => (is => 'ro', isa => 'Net::OpenStack::Compute', required => 1); | ||
| 11 | |||
| 12 | sub create_servers { | ||
| 13 | my ($self, $num, $image) = @_; | ||
| 14 | $image ||= $self->get_any_image(); | ||
| 15 | my $body = to_json { | ||
| 16 | server => { | ||
| 17 | name => 'stackattack', | ||
| 18 | imageRef => $image, | ||
| 19 | flavorRef => 1, | ||
| 20 | } | ||
| 21 | }; | ||
| 22 | my @reqs = map $self->make_req(POST => '/servers', $body), 1 .. $num; | ||
| 23 | return $self->send_reqs(@reqs); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub delete_servers { | ||
| 27 | my ($self, $servers) = @_; | ||
| 28 | my @reqs = map $self->make_req(DELETE => "/servers/$_->{id}"), @$servers; | ||
| 29 | return $self->send_reqs(@reqs); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub rebuild_servers { | ||
| 33 | my ($self, $servers, $image) = @_; | ||
| 34 | $image ||= $self->get_any_image(); | ||
| 35 | my $body = to_json { | ||
| 36 | rebuild => { | ||
| 37 | imageRef => $image, | ||
| 38 | } | ||
| 39 | }; | ||
| 40 | my @reqs = map $self->make_req(POST => "/servers/$_->{id}/action", $body), | ||
| 41 | @$servers; | ||
| 42 | return $self->send_reqs(@reqs); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub attack { | ||
| 46 | my ($self, $method, $resource, $num) = @_; | ||
| 47 | my @reqs = map $self->make_req($method => $resource), 1 .. $num; | ||
| 48 | return $self->send_reqs(@reqs); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub make_req { | ||
| 52 | my ($self, $method, $resource, $body) = @_; | ||
| 53 | my $url = $self->compute->base_url . $resource; | ||
| 54 | my $headers = [ | ||
| 55 | x_auth_token => $self->compute->token, | ||
| 56 | content_type => 'application/json', | ||
| 57 | ]; | ||
| 58 | return HTTP::Request->new($method => $url, $headers, $body); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub send_reqs { | ||
| 62 | my ($self, @reqs) = @_; | ||
| 63 | my $async = HTTP::Async->new; | ||
| 64 | $async->add(@reqs); | ||
| 65 | my ($successes, $failures, @errmsgs) = (0, 0); | ||
| 66 | while (my $res = $async->wait_for_next_response) { | ||
| 67 | if ($res->is_success) { | ||
| 68 | $successes++; | ||
| 69 | } else { | ||
| 70 | $failures++; | ||
| 71 | warn sprintf "Error: %s: %s\n", $res->status_line, $res->content; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | return { successes => $successes, failures => $failures }; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub get_any_image { shift->compute->get_images(detail => 0)->[0]{id} } | ||
| 79 | |||
| 80 | # ABSTRACT: Tools for stress testing an OpenStack deployment. | ||
| 81 | |||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | __END__ |