Beekeeper

 view release on metacpan or  search on metacpan

t/lib/Tests/Examples.pm  view on Meta::CPAN


use strict;
use warnings;

use Test::Class;
use Test::More;
use base 'Test::Class';

use AnyEvent;
use AnyEvent::Handle;
use IPC::Open3;

my $VERBOSE = $ENV{'HARNESS_IS_VERBOSE'};


sub run_cmd {
    my ($self, @cmds) = @_;

    my ($chld_in, $chld_out);
    open3($chld_in, $chld_out, 0, '/bin/bash');

    my $test_end = AnyEvent->condvar;
    my $output = '';

    my $stdin; $stdin = AnyEvent::Handle->new(
        fh => $chld_in,
        on_error => $test_end,
    );

    my $stdout; $stdout = AnyEvent::Handle->new(
        fh => $chld_out,
        on_error => $test_end,
        on_read => sub {
            $VERBOSE && diag $stdout->{rbuf};
            $output .= $stdout->{rbuf};
            $stdout->{rbuf} = '';
            $test_end->send if $output =~ s/### TEST_END ###\n$//s;
        },
    );

    push @cmds, 'echo -e "### TEST_END ###"';

    foreach my $cmd (@cmds) {

        if ($cmd =~ m/^await (\d+)$/s) {
            $self->async_wait($1);
            next;
        }

        $stdin->push_write("$cmd\n");
    }

    $test_end->recv;

    return $output;
}

sub async_wait {
    my ($self, $time) = @_;
    my $cv = AnyEvent->condvar; 
    my $tmr = AnyEvent->timer( after => $time, cb => $cv ); 
    $cv->recv;
}


sub check_01_supported_os : Test(startup => 1) {
    my ($self) =  @_;

    unless ($^O eq 'linux') {
        $self->BAILOUT("OS unsupported");
    }

    ok( 1, "Supported OS ($^O)");
}

sub check_02_author_testing : Test(startup => 1) {
    my ($self) =  @_;

    unless ($ENV{'AUTHOR_TESTING'}) {
        # Fiddling with shell stdin/stdout makes test fail when run by cpanm or dzil
        $self->SKIP_ALL("This test fiddles with stdout and fails when run by cpanm or dzil");
    }

    ok( 1, "Author testing");
}

sub check_03_dzil_release_testing : Test(startup => 1) {
    my ($self) =  @_;

    if ($ENV{'RELEASE_TESTING'}) {
        # But 'dzil test' pass just fine
        $self->SKIP_ALL("This test fails when run by 'dzil release'");
    }

    ok( 1, "Not dzil release testing");
}

sub check_04_root_permissions : Test(startup => 1) {
    my ($self) =  @_;

    unless ($> == 0) {
        # Starting and stopping daemons requires root permissions
        $self->SKIP_ALL("This test requires root permissions");
    }

    ok( 1, "Root permissions");
}

sub test_01_example_basic : Test(1) {
    my ($self) =  @_;

    my $output = $self->run_cmd(
        'cd examples/basic',
        'source setup.sh',
        './run.sh',
        'await 2',
        './client.pl',
        './run.sh stop',
    );

    my $expected = quotemeta qq{



( run in 0.919 second using v1.01-cache-2.11-cpan-6aa56a78535 )