From cf16578b3ecf2da1b38a724107dfaa802b03339c Mon Sep 17 00:00:00 2001 From: JINMEI Tatuya Date: Wed, 4 Apr 2012 21:40:25 -0700 Subject: [PATCH] [1791] initial refactoring: extracted createSQLite3Client so it can be shared. the plan is to use it in in-memory tests for this branch. No functional change or add/delete test cases yet. --- src/lib/datasrc/tests/Makefile.am | 1 + src/lib/datasrc/tests/test_client.h | 71 +++++++++++++++++++ .../tests/zone_finder_context_unittest.cc | 26 +++---- 3 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 src/lib/datasrc/tests/test_client.h diff --git a/src/lib/datasrc/tests/Makefile.am b/src/lib/datasrc/tests/Makefile.am index ac20e34791..c635c33d8b 100644 --- a/src/lib/datasrc/tests/Makefile.am +++ b/src/lib/datasrc/tests/Makefile.am @@ -48,6 +48,7 @@ run_unittests_SOURCES += datasrc_unittest.cc run_unittests_SOURCES += static_unittest.cc run_unittests_SOURCES += query_unittest.cc run_unittests_SOURCES += cache_unittest.cc +run_unittests_SOURCES += test_client.h test_client.cc run_unittests_SOURCES += test_datasrc.h test_datasrc.cc run_unittests_SOURCES += rbtree_unittest.cc run_unittests_SOURCES += logger_unittest.cc diff --git a/src/lib/datasrc/tests/test_client.h b/src/lib/datasrc/tests/test_client.h new file mode 100644 index 0000000000..2c692d3745 --- /dev/null +++ b/src/lib/datasrc/tests/test_client.h @@ -0,0 +1,71 @@ +// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC") +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +#ifndef __TEST_DATA_SOURCE_CLIENT_H +#define __TEST_DATA_SOURCE_CLIENT_H 1 + +#include +#include + +#include + +#include + +namespace isc { +namespace datasrc { +namespace unittest { + +// Here we define utility modules for the convenience of tests that create +// a data source client according to the specified conditions. + +/// \brief Create an SQLite3 data source client from a zone file. +/// +/// This function creates an SQLite3 client for the specified zone containing +/// RRs in the specified zone file. The zone will be created in the given +/// SQLite3 database file. The database file does not have to exist; this +/// function will automatically create a new file for the test; if the given +/// file already exists this function overrides the content (so basically the +/// file must be an ephemeral one only for that test case). +/// +/// The zone file must be formatted so it's accepted by the dns::masterLoad() +/// function. +/// +/// \param zclass The RR class of the zone +/// \param zname The origin name of the zone +/// \param db_file The SQLite3 data base file in which the zone data should be +/// installed. +/// \param zone_file The filename of the zone data in the textual format. +/// \return Newly created \c DataSourceClient using the SQLite3 data source +boost::shared_ptr +createSQLite3Client(dns::RRClass zclass, const dns::Name& zname, + const char* const db_file, const char* const zone_file); + +/// \brief Create an SQLite3 data source client from a stream. +/// +/// This is similar to the other version of the function, but takes an input +/// stream for the zone data. The stream produces strings as the corresponding +/// dns::masterLoad() function expects. +boost::shared_ptr +createSQLite3Client(dns::RRClass zclass, const dns::Name& zname, + const char* const db_file, std::istream& rr_stream); + +} // end of unittest +} // end of datasrc +} // end of isc + +#endif // __TEST_DATA_SOURCE_CLIENT_H + +// Local Variables: +// mode: c++ +// End: diff --git a/src/lib/datasrc/tests/zone_finder_context_unittest.cc b/src/lib/datasrc/tests/zone_finder_context_unittest.cc index cb48e7e781..f4ef70589d 100644 --- a/src/lib/datasrc/tests/zone_finder_context_unittest.cc +++ b/src/lib/datasrc/tests/zone_finder_context_unittest.cc @@ -12,6 +12,8 @@ // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. +#include + #include #include #include @@ -21,6 +23,7 @@ #include #include +#include "test_client.h" #include #include @@ -29,6 +32,8 @@ #include #include +#include +#include #include #include @@ -66,8 +71,6 @@ createInMemoryClient(RRClass zclass, const Name& zname) { return (client); } -// Creator for the SQLite3 client to be tested. addRRset() is a helper -// subroutine. void addRRset(ZoneUpdaterPtr updater, ConstRRsetPtr rrset) { updater->addRRset(*rrset); @@ -78,25 +81,14 @@ createSQLite3Client(RRClass zclass, const Name& zname) { // We always begin with an empty template SQLite3 DB file and install // the zone data from the zone file to ensure both cases have the // same test data. + DataSourceClientPtr client = unittest::createSQLite3Client( + zclass, zname, TEST_DATA_BUILDDIR "/contexttest.sqlite3.copied", + TEST_ZONE_FILE); - const char* const install_cmd = INSTALL_PROG " " TEST_DATA_DIR - "/rwtest.sqlite3 " TEST_DATA_BUILDDIR "/contexttest.sqlite3.copied"; - if (system(install_cmd) != 0) { - isc_throw(isc::Unexpected, - "Error setting up; command failed: " << install_cmd); - } - - shared_ptr accessor( - new SQLite3Accessor(TEST_DATA_BUILDDIR "/contexttest.sqlite3.copied", - zclass.toText())); - shared_ptr client(new DatabaseClient(zclass, accessor)); - - ZoneUpdaterPtr updater = client->getUpdater(zname, true); - masterLoad(TEST_ZONE_FILE, zname, zclass, boost::bind(addRRset, updater, - _1)); // Insert an out-of-zone name to test if it's incorrectly returned. // Note that neither updater nor SQLite3 accessor checks this condition, // so this should succeed. + ZoneUpdaterPtr updater = client->getUpdater(zname, false); stringstream ss("ns.example.com. 3600 IN A 192.0.2.7"); masterLoad(ss, Name::ROOT_NAME(), zclass, boost::bind(addRRset, updater, _1));