MySQL 9.1.0
Source Code Documentation
test_harness_suite.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef TEST_HARNESS_SUITE_H
25#define TEST_HARNESS_SUITE_H
26
27#include <gtest/gtest.h>
32
33/**
34 Implements a Gmock Test suite class to test a component.
35
36 Load the component under test and fetches a reference to it.
37
38 It can also load an optional local test harness component, should the
39 component under test require extra services the minimal chassis doesn't
40 provide. Usually the harness is test oriented and mocks the services so
41 that the test can run.
42
43 @tparam test_service_t The service type of the service to test
44 @tparam component_name The name of the component to load, or nullptr to avoid
45 loading extra components
46 @tparam service_name The name of the service to instantiate, or nullptr to
47 avoid fetching service refs
48 @tparam component_instance The instance of the test harness component to load
49 with the minchassis, or nullptr if none
50
51 @sa @ref PAGE_COMPONENT_MOCK_UNIT_TEST_TOOLS.
52*/
53template <typename test_service_t, char const *component_name,
54 char const *service_name, mysql_component_t *component_instance>
55class TestHarnessSuite_templ : public ::testing::Test {
56 using mysql_dyloader_t = SERVICE_TYPE(dynamic_loader);
58
59 protected:
60 bool component_load(char const *component_to_load, char const *svc_to_load) {
61 EXPECT_TRUE(m_urn.empty());
62
63 EXPECT_EQ(m_dl, nullptr);
64 m_dl = new my_service<mysql_dyloader_t>("dynamic_loader", m_reg);
65 EXPECT_TRUE(m_dl->is_valid());
66
67 m_urn = "file://";
68 if (PLUGIN_DIR != nullptr) {
69 const char *path = PLUGIN_DIR;
70#ifdef WIN32
71 std::string plugin_buf(PLUGIN_DIR);
72 for (auto f : plugin_buf)
73 if (f == '\\') f = '/';
74 path = plugin_buf.c_str();
75#endif
76 m_urn.append(path).append("/");
77 }
78 m_urn.append(component_to_load);
79 const char *urns[] = {m_urn.c_str()};
80 if ((*m_dl)->load(urns, 1)) {
81 m_urn.clear();
82 return true;
83 }
84 assert(!m_test_svc);
85 if (m_dl && svc_to_load != nullptr)
87 else
88 m_test_svc = nullptr;
89 return false;
90 }
92 if (m_test_svc) {
93 delete m_test_svc;
94 m_test_svc = nullptr;
95 }
96 if (m_urn.size() > 0 && m_dl->is_valid()) {
97 const char *urns[] = {m_urn.c_str()};
98 (*m_dl)->unload(urns, 1);
99 m_urn.clear();
100 }
101 }
104 EXPECT_FALSE(minimal_chassis_init(&m_reg, component_instance));
105
106 if (component_name != nullptr &&
107 component_load(component_name, service_name))
108 m_urn.clear();
109 }
110
112 if (component_name != nullptr) component_unload();
113 delete m_dl;
114 EXPECT_FALSE(minimal_chassis_deinit(m_reg, component_instance));
115 }
116
117 void SetUp() override {}
118
119 void TearDown() override {}
120
121 protected:
122 mysql_registry_t *m_reg; /// Reference to the registry
123 my_service<mysql_dyloader_t> *m_dl; /// reference to the dynamic loader
124 my_service<test_service_t> *m_test_svc; /// reference to the test service
125 std::string m_urn; /// the calculated URN of the component
126};
127
128#endif
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Implements a Gmock Test suite class to test a component.
Definition: test_harness_suite.h:55
void TearDown() override
Definition: test_harness_suite.h:119
void SetUp() override
Definition: test_harness_suite.h:117
bool component_load(char const *component_to_load, char const *svc_to_load)
Definition: test_harness_suite.h:60
my_service< mysql_dyloader_t > * m_dl
Reference to the registry.
Definition: test_harness_suite.h:123
TestHarnessSuite_templ()
Definition: test_harness_suite.h:102
my_service< test_service_t > * m_test_svc
reference to the dynamic loader
Definition: test_harness_suite.h:124
std::string m_urn
reference to the test service
Definition: test_harness_suite.h:125
~TestHarnessSuite_templ() override
Definition: test_harness_suite.h:111
mysql_registry_t * m_reg
Definition: test_harness_suite.h:122
void component_unload()
Definition: test_harness_suite.h:91
Wraps my_h_service struct conforming ABI into RAII C++ object with ability to cast to desired service...
Definition: my_service.h:35
bool minimal_chassis_init(mysql_service_registry_t **registry, mysql_component_t *comp_ref)
This is the entry function for minimal_chassis static library, which has to be called by the applicat...
Definition: minimal_chassis.cc:162
bool minimal_chassis_deinit(mysql_service_registry_t *registry, mysql_component_t *comp_ref)
This is the exit function for minimal_chassis static library, which has to be called just before the ...
Definition: minimal_chassis.cc:237
static char * path
Definition: mysqldump.cc:149
#define SERVICE_TYPE(name)
Generates the standard Service type name.
Definition: service.h:76
#define SERVICE_TYPE_NO_CONST(name)
Generates the standard Service type name.
Definition: service.h:71
Carries information on the specific Component, all Service Implementations it provides,...
Definition: dynamic_loader.h:263
Service for managing the list of loaded Components.
Definition: dynamic_loader.h:41
Service for acquiring and releasing references to all registered Service Implementations.
Definition: registry.h:48