From e5756587a3edd939789d32932f48a68a87a074ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Wed, 11 Oct 2017 17:58:46 +0200 Subject: [PATCH] add log database loading tests --- libguh-core/guhcore.cpp | 2 +- libguh-core/logging/logengine.cpp | 4 +- libguh-core/logging/logengine.h | 2 +- tests/auto/auto.pro | 1 + tests/auto/logging/testlogging.cpp | 5 +- tests/auto/loggingloading/guhd-broken.sqlite | Bin 0 -> 20480 bytes tests/auto/loggingloading/guhd-v2.sqlite | Bin 0 -> 20480 bytes tests/auto/loggingloading/loggingloading.pro | 6 + tests/auto/loggingloading/loggingloading.qrc | 6 + .../loggingloading/testloggingloading.cpp | 139 ++++++++++++++++++ 10 files changed, 159 insertions(+), 6 deletions(-) create mode 100755 tests/auto/loggingloading/guhd-broken.sqlite create mode 100755 tests/auto/loggingloading/guhd-v2.sqlite create mode 100644 tests/auto/loggingloading/loggingloading.pro create mode 100644 tests/auto/loggingloading/loggingloading.qrc create mode 100644 tests/auto/loggingloading/testloggingloading.cpp diff --git a/libguh-core/guhcore.cpp b/libguh-core/guhcore.cpp index 967ca75d..38eabfd8 100644 --- a/libguh-core/guhcore.cpp +++ b/libguh-core/guhcore.cpp @@ -424,7 +424,7 @@ void GuhCore::init() { m_timeManager = new TimeManager(QTimeZone::systemTimeZoneId(), this); qCDebug(dcApplication) << "Creating Log Engine"; - m_logger = new LogEngine(this); + m_logger = new LogEngine(GuhSettings::logPath(), this); qCDebug(dcApplication) << "Creating Device Manager (locale:" << m_configuration->locale() << ")"; m_deviceManager = new DeviceManager(m_configuration->locale(), this); diff --git a/libguh-core/logging/logengine.cpp b/libguh-core/logging/logengine.cpp index 2c81008b..a0e2a9bf 100644 --- a/libguh-core/logging/logengine.cpp +++ b/libguh-core/logging/logengine.cpp @@ -132,11 +132,11 @@ namespace guhserver { /*! Constructs the log engine with the given \a parent. */ -LogEngine::LogEngine(QObject *parent): +LogEngine::LogEngine(const QString &logPath, QObject *parent): QObject(parent) { m_db = QSqlDatabase::addDatabase("QSQLITE", "logs"); - m_db.setDatabaseName(GuhSettings::logPath()); + m_db.setDatabaseName(logPath); m_dbMaxSize = 50000; m_overflow = 100; diff --git a/libguh-core/logging/logengine.h b/libguh-core/logging/logengine.h index dc02f18d..38441fc3 100644 --- a/libguh-core/logging/logengine.h +++ b/libguh-core/logging/logengine.h @@ -38,7 +38,7 @@ class LogEngine: public QObject { Q_OBJECT public: - LogEngine(QObject *parent = 0); + LogEngine(const QString &logPath = GuhSettings::logPath(), QObject *parent = 0); ~LogEngine(); QList logEntries(const LogFilter &filter = LogFilter()) const; diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 23e92a51..41d560bd 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -16,6 +16,7 @@ SUBDIRS = versioning \ websocketserver \ logging \ loggingdirect \ + loggingloading \ restlogging \ #coap \ # temporary removed until fixed configurations \ diff --git a/tests/auto/logging/testlogging.cpp b/tests/auto/logging/testlogging.cpp index 4d59f2b1..b0ca6ce6 100644 --- a/tests/auto/logging/testlogging.cpp +++ b/tests/auto/logging/testlogging.cpp @@ -64,6 +64,7 @@ private slots: void testHouseKeeping(); + // this has to be the last test void removeDevice(); }; @@ -326,8 +327,8 @@ void TestLogging::actionLog() clientSpy.wait(200); - QVariant notification = checkNotification(clientSpy, "Logging.LogEntryAdded"); - QVERIFY(!notification.isNull()); + loggEntryAddedVariants = checkNotifications(clientSpy, "Logging.LogEntryAdded"); + QVERIFY(!loggEntryAddedVariants.isEmpty()); // get this logentry with filter params.clear(); diff --git a/tests/auto/loggingloading/guhd-broken.sqlite b/tests/auto/loggingloading/guhd-broken.sqlite new file mode 100755 index 0000000000000000000000000000000000000000..43329a10479333934eeee44a739ce61aac06d0d8 GIT binary patch literal 20480 zcmeHNYiu0F9pByYJ5d4zVv+zka052YUUp~SO=)AyB@GDyY^T&I$9W&K=7QG__Sqt` ztO9BgUr+_5Qv0DVsZ=fMx3++U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J;-8?A_Q6O5Bwkb@f#3I_{LE;-QnA5GW)n(}Mqh$71m$bHgJq){&Z zPvi3DBBih?T^KE`-z0QZ3G+mvG7>HI+$U5bn2H>3M0T)n-7krT<7d84vT&V06Aj0+ zyPSpVI4By9V^Uu+>QgH;W&0>OyOwEUv7wvdAF>y=FjgFYu-vUR;o1HA&hNGh4xUos&zeg&h@vkc{C^coh{3H1q>2>KYsDv^;NV}75$uD{E@F;wv z`AQx^_(t<*DMGgCpK&-N8_l2Ld5GR<9>0P|5dM1P)4Wn=;`K;n7e&dE^pdw0Er{MQ z{}ce!H_QQ`xvlW#THWl?&eO%MJ0x_rf#6JBYG2P5&-Tl!5dYm$MMB>~S12zjhm|!D z|5r%QLY0V;T*I*hJ?QGm-W=R^q011gH*9r%E(dp^Sav4=AX45#;bhR62cY6_R_&M!R=n-FJUcYcUs(1|R?5WG7- zeia2cm3>9ONT%!mB+dU=c~Y5x_z^JH{{^p{g1$Bv-H( z>6Xwjp|7x$YjfjpkJxdr`Ee-kr*PDXj{%&(Cs9L7_OWczj49Hq8s65>`1k7j|5wW| z$Z@v+?{TrqUzTrTWq5h7(0&?L@$*%R3pz=@cCXN-s;4IrE7EK2LPdg?Bz!!DEZnO% zhz(zer+lVQ0v7J~e-z8kZD8lKa7(`u3)k?onPfZ%w?(v=+>e6F6$(c^@f9MNUHN9# z{LAl&HE&nS)I44ErRGIWJ|^A&?~?A#<$vj2s95EaB#c)a-vWRh5O49~KSAMRalt1GjW?Q8XMs8&9xse|=gs z9M3Lh;of}c3eJ?ECnC$e^KmW&`ORl5p%rn@EMMn a@-)k4>o0|}aouE!WwUj+P&SQA+y4Tgv8i?d literal 0 HcmV?d00001 diff --git a/tests/auto/loggingloading/loggingloading.pro b/tests/auto/loggingloading/loggingloading.pro new file mode 100644 index 00000000..aaef3966 --- /dev/null +++ b/tests/auto/loggingloading/loggingloading.pro @@ -0,0 +1,6 @@ +include(../../../guh.pri) +include(../autotests.pri) + +RESOURCES += loggingloading.qrc +TARGET = loggingloading +SOURCES += testloggingloading.cpp diff --git a/tests/auto/loggingloading/loggingloading.qrc b/tests/auto/loggingloading/loggingloading.qrc new file mode 100644 index 00000000..c38aebda --- /dev/null +++ b/tests/auto/loggingloading/loggingloading.qrc @@ -0,0 +1,6 @@ + + + guhd-v2.sqlite + guhd-broken.sqlite + + diff --git a/tests/auto/loggingloading/testloggingloading.cpp b/tests/auto/loggingloading/testloggingloading.cpp new file mode 100644 index 00000000..d842831d --- /dev/null +++ b/tests/auto/loggingloading/testloggingloading.cpp @@ -0,0 +1,139 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Simon Stürz * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include + +#include "logging/logengine.h" +#include "logging/logvaluetool.h" + +using namespace guhserver; + +class TestLoggingLoading: public QObject +{ + Q_OBJECT +public: + TestLoggingLoading(QObject* parent = nullptr); + +private slots: + void testLogMigration(); + void testLogfileRotation(); + + void databaseSerializationTest_data(); + void databaseSerializationTest(); +}; + +TestLoggingLoading::TestLoggingLoading(QObject *parent): QObject(parent) +{ + +} + +void TestLoggingLoading::testLogMigration() +{ + Q_INIT_RESOURCE(loggingloading); + + // Create LogEngine with log db from resource file + QString temporaryDbName = GuhSettings::settingsPath() + "/guhd-v2.sqlite"; + + if (QFile::exists(temporaryDbName)) + QVERIFY(QFile(temporaryDbName).remove()); + + QVERIFY(QFile::copy(":/guhd-v2.sqlite", temporaryDbName)); + QVERIFY(QFile::setPermissions(temporaryDbName, QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther)); + + LogEngine *logEngine = new LogEngine(temporaryDbName, this); + // Check there is no rotated logfile + QVERIFY(!QFile::exists(temporaryDbName + ".1")); + + delete logEngine; + + QVERIFY(QFile(temporaryDbName).remove()); +} + +void TestLoggingLoading::testLogfileRotation() +{ + Q_INIT_RESOURCE(loggingloading); + + // Create LogEngine with log db from resource file + QString temporaryDbName = GuhSettings::settingsPath() + "/guhd-broken.sqlite"; + QString rotatedDbName = GuhSettings::settingsPath() + "/guhd-broken.sqlite.1"; + + // Remove the files if there are some left + if (QFile::exists(temporaryDbName)) + QVERIFY(QFile(temporaryDbName).remove()); + + if (QFile::exists(rotatedDbName)) + QVERIFY(QFile(rotatedDbName).remove()); + + QVERIFY(QFile::copy(":/guhd-broken.sqlite", temporaryDbName)); + QVERIFY(QFile::setPermissions(temporaryDbName, QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther)); + + QVERIFY(!QFile::exists(rotatedDbName)); + LogEngine *logEngine = new LogEngine(temporaryDbName, this); + QVERIFY(QFile::exists(rotatedDbName)); + + delete logEngine; + + QVERIFY(QFile(temporaryDbName).remove()); + QVERIFY(QFile(rotatedDbName).remove()); +} + +void TestLoggingLoading::databaseSerializationTest_data() +{ + QUuid uuid = QUuid("3782732b-61b4-48e8-8d6d-b5205159d7cd"); + + QVariantMap variantMap; + variantMap.insert("string", "value"); + variantMap.insert("int", 5); + variantMap.insert("double", 3.14); + variantMap.insert("uuid", uuid); + + QVariantList variantList; + variantList.append(variantMap); + variantList.append("String"); + variantList.append(3.14); + variantList.append(uuid); + + QTest::addColumn("value"); + + QTest::newRow("QString") << QVariant(QString("Hello")); + QTest::newRow("Integer") << QVariant((int)2); + QTest::newRow("Double") << QVariant((double)2.34); + QTest::newRow("Float") << QVariant((float)2.34); + QTest::newRow("QColor") << QVariant(QColor(0,255,128)); + QTest::newRow("QByteArray") << QVariant(QByteArray("\nthisisatestarray\n")); + QTest::newRow("QUuid") << QVariant(uuid); + QTest::newRow("QVariantMap") << QVariant(variantMap); + QTest::newRow("QVariantList") << QVariant(variantList); +} + +void TestLoggingLoading::databaseSerializationTest() +{ + QFETCH(QVariant, value); + + QString serializedValue = LogValueTool::serializeValue(value); + QVariant deserializedValue = LogValueTool::deserializeValue(serializedValue); + + qDebug() << "Stored:" << value; + qDebug() << "Loaded:" << deserializedValue; + QCOMPARE(deserializedValue, value); +} + +#include "testloggingloading.moc" +QTEST_MAIN(TestLoggingLoading)