From 58b1eb83d4d57ca9142475b6d8d8f181b38dbbe4 Mon Sep 17 00:00:00 2001 From: Mats Kindahl Date: Wed, 19 May 2021 22:46:11 +0200 Subject: [PATCH] Add TimescaleNode subclasses for TAP Testing Use subclassing to inherit from `PostgresNode` and create a hierarchy containing `AccessNode` and `DataNode` to simplify creating tests with multiple nodes. Also, two new functions are added: `TimescaleNode::create`: Creates the new node by calling `get_new_node`, `init` and `start` in that order. `AccessNode::add_data_node`: Adds a new data node to the access node. Also rewrite the test to use the new hierarchy. --- test/perl/AccessNode.pm | 21 +++++++++++++ test/perl/DataNode.pm | 10 +++++++ test/perl/TimescaleNode.pm | 30 +++++-------------- tsl/test/t/001_simple_multinode.pl | 48 ++++++++++-------------------- 4 files changed, 53 insertions(+), 56 deletions(-) create mode 100644 test/perl/AccessNode.pm create mode 100644 test/perl/DataNode.pm diff --git a/test/perl/AccessNode.pm b/test/perl/AccessNode.pm new file mode 100644 index 000000000..09a633bb3 --- /dev/null +++ b/test/perl/AccessNode.pm @@ -0,0 +1,21 @@ +# This file and its contents are licensed under the Timescale License. +# Please see the included NOTICE for copyright information and +# LICENSE-TIMESCALE for a copy of the license. + +package AccessNode; +use parent qw(TimescaleNode); +use strict; +use warnings; + +sub add_data_node +{ + my ($self, $dn) = @_; + my $name = $dn->name; + my $host = $dn->host; + my $port = $dn->port; + $self->safe_psql('postgres', + "SELECT add_data_node('$name', host => '$host', port => $port)"); + return $self; +} + +1; diff --git a/test/perl/DataNode.pm b/test/perl/DataNode.pm new file mode 100644 index 000000000..accc37cad --- /dev/null +++ b/test/perl/DataNode.pm @@ -0,0 +1,10 @@ +# This file and its contents are licensed under the Timescale License. +# Please see the included NOTICE for copyright information and +# LICENSE-TIMESCALE for a copy of the license. + +package DataNode; +use parent qw(TimescaleNode); +use strict; +use warnings; + +1; diff --git a/test/perl/TimescaleNode.pm b/test/perl/TimescaleNode.pm index 039512f84..81f4db525 100644 --- a/test/perl/TimescaleNode.pm +++ b/test/perl/TimescaleNode.pm @@ -6,7 +6,7 @@ # routines for setup. package TimescaleNode; -use parent ("PostgresNode"); +use parent qw(PostgresNode); use TestLib qw(slurp_file); use strict; use warnings; @@ -14,29 +14,13 @@ use warnings; use Carp 'verbose'; $SIG{__DIE__} = \&Carp::confess; -use Exporter 'import'; -use vars qw(@EXPORT @EXPORT_OK); -@EXPORT = qw( - get_new_ts_node -); -@EXPORT_OK = qw( -); - -# -# Get a new TS-enabled PostgreSQL instance -# -# It's not created yet, but ready to restore from backup, -# initdb, etc. -# -sub get_new_ts_node +sub create { - my ($name, $class) = @_; - - $class //= 'TimescaleNode'; - - my $self = PostgresNode::get_new_node($name); - $self = bless $self, $class; - + my ($class, $name, %kwargs) = @_; + my $self = $class->get_new_node($name); + $self->init(%kwargs); + $self->start(%kwargs); + $self->safe_psql('postgres', 'CREATE EXTENSION timescaledb'); return $self; } diff --git a/tsl/test/t/001_simple_multinode.pl b/tsl/test/t/001_simple_multinode.pl index 8f3f627bb..b2bad20d5 100644 --- a/tsl/test/t/001_simple_multinode.pl +++ b/tsl/test/t/001_simple_multinode.pl @@ -5,40 +5,22 @@ # test a simple multi node cluster creation and basic operations use strict; use warnings; -use TimescaleNode qw(get_new_ts_node); +use AccessNode; +use DataNode; use TestLib; use Test::More tests => 9; #Initialize all the multi-node instances -my @nodes = (); -foreach my $nodename ('an', 'dn1', 'dn2') -{ - my $node = get_new_ts_node($nodename); - $node->init; - $node->start; - # set up the access node - if ($node->name eq 'an') - { - $node->safe_psql('postgres', "CREATE DATABASE an"); - $node->safe_psql('an', "CREATE EXTENSION timescaledb"); - } - push @nodes, $node; -} +my $an = AccessNode->create('an'); +my $dn1 = DataNode->create('dn1'); +my $dn2 = DataNode->create('dn2'); -#Add the data nodes from the access node -foreach my $i (1 .. 2) -{ - my $host = $nodes[$i]->host(); - my $port = $nodes[$i]->port(); - - $nodes[0]->safe_psql('an', - "SELECT add_data_node('dn$i', database => 'dn$i', host => '$host', port => $port)" - ); -} +$an->add_data_node($dn1); +$an->add_data_node($dn2); #Create a distributed hypertable and insert a few rows -$nodes[0]->safe_psql( - 'an', +$an->safe_psql( + 'postgres', qq[ CREATE TABLE test(time timestamp NOT NULL, device int, temp float); SELECT create_distributed_hypertable('test', 'time', 'device', 3); @@ -49,23 +31,23 @@ $nodes[0]->safe_psql( my $query = q[SELECT * from show_chunks('test');]; #Query Access node -$nodes[0]->psql_is( - 'an', $query, q[_timescaledb_internal._dist_hyper_1_1_chunk +$an->psql_is( + 'postgres', $query, q[_timescaledb_internal._dist_hyper_1_1_chunk _timescaledb_internal._dist_hyper_1_2_chunk _timescaledb_internal._dist_hyper_1_3_chunk _timescaledb_internal._dist_hyper_1_4_chunk], 'AN shows correct set of chunks' ); #Query datanode1 -$nodes[1]->psql_is( - 'dn1', +$dn1->psql_is( + 'postgres', $query, "_timescaledb_internal._dist_hyper_1_1_chunk\n_timescaledb_internal._dist_hyper_1_3_chunk\n_timescaledb_internal._dist_hyper_1_4_chunk", 'DN1 shows correct set of chunks'); #Query datanode2 -$nodes[2]->psql_is( - 'dn2', $query, +$dn2->psql_is( + 'postgres', $query, "_timescaledb_internal._dist_hyper_1_2_chunk", 'DN2 shows correct set of chunks');