diff --git a/server/core/test/test_utils.h b/server/core/test/test_utils.h index b1c8691fe..9c09bc297 100644 --- a/server/core/test/test_utils.h +++ b/server/core/test/test_utils.h @@ -31,7 +31,18 @@ #include "../internal/poll.hh" #include "../internal/modules.hh" -void init_test_env(char* path) +void preload_module(const char* name, const char* path, const char* type) +{ + std::string old_libdir = get_libdir(); + std::string fullpath = TEST_DIR; + fullpath += "/"; + fullpath += path; + set_libdir(MXS_STRDUP(fullpath.c_str())); + load_module(name, type); + set_libdir(MXS_STRDUP(old_libdir.c_str())); +} + +void init_test_env(char* __attribute((unused)) path = nullptr, uint32_t init_type = QC_INIT_BOTH) { config_get_global_options()->n_threads = 1; @@ -41,21 +52,19 @@ void init_test_env(char* path) } atexit(mxs_log_finish); dcb_global_init(); + std::string old_libdir = get_libdir(); set_libdir(MXS_STRDUP(TEST_DIR "/query_classifier/qc_sqlite/")); qc_setup(NULL, QC_SQL_MODE_DEFAULT, NULL, NULL); - qc_process_init(QC_INIT_BOTH); + qc_process_init(init_type); poll_init(); maxbase::init(); maxscale::RoutingWorker::init(); hkinit(); - set_libdir(MXS_STRDUP(TEST_DIR "/server/modules/protocol/MySQL/mariadbclient/")); - load_module("mariadbclient", MODULE_PROTOCOL); - set_libdir(MXS_STRDUP(TEST_DIR "/server/modules/routing/readconnroute/")); - load_module("readconnroute", MODULE_ROUTER); - set_libdir(MXS_STRDUP(TEST_DIR "/server/modules/routing/readwritesplit/")); - load_module("readwritesplit", MODULE_ROUTER); - set_libdir(MXS_STRDUP(TEST_DIR "/server/modules/authenticator/MySQLAuth/")); - load_module("mysqlauth", MODULE_AUTHENTICATOR); + set_libdir(MXS_STRDUP(old_libdir.c_str())); + + preload_module("mariadbclient", "server/modules/protocol/MySQL/mariadbclient/", MODULE_PROTOCOL); + preload_module("readconnroute", "server/modules/routing/readconnroute/", MODULE_ROUTER); + preload_module("mysqlauth", "/server/modules/authenticator/MySQLAuth/", MODULE_AUTHENTICATOR); } #endif diff --git a/server/modules/filter/cache/test/test_cacheoptions.cc b/server/modules/filter/cache/test/test_cacheoptions.cc index db4a0878c..200a0057e 100644 --- a/server/modules/filter/cache/test/test_cacheoptions.cc +++ b/server/modules/filter/cache/test/test_cacheoptions.cc @@ -20,6 +20,8 @@ #include #include "../cachefilter.hh" +#include "../../../../core/test/test_utils.h" + using namespace std; using maxscale::FilterModule; namespace mock = maxscale::mock; @@ -286,11 +288,14 @@ int test(FilterModule::Instance& filter_instance, const TEST_CASE& tc) { int rv = 0; - mock::ResultSetBackend backend; - mock::RouterSession router_session(&backend); + auto service = service_alloc("service", "readconnroute", nullptr); + auto listener = Listener::create(service, "listener", "mariadbclient", "0.0.0.0", 3306, "", "", nullptr); mock::Client client("bob", "127.0.0.1"); - mock::Session session(&client); + mock::Session session(&client, listener); + mock::ResultSetBackend backend; + mock::RouterSession router_session(&backend, &session); + auto_ptr sFilter_session = filter_instance.newSession(&session); @@ -419,30 +424,14 @@ int main(int argc, char* argv[]) if (rv == 0) { - if (mxs_log_init(NULL, ".", MXS_LOG_TARGET_DEFAULT)) - { - if (qc_setup(NULL, QC_SQL_MODE_DEFAULT, "qc_sqlite", NULL)) - { - if (qc_process_init(QC_INIT_SELF)) - { - rv = run(); + init_test_env(nullptr, QC_INIT_SELF); + preload_module("cache", "server/modules/filter/cache/", MODULE_FILTER); - cout << rv << " failures." << endl; + rv = run(); - qc_process_end(QC_INIT_SELF); - } - else - { - cerr << "error: Could not initialize query classifier." << endl; - } - } - else - { - cerr << "error: Could not setup query classifier." << endl; - } + cout << rv << " failures." << endl; - mxs_log_finish(); - } + qc_process_end(QC_INIT_SELF); } else { diff --git a/server/modules/filter/dbfwfilter/test/test_dbfwfilter.cc b/server/modules/filter/dbfwfilter/test/test_dbfwfilter.cc index 318622b89..dddd41602 100644 --- a/server/modules/filter/dbfwfilter/test/test_dbfwfilter.cc +++ b/server/modules/filter/dbfwfilter/test/test_dbfwfilter.cc @@ -23,6 +23,8 @@ #include #include "tempfile.hh" +#include "../../../../core/test/test_utils.h" + using namespace std; using maxscale::FilterModule; using maxscale::QueryClassifierModule; @@ -757,9 +759,6 @@ int test(FilterModule::Instance& filter_instance, const FW_TEST& t) { int rv = 0; - mock::OkBackend backend; - mock::RouterSession router_session(&backend); - for (size_t i = 0; i < N_MAX_CASES; ++i) { const FW_TEST_CASE& c = t.cases[i]; @@ -770,7 +769,11 @@ int test(FilterModule::Instance& filter_instance, const FW_TEST& t) const char* zHost = c.zHost ? c.zHost : DEFAULT_HOST; mock::Client client(zUser, zHost); - mock::Session session(&client); + auto service = service_alloc("service", "readconnroute", nullptr); + auto listener = Listener::create(service, "listener", "mariadbclient", "0.0.0.0", 3306, "", "", nullptr); + mock::Session session(&client, listener); + mock::OkBackend backend; + mock::RouterSession router_session(&backend, &session); auto_ptr sFilter_session = filter_instance.newSession(&session); @@ -991,30 +994,14 @@ int main(int argc, char* argv[]) if (rv == 0) { - if (mxs_log_init(NULL, ".", MXS_LOG_TARGET_STDOUT)) - { - if (qc_setup(NULL, QC_SQL_MODE_DEFAULT, "qc_sqlite", NULL)) - { - if (qc_process_init(QC_INIT_SELF)) - { - rv = run(); + init_test_env(nullptr, QC_INIT_SELF); + preload_module("dbfwfilter", "server/modules/filter/dbfwfilter/", MODULE_FILTER); - cout << rv << " failures." << endl; + rv = run(); - qc_process_end(QC_INIT_SELF); - } - else - { - cerr << "error: Could not initialize query classifier." << endl; - } - } - else - { - cerr << "error: Could not setup query classifier." << endl; - } + cout << rv << " failures." << endl; - mxs_log_finish(); - } + qc_process_end(QC_INIT_SELF); } else { diff --git a/server/modules/filter/test/maxscale/mock/routersession.hh b/server/modules/filter/test/maxscale/mock/routersession.hh index 49b0e1381..7087c84d0 100644 --- a/server/modules/filter/test/maxscale/mock/routersession.hh +++ b/server/modules/filter/test/maxscale/mock/routersession.hh @@ -16,8 +16,10 @@ #include #include #include +#include #include "../filtermodule.hh" #include "mock.hh" +#include "session.hh" namespace maxscale { @@ -42,7 +44,7 @@ public: * * @param pBackend The backend associated with the router. */ - RouterSession(Backend* pBackend); + RouterSession(Backend* pBackend, maxscale::mock::Session* session); ~RouterSession(); /** @@ -90,6 +92,11 @@ public: */ void discard_all_responses(); + MXS_SESSION* session() const + { + return static_cast(m_pSession); + } + private: int32_t routeQuery(MXS_ROUTER* pInstance, GWBUF* pStatement); @@ -99,6 +106,8 @@ private: MXS_ROUTER m_instance; Backend* m_pBackend; FilterModule::Session* m_pUpstream_filter_session; + + maxscale::mock::Session* m_pSession; }; } } diff --git a/server/modules/filter/test/maxscale/mock/session.hh b/server/modules/filter/test/maxscale/mock/session.hh index cf1c5a9d3..3f6cda073 100644 --- a/server/modules/filter/test/maxscale/mock/session.hh +++ b/server/modules/filter/test/maxscale/mock/session.hh @@ -14,6 +14,7 @@ #include "mock.hh" #include +#include #include "client.hh" #include "../../../core/internal/session.hh" @@ -42,7 +43,7 @@ public: * @param pClient The client of the session. Must remain valid for * the lifetime of the Session. */ - Session(Client* pClient); + Session(Client* pClient, const SListener& listener); ~Session(); Client& client() const; diff --git a/server/modules/filter/test/mock_backend.cc b/server/modules/filter/test/mock_backend.cc index eb949d5ca..fe009a591 100644 --- a/server/modules/filter/test/mock_backend.cc +++ b/server/modules/filter/test/mock_backend.cc @@ -173,8 +173,8 @@ namespace class ResultSetDCB : public DCB { public: - ResultSetDCB() - : DCB(DCB_ROLE_CLIENT_HANDLER, nullptr) + ResultSetDCB(MXS_SESSION* session) + : DCB(DCB_ROLE_CLIENT_HANDLER, session) { DCB* pDcb = this; @@ -219,7 +219,7 @@ void ResultSetBackend::handle_statement(RouterSession* pSession, GWBUF* pStateme { std::unique_ptr set = ResultSet::create({"a"}); set->add_row({std::to_string(++m_counter)}); - ResultSetDCB dcb; + ResultSetDCB dcb(pSession->session()); set->write(&dcb); enqueue_response(pSession, dcb.create_response()); diff --git a/server/modules/filter/test/mock_dcb.cc b/server/modules/filter/test/mock_dcb.cc index e93c88985..7b2d86ad5 100644 --- a/server/modules/filter/test/mock_dcb.cc +++ b/server/modules/filter/test/mock_dcb.cc @@ -34,7 +34,7 @@ Dcb::Dcb(MXS_SESSION* pSession, const char* zUser, const char* zHost, Handler* pHandler) - : DCB(DCB_ROLE_CLIENT_HANDLER, nullptr) + : DCB(DCB_ROLE_CLIENT_HANDLER, pSession) , m_user(zUser) , m_host(zHost) , m_pHandler(pHandler) @@ -43,8 +43,8 @@ Dcb::Dcb(MXS_SESSION* pSession, initialize_dcb(this); pDcb->session = pSession; - pDcb->remote = const_cast(zHost); - pDcb->user = const_cast(zUser); + pDcb->remote = MXS_STRDUP(zHost); + pDcb->user = MXS_STRDUP(zUser); pDcb->func.write = &Dcb::write; } diff --git a/server/modules/filter/test/mock_routersession.cc b/server/modules/filter/test/mock_routersession.cc index 3ae211249..2eb8b32d5 100644 --- a/server/modules/filter/test/mock_routersession.cc +++ b/server/modules/filter/test/mock_routersession.cc @@ -20,8 +20,9 @@ namespace maxscale namespace mock { -RouterSession::RouterSession(Backend* pBackend) +RouterSession::RouterSession(Backend* pBackend, maxscale::mock::Session* session) : m_pBackend(pBackend) + , m_pSession(session) { memset(&m_instance, 0, sizeof(m_instance)); } diff --git a/server/modules/filter/test/mock_session.cc b/server/modules/filter/test/mock_session.cc index 35732009c..957f8d909 100644 --- a/server/modules/filter/test/mock_session.cc +++ b/server/modules/filter/test/mock_session.cc @@ -13,20 +13,14 @@ #include "maxscale/mock/session.hh" -namespace -{ - -SERVICE dummy_service; -} - namespace maxscale { namespace mock { -Session::Session(Client* pClient) - : mxs::Session(nullptr) +Session::Session(Client* pClient, const SListener& listener) + : mxs::Session(listener) , m_client(*pClient) , m_client_dcb(this, pClient->user(), pClient->host(), pClient) { @@ -47,6 +41,8 @@ Session::Session(Client* pClient) Session::~Session() { + // This prevents the protocol module from freeing the data + m_client_dcb.data = nullptr; } Client& Session::client() const