test/lib/Test/ktap.pm - ktap

Data types defined

Source code

  1. # Copyright (C) Yichun Zhang (agentzh)

  2. package Test::ktap;

  3. use Test::Base -Base;
  4. use POSIX ();
  5. use IPC::Run ();

  6. our @EXPORT = qw( run_tests );

  7. sub run_tests () {
  8.     for my $block (Test::Base::blocks()) {
  9.         run_test($block);
  10.     }
  11. }

  12. sub bail_out (@) {
  13.     Test::More::BAIL_OUT(@_);
  14. }

  15. sub parse_cmd ($) {
  16.     my $cmd = shift;
  17.     my @cmd;
  18.     while (1) {
  19.         if ($cmd =~ /\G\s*"(.*?)"/gmsc) {
  20.             push @cmd, $1;

  21.         } elsif ($cmd =~ /\G\s*'(.*?)'/gmsc) {
  22.             push @cmd, $1;

  23.         } elsif ($cmd =~ /\G\s*(\S+)/gmsc) {
  24.             push @cmd, $1;

  25.         } else {
  26.             last;
  27.         }
  28.     }
  29.     return @cmd;
  30. }

  31. sub run_test ($) {
  32.     my $block = shift;
  33.     my $name = $block->name;

  34.     my $timeout = $block->timeout() || 10;
  35.     my $opts = $block->opts;
  36.     my $args = $block->args;

  37.     my $cmd = "./ktap";

  38.     if (defined $opts) {
  39.         $cmd .= " $opts";
  40.     }

  41.     my $kpfile;
  42.     if (defined $block->src) {
  43.         $kpfile = POSIX::tmpnam() . ".kp";
  44.         open my $out, ">$kpfile" or
  45.             bail_out("cannot open $kpfile for writing: $!");
  46.         print $out ($block->src);
  47.         close $out;
  48.         $cmd .= " $kpfile"
  49.     }

  50.     if (defined $args) {
  51.         $cmd .= " $args";
  52.     }

  53.     #warn "CMD: $cmd\n";

  54.     my @cmd = parse_cmd($cmd);

  55.     my ($out, $err);

  56.     eval {
  57.         IPC::Run::run(\@cmd, \undef, \$out, \$err,
  58.                       IPC::Run::timeout($timeout));
  59.     };
  60.     if ($@) {
  61.         # timed out
  62.         if ($@ =~ /timeout/) {
  63.             if (!defined $block->expect_timeout) {
  64.                 fail("$name: ktap process timed out");
  65.             }
  66.     } else {
  67.             fail("$name: failed to run command [$cmd]: $@");
  68.         }
  69.     }

  70.     my $ret = ($? >> 8);

  71.     if (defined $kpfile) {
  72.         unlink $kpfile;
  73.     }

  74.     if (defined $block->out) {
  75.         is $out, $block->out, "$name - stdout eq okay";
  76.     }

  77.     my $regex = $block->out_like;
  78.     if (defined $regex) {
  79.         if (!ref $regex) {
  80.             $regex = qr/$regex/ms;
  81.         }
  82.         like $out, $regex, "$name - stdout like okay";
  83.     }

  84.     if (defined $block->err) {
  85.         is $err, $block->err, "$name - stderr eq okay";
  86.     }

  87.     $regex = $block->err_like;
  88.     if (defined $regex) {
  89.         if (!ref $regex) {
  90.             $regex = qr/$regex/ms;
  91.         }
  92.         like $err, $regex, "$name - stderr like okay";
  93.     }

  94.     my $exp_ret = $block->ret;
  95.     if (!defined $exp_ret) {
  96.         $exp_ret = 0;
  97.     }
  98.     is $ret, $exp_ret, "$name - exit code okay";
  99. }

  100. 1;
  101. # vi: et