source: branches/cname-collision/t/DNSTest.pm@ 1002

Last change on this file since 1002 was 999, checked in by Kris Deugau, 6 days ago

/branches/cname-collision

Set a default for coerce_cname_timestamp
Override coerce_cname_timestamp in the test support module to better
exercise deeper branches of the CNAME collision checks
See #72

  • Property svn:keywords set to Date Rev Author Id
File size: 3.2 KB
Line 
1# -*- Perl -*-
2
3# Base class for DNSDB.pm unit testing
4# Sets up DB connection and repopulates it for a consistent test reference
5##
6# $Id: DNSTest.pm 999 2026-01-23 21:00:29Z kdeugau $
7# Copyright 2025 Kris Deugau <kdeugau@deepnet.cx>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21##
22
23package DNSTest;
24use strict;
25use warnings;
26use Exporter;
27use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28@ISA = qw(Exporter);
29@EXPORT = qw($dnsdb $dbh);
30
31use Test::More;
32
33use lib '.';
34use DNSDB;
35
36our $dnsdb;
37our $dbh;
38our $debug;
39
40sub new {
41 my $this = shift;
42 my $class = ref($this) || $this;
43 my %args = @_;
44
45 $dnsdb = new DNSDB(
46 dbhost => 'localhost',
47 dbname => 'dnstest',
48 dbuser => 'dnstest',
49 dbpass => 'dnstestpwd',
50
51 # This exercises more branches of the CNAME collision check with more fine-grained results.
52 coerce_cname_timestamp => 'adjust',
53 );
54 ok( defined $dnsdb );
55 ok( $dnsdb->isa('DNSDB') );
56
57 $debug = 1 if $args{debug};
58
59 ## Prepare the DB
60 # Check that we aren't in an obviously production DB before blowing it away.
61 # A DB instantiated for these tests should have a known set of domains and records.
62
63 $dbh = $dnsdb->{dbh};
64
65 my ($dcount) = $dbh->selectrow_array("SELECT count(*) FROM domains WHERE NOT domain IN ".
66 "('example.com','example.net','example.org','expiry1.test','expiry2.test')");
67 BAIL_OUT("# DB looks like it may not be a test DB, found $dcount > 0 non-test domains!\n")
68 if $dcount > 0;
69 cmp_ok( $dcount, '==', 0, "non-test domain count ($dcount): looks like a test DB" );
70
71 my ($rcount) = $dbh->selectrow_array(qq{SELECT count(*) FROM records WHERE NOT (
72 host like '%example.com' or host like '%example.net' or host like '%example.org' or
73 host like '%expiry1.test' or host like '%expiry2.test' or inetlazy(val) << '192.168.2.0/27'
74 )});
75 my $maxrecs = 0;
76 BAIL_OUT("# DB looks like it may not be a test DB, found $rcount > $maxrecs records!\n")
77 if $rcount > $maxrecs;
78 cmp_ok( $rcount, '<=', $maxrecs, "non-test record ($rcount): looks like a test DB" );
79
80 # drop all tables etc
81 $ENV{PGPASSWORD} = $dnsdb->{dbpass};
82# neither diag or note seem to suppress output from qx
83 my $dropdata = qx( psql -h $dnsdb->{dbhost} -U $dnsdb->{dbuser} $dnsdb->{dbname} 2>&1 < t/dropall.sql );
84 diag( $dropdata ) if $debug;
85 # load some standard test data
86 my $reload = qx( psql -h $dnsdb->{dbhost} -U $dnsdb->{dbuser} $dnsdb->{dbname} 2>&1 < t/dns-unitbase.sql );
87 diag( $reload ) if $debug;
88 # Set timestamps to a sliding window
89 my $stampwindow = qx( psql -h $dnsdb->{dbhost} -U $dnsdb->{dbuser} $dnsdb->{dbname} 2>&1 < t/test-cname-timestamps.sql );
90 diag( $stampwindow ) if $debug;
91 undef $ENV{PGPASSWORD};
92} # new()
93
94sub DESTROY {
95 $dnsdb->finish;
96}
97
981;
Note: See TracBrowser for help on using the repository browser.