From 2235299e75427faf8e35cdbf10b60f6ec9b07eca Mon Sep 17 00:00:00 2001 From: Greg Sabino Mullane Date: Thu, 30 Aug 2012 14:22:31 -0400 Subject: [PATCH] Tests for same-database --- t/20-postgres.t | 36 ++++++++++++++++++++++++++++++++---- t/BucardoTesting.pm | 7 ++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/t/20-postgres.t b/t/20-postgres.t index 077487fb7..49894f25c 100644 --- a/t/20-postgres.t +++ b/t/20-postgres.t @@ -23,7 +23,7 @@ my $bct = BucardoTesting->new({location => 'postgres'}) ## The above runs one test for each passed in database x the number of test tables my $numtables = keys %tabletype; my $numsequences = keys %sequences; -my $single_tests = 30; +my $single_tests = 35; my $check_for_row_1 = 1; my $check_for_row_2 = 1; my $check_for_row_3 = 2; @@ -58,7 +58,7 @@ $dbhD = $bct->repopulate_cluster('D'); $dbhX = $bct->setup_bucardo('A'); ## Teach Bucardo about four databases -for my $name (qw/ A B C D /) { +for my $name (qw/ A B C D A1 /) { $t = "Adding database from cluster $name works"; my ($dbuser,$dbport,$dbhost) = $bct->add_db_args($name); $command = "bucardo add db $name dbname=bucardo_test user=$dbuser port=$dbport host=$dbhost"; @@ -129,6 +129,26 @@ $dbhA->commit(); $dbhB->do($SQL); $dbhB->commit(); +## Create a copy of table1, but with a different name for same-database replication testing +$SQL = 'CREATE TABLE bucardo_test1_copy (LIKE bucardo_test1)'; +$dbhA->do($SQL); +$dbhA->commit(); +$dbhB->do($SQL); +$dbhB->commit(); +## Create a herd for same-database testing +$t = q{Created a new herd sameherd}; +$res = $bct->ctl('bucardo add herd sameherd bucardo_test1'); +like ($res, qr/Created herd "sameherd"/, $t); + +## We want all access to A1 to use the alternate table +$t = q{Created a customname to force usage of bucardo_test1_copy}; +$res = $bct->ctl('bucardo add customname bucardo_test1 bucardo_test1_copy db=A1'); +like ($res, qr/\Qpublic.bucardo_test1 to bucardo_test1_copy (for database A1)/, $t); + +$t = q{Created a new sync for samedb}; +$res = $bct->ctl('bucardo add sync samedb herd=sameherd dbs=A,A1 status=inactive'); +like ($res, qr/Added sync "samedb"/, $t); + ## Create new herds, goats, and a sync $t = q{Created a new herd mherd}; $res = $bct->ctl('bucardo add herd mherd mtest'); @@ -151,14 +171,16 @@ sub d { ## and Bucardo should exit $bct->restart_bucardo($dbhX, 'bucardo_stopped'); -## Activate the pg1 and mtest syncs -$t = q{Activated sync pgtest1}; +## Activate the pg1, mtest, and samedb syncs $bct->ctl('bucardo update sync pgtest1 status=active'); $bct->ctl('bucardo update sync mtest status=active'); +$bct->ctl('bucardo update sync samedb status=active'); ## Start listening for a syncdone message ## Bucardo should fire the sync off right away without a kick $dbhX->do('LISTEN bucardo_syncdone_pgtest1'); +$dbhX->do('LISTEN bucardo_syncdone_mtest'); +$dbhX->do('LISTEN bucardo_syncdone_samedb'); $dbhX->commit(); ## Start up Bucardo again @@ -170,6 +192,12 @@ $bct->wait_for_notice($dbhX, 'bucardo_syncdone_pgtest1'); ## See if things are on the other databases $bct->check_for_row([[1]], [qw/ B C D/]); +## Check that our "samedb" process worked +$t = q{Replicating to the same database via customname works}; +$SQL = 'SELECT inty FROM bucardo_test1_copy'; +$res = $dbhA->selectall_arrayref($SQL); +is_deeply($res, [[1]], $t); + ## Switch to a 2 source sync $bct->ctl('bucardo update sync pgtest1 status=inactive'); $bct->ctl('bucardo update sync pgtest5 status=active'); diff --git a/t/BucardoTesting.pm b/t/BucardoTesting.pm index 330b25477..b84114a6c 100644 --- a/t/BucardoTesting.pm +++ b/t/BucardoTesting.pm @@ -840,19 +840,20 @@ sub add_db_args { ## Arguments: ## 1. Name of a cluster ## Returns: DSN-like string to connect to that cluster - ## May return string or array dependig on how it was called + ## Allows for "same" databases o the form X# e.g. A1, B1 + ## May return string or array depending on how it was called my $self = shift; my $clustername = shift or die; + $clustername =~ s/\d+$//; + ## Build the DSN to connect with my $info = $pgver{$clustername}; my $dbport = $info->{port}; my $dbhost = getcwd . "/$info->{dirname}/socket"; my $dsn = "dbi:Pg:dbname=$dbname;port=$dbport;host=$dbhost"; - my $arg = - return wantarray ? ($user,$dbport,$dbhost) : "name=$dbname user=$user port=$dbport host=$dbhost"; -- 2.39.5