postgres-integration-tests #89
13 changed files with 203 additions and 32 deletions
2
.idea/pgLab.iml
generated
2
.idea/pgLab.iml
generated
|
|
@ -1,2 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<module classpath="CIDR" type="CPP_MODULE" version="4" />
|
|
||||||
|
|
@ -205,26 +205,9 @@ void DatabasesTableModel::StartLoadDatabaseSizes(std::map<Oid, int> oidIndex)
|
||||||
|
|
||||||
DatabasesTableModel::DatabaseSizes DatabasesTableModel::QueryDatabaseSizes()
|
DatabasesTableModel::DatabaseSizes DatabasesTableModel::QueryDatabaseSizes()
|
||||||
{
|
{
|
||||||
std::string q =
|
|
||||||
"SELECT pg_database.oid, "
|
|
||||||
" case when has_database_privilege(current_role, pg_database.oid, \n"
|
|
||||||
" 'connect') then pg_database_size(pg_database.oid) else -1 end \n"
|
|
||||||
"FROM pg_database";
|
|
||||||
|
|
||||||
Pgsql::Connection connection;
|
Pgsql::Connection connection;
|
||||||
connection.connect(openDatabase->config().connectionString());
|
connection.connect(openDatabase->config().connectionString());
|
||||||
Pgsql::Result sizesResult = connection.query(q.c_str());
|
return loadDatabaseSizes(connection);
|
||||||
|
|
||||||
DatabaseSizes result;
|
|
||||||
result.reserve(sizesResult.rows());
|
|
||||||
for (auto& row : sizesResult)
|
|
||||||
{
|
|
||||||
DatabaseSize ds;
|
|
||||||
ds.oid = row.get(0);
|
|
||||||
ds.totalBytes = row.get(1);
|
|
||||||
result.push_back(ds);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DatabasesTableModel::PopulateSizes(std::map<Oid, int> oidIndex, DatabaseSizes sizes)
|
void DatabasesTableModel::PopulateSizes(std::map<Oid, int> oidIndex, DatabaseSizes sizes)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "BaseTableModel.h"
|
#include "BaseTableModel.h"
|
||||||
#include "catalog/PgDatabase.h"
|
#include "catalog/PgDatabase.h"
|
||||||
|
#include "catalog/PgDatabaseSizes.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
class OpenDatabase;
|
class OpenDatabase;
|
||||||
|
|
@ -45,17 +46,8 @@ public:
|
||||||
protected:
|
protected:
|
||||||
virtual QVariant getDataMeaning(const QModelIndex &index) const override;
|
virtual QVariant getDataMeaning(const QModelIndex &index) const override;
|
||||||
private:
|
private:
|
||||||
class DatabaseSize {
|
using DatabaseSize = PgDatabaseSize;
|
||||||
public:
|
using DatabaseSizes = PgDatabaseSizes;
|
||||||
Oid oid;
|
|
||||||
int64_t totalBytes;
|
|
||||||
|
|
||||||
DatabaseSize()
|
|
||||||
: oid(InvalidOid)
|
|
||||||
, totalBytes(-1)
|
|
||||||
{}
|
|
||||||
};
|
|
||||||
using DatabaseSizes = std::vector<DatabaseSize>;
|
|
||||||
|
|
||||||
class Database {
|
class Database {
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ add_library(pglablib STATIC
|
||||||
catalog/PgIndexContainer.cpp
|
catalog/PgIndexContainer.cpp
|
||||||
catalog/PgConstraint.cpp
|
catalog/PgConstraint.cpp
|
||||||
catalog/PgConstraintContainer.cpp
|
catalog/PgConstraintContainer.cpp
|
||||||
|
catalog/PgDatabaseSizes.cpp
|
||||||
ParamListJson.cpp
|
ParamListJson.cpp
|
||||||
ParamListModel.cpp
|
ParamListModel.cpp
|
||||||
sqlite/SQLiteConnection.cpp
|
sqlite/SQLiteConnection.cpp
|
||||||
|
|
@ -95,6 +96,7 @@ add_library(pglablib STATIC
|
||||||
catalog/PgIndexContainer.h
|
catalog/PgIndexContainer.h
|
||||||
catalog/PgConstraint.h
|
catalog/PgConstraint.h
|
||||||
catalog/PgConstraintContainer.h
|
catalog/PgConstraintContainer.h
|
||||||
|
catalog/PgDatabaseSizes.h
|
||||||
ParamListJson.h
|
ParamListJson.h
|
||||||
ParamListModel.h
|
ParamListModel.h
|
||||||
sqlite/SQLiteConnection.h
|
sqlite/SQLiteConnection.h
|
||||||
|
|
|
||||||
25
pglablib/catalog/PgDatabaseSizes.cpp
Normal file
25
pglablib/catalog/PgDatabaseSizes.cpp
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#include "PgDatabaseSizes.h"
|
||||||
|
|
||||||
|
#include "Pgsql_Result.h"
|
||||||
|
|
||||||
|
PgDatabaseSizes loadDatabaseSizes(Pgsql::Connection &connection)
|
||||||
|
{
|
||||||
|
std::string q =
|
||||||
|
"SELECT pg_database.oid, "
|
||||||
|
" case when has_database_privilege(current_role, pg_database.oid, \n"
|
||||||
|
" 'connect') then pg_database_size(pg_database.oid) else -1 end \n"
|
||||||
|
"FROM pg_database";
|
||||||
|
|
||||||
|
Pgsql::Result sizesResult = connection.query(q.c_str());
|
||||||
|
|
||||||
|
PgDatabaseSizes result;
|
||||||
|
result.reserve(sizesResult.rows());
|
||||||
|
for (auto& row : sizesResult)
|
||||||
|
{
|
||||||
|
PgDatabaseSize ds;
|
||||||
|
ds.oid = row.get(0);
|
||||||
|
ds.totalBytes = row.get(1);
|
||||||
|
result.push_back(ds);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
25
pglablib/catalog/PgDatabaseSizes.h
Normal file
25
pglablib/catalog/PgDatabaseSizes.h
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef PGDATABASESIZES_H
|
||||||
|
#define PGDATABASESIZES_H
|
||||||
|
|
||||||
|
#include "Pgsql_Connection.h"
|
||||||
|
#include <libpq-fe.h>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class PgDatabaseSize {
|
||||||
|
public:
|
||||||
|
Oid oid;
|
||||||
|
int64_t totalBytes;
|
||||||
|
|
||||||
|
PgDatabaseSize()
|
||||||
|
: oid(InvalidOid)
|
||||||
|
, totalBytes(-1)
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
using PgDatabaseSizes = std::vector<PgDatabaseSize>;
|
||||||
|
|
||||||
|
/// Returns the size of every database. The size is -1 when the current role has no CONNECT privilege.
|
||||||
|
PgDatabaseSizes loadDatabaseSizes(Pgsql::Connection &connection);
|
||||||
|
|
||||||
|
#endif // PGDATABASESIZES_H
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
add_executable(IntegrationTests
|
add_executable(IntegrationTests
|
||||||
main.cpp
|
main.cpp
|
||||||
tst_Smoke.cpp
|
tst_Smoke.cpp
|
||||||
|
tst_CatalogLoad.cpp
|
||||||
|
tst_DatabaseSizes.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(IntegrationTests PRIVATE
|
target_link_libraries(IntegrationTests PRIVATE
|
||||||
pgintegration
|
pgintegration
|
||||||
|
pglablib
|
||||||
GTest::gtest
|
GTest::gtest
|
||||||
Qt6::Core
|
Qt6::Core
|
||||||
)
|
)
|
||||||
|
|
|
||||||
36
tests/integration/tst_CatalogLoad.cpp
Normal file
36
tests/integration/tst_CatalogLoad.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
#include "DbTest.h"
|
||||||
|
#include "PgTestEnvironment.h"
|
||||||
|
#include "catalog/PgClassContainer.h"
|
||||||
|
#include "catalog/PgDatabaseCatalog.h"
|
||||||
|
#include "Pgsql_Result.h"
|
||||||
|
|
||||||
|
using namespace pgtest;
|
||||||
|
|
||||||
|
class CatalogLoad : public DbTest {};
|
||||||
|
|
||||||
|
TEST_F(CatalogLoad, SuperuserLoadsCatalog)
|
||||||
|
{
|
||||||
|
PgDatabaseCatalog catalog;
|
||||||
|
catalog.loadAll(conn(), {});
|
||||||
|
|
||||||
|
EXPECT_EQ(catalog.serverVersion(), serverVersionNum());
|
||||||
|
EXPECT_EQ(catalog.getDBName(), dbName());
|
||||||
|
ASSERT_NE(catalog.classes(), nullptr);
|
||||||
|
EXPECT_NE(catalog.classes()->getByObjectNsAndName("public", "fixture_item"), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(CatalogLoad, PlainRoleLoadsCatalog)
|
||||||
|
{
|
||||||
|
auto plain = plainConn();
|
||||||
|
Pgsql::Result r = plain->query("SELECT rolsuper FROM pg_roles WHERE rolname = current_user");
|
||||||
|
ASSERT_EQ(r.rows(), 1);
|
||||||
|
EXPECT_EQ(QString::fromUtf8(r.val(0, 0)), QString("f"));
|
||||||
|
|
||||||
|
PgDatabaseCatalog catalog;
|
||||||
|
catalog.loadAll(*plain, {});
|
||||||
|
|
||||||
|
EXPECT_EQ(catalog.serverVersion(), serverVersionNum());
|
||||||
|
EXPECT_EQ(catalog.getDBName(), dbName());
|
||||||
|
ASSERT_NE(catalog.classes(), nullptr);
|
||||||
|
EXPECT_NE(catalog.classes()->getByObjectNsAndName("public", "fixture_item"), nullptr);
|
||||||
|
}
|
||||||
68
tests/integration/tst_DatabaseSizes.cpp
Normal file
68
tests/integration/tst_DatabaseSizes.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
#include "DbTest.h"
|
||||||
|
#include "PgTestEnvironment.h"
|
||||||
|
#include "catalog/PgDatabaseSizes.h"
|
||||||
|
#include "Pgsql_Result.h"
|
||||||
|
|
||||||
|
using namespace pgtest;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
Oid oidOfDatabase(Pgsql::Connection &c, const QString &db)
|
||||||
|
{
|
||||||
|
Pgsql::Result r = c.query(QString("SELECT oid FROM pg_database WHERE datname = '%1'").arg(db));
|
||||||
|
if (!r || r.rows() != 1)
|
||||||
|
return InvalidOid;
|
||||||
|
return QString::fromUtf8(r.val(0, 0)).toUInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
const PgDatabaseSize *findSize(const PgDatabaseSizes &sizes, Oid oid)
|
||||||
|
{
|
||||||
|
for (const auto &s : sizes) {
|
||||||
|
if (s.oid == oid)
|
||||||
|
return &s;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
class DatabaseSizes : public DbTest {};
|
||||||
|
|
||||||
|
TEST_F(DatabaseSizes, ConnectableDatabaseReturnsSize)
|
||||||
|
{
|
||||||
|
auto plain = plainConn();
|
||||||
|
Oid oid = oidOfDatabase(*plain, dbName());
|
||||||
|
ASSERT_NE(oid, InvalidOid);
|
||||||
|
|
||||||
|
PgDatabaseSizes sizes = loadDatabaseSizes(*plain);
|
||||||
|
const PgDatabaseSize *s = findSize(sizes, oid);
|
||||||
|
ASSERT_NE(s, nullptr);
|
||||||
|
EXPECT_GE(s->totalBytes, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DatabaseSizes, RevokedConnectReturnsMinusOne)
|
||||||
|
{
|
||||||
|
Oid oid = oidOfDatabase(conn(), dbName());
|
||||||
|
ASSERT_NE(oid, InvalidOid);
|
||||||
|
conn().query(QString("REVOKE CONNECT ON DATABASE %1 FROM PUBLIC").arg(quoteIdent(dbName())));
|
||||||
|
|
||||||
|
auto plain = std::make_unique<Pgsql::Connection>();
|
||||||
|
plain->connect(urlForDbAsPlainRole("postgres"));
|
||||||
|
|
||||||
|
PgDatabaseSizes sizes = loadDatabaseSizes(*plain);
|
||||||
|
const PgDatabaseSize *s = findSize(sizes, oid);
|
||||||
|
ASSERT_NE(s, nullptr);
|
||||||
|
EXPECT_EQ(s->totalBytes, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DatabaseSizes, SuperuserSeesAllDatabases)
|
||||||
|
{
|
||||||
|
PgDatabaseSizes sizes = loadDatabaseSizes(conn());
|
||||||
|
|
||||||
|
Pgsql::Result r = conn().query("SELECT count(*) FROM pg_database");
|
||||||
|
ASSERT_EQ(r.rows(), 1);
|
||||||
|
EXPECT_EQ(static_cast<size_t>(QString::fromUtf8(r.val(0, 0)).toULongLong()), sizes.size());
|
||||||
|
|
||||||
|
for (const auto &s : sizes)
|
||||||
|
EXPECT_GE(s.totalBytes, 0) << "database oid " << s.oid;
|
||||||
|
}
|
||||||
|
|
@ -15,6 +15,13 @@ QString DbTest::dbUrl() const
|
||||||
return urlForDb(dbName_);
|
return urlForDb(dbName_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<Pgsql::Connection> DbTest::plainConn() const
|
||||||
|
{
|
||||||
|
auto c = std::make_unique<Pgsql::Connection>();
|
||||||
|
c->connect(urlForDbAsPlainRole(dbName_));
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
void DbTest::SetUp()
|
void DbTest::SetUp()
|
||||||
{
|
{
|
||||||
if (!available())
|
if (!available())
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ protected:
|
||||||
Pgsql::Connection& conn() { return *conn_; }
|
Pgsql::Connection& conn() { return *conn_; }
|
||||||
QString dbName() const { return dbName_; }
|
QString dbName() const { return dbName_; }
|
||||||
QString dbUrl() const;
|
QString dbUrl() const;
|
||||||
|
/// Opens a new connection to this test's database as the plain (non-superuser) role.
|
||||||
|
std::unique_ptr<Pgsql::Connection> plainConn() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString dbName_;
|
QString dbName_;
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@ namespace {
|
||||||
bool g_available = false;
|
bool g_available = false;
|
||||||
int g_versionNum = 0;
|
int g_versionNum = 0;
|
||||||
|
|
||||||
|
const char *kPlainRolePassword = "pglab_plain_test";
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
std::optional<QString> baseUrl()
|
std::optional<QString> baseUrl()
|
||||||
|
|
@ -56,6 +58,23 @@ QString templateDbName()
|
||||||
return QString("tmpl_%1").arg(processId());
|
return QString("tmpl_%1").arg(processId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString plainRoleName()
|
||||||
|
{
|
||||||
|
return QString("tst_%1").arg(processId());
|
||||||
|
}
|
||||||
|
|
||||||
|
QString urlForDbAsPlainRole(const QString &db)
|
||||||
|
{
|
||||||
|
auto base = baseUrl();
|
||||||
|
if (!base)
|
||||||
|
throw std::runtime_error("TEST_DATABASE_URL is not set");
|
||||||
|
QUrl url(*base);
|
||||||
|
url.setPath("/" + db);
|
||||||
|
url.setUserName(plainRoleName());
|
||||||
|
url.setPassword(QString::fromUtf8(kPlainRolePassword));
|
||||||
|
return url.toString();
|
||||||
|
}
|
||||||
|
|
||||||
QString quoteIdent(const QString &name)
|
QString quoteIdent(const QString &name)
|
||||||
{
|
{
|
||||||
QString r = name;
|
QString r = name;
|
||||||
|
|
@ -82,6 +101,9 @@ void PgEnv::SetUp()
|
||||||
g_versionNum = QString::fromUtf8(r.val(0, 0)).toInt();
|
g_versionNum = QString::fromUtf8(r.val(0, 0)).toInt();
|
||||||
admin.query(QString("DROP DATABASE IF EXISTS %1 WITH (FORCE)").arg(quoteIdent(tmpl)));
|
admin.query(QString("DROP DATABASE IF EXISTS %1 WITH (FORCE)").arg(quoteIdent(tmpl)));
|
||||||
admin.query(QString("CREATE DATABASE %1").arg(quoteIdent(tmpl)));
|
admin.query(QString("CREATE DATABASE %1").arg(quoteIdent(tmpl)));
|
||||||
|
admin.query(QString("DROP ROLE IF EXISTS %1").arg(quoteIdent(plainRoleName())));
|
||||||
|
admin.query(QString("CREATE ROLE %1 LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE PASSWORD '%2'")
|
||||||
|
.arg(quoteIdent(plainRoleName()), QString::fromUtf8(kPlainRolePassword)));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
QFile f(fixtureDir_ + "/schema.sql");
|
QFile f(fixtureDir_ + "/schema.sql");
|
||||||
|
|
@ -101,6 +123,7 @@ void PgEnv::SetUp()
|
||||||
Pgsql::Connection admin;
|
Pgsql::Connection admin;
|
||||||
admin.connect(*base);
|
admin.connect(*base);
|
||||||
admin.query(QString("DROP DATABASE IF EXISTS %1 WITH (FORCE)").arg(quoteIdent(tmpl)));
|
admin.query(QString("DROP DATABASE IF EXISTS %1 WITH (FORCE)").arg(quoteIdent(tmpl)));
|
||||||
|
admin.query(QString("DROP ROLE IF EXISTS %1").arg(quoteIdent(plainRoleName())));
|
||||||
}
|
}
|
||||||
catch (const std::exception &) {
|
catch (const std::exception &) {
|
||||||
}
|
}
|
||||||
|
|
@ -115,9 +138,10 @@ void PgEnv::TearDown()
|
||||||
Pgsql::Connection admin;
|
Pgsql::Connection admin;
|
||||||
admin.connect(*baseUrl());
|
admin.connect(*baseUrl());
|
||||||
admin.query(QString("DROP DATABASE IF EXISTS %1 WITH (FORCE)").arg(quoteIdent(templateDbName())));
|
admin.query(QString("DROP DATABASE IF EXISTS %1 WITH (FORCE)").arg(quoteIdent(templateDbName())));
|
||||||
|
admin.query(QString("DROP ROLE IF EXISTS %1").arg(quoteIdent(plainRoleName())));
|
||||||
}
|
}
|
||||||
catch (const std::exception &e) {
|
catch (const std::exception &e) {
|
||||||
ADD_FAILURE() << "Dropping template database failed: " << e.what();
|
ADD_FAILURE() << "Dropping template database or plain role failed: " << e.what();
|
||||||
}
|
}
|
||||||
g_available = false;
|
g_available = false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,12 @@ qint64 processId();
|
||||||
/// Name of the template database for this process.
|
/// Name of the template database for this process.
|
||||||
QString templateDbName();
|
QString templateDbName();
|
||||||
|
|
||||||
|
/// Name of the login role without superuser rights, unique per process.
|
||||||
|
QString plainRoleName();
|
||||||
|
|
||||||
|
/// Same as urlForDb() but with the plain role as user.
|
||||||
|
QString urlForDbAsPlainRole(const QString &db);
|
||||||
|
|
||||||
/// Quotes an identifier for use in SQL.
|
/// Quotes an identifier for use in SQL.
|
||||||
QString quoteIdent(const QString &name);
|
QString quoteIdent(const QString &name);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue