diff -r 7dd45173f68a -r 285d483db2fb src/server/plugins/postgresql/pgtest.c --- a/src/server/plugins/postgresql/pgtest.c Sat Apr 16 14:36:08 2022 +0200 +++ b/src/server/plugins/postgresql/pgtest.c Sat Apr 16 21:05:29 2022 +0200 @@ -28,8 +28,136 @@ #include -#include +#include "../../util/util.h" +#include "../../test/testutils.h" +#include "../../public/nsapi.h" + +#include +#include + +#include "pgtest.h" +#include "vfs.h" + +#include + + +static char *pg_connstr = "postgresql://localhost/test1"; +static int abort_pg_tests = 0; +static PGconn *test_connection; +static ResourceData resdata; void register_pg_tests(int argc, char **argv, UcxTestSuite *suite) { + test_connection = PQconnectdb(pg_connstr); + if(!test_connection) { + abort_pg_tests = 1; + } + + if(PQstatus(test_connection) != CONNECTION_OK) { + abort_pg_tests = 1; + } + + ucx_test_register(suite, test_pg_conn); + if(!abort_pg_tests) { + resdata.data = test_connection; + + ucx_test_register(suite, test_pg_vfs_open); + ucx_test_register(suite, test_pg_vfs_io); + + PGresult *result = PQexec(test_connection, "BEGIN"); + } } + +UCX_TEST(test_pg_conn) { + char *msg = test_connection ? PQerrorMessage(test_connection) : "no connection"; + + UCX_TEST_BEGIN; + + if(abort_pg_tests) { + int msglen = strlen(msg); + if(msglen > 0 && msg[msglen-1] == '\n') { + msglen--; + } + fprintf(stdout, "%.*s: ", msglen, msg); + UCX_TEST_ASSERT(1 == 0, "skip pg tests"); + } else { + UCX_TEST_ASSERT(1 == 1, "ok"); + } + + UCX_TEST_END; +} + + +static VFS* create_test_pgvfs(Session *sn, Request *rq) { + return pg_vfs_create_from_resourcedata(sn, rq, &resdata); +} + + +UCX_TEST(test_pg_vfs_open) { + Session *sn = testutil_session(); + Request *rq = testutil_request(sn->pool, "PUT", "/"); + rq->vfs = create_test_pgvfs(sn, rq); + VFSContext *vfs = vfs_request_context(sn, rq); + SYS_FILE file; + + UCX_TEST_BEGIN; + + file = vfs_open(vfs, "/test_notfound1", O_RDONLY); + UCX_TEST_ASSERT(!file, "/test_notfound should not exist"); + + file = vfs_open(vfs, "/test_file1", O_RDWR | O_CREAT); + UCX_TEST_ASSERT(file, "cannot create file 1"); + + vfs_close(file); + + UCX_TEST_END; +} + +UCX_TEST(test_pg_vfs_io) { + Session *sn = testutil_session(); + Request *rq = testutil_request(sn->pool, "PUT", "/"); + rq->vfs = create_test_pgvfs(sn, rq); + VFSContext *vfs = vfs_request_context(sn, rq); + SYS_FILE file; + SYS_FILE file2; + + UCX_TEST_BEGIN; + + file = vfs_open(vfs, "/test_f1", O_WRONLY | O_CREAT); + UCX_TEST_ASSERT(file, "cannot open file1"); + + int w = system_fwrite(file, "test1\n", 6); + UCX_TEST_ASSERT(w == 6, "fwrite ret (1)"); + w = system_fwrite(file, "2", 1); + UCX_TEST_ASSERT(w == 1, "fwrite ret (2)"); + + vfs_close(file); + + file = vfs_open(vfs, "/test_f1", O_RDONLY); + file2 = vfs_open(vfs, "/test_f2", O_WRONLY | O_CREAT); + UCX_TEST_ASSERT(file, "cannot open file1"); + UCX_TEST_ASSERT(file2, "cannot open file2"); + + char buf[128]; + int r = system_fread(file, buf, 128); + UCX_TEST_ASSERT(r == 7, "cannot read from file1"); + + w = system_fwrite(file2, buf, r); + UCX_TEST_ASSERT(w == 7, "cannot write to file2"); + + vfs_close(file); + vfs_close(file2); + + file2 = vfs_open(vfs, "/test_f2", O_RDONLY); + + r = system_fread(file, buf, 128); + UCX_TEST_ASSERT(r == 7, "fread ret"); + UCX_TEST_ASSERT(!memcmp(buf, "test1\n2", 7), "wrong buffer content after read"); + + vfs_close(file2); + + + UCX_TEST_END; + +} +