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 diff --git a/src/modules/partition/tests/CMakeLists.txt b/src/modules/partition/tests/CMakeLists.txt index 8edef484c..2839270fb 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,27 +42,32 @@ calamares_add_test( calamares_add_test( - createlayoutstests + 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 - Qt5::Gui + Calamares::calamaresui DEFINITIONS ${_partition_defs} ) calamares_add_test( - automounttests + partitionautomounttest SOURCES ${PartitionModule_SOURCE_DIR}/jobs/AutoMountManagementJob.cpp AutoMountTests.cpp - LIBRARIES - calamares +) + +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 new file mode 100644 index 000000000..c63d7476d --- /dev/null +++ b/src/modules/partition/tests/DevicesTests.cpp @@ -0,0 +1,90 @@ +/* === 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 "partition/KPMManager.h" +#include "utils/Logger.h" + +#include +#include + +#include +#include + +#include + +#include + +class DevicesTests : public QObject +{ + Q_OBJECT + +public: + DevicesTests(); + +private Q_SLOTS: + void testKPMScanDevices(); + void testPartUtilScanDevices(); + +private: + std::unique_ptr< CalamaresUtils::Partition::KPMManager > m_d; + bool m_isRoot = false; +}; + +DevicesTests::DevicesTests() + : m_d( std::make_unique< CalamaresUtils::Partition::KPMManager >() ) + , m_isRoot( geteuid() == 0 ) +{ +} + +void +DevicesTests::testKPMScanDevices() +{ + Logger::setupLogLevel( Logger::LOGVERBOSE ); + + cDebug() << "Getting devices via KPMCore"; + CoreBackend* backend = CoreBackendManager::self()->backend(); + QVERIFY( backend ); +#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."; + + if ( !m_isRoot ) + { + QEXPECT_FAIL( "", "Test invalid when not root", Continue ); + } + QVERIFY( devices.count() > 0 ); +} + +void +DevicesTests::testPartUtilScanDevices() +{ + Logger::setupLogLevel( Logger::LOGVERBOSE ); + + cDebug() << "Getting devices via PartUtils"; + 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 ); +} + +QTEST_GUILESS_MAIN( DevicesTests ) + +#include "utils/moc-warnings.h" + +#include "DevicesTests.moc"