Changeset 352
- Timestamp:
- 01/17/07 00:08:24 (2 years ago)
- Files:
-
- library/perl/trunk/Assurer/assurer.pl (modified) (2 diffs)
- library/perl/trunk/Assurer/examples/config.yaml (modified) (1 diff)
- library/perl/trunk/Assurer/lib/Assurer.pm (modified) (6 diffs)
- library/perl/trunk/Assurer/lib/Assurer/Plugin.pm (modified) (1 diff)
- library/perl/trunk/Assurer/lib/Assurer/Plugin/Format.pm (modified) (1 diff)
- library/perl/trunk/Assurer/lib/Assurer/Plugin/Test.pm (modified) (3 diffs)
- library/perl/trunk/Assurer/lib/Assurer/Plugin/Test/HTTP.pm (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
library/perl/trunk/Assurer/assurer.pl
r344 r352 12 12 13 13 my $config = File::Spec->catfile($FindBin::Bin, 'config.yaml'); 14 GetOptions('--config=s', \$config, '--version' => \my $version); 14 GetOptions( 15 '--config=s' => \$config, 16 '--host=s' => \my $host, 17 '--version' => \my $version, 18 ); 15 19 16 20 Getopt::Long::Configure('bundling'); … … 21 25 } 22 26 23 Assurer->bootstrap({ config => $config }); 27 Assurer->bootstrap({ 28 config => $config, 29 host => $host, 30 }); 24 31 25 32 exit; library/perl/trunk/Assurer/examples/config.yaml
r351 r352 4 4 test: 5 5 - module: HTTP 6 name: Apache ãã¹ã #07 6 config: 8 - url: http://svn.mizzy.org/ 9 match: It not works! 10 - url: http://svn.mizzy.org/ 11 match: It works! 12 - url: http://svn.mizzy.org/ 13 match: It not works! 14 - url: http://svn.mizzy.org/ 15 match: It works! 16 - url: http://svn.mizzy.org/ 17 match: It works! 18 - url: http://svn.mizzy.org/ 19 match: It works! 20 - url: http://svn.mizzy.org/ 21 match: It works! 22 23 - module: HTTP 24 name: Apache ãã¹ã #1 25 config: 26 - url: http://svn.mizzy.org/ 27 match: It works! 28 - url: http://svn.mizzy.org/ 29 match: It not works! 30 - url: http://svn.mizzy.org/ 31 match: It works! 32 - url: http://svn.mizzy.org/ 33 match: It works! 34 35 - module: HTTP 36 name: Apache ãã¹ã #2 37 config: 38 - url: http://svn.mizzy.org/ 39 match: It works! 40 - url: http://svn.mizzy.org/ 41 match: It works! 42 - url: http://svn.mizzy.org/ 43 match: It works! 44 45 - module: HTTP 46 name: Apache ãã¹ã #3 47 config: 48 - url: http://svn.mizzy.org/ 49 match: It not works! 50 - url: http://svn.mizzy.org/ 51 match: It not works! 7 content: It works! 8 role: web 52 9 53 10 format: 54 - module: HTML55 config:56 css_uri: htmlmatrix.css11 - module: Text 12 # config: 13 # css_uri: htmlmatrix.css 57 14 58 15 publish: 59 16 - module: Term 17 18 hosts: 19 web: 20 - svn.mizzy.org 21 - trac.mizzy.org library/perl/trunk/Assurer/lib/Assurer.pm
r351 r352 12 12 use Assurer::Test; 13 13 use UNIVERSAL::require; 14 use Encode; 14 15 15 16 __PACKAGE__->mk_accessors( qw/ test / ); … … 37 38 my $config_loader = Assurer::ConfigLoader->new; 38 39 $self->{config} = $config_loader->load($opts->{config}); 40 $self->{config}->{global}->{host} ||= $opts->{host}; 39 41 Assurer->set_context($self); 40 42 41 43 $self->{test} = Assurer::Test->new; 44 45 if ( eval { require Term::Encoding } ) { 46 $self->{config}->{global}->{log}->{encoding} ||= Term::Encoding::get_encoding(); 47 } 48 49 if ( my $hosts = $self->{config}->{hosts} ) { 50 $self->{hosts} = []; 51 if ( ref $hosts eq 'HASH' ) { 52 for my $role ( keys %$hosts ) { 53 for my $host ( @{ $hosts->{$role} } ) { 54 push @{ $self->{hosts} }, { role => $role, host => $host }; 55 } 56 } 57 } 58 else { 59 for my $host ( @$hosts ) { 60 push @{ $self->{hosts} }, { role => undef, host => $host }; 61 } 62 } 63 } 42 64 43 65 return $self; … … 51 73 52 74 $self->run_hook('test'); 75 53 76 $self->run_hook('format'); 54 77 … … 63 86 64 87 for my $plugin ( @{ $self->{hooks}->{$hook} || [] } ) { 65 $plugin->pre_run($context, $args); 66 $plugin->finalize($context, $args) if $plugin->can('finalize'); 88 if ( $hook eq 'test' and $self->{hosts} ) { 89 for my $host ( @{ $self->{hosts} } ) { 90 next if ( $plugin->{role} and ( !defined $host->{role} or $host->{role} ne $plugin->{role} ) ); 91 $plugin->conf->{host} = $host->{host}; 92 $plugin->pre_run($context, $args); 93 $plugin->post_run($context, $args) if $plugin->can('post_run'); 94 } 95 } 96 else { 97 $plugin->pre_run($context, $args); 98 $plugin->post_run($context, $args) if $plugin->can('post_run'); 99 } 67 100 } 68 101 } … … 75 108 my $class = "Assurer::Plugin::" . ucfirst $hook . "::$plugin->{module}"; 76 109 $class->use or die $@; 110 $plugin->{config} ||= {}; 77 111 push @{ $self->{hooks}->{$hook} }, $class->new($plugin); 78 112 } … … 81 115 82 116 sub log { 83 my ($self, $level, $msg) = @_; 84 warn "[$level] $msg\n"; 117 my ( $self, $level, $msg, %opts ) = @_; 118 119 my $caller = $opts{caller}; 120 121 unless ($caller) { 122 my $i = 0; 123 while (my $c = caller($i++)) { 124 last if $c !~ /Plugin/; 125 $caller = $c; 126 } 127 $caller ||= caller(0); 128 } 129 130 if ($self->conf->{log}->{encoding}) { 131 $msg = Encode::decode_utf8($msg) unless utf8::is_utf8($msg); 132 $msg = Encode::encode($self->conf->{log}->{encoding}, $msg); 133 } 134 135 warn "$caller [$level] $msg\n"; 85 136 } 86 137 library/perl/trunk/Assurer/lib/Assurer/Plugin.pm
r351 r352 9 9 10 10 my $self = { %$args }; 11 12 11 bless $self, $class; 13 12 library/perl/trunk/Assurer/lib/Assurer/Plugin/Format.pm
r351 r352 7 7 use Assurer::Format; 8 8 9 sub pre_run { 10 my $self = shift; 11 return unless Assurer->context->results; 12 $self->run(@_); 13 } 14 9 15 1; library/perl/trunk/Assurer/lib/Assurer/Plugin/Test.pm
r351 r352 8 8 sub init { 9 9 my $self = shift; 10 my $name = $self->{name} || 'no name'; 11 10 $self->{name} ||= 'no name'; 12 11 return $self; 13 12 } … … 15 14 sub pre_run { 16 15 my $self = shift; 16 $self->log( info => qq{Testing $self->{name} ...}, caller => ref $self ); 17 17 Assurer->context->test->init($self->{name}); 18 19 $self->conf->{host} ||= Assurer->context->conf->{host}; 20 18 21 $self->run(@_); 19 22 } 20 23 21 sub finalize{24 sub post_run { 22 25 my ( $self, $context, $args ) = @_; 23 26 … … 37 40 } 38 41 39 42 sub conf { 43 my $self = shift; 44 return $self->{config}; 45 } 40 46 41 47 1; library/perl/trunk/Assurer/lib/Assurer/Plugin/Test/HTTP.pm
r351 r352 5 5 use base qw( Assurer::Plugin::Test ); 6 6 use Assurer::Test; 7 use LWP::Simple; 7 use LWP::UserAgent; 8 use HTTP::Request; 8 9 9 10 sub run { … … 11 12 12 13 my $test = $context->test; 14 my $conf = $self->conf; 13 15 14 my $config = $self->{config}; 16 my $host = $conf->{host} || $context->conf->{host}; 17 my $port = $conf->{port} || '80'; 18 my $path = $conf->{path} || '/'; 19 $path = "/$path" if $path !~ m!^/!; 20 my $url = "http://$host:$port$path"; 15 21 16 $config = [ $config ] if ref $config eq 'HASH'; 22 my $ua = LWP::UserAgent->new; 23 $ua->agent("Assurer/$Assurer::VERSION (http://mizzy.org/)"); 17 24 18 for ( @$config ) { 19 my $url = $_->{url}; 20 my $match = $_->{match}; 25 my $req = HTTP::Request->new( GET => $url ); 26 my $res = $ua->request($req); 21 27 22 $test->like ( 23 get($url), 24 qr/$match/, 25 "Content of $url matches '$match'", 28 $test->is_num( $res->code, 200, "HTTP status code of $url is 200" ); 29 30 my $content = $conf->{content}; 31 if ( $content ) { 32 $test->like( 33 $res->content, 34 qr/$content/, 35 "Content of $url matches '$content'", 26 36 ); 27 37 } 38 28 39 } 29 40
