Debian Codegraph @

components/libminchassis/gunit_harness/include/test_harness_suite.h

raw symbols SCIP
1 /* Copyright (c) 2023, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, 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>
28 #include "mysql/components/minimal_chassis.h"
29 #include "mysql/components/my_service.h"
30 #include "mysql/components/services/dynamic_loader.h"
31 #include "mysql/components/services/registry.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 @param test_service_t The service type of the service to test
44 @param component_name The name of the component to load, or nullptr to avoid
45 loading extra components
46 @param service_name The name of the service to instantiate, or nullptr to
47 avoid fetching service refs
48 @param 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 */
53 template <typename test_service_t, char const *component_name,
54 char const *service_name, mysql_component_t *component_instance>
55 class TestHarnessSuite_templ : public ::testing::Test {
56 using mysql_dyloader_t = SERVICE_TYPE(dynamic_loader);
57 using mysql_registry_t = SERVICE_TYPE_NO_CONST(registry);
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)
86 m_test_svc = new my_service<test_service_t>(svc_to_load, m_reg);
87 else
88 m_test_svc = nullptr;
89 return false;
90 }
91 void component_unload() {
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 }
102 TestHarnessSuite_templ()
103 : m_reg(nullptr), m_dl(nullptr), m_test_svc(nullptr) {
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
111 ~TestHarnessSuite_templ() override {
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