From e294221a2d6628bfac997d88affa99c287b3b5ed Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 31 Aug 2021 13:08:54 +0200 Subject: [PATCH 1/8] [partition] Rename test executables - all partition tests are now named partitiontest --- src/modules/partition/tests/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/modules/partition/tests/CMakeLists.txt b/src/modules/partition/tests/CMakeLists.txt index 8edef484c..c8c6f7fd4 100644 --- a/src/modules/partition/tests/CMakeLists.txt +++ b/src/modules/partition/tests/CMakeLists.txt @@ -15,7 +15,7 @@ include_directories( ) calamares_add_test( - partitionjobtests + partitionjobtest SOURCES PartitionJobTests.cpp ${PartitionModule_SOURCE_DIR}/core/KPMHelpers.cpp @@ -31,7 +31,7 @@ calamares_add_test( ) calamares_add_test( - clearmountsjobtests + partitionclearmountsjobtest SOURCES ${PartitionModule_SOURCE_DIR}/jobs/ClearMountsJob.cpp ClearMountsJobTests.cpp @@ -42,7 +42,7 @@ calamares_add_test( calamares_add_test( - createlayoutstests + partitioncreatelayoutstest SOURCES ${PartitionModule_SOURCE_DIR}/core/KPMHelpers.cpp ${PartitionModule_SOURCE_DIR}/core/PartitionInfo.cpp @@ -59,7 +59,7 @@ calamares_add_test( ) calamares_add_test( - automounttests + partitionautomounttest SOURCES ${PartitionModule_SOURCE_DIR}/jobs/AutoMountManagementJob.cpp AutoMountTests.cpp From 22ba21f9375aa33acff3fe83ffd845ff74443605 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 31 Aug 2021 13:16:45 +0200 Subject: [PATCH 2/8] [partition] Stub of a test of scandevice --- src/modules/partition/tests/CMakeLists.txt | 11 ++-- src/modules/partition/tests/DevicesTests.cpp | 56 ++++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/modules/partition/tests/DevicesTests.cpp diff --git a/src/modules/partition/tests/CMakeLists.txt b/src/modules/partition/tests/CMakeLists.txt index c8c6f7fd4..6c7abc183 100644 --- a/src/modules/partition/tests/CMakeLists.txt +++ b/src/modules/partition/tests/CMakeLists.txt @@ -52,9 +52,6 @@ calamares_add_test( CreateLayoutsTests.cpp LIBRARIES kpmcore - calamares - calamaresui - Qt5::Gui DEFINITIONS ${_partition_defs} ) @@ -63,6 +60,10 @@ calamares_add_test( SOURCES ${PartitionModule_SOURCE_DIR}/jobs/AutoMountManagementJob.cpp AutoMountTests.cpp - LIBRARIES - calamares +) + +calamares_add_test( + partitiondevicestest + SOURCES + DevicesTests.cpp ) diff --git a/src/modules/partition/tests/DevicesTests.cpp b/src/modules/partition/tests/DevicesTests.cpp new file mode 100644 index 000000000..82311b288 --- /dev/null +++ b/src/modules/partition/tests/DevicesTests.cpp @@ -0,0 +1,56 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ + +#include "core/DeviceList.h" + +#include "utils/Logger.h" + +#include +#include + +class DevicesTests : public QObject +{ + Q_OBJECT + +public: + DevicesTests(); + +private Q_SLOTS: + void testKPMScanDevices(); + void testPartUtilScanDevices(); +}; + +DevicesTests::DevicesTests() {} + +void +DevicesTests::testKPMScanDevices() +{ + Logger::setupLogLevel( Logger::LOGVERBOSE ); + +#if defined( WITH_KPMCORE4API ) + cDebug() << "Getting devices via KPMCore"; + CoreBackend* backend = CoreBackendManager::self()->backend(); + DeviceList devices = backend->scanDevices( /* not includeReadOnly, not includeLoopback */ ScanFlag( 0 ) ); + cDebug() << Logger::SubEntry << "Done getting devices."; +#else + cWarning() << "Test skipped; use KPMCore4"; +#endif +} + +void +DevicesTests::testPartUtilScanDevices() +{ + Logger::setupLogLevel( Logger::LOGVERBOSE ); + + cDebug() << "Getting devices via PartUtils"; + auto l = PartUtils::getDevices(); + cDebug() << Logger::SubEntry << "Done getting devices."; + + QVERIFY( l.count() > 0 ); +} From 854eb845d2d2c8226019cfde1968994d29cc7d20 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 5 Aug 2021 15:49:29 +0200 Subject: [PATCH 3/8] [partition] Fix build of layoutstest This test needs UI because the devicemodel uses GUI parts. --- src/modules/partition/tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/partition/tests/CMakeLists.txt b/src/modules/partition/tests/CMakeLists.txt index 6c7abc183..bb0b59af4 100644 --- a/src/modules/partition/tests/CMakeLists.txt +++ b/src/modules/partition/tests/CMakeLists.txt @@ -52,6 +52,7 @@ calamares_add_test( CreateLayoutsTests.cpp LIBRARIES kpmcore + Calamares::calamaresui DEFINITIONS ${_partition_defs} ) From ede19c8a6179a4e8cd049317663844cfa586b10b Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 31 Aug 2021 13:45:15 +0200 Subject: [PATCH 4/8] [partition] Fix build of devices test --- src/modules/partition/tests/CMakeLists.txt | 5 ++++- src/modules/partition/tests/DevicesTests.cpp | 22 +++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/modules/partition/tests/CMakeLists.txt b/src/modules/partition/tests/CMakeLists.txt index bb0b59af4..2839270fb 100644 --- a/src/modules/partition/tests/CMakeLists.txt +++ b/src/modules/partition/tests/CMakeLists.txt @@ -44,12 +44,12 @@ calamares_add_test( calamares_add_test( partitioncreatelayoutstest SOURCES + CreateLayoutsTests.cpp ${PartitionModule_SOURCE_DIR}/core/KPMHelpers.cpp ${PartitionModule_SOURCE_DIR}/core/PartitionInfo.cpp ${PartitionModule_SOURCE_DIR}/core/PartitionLayout.cpp ${PartitionModule_SOURCE_DIR}/core/PartUtils.cpp ${PartitionModule_SOURCE_DIR}/core/DeviceModel.cpp - CreateLayoutsTests.cpp LIBRARIES kpmcore Calamares::calamaresui @@ -67,4 +67,7 @@ calamares_add_test( partitiondevicestest SOURCES DevicesTests.cpp + ${PartitionModule_SOURCE_DIR}/core/DeviceList.cpp + LIBRARIES + kpmcore ) diff --git a/src/modules/partition/tests/DevicesTests.cpp b/src/modules/partition/tests/DevicesTests.cpp index 82311b288..02b1dc62e 100644 --- a/src/modules/partition/tests/DevicesTests.cpp +++ b/src/modules/partition/tests/DevicesTests.cpp @@ -11,6 +11,9 @@ #include "utils/Logger.h" +#include +#include + #include #include @@ -33,14 +36,13 @@ DevicesTests::testKPMScanDevices() { Logger::setupLogLevel( Logger::LOGVERBOSE ); -#if defined( WITH_KPMCORE4API ) cDebug() << "Getting devices via KPMCore"; CoreBackend* backend = CoreBackendManager::self()->backend(); - DeviceList devices = backend->scanDevices( /* not includeReadOnly, not includeLoopback */ ScanFlag( 0 ) ); + QVERIFY( backend ); + auto devices = backend->scanDevices(); // Whatever the default is /* not includeReadOnly, not includeLoopback */ ScanFlag( 0 ) ); cDebug() << Logger::SubEntry << "Done getting devices."; -#else - cWarning() << "Test skipped; use KPMCore4"; -#endif + + QVERIFY( devices.count() > 0 ); } void @@ -49,8 +51,14 @@ DevicesTests::testPartUtilScanDevices() Logger::setupLogLevel( Logger::LOGVERBOSE ); cDebug() << "Getting devices via PartUtils"; - auto l = PartUtils::getDevices(); + auto devices = PartUtils::getDevices(); cDebug() << Logger::SubEntry << "Done getting devices."; - QVERIFY( l.count() > 0 ); + QVERIFY( devices.count() > 0 ); } + +QTEST_GUILESS_MAIN( DevicesTests ) + +#include "utils/moc-warnings.h" + +#include "DevicesTests.moc" From 3d0709c779be330ae3259dff9a80c431b944f3c2 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 31 Aug 2021 13:46:25 +0200 Subject: [PATCH 5/8] [partition] Avoid crash when there is no KPM backend --- src/modules/partition/core/DeviceList.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/modules/partition/core/DeviceList.cpp b/src/modules/partition/core/DeviceList.cpp index 6b770a982..c7de12e88 100644 --- a/src/modules/partition/core/DeviceList.cpp +++ b/src/modules/partition/core/DeviceList.cpp @@ -112,6 +112,11 @@ QList< Device* > getDevices( DeviceType which ) { CoreBackend* backend = CoreBackendManager::self()->backend(); + if ( !backend ) + { + cWarning() << "No KPM backend found."; + return {}; + } #if defined( WITH_KPMCORE4API ) DeviceList devices = backend->scanDevices( /* not includeReadOnly, not includeLoopback */ ScanFlag( 0 ) ); #else From 1554c3a07cbf3779589d923778e0811e56bef1f2 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 31 Aug 2021 13:54:23 +0200 Subject: [PATCH 6/8] [partition] Create KPM backend at start of test --- src/modules/partition/tests/DevicesTests.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/modules/partition/tests/DevicesTests.cpp b/src/modules/partition/tests/DevicesTests.cpp index 02b1dc62e..6d42e71e4 100644 --- a/src/modules/partition/tests/DevicesTests.cpp +++ b/src/modules/partition/tests/DevicesTests.cpp @@ -9,6 +9,7 @@ #include "core/DeviceList.h" +#include "partition/KPMManager.h" #include "utils/Logger.h" #include @@ -17,6 +18,8 @@ #include #include +#include + class DevicesTests : public QObject { Q_OBJECT @@ -27,9 +30,13 @@ public: private Q_SLOTS: void testKPMScanDevices(); void testPartUtilScanDevices(); + +private: + std::unique_ptr< CalamaresUtils::Partition::KPMManager > m_d; }; -DevicesTests::DevicesTests() {} +DevicesTests::DevicesTests() : m_d( std::make_unique< CalamaresUtils::Partition::KPMManager >() ) +{} void DevicesTests::testKPMScanDevices() @@ -39,7 +46,12 @@ DevicesTests::testKPMScanDevices() cDebug() << "Getting devices via KPMCore"; CoreBackend* backend = CoreBackendManager::self()->backend(); QVERIFY( backend ); - auto devices = backend->scanDevices(); // Whatever the default is /* not includeReadOnly, not includeLoopback */ ScanFlag( 0 ) ); +#if defined( WITH_KPMCORE4API ) + auto flags = ScanFlag( ~0 ); +#else + auto flags = true; +#endif + auto devices = backend->scanDevices( flags ); // These flags try to get "all" cDebug() << Logger::SubEntry << "Done getting devices."; QVERIFY( devices.count() > 0 ); From 09491e4cbb70b78ad1462d729d4fe816dcf9b9bd Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 31 Aug 2021 13:58:23 +0200 Subject: [PATCH 7/8] [partition] expect failures when kpm can't read the disk --- src/modules/partition/tests/DevicesTests.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/modules/partition/tests/DevicesTests.cpp b/src/modules/partition/tests/DevicesTests.cpp index 6d42e71e4..6513e955b 100644 --- a/src/modules/partition/tests/DevicesTests.cpp +++ b/src/modules/partition/tests/DevicesTests.cpp @@ -20,6 +20,8 @@ #include +#include + class DevicesTests : public QObject { Q_OBJECT @@ -33,9 +35,12 @@ private Q_SLOTS: private: std::unique_ptr< CalamaresUtils::Partition::KPMManager > m_d; + bool m_isRoot = false; }; -DevicesTests::DevicesTests() : m_d( std::make_unique< CalamaresUtils::Partition::KPMManager >() ) +DevicesTests::DevicesTests() + : m_d( std::make_unique< CalamaresUtils::Partition::KPMManager >() ) + , m_isRoot( geteuid() == 0 ) {} void @@ -54,6 +59,10 @@ DevicesTests::testKPMScanDevices() auto devices = backend->scanDevices( flags ); // These flags try to get "all" cDebug() << Logger::SubEntry << "Done getting devices."; + if ( !m_isRoot ) + { + QEXPECT_FAIL( "", "Test invalid when not root", Continue ); + } QVERIFY( devices.count() > 0 ); } @@ -66,6 +75,10 @@ DevicesTests::testPartUtilScanDevices() auto devices = PartUtils::getDevices(); cDebug() << Logger::SubEntry << "Done getting devices."; + if ( !m_isRoot ) + { + QEXPECT_FAIL( "", "Test invalid when not root", Continue ); + } QVERIFY( devices.count() > 0 ); } From 2f88ba0d3f2d1df4a35df67d5557d2c0f1ea2062 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 31 Aug 2021 14:03:04 +0200 Subject: [PATCH 8/8] [partition] Coding style --- src/modules/partition/tests/DevicesTests.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/partition/tests/DevicesTests.cpp b/src/modules/partition/tests/DevicesTests.cpp index 6513e955b..c63d7476d 100644 --- a/src/modules/partition/tests/DevicesTests.cpp +++ b/src/modules/partition/tests/DevicesTests.cpp @@ -41,7 +41,8 @@ private: DevicesTests::DevicesTests() : m_d( std::make_unique< CalamaresUtils::Partition::KPMManager >() ) , m_isRoot( geteuid() == 0 ) -{} +{ +} void DevicesTests::testKPMScanDevices() @@ -56,7 +57,7 @@ DevicesTests::testKPMScanDevices() #else auto flags = true; #endif - auto devices = backend->scanDevices( flags ); // These flags try to get "all" + auto devices = backend->scanDevices( flags ); // These flags try to get "all" cDebug() << Logger::SubEntry << "Done getting devices."; if ( !m_isRoot )