Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/***********************************************************************************************************************************
Test Server Command
***********************************************************************************************************************************/
#include "command/exit.h"
#include "storage/posix/storage.h"
#include "storage/remote/storage.h"
 
#include "harness/config.h"
#include "harness/fork.h"
#include "harness/server.h"
#include "harness/storage.h"
#include "harness/systemd.h"
 
/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
static void
testRun(void)
{
    FUNCTION_HARNESS_VOID();
 
    Storage *storageTest = storagePosixNewP(TEST_PATH_STR, .write = true);
 
    harnessLogLevelSet(logLevelDetail);
 
    // *****************************************************************************************************************************
    if (testBegin("cmdServer()"))
    {
        TEST_TITLE("server");
 
        HRN_FORK_BEGIN(.timeout = 15000)
        {
            const unsigned int testPort = hrnServerPortNext();
 
            HRN_FORK_CHILD_BEGIN(.prefix = "client repo")
            {
                StringList *argList = strLstNew();
                hrnCfgArgRawZ(argList, cfgOptPgPath, "/BOGUS");
                hrnCfgArgRaw(argList, cfgOptRepoHost, hrnServerHost());
                hrnCfgArgRawZ(argList, cfgOptRepoHostConfig, TEST_PATH "/pgbackrest.conf");
                hrnCfgArgRawZ(argList, cfgOptRepoHostType, "tls");
#if !TEST_IN_CONTAINER
                hrnCfgArgRawZ(argList, cfgOptRepoHostCaFile, HRN_SERVER_CA);
#endif
                hrnCfgArgRawZ(argList, cfgOptRepoHostCertFile, HRN_SERVER_CLIENT_CERT);
                hrnCfgArgRawZ(argList, cfgOptRepoHostKeyFile, HRN_SERVER_CLIENT_KEY);
                hrnCfgArgRawFmt(argList, cfgOptRepoHostPort, "%u", testPort);
                hrnCfgArgRawZ(argList, cfgOptStanza, "db");
                HRN_CFG_LOAD(cfgCmdArchiveGet, argList);
 
                // Client 1
                ProtocolClient *clientRemote = protocolRemoteGet(protocolStorageTypeRepo, 0, true);
                const Storage *storageRemote = NULL;
                TEST_ASSIGN(
                    storageRemote,
                    storageRemoteNew(
                        STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, 0, NULL,
                        protocolRemoteGet(protocolStorageTypeRepo, 0, true), cfgOptionUInt(cfgOptCompressLevelNetwork)),
                    "new storage 1");
 
                HRN_STORAGE_PUT_Z(storageRemote, "client1.txt", "CLIENT1");
 
                TEST_RESULT_VOID(protocolHelperFree(clientRemote), "free client 1");
 
                // Client 2
                clientRemote = protocolRemoteGet(protocolStorageTypeRepo, 0, true);
 
                TEST_ASSIGN(
                    storageRemote,
                    storageRemoteNew(
                        STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, 0, NULL,
                        protocolRemoteGet(protocolStorageTypeRepo, 0, true), cfgOptionUInt(cfgOptCompressLevelNetwork)),
                    "new storage 2");
 
                HRN_STORAGE_PUT_Z(storageRemote, "client2.txt", "CLIENT2");
 
                TEST_RESULT_VOID(protocolHelperFree(clientRemote), "free client 2");
 
                // Notify parent on exit
                HRN_FORK_CHILD_NOTIFY_PUT();
            }
            HRN_FORK_CHILD_END();
 
            HRN_FORK_CHILD_BEGIN(.prefix = "client pg")
            {
                StringList *argList = strLstNew();
                hrnCfgArgRawZ(argList, cfgOptRepoPath, "/BOGUS");
                hrnCfgArgRaw(argList, cfgOptPgHost, hrnServerHost());
                hrnCfgArgRawZ(argList, cfgOptPgPath, TEST_PATH "/pg");
                hrnCfgArgRawZ(argList, cfgOptPgHostType, "tls");
#if !TEST_IN_CONTAINER
                hrnCfgArgRawZ(argList, cfgOptPgHostCaFile, HRN_SERVER_CA);
#endif
                hrnCfgArgRawZ(argList, cfgOptPgHostCertFile, HRN_SERVER_CLIENT_CERT);
                hrnCfgArgRawZ(argList, cfgOptPgHostKeyFile, HRN_SERVER_CLIENT_KEY);
                hrnCfgArgRawFmt(argList, cfgOptPgHostPort, "%u", testPort);
                hrnCfgArgRawZ(argList, cfgOptStanza, "db");
                hrnCfgArgRawZ(argList, cfgOptProcess, "1");
                HRN_CFG_LOAD(cfgCmdBackup, argList, .role = cfgCmdRoleLocal);
 
                // Client 3
                ProtocolClient *const clientRemote = protocolRemoteGet(protocolStorageTypePg, 0, true);
                const Storage *storageRemote = NULL;
 
                TEST_ASSIGN(
                    storageRemote,
                    storageRemoteNew(
                        STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, 0, NULL,
                        protocolRemoteGet(protocolStorageTypePg, 0, true), cfgOptionUInt(cfgOptCompressLevelNetwork)),
                    "new storage 3");
 
                HRN_STORAGE_PUT_Z(storageRemote, "client3.txt", "CLIENT3");
 
                TEST_RESULT_VOID(protocolHelperFree(clientRemote), "free client 3");
 
                // Notify parent on exit
                HRN_FORK_CHILD_NOTIFY_PUT();
            }
            HRN_FORK_CHILD_END();
 
            HRN_FORK_PARENT_BEGIN(.prefix = "client control")
            {
                HRN_FORK_BEGIN(.timeout = 15000)
                {
                    HRN_FORK_CHILD_BEGIN(.prefix = "server")
                    {
                        // Write a config file to demonstrate that options are loaded and reloaded
                        HRN_STORAGE_PUT_Z(
                            storageTest,
                            "pgbackrest.conf",
                            "[global]\n"
                            CFGOPT_TLS_SERVER_ADDRESS "=127.0.0.1\n"
                            CFGOPT_TLS_SERVER_CA_FILE "=" HRN_SERVER_CA "\n"
                            CFGOPT_TLS_SERVER_CERT_FILE "=" HRN_SERVER_CERT "\n"
                            CFGOPT_TLS_SERVER_KEY_FILE "=" HRN_SERVER_KEY "\n"
                            CFGOPT_TLS_SERVER_AUTH "=pgbackrest-client=bogus1,db,bogus2\n"
                            "repo1-path=" TEST_PATH "/repo\n");
 
                        StringList *argList = strLstNew();
                        hrnCfgArgRawZ(argList, cfgOptConfig, TEST_PATH "/pgbackrest.conf");
                        hrnCfgArgRawFmt(argList, cfgOptTlsServerPort, "%u", testPort);
                        hrnCfgArgRawZ(argList, cfgOptLogLevelStderr, "off");
                        HRN_CFG_LOAD(cfgCmdServer, argList);
 
                        // Init exit signal handlers
                        exitInit();
 
                        // No log testing needed
                        harnessLogLevelSet(logLevelError);
 
                        // Add a fake pid to ensure SIGTERM is sent to unterminated children
                        cmdServerInit();
 
                        int fakePid = INT_MAX;
                        lstAdd(serverLocal.processList, &fakePid);
 
                        // Get pid of this process to identify child process later
                        pid_t pid = getpid();
 
                        // Add parameters to arg list required for a reload
                        strLstInsert(argList, 0, cfgBin());
                        strLstAddZ(argList, CFGCMD_SERVER);
 
                        TEST_RESULT_VOID(cmdServer(strLstSize(argList), strLstPtr(argList)), "server");
 
                        // If this is a child process then exit immediately. Call exitSafe() before notifying the parent so a
                        // signal sent in response to the notification cannot arrive before the exit in progress flag is set.
                        if (pid != getpid())
                        {
                            const int result = exitSafe(0, false);
 
                            HRN_FORK_CHILD_NOTIFY_PUT();
                            exit(result);
                        }
 
                        // Check that sd_notify calls were made
#ifdef HAVE_LIBSYSTEMD
                        hrnSystemDCheck();
#endif
                    }
                    HRN_FORK_CHILD_END();
 
                    HRN_FORK_PARENT_BEGIN(.prefix = "server control")
                    {
                        // Wait for forked server processes to exit
                        HRN_FORK_PARENT_NOTIFY_GET(0);
                        kill(HRN_FORK_PROCESS_ID(0), SIGHUP);
                        HRN_FORK_PARENT_NOTIFY_GET(0);
                        HRN_FORK_PARENT_NOTIFY_GET(0);
 
                        // Send term to server processes
                        kill(HRN_FORK_PROCESS_ID(0), SIGTERM);
                    }
                    HRN_FORK_PARENT_END();
                }
                HRN_FORK_END();
 
                // Wait for both child processes to exit
                HRN_FORK_PARENT_NOTIFY_GET(0);
                HRN_FORK_PARENT_NOTIFY_GET(1);
 
                // Check that files written by child processes are present
                TEST_STORAGE_GET(storageTest, "repo/client1.txt", "CLIENT1");
                TEST_STORAGE_GET(storageTest, "repo/client2.txt", "CLIENT2");
                TEST_STORAGE_GET(storageTest, "pg/client3.txt", "CLIENT3");
            }
            HRN_FORK_PARENT_END();
        }
        HRN_FORK_END();
    }
 
    // *****************************************************************************************************************************
    if (testBegin("cmdServerPing()"))
    {
        TEST_TITLE("error on extra parameters");
 
        StringList *argList = strLstNew();
        strLstAddZ(argList, "host");
        strLstAddZ(argList, "bogus");
        HRN_CFG_LOAD(cfgCmdServerPing, argList);
 
        TEST_ERROR(cmdServerPing(), ParamInvalidError, "extra parameters found");
 
        HRN_FORK_BEGIN(.timeout = 15000)
        {
            const unsigned int testPort = hrnServerPortNext();
 
            HRN_FORK_CHILD_BEGIN(.prefix = "client")
            {
                TEST_TITLE("ping localhost");
 
                argList = strLstNew();
                hrnCfgArgRawFmt(argList, cfgOptTlsServerPort, "%u", testPort);
                HRN_CFG_LOAD(cfgCmdServerPing, argList);
 
                TEST_RESULT_VOID(cmdServerPing(), "ping");
 
                // -----------------------------------------------------------------------------------------------------------------
                TEST_TITLE("ping 127.0.0.1");
 
                argList = strLstNew();
                hrnCfgArgRawFmt(argList, cfgOptTlsServerPort, "%u", testPort);
                strLstAddZ(argList, "127.0.0.1");
                HRN_CFG_LOAD(cfgCmdServerPing, argList);
 
                TEST_RESULT_VOID(cmdServerPing(), "ping");
 
                // Notify parent on exit
                HRN_FORK_CHILD_NOTIFY_PUT();
            }
            HRN_FORK_CHILD_END();
 
            HRN_FORK_PARENT_BEGIN(.prefix = "client control")
            {
                HRN_FORK_BEGIN(.timeout = 15000)
                {
                    HRN_FORK_CHILD_BEGIN(.prefix = "server")
                    {
                        StringList *argList = strLstNew();
                        hrnCfgArgRawZ(argList, cfgOptTlsServerAddress, "127.0.0.1");
                        hrnCfgArgRawZ(argList, cfgOptTlsServerCaFile, HRN_SERVER_CA);
                        hrnCfgArgRawZ(argList, cfgOptTlsServerCertFile, HRN_SERVER_CERT);
                        hrnCfgArgRawZ(argList, cfgOptTlsServerKeyFile, HRN_SERVER_KEY);
                        hrnCfgArgRawZ(argList, cfgOptTlsServerAuth, "bogus=*");
                        hrnCfgArgRawFmt(argList, cfgOptTlsServerPort, "%u", testPort);
                        HRN_CFG_LOAD(cfgCmdServer, argList);
 
                        // Init exit signal handlers
                        exitInit();
 
                        // No log testing needed
                        harnessLogLevelSet(logLevelError);
 
                        // Get pid of this process to identify child process later
                        pid_t pid = getpid();
 
                        TEST_RESULT_VOID(cmdServer(strLstSize(argList), strLstPtr(argList)), "server");
 
                        // If this is a child process then exit immediately. Call exitSafe() before notifying the parent so a
                        // signal sent in response to the notification cannot arrive before the exit in progress flag is set.
                        if (pid != getpid())
                        {
                            const int result = exitSafe(0, false);
 
                            HRN_FORK_CHILD_NOTIFY_PUT();
                            exit(result);
                        }
 
                        // Check that sd_notify calls were made
#ifdef HAVE_LIBSYSTEMD
                        hrnSystemDCheck();
#endif
                    }
                    HRN_FORK_CHILD_END();
 
                    HRN_FORK_PARENT_BEGIN(.prefix = "server control")
                    {
                        // Wait for forked child processes to exit
                        HRN_FORK_PARENT_NOTIFY_GET(0);
                        HRN_FORK_PARENT_NOTIFY_GET(0);
 
                        // Send term to child processes
                        kill(HRN_FORK_PROCESS_ID(0), SIGTERM);
                    }
                    HRN_FORK_PARENT_END();
                }
                HRN_FORK_END();
 
                // Wait for child process to exit
                HRN_FORK_PARENT_NOTIFY_GET(0);
            }
            HRN_FORK_PARENT_END();
        }
        HRN_FORK_END();
    }
 
    FUNCTION_HARNESS_RETURN_VOID();
}