# -*- Perl -*- # Base class for DNSDB.pm unit testing # Sets up DB connection and repopulates it for a consistent test reference ## # $Id: DNSTest.pm 946 2025-12-24 17:39:42Z kdeugau $ # Copyright 2025 Kris Deugau # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ## package DNSTest; use strict; use warnings; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @ISA = qw(Exporter); @EXPORT = qw($dnsdb $dbh); use Test::More; use lib '.'; use DNSDB; our $dnsdb; our $dbh; sub new { $dnsdb = new DNSDB( dbhost => 'localhost', dbname => 'dnstest', dbuser => 'dnstest', dbpass => 'dnstestpwd', ); ok( defined $dnsdb ); ok( $dnsdb->isa('DNSDB') ); ## Prepare the DB # Check that we aren't in an obviously production DB before blowing it away. # A DB instantiated for these tests should NEVER have more than a handful of domains and maybe 20-30 records. $dbh = $dnsdb->{dbh}; my ($dcount) = $dbh->selectrow_array("SELECT count(*) FROM domains"); BAIL_OUT("# DB looks like it may not be a test DB, found $dcount > 10 domains!\n") if $dcount > 10; ok( $dcount, '<=', 10, "domain count ($dcount): looks like a test DB" ); my ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records"); BAIL_OUT("# DB looks like it may not be a test DB, found $rcount > 30 records!\n") if $rcount > 50; ok( $rcount, '<=', 50, "record ($rcount): looks like a test DB" ); # drop all tables etc $ENV{PGPASSWORD} = $dnsdb->{dbpass}; diag qx( psql -h $dnsdb->{dbhost} -U $dnsdb->{dbuser} $dnsdb->{dbname} < t/dropall.sql ); # load some standard test data diag qx( psql -h $dnsdb->{dbhost} -U $dnsdb->{dbuser} $dnsdb->{dbname} < t/dns-unitbase.sql ); } sub DESTROY { $dnsdb->finish; } 1;