| 1 | use utf8; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use feature 'state'; | ||
| 6 | package DR::Tnt::Test; | ||
| 7 | use DR::Tnt::Test::TntInstance; | ||
| 8 | use IO::Socket::INET; | ||
| 9 | use Test::More; | ||
| 10 | use base qw(Exporter); | ||
| 11 | our @EXPORT = qw( | ||
| 12 | free_port | ||
| 13 | tarantool_version | ||
| 14 | tarantool_version_check | ||
| 15 | start_tarantool | ||
| 16 | ); | ||
| 17 | |||
| 18 | |||
| 19 | sub tarantool_version() { | ||
| 20 | |||
| 21 | local $SIG{__WARN__} = sub { }; | ||
| 22 | if (open my $fh, '-|', 'tarantool', '-V') { | ||
| 23 | my $v = <$fh>; | ||
| 24 | return undef unless $v =~ /^Tarantool\s+(\S+)/; | ||
| 25 | return $1; | ||
| 26 | } | ||
| 27 | return undef; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | sub tarantool_version_check($) { | ||
| 32 | my ($version) = @_; | ||
| 33 | $version ||= '1.6'; | ||
| 34 | |||
| 35 | my $v = tarantool_version; | ||
| 36 | |||
| 37 | goto FAIL unless $v; | ||
| 38 | my @p = split /\./, $version; | ||
| 39 | $v =~ s/-.*//; | ||
| 40 | my @v = split /\./, $v; | ||
| 41 | |||
| 42 | for (my $i = 0; $i < @p; $i++) { | ||
| 43 | last unless $i < @v; | ||
| 44 | goto OK if $v[$i] > $p[$i]; | ||
| 45 | goto FAIL unless $v[$i] >= $p[$i]; | ||
| 46 | } | ||
| 47 | |||
| 48 | goto FAIL unless @v >= @p; | ||
| 49 | |||
| 50 | OK: | ||
| 51 | return; | ||
| 52 | |||
| 53 | FAIL: { | ||
| 54 | my $tb = Test::More->builder; | ||
| 55 | my @passed = $tb->details; | ||
| 56 | for (my $i = @passed; $i < $tb->expected_tests; $i++) { | ||
| 57 | $tb->skip("tarantool $version is not found"); | ||
| 58 | } | ||
| 59 | exit; | ||
| 60 | } | ||
| 61 | |||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | sub free_port() { | ||
| 67 | state $busy_ports = {}; | ||
| 68 | while( 1 ) { | ||
| 69 | my $port = 10000 + int rand 30000; | ||
| 70 | next if exists $busy_ports->{ $port }; | ||
| 71 | next unless IO::Socket::INET->new( | ||
| 72 | Listen => 5, | ||
| 73 | LocalAddr => '127.0.0.1', | ||
| 74 | LocalPort => $port, | ||
| 75 | Proto => 'tcp', | ||
| 76 | (($^O eq 'MSWin32') ? () : (ReuseAddr => 1)), | ||
| 77 | ); | ||
| 78 | return $busy_ports->{ $port } = $port; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub start_tarantool { | ||
| 83 | my %opts = @_; | ||
| 84 | $opts{-port} = free_port; | ||
| 85 | DR::Tnt::Test::TntInstance->new(%opts); | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; |