Start working on tests for partition jobs
This commit is contained in:
parent
031c405c60
commit
43f29b8058
@ -112,6 +112,7 @@ JobQueue::start()
|
|||||||
{
|
{
|
||||||
Q_ASSERT( !m_thread->isRunning() );
|
Q_ASSERT( !m_thread->isRunning() );
|
||||||
m_thread->setJobs( m_jobs );
|
m_thread->setJobs( m_jobs );
|
||||||
|
m_jobs.clear();
|
||||||
m_thread->start();
|
m_thread->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
project( PartitionModule )
|
||||||
|
|
||||||
if( WITH_PARTITIONMANAGER )
|
if( WITH_PARTITIONMANAGER )
|
||||||
|
|
||||||
add_subdirectory( partitionmanager/calamares )
|
add_subdirectory( partitionmanager/calamares )
|
||||||
@ -12,6 +14,8 @@ find_package( KF5 REQUIRED CoreAddons )
|
|||||||
|
|
||||||
add_definitions( -DCALAMARES )
|
add_definitions( -DCALAMARES )
|
||||||
|
|
||||||
|
add_subdirectory( tests )
|
||||||
|
|
||||||
include_directories( ${PROJECT_BINARY_DIR}/src/libcalamaresui )
|
include_directories( ${PROJECT_BINARY_DIR}/src/libcalamaresui )
|
||||||
calamares_add_plugin( partition
|
calamares_add_plugin( partition
|
||||||
TYPE viewmodule
|
TYPE viewmodule
|
||||||
|
27
src/modules/partition/tests/CMakeLists.txt
Normal file
27
src/modules/partition/tests/CMakeLists.txt
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
find_package( Qt5 COMPONENTS Test REQUIRED )
|
||||||
|
include( ECMAddTests )
|
||||||
|
|
||||||
|
set( jobtests_SRCS
|
||||||
|
${PartitionModule_SOURCE_DIR}/CreatePartitionJob.cpp
|
||||||
|
${PartitionModule_SOURCE_DIR}/CreatePartitionTableJob.cpp
|
||||||
|
${PartitionModule_SOURCE_DIR}/DeletePartitionJob.cpp
|
||||||
|
${PartitionModule_SOURCE_DIR}/PMUtils.cpp
|
||||||
|
JobTests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
include_directories(
|
||||||
|
${PartitionModule_SOURCE_DIR}
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
)
|
||||||
|
|
||||||
|
ecm_add_test( ${jobtests_SRCS}
|
||||||
|
TEST_NAME jobtests
|
||||||
|
LINK_LIBRARIES
|
||||||
|
calapm
|
||||||
|
${CALAMARES_LIBRARIES}
|
||||||
|
calamareslib
|
||||||
|
Qt5::Core
|
||||||
|
Qt5::Test
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties( jobtests PROPERTIES AUTOMOC TRUE )
|
149
src/modules/partition/tests/JobTests.cpp
Normal file
149
src/modules/partition/tests/JobTests.cpp
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
#include <JobTests.h>
|
||||||
|
|
||||||
|
#include <CreatePartitionJob.h>
|
||||||
|
#include <CreatePartitionTableJob.h>
|
||||||
|
#include <PMUtils.h>
|
||||||
|
|
||||||
|
// CalaPM
|
||||||
|
#include <CalaPM.h>
|
||||||
|
#include <backend/corebackend.h>
|
||||||
|
#include <backend/corebackendmanager.h>
|
||||||
|
#include <fs/filesystemfactory.h>
|
||||||
|
|
||||||
|
// Qt
|
||||||
|
#include <QEventLoop>
|
||||||
|
#include <QtTest/QtTest>
|
||||||
|
|
||||||
|
QTEST_GUILESS_MAIN( JobTests )
|
||||||
|
|
||||||
|
static const qint64 MB = 1024 * 1024;
|
||||||
|
|
||||||
|
using namespace Calamares;
|
||||||
|
|
||||||
|
QueueRunner::QueueRunner( JobQueue* queue )
|
||||||
|
: m_queue( queue )
|
||||||
|
{
|
||||||
|
connect( m_queue, &JobQueue::progress, this, &QueueRunner::onProgress );
|
||||||
|
connect( m_queue, &JobQueue::failed, this, &QueueRunner::onFailed );
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
QueueRunner::run()
|
||||||
|
{
|
||||||
|
m_done = false;
|
||||||
|
m_queue->start();
|
||||||
|
QEventLoop loop;
|
||||||
|
while ( !m_done )
|
||||||
|
loop.processEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
QueueRunner::onProgress( int current, int total, const QString& prettyName )
|
||||||
|
{
|
||||||
|
QVERIFY( current <= total );
|
||||||
|
if ( current < total )
|
||||||
|
return;
|
||||||
|
m_done = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
QueueRunner::onFailed( const QString& message, const QString& details )
|
||||||
|
{
|
||||||
|
m_done = true;
|
||||||
|
QString msg = message + "\ndetails: " + details;
|
||||||
|
QFAIL( qPrintable( msg ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
//----------------------------------------------------------
|
||||||
|
JobTests::JobTests()
|
||||||
|
: m_runner( &m_queue )
|
||||||
|
{}
|
||||||
|
|
||||||
|
void
|
||||||
|
JobTests::initTestCase()
|
||||||
|
{
|
||||||
|
QString deviceName = qgetenv( "CALAMARES_TEST_DISK" );
|
||||||
|
if ( deviceName.isEmpty() )
|
||||||
|
{
|
||||||
|
QSKIP( "Skipping test, CALAMARES_TEST_DISK is not set. It should point to a disk which can be safely formatted" );
|
||||||
|
}
|
||||||
|
|
||||||
|
QVERIFY( CalaPM::init() );
|
||||||
|
|
||||||
|
CoreBackend* backend = CoreBackendManager::self()->backend();
|
||||||
|
m_device.reset( backend->scanDevice( deviceName ) );
|
||||||
|
QVERIFY( !m_device.isNull() );
|
||||||
|
|
||||||
|
FileSystemFactory::init();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
JobTests::testPartitionTable()
|
||||||
|
{
|
||||||
|
queuePartitionTableCreation();
|
||||||
|
m_runner.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
JobTests::queuePartitionTableCreation()
|
||||||
|
{
|
||||||
|
auto job = new CreatePartitionTableJob( m_device.data(), PartitionTable::gpt );
|
||||||
|
job->updatePreview();
|
||||||
|
m_queue.enqueue( job_ptr( job ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
CreatePartitionJob*
|
||||||
|
JobTests::newCreatePartitionJob( Partition* freeSpacePartition, PartitionRole role, FileSystem::Type type, qint64 size )
|
||||||
|
{
|
||||||
|
Q_ASSERT( freeSpacePartition );
|
||||||
|
|
||||||
|
qint64 firstSector = freeSpacePartition->firstSector();
|
||||||
|
qint64 lastSector = firstSector + size / m_device->logicalSectorSize();
|
||||||
|
FileSystem* fs = FileSystemFactory::create( type, firstSector, lastSector );
|
||||||
|
|
||||||
|
Partition* partition = new Partition(
|
||||||
|
freeSpacePartition->parent(),
|
||||||
|
*m_device,
|
||||||
|
role,
|
||||||
|
fs, firstSector, lastSector,
|
||||||
|
QString() /* path */,
|
||||||
|
PartitionTable::FlagNone /* availableFlags */,
|
||||||
|
QString() /* mountPoint */,
|
||||||
|
false /* mounted */,
|
||||||
|
PartitionTable::FlagNone /* activeFlags */,
|
||||||
|
Partition::StateNew
|
||||||
|
);
|
||||||
|
return new CreatePartitionJob( m_device.data(), partition );
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
Partition* firstFreePartition( PartitionNode* parent )
|
||||||
|
{
|
||||||
|
for( auto child : parent->children() )
|
||||||
|
if ( PMUtils::isPartitionFreeSpace( child ) )
|
||||||
|
return child;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
JobTests::testCreatePartition()
|
||||||
|
{
|
||||||
|
queuePartitionTableCreation();
|
||||||
|
CreatePartitionJob* job;
|
||||||
|
|
||||||
|
Partition* partition = firstFreePartition( m_device->partitionTable() );
|
||||||
|
job = newCreatePartitionJob( partition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 10 * MB);
|
||||||
|
QVERIFY( job );
|
||||||
|
job->updatePreview();
|
||||||
|
m_queue.enqueue( job_ptr( job ) );
|
||||||
|
|
||||||
|
job = newCreatePartitionJob( partition, PartitionRole( PartitionRole::Primary ), FileSystem::LinuxSwap, 10 * MB);
|
||||||
|
job->updatePreview();
|
||||||
|
m_queue.enqueue( job_ptr( job ) );
|
||||||
|
|
||||||
|
job = newCreatePartitionJob( partition, PartitionRole( PartitionRole::Primary ), FileSystem::Btrfs, 10 * MB);
|
||||||
|
job->updatePreview();
|
||||||
|
m_queue.enqueue( job_ptr( job ) );
|
||||||
|
|
||||||
|
m_runner.run();
|
||||||
|
}
|
50
src/modules/partition/tests/JobTests.h
Normal file
50
src/modules/partition/tests/JobTests.h
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#ifndef JOBTESTS_H
|
||||||
|
#define JOBTESTS_H
|
||||||
|
|
||||||
|
#include <JobQueue.h>
|
||||||
|
|
||||||
|
// CalaPM
|
||||||
|
#include <core/device.h>
|
||||||
|
#include <core/partition.h>
|
||||||
|
#include <core/partitionrole.h>
|
||||||
|
#include <fs/filesystem.h>
|
||||||
|
|
||||||
|
// Qt
|
||||||
|
#include <QObject>
|
||||||
|
#include <QScopedPointer>
|
||||||
|
|
||||||
|
class QueueRunner : public QObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QueueRunner( Calamares::JobQueue* queue );
|
||||||
|
|
||||||
|
void run();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void onProgress( int current, int total, const QString& prettyName );
|
||||||
|
void onFailed( const QString& message, const QString& details );
|
||||||
|
Calamares::JobQueue* m_queue;
|
||||||
|
bool m_done;
|
||||||
|
};
|
||||||
|
|
||||||
|
class JobTests : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
JobTests();
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void initTestCase();
|
||||||
|
void testPartitionTable();
|
||||||
|
void testCreatePartition();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QScopedPointer< Device > m_device;
|
||||||
|
Calamares::JobQueue m_queue;
|
||||||
|
QueueRunner m_runner;
|
||||||
|
|
||||||
|
void queuePartitionTableCreation();
|
||||||
|
CreatePartitionJob* newCreatePartitionJob( Partition* freeSpacePartition, PartitionRole, FileSystem::Type type, qint64 size );
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* JOBTESTS_H */
|
Loading…
Reference in New Issue
Block a user