From 172e3ed4b2512712c93c7f67d7a3be8184802137 Mon Sep 17 00:00:00 2001 From: "Mark A. Jensen" Date: Tue, 9 Jun 2020 21:42:10 -0400 Subject: [PATCH] test(docker) - encapsulate docker stuff in package t::NeoCon --- perl/t/007_db.t | 37 ++++---------- perl/t/008_mapmodel.t | 42 ++++------------ perl/t/009_meta.t | 40 ++++------------ perl/t/010_meta__load_all_db_models.t | 40 ++++------------ perl/t/NeoCon.pm | 69 +++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 122 deletions(-) create mode 100644 perl/t/NeoCon.pm diff --git a/perl/t/007_db.t b/perl/t/007_db.t index 03cd2ed..8683ae1 100644 --- a/perl/t/007_db.t +++ b/perl/t/007_db.t @@ -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>',\$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 : { @@ -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); @@ -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; } diff --git a/perl/t/008_mapmodel.t b/perl/t/008_mapmodel.t index ce25ef7..f542599 100644 --- a/perl/t/008_mapmodel.t +++ b/perl/t/008_mapmodel.t @@ -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>',\$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; @@ -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"; @@ -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; } diff --git a/perl/t/009_meta.t b/perl/t/009_meta.t index d78383e..6a0c31f 100644 --- a/perl/t/009_meta.t +++ b/perl/t/009_meta.t @@ -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>',\$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; @@ -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; } diff --git a/perl/t/010_meta__load_all_db_models.t b/perl/t/010_meta__load_all_db_models.t index 56ce4d2..ea94b15 100644 --- a/perl/t/010_meta__load_all_db_models.t +++ b/perl/t/010_meta__load_all_db_models.t @@ -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; @@ -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>',\$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 : { @@ -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; } diff --git a/perl/t/NeoCon.pm b/perl/t/NeoCon.pm new file mode 100644 index 0000000..98f62d4 --- /dev/null +++ b/perl/t/NeoCon.pm @@ -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 => +# name => +# ports => 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;