| [943] | 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 946 2025-12-24 17:39:42Z 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 |
|
|---|
| [946] | 23 | package DNSTest;
|
|---|
| [943] | 24 | use strict;
|
|---|
| 25 | use warnings;
|
|---|
| [946] | 26 | use Exporter;
|
|---|
| 27 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
|
|---|
| 28 | @ISA = qw(Exporter);
|
|---|
| 29 | @EXPORT = qw($dnsdb $dbh);
|
|---|
| [943] | 30 |
|
|---|
| 31 | use Test::More;
|
|---|
| 32 |
|
|---|
| 33 | use lib '.';
|
|---|
| 34 | use DNSDB;
|
|---|
| 35 |
|
|---|
| 36 | our $dnsdb;
|
|---|
| 37 | our $dbh;
|
|---|
| 38 |
|
|---|
| 39 | sub new {
|
|---|
| 40 | $dnsdb = new DNSDB(
|
|---|
| 41 | dbhost => 'localhost',
|
|---|
| 42 | dbname => 'dnstest',
|
|---|
| 43 | dbuser => 'dnstest',
|
|---|
| 44 | dbpass => 'dnstestpwd',
|
|---|
| 45 | );
|
|---|
| 46 | ok( defined $dnsdb );
|
|---|
| 47 | ok( $dnsdb->isa('DNSDB') );
|
|---|
| 48 |
|
|---|
| 49 | ## Prepare the DB
|
|---|
| 50 | # Check that we aren't in an obviously production DB before blowing it away.
|
|---|
| 51 | # A DB instantiated for these tests should NEVER have more than a handful of domains and maybe 20-30 records.
|
|---|
| 52 |
|
|---|
| [945] | 53 | $dbh = $dnsdb->{dbh};
|
|---|
| 54 |
|
|---|
| [943] | 55 | my ($dcount) = $dbh->selectrow_array("SELECT count(*) FROM domains");
|
|---|
| 56 | BAIL_OUT("# DB looks like it may not be a test DB, found $dcount > 10 domains!\n")
|
|---|
| 57 | if $dcount > 10;
|
|---|
| [945] | 58 | ok( $dcount, '<=', 10, "domain count ($dcount): looks like a test DB" );
|
|---|
| [943] | 59 |
|
|---|
| 60 | my ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records");
|
|---|
| 61 | BAIL_OUT("# DB looks like it may not be a test DB, found $rcount > 30 records!\n")
|
|---|
| [945] | 62 | if $rcount > 50;
|
|---|
| 63 | ok( $rcount, '<=', 50, "record ($rcount): looks like a test DB" );
|
|---|
| [943] | 64 |
|
|---|
| [944] | 65 | # drop all tables etc
|
|---|
| 66 | $ENV{PGPASSWORD} = $dnsdb->{dbpass};
|
|---|
| 67 | diag qx( psql -h $dnsdb->{dbhost} -U $dnsdb->{dbuser} $dnsdb->{dbname} < t/dropall.sql );
|
|---|
| 68 | # load some standard test data
|
|---|
| 69 | diag qx( psql -h $dnsdb->{dbhost} -U $dnsdb->{dbuser} $dnsdb->{dbname} < t/dns-unitbase.sql );
|
|---|
| 70 |
|
|---|
| [943] | 71 | }
|
|---|
| 72 |
|
|---|
| 73 | sub DESTROY {
|
|---|
| 74 | $dnsdb->finish;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | 1;
|
|---|