Skip to content

Commit

Permalink
test(docker) - encapsulate docker stuff
Browse files Browse the repository at this point in the history
in package t::NeoCon
  • Loading branch information
majensen committed Jun 10, 2020
1 parent 22dd2f9 commit 172e3ed
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 122 deletions.
37 changes: 9 additions & 28 deletions perl/t/007_db.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,21 @@ use Test::Warn;
use Test::Exception;
use IPC::Run qw/run/;
use Neo4j::Bolt;
use lib '../lib';
use lib qw{../lib ..};
use Log::Log4perl qw/:easy/;
use Bento::Meta::Model::ObjectMap;
use Bento::Meta::Model::Node;
use Bento::Meta::Model::ValueSet;
use Bento::Meta::Model::Term;

Log::Log4perl->easy_init($FATAL);
my $ctr_name = "test$$";
my $img = 'maj1/test-db-bento-meta';

Log::Log4perl->easy_init($INFO);
####
diag "Starting docker container '$ctr_name' with image $img";
my @startcmd = split / /,
"docker run -d -P --name $ctr_name $img";
my @portcmd = split / /, "docker container port $ctr_name";
my @stopcmd = split / /,"docker kill $ctr_name";
my @rmcmd = split / /, "docker rm $ctr_name";

my ($in, $out, $err);
unless (run(['docker'],'<pty<',\$in,'>pty>',\$out)) {
unless (eval 'require t::NeoCon; 1') {
plan skip_all => "Docker not available for test database setup: skipping.";
}
run \@startcmd, \$in, \$out, \$err;
if ($err) {
diag "docker error: $err";
}
sleep 10;
$in=$err=$out='';
run \@portcmd, \$in, \$out, \$err;
my ($port) = grep /7687.tcp/, split /\n/,$out;
($port) = $port =~ m/([0-9]+)$/;
my $docker = t::NeoCon->new();
$docker->start;
my $port = $docker->port(7687);
####
ok my $cxn = Neo4j::Bolt->connect("bolt://localhost:$port"), 'create neo4j connection';
SKIP : {
Expand Down Expand Up @@ -62,7 +45,7 @@ SKIP : {
is $omap, $Bento::Meta::Model::Node::OBJECT_MAP, "pointer same";

diag "test get()";
ok $res = $cxn->run_query('match (a:node) return id(a) limit 1');
ok $res = $cxn->run_query('match (a:node) return id(a) limit 1 ');
ok my ($n_id) = $res->fetch_next;
my $node = Bento::Meta::Model::Node->new();
$node->set_neoid($n_id);
Expand Down Expand Up @@ -195,8 +178,6 @@ SKIP : {
done_testing;

END {
diag "Stopping container $ctr_name";
run \@stopcmd;
diag "Removing container $ctr_name";
run \@rmcmd;
$docker->stop;
$docker->rm;
}
42 changes: 10 additions & 32 deletions perl/t/008_mapmodel.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,18 @@ use Test::Warn;
use Test::Exception;
use IPC::Run qw/run/;
use Neo4j::Bolt;
use lib '../lib';
use lib qw'../lib ..';
use Log::Log4perl qw/:easy/;
use Bento::Meta::Model;

Log::Log4perl->easy_init($FATAL);
my $ctr_name = "test$$";
my $img = 'maj1/test-db-bento-meta';

####
diag "Starting docker container '$ctr_name' with image $img";
my @startcmd = split / /,
"docker run -d -P --name $ctr_name $img";
my @portcmd = split / /, "docker container port $ctr_name";
my @stopcmd = split / /,"docker kill $ctr_name";
my @rmcmd = split / /, "docker rm $ctr_name";

my ($in, $out, $err);
unless (run(['docker'],'<pty<',\$in,'>pty>',\$out)) {
Log::Log4perl->easy_init($INFO);
unless (eval 'require t::NeoCon; 1') {
plan skip_all => "Docker not available for test database setup: skipping.";
}
run \@startcmd, \$in, \$out, \$err;
if ($err) {
diag "docker error: $err";
}
else {
sleep 10;
}
$in=$err=$out='';
run \@portcmd, \$in, \$out, \$err;
my ($port) = grep /7687.tcp/, split /\n/,$out;
($port) = $port =~ m/([0-9]+)$/;
####
my $docker = t::NeoCon->new;
$docker->start;
my $port = $docker->port(7687);

ok my $cxn = Neo4j::Bolt->connect("bolt://localhost:$port"), 'create neo4j connection';
SKIP : {
skip "Can't connect to test db: ".$cxn->errmsg, 1 unless $cxn->connected;
Expand Down Expand Up @@ -135,7 +115,7 @@ QRY

ok my $t = Bento::Meta::Model::Term->new({value => "electric_boogaloo"}), "create new term";
is $t->dirty, 1, "new term dirty";
$DB::single=1 ;

ok $model->add_terms($prop => $t), "add term to property";
is $prop->dirty, 1, "prop dirty";
is $prop->value_set->dirty, 1, "value set dirty";
Expand Down Expand Up @@ -240,8 +220,6 @@ QRY
done_testing;

END {
diag "Stopping container $ctr_name";
run \@stopcmd;
diag "Removing container $ctr_name";
run \@rmcmd;
$docker->stop;
$docker->rm;
}
40 changes: 10 additions & 30 deletions perl/t/009_meta.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,21 @@ use Test::Warn;
use Test::Exception;
use IPC::Run qw/run/;
use Neo4j::Bolt;
use lib '../lib';
use lib qw'../lib ..';
use Log::Log4perl qw/:easy/;
use Bento::Meta;

Log::Log4perl->easy_init($FATAL);
my $ctr_name = "test$$";
my $img = 'maj1/test-db-bento-meta';
Log::Log4perl->easy_init($INFO);
my $d = (-e 't' ? 't' : '.');

####
diag "Starting docker container '$ctr_name' with image $img";
my @startcmd = split / /,
"docker run -d -P --name $ctr_name $img";
my @portcmd = split / /, "docker container port $ctr_name";
my @stopcmd = split / /,"docker kill $ctr_name";
my @rmcmd = split / /, "docker rm $ctr_name";

my ($in, $out, $err);
unless (run(['docker'],'<pty<',\$in,'>pty>',\$out)) {
unless (eval 'require t::NeoCon; 1') {
plan skip_all => "Docker not available for test database setup: skipping.";
}
run \@startcmd, \$in, \$out, \$err;
if ($err) {
diag "docker error: $err";
}
else {
sleep 10;
}
$in=$err=$out='';
run \@portcmd, \$in, \$out, \$err;
my ($port) = grep /7687.tcp/, split /\n/,$out;
($port) = $port =~ m/([0-9]+)$/;
####
my $docker = t::NeoCon->new;
$docker->start;
my $port = $docker->port(7687);


ok my $cxn = Neo4j::Bolt->connect("bolt://localhost:$port"), 'create neo4j connection';
SKIP : {
skip "Can't connect to test db: ".$cxn->errmsg, 1 unless $cxn->connected;
Expand All @@ -56,8 +38,6 @@ SKIP : {
done_testing;

END {
diag "Stopping container $ctr_name";
run \@stopcmd;
diag "Removing container $ctr_name";
run \@rmcmd;
$docker->stop;
$docker->rm;
}
40 changes: 8 additions & 32 deletions perl/t/010_meta__load_all_db_models.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Test::Deep;
use Test::Exception;
use IPC::Run qw/run/;
use Neo4j::Bolt;
use lib '../lib';
use lib qw'../lib ..';
use Log::Log4perl qw/:easy/;
use Bento::Meta;

Expand All @@ -14,35 +14,13 @@ use Bento::Meta;
# broke out from 009_meta.t so that db loaded models would not conflict
# with file loaded models (cannot load two models named ICDC)

Log::Log4perl->easy_init($FATAL);
my $ctr_name = "test$$";
my $img = 'maj1/test-db-bento-meta';
my $d = (-e 't' ? 't' : '.');

####
diag "Starting docker container '$ctr_name' with image $img";
my @startcmd = split / /,
"docker run -d -P --name $ctr_name $img";
my @portcmd = split / /, "docker container port $ctr_name";
my @stopcmd = split / /,"docker kill $ctr_name";
my @rmcmd = split / /, "docker rm $ctr_name";

my ($in, $out, $err);
unless (run(['docker'],'<pty<',\$in,'>pty>',\$out)) {
Log::Log4perl->easy_init($INFO);
unless (eval 'require t::NeoCon; 1') {
plan skip_all => "Docker not available for test database setup: skipping.";
}
run \@startcmd, \$in, \$out, \$err;
if ($err) {
diag "docker error: $err";
}
else {
sleep 10;
}
$in=$err=$out='';
run \@portcmd, \$in, \$out, \$err;
my ($port) = grep /7687.tcp/, split /\n/,$out;
($port) = $port =~ m/([0-9]+)$/;
####
my $docker = t::NeoCon->new();
$docker->start;
my $port = $docker->port(7687);

ok my $cxn = Neo4j::Bolt->connect("bolt://localhost:$port"), 'create neo4j connection';
SKIP : {
Expand All @@ -64,8 +42,6 @@ SKIP : {
done_testing;

END {
diag "Stopping container $ctr_name";
run \@stopcmd;
diag "Removing container $ctr_name";
run \@rmcmd;
$docker->stop;
$docker->rm;
}
69 changes: 69 additions & 0 deletions perl/t/NeoCon.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package t::NeoCon;
use Log::Log4perl qw/:easy/;
use IPC::Run qw/run/;
use Carp qw/croak/;
use strict;

my ($in,$out,$err);
my $TAG = 'maj1/test-db-bento-meta';
unless (eval 'require IO::Pty;1') {
croak __PACKAGE__." requires IO::Pty - please install it"
}
unless (run ['docker'],\$in,\$out,\$err) {
croak __PACKAGE__." requires docker - please install it"
}

# tag => <docker tag>
# name => <container name>
# ports => <hash of port mappings container => host>

sub new {
my $class = shift;
my %args = @_;
my $self = bless \%args, $class;
$self->{tag} //= $TAG;
$self->{name} //= "test$$";
$self->{ports} //= {7687 => undef};
return $self;
}
sub error { shift->{_error} }
sub port { shift->{ports}{shift()} }
sub ports { shift->{ports} }
sub start {
my $self = shift;
my @startcmd = split / /, "docker run -d -P --name $$self{name} $$self{tag}";
INFO "Starting docker $$self{tag} as $$self{name}";
unless (run \@startcmd, \$in, \$out, \$err) {
$self->{_error} = $err;
return;
}
sleep 10;
run [split / /, "docker container port $$self{name}"], \$in, \$out, \$err;
for my $port (keys %{$self->{ports}}) {
my ($p) = grep /${port}.tcp/, split /\n/,$out;
($p) = $p =~ m/([0-9]+)$/;
$self->{ports}{$port} = $p;
}
return 1;
}

sub stop {
my $self = shift;
INFO "Stopping docker container $$self{name}";
unless (run [split / /,"docker kill $$self{name}"], \$in,\$out,\$err) {
$self->{_error} = $err;
return;
}
return 1;
}

sub rm {
my $self = shift;
INFO "Removing docker container $$self{name}";
unless ( run [split / /, "docker rm $$self{name}"],\$in,\$out,\$err ) {
$self->{_error} = $err;
return;
}
return 1;
}
1;

0 comments on commit 172e3ed

Please sign in to comment.