2014-07-17 17:57:55 +02:00
|
|
|
#include <JobTests.h>
|
|
|
|
|
|
|
|
#include <CreatePartitionJob.h>
|
|
|
|
#include <CreatePartitionTableJob.h>
|
2014-08-05 14:57:00 +02:00
|
|
|
#include <ResizePartitionJob.h>
|
2014-07-17 17:57:55 +02:00
|
|
|
#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;
|
|
|
|
|
2014-07-18 18:37:44 +02:00
|
|
|
static
|
|
|
|
Partition* firstFreePartition( PartitionNode* parent )
|
|
|
|
{
|
|
|
|
for( auto child : parent->children() )
|
|
|
|
if ( PMUtils::isPartitionFreeSpace( child ) )
|
|
|
|
return child;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:57:55 +02:00
|
|
|
QueueRunner::QueueRunner( JobQueue* queue )
|
|
|
|
: m_queue( queue )
|
|
|
|
{
|
2014-07-23 10:58:08 +02:00
|
|
|
connect( m_queue, &JobQueue::finished, this, &QueueRunner::onFinished );
|
2014-07-17 17:57:55 +02:00
|
|
|
connect( m_queue, &JobQueue::failed, this, &QueueRunner::onFailed );
|
|
|
|
}
|
|
|
|
|
2014-07-18 11:27:03 +02:00
|
|
|
bool
|
2014-07-17 17:57:55 +02:00
|
|
|
QueueRunner::run()
|
|
|
|
{
|
2014-07-23 10:58:08 +02:00
|
|
|
m_finished = false;
|
|
|
|
m_success = true;
|
2014-07-17 17:57:55 +02:00
|
|
|
m_queue->start();
|
|
|
|
QEventLoop loop;
|
2014-07-23 10:58:08 +02:00
|
|
|
while ( !m_finished )
|
2014-07-17 17:57:55 +02:00
|
|
|
loop.processEvents();
|
2014-07-18 11:27:03 +02:00
|
|
|
return m_success;
|
2014-07-17 17:57:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-23 10:58:08 +02:00
|
|
|
QueueRunner::onFinished()
|
2014-07-17 17:57:55 +02:00
|
|
|
{
|
2014-07-23 10:58:08 +02:00
|
|
|
m_finished = true;
|
2014-07-17 17:57:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QueueRunner::onFailed( const QString& message, const QString& details )
|
|
|
|
{
|
2014-07-23 10:58:08 +02:00
|
|
|
m_success = false;
|
2014-07-17 17:57:55 +02:00
|
|
|
QString msg = message + "\ndetails: " + details;
|
|
|
|
QFAIL( qPrintable( msg ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------
|
|
|
|
JobTests::JobTests()
|
|
|
|
: m_runner( &m_queue )
|
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
|
|
|
JobTests::initTestCase()
|
|
|
|
{
|
2014-07-23 14:34:06 +02:00
|
|
|
QString devicePath = qgetenv( "CALAMARES_TEST_DISK" );
|
|
|
|
if ( devicePath.isEmpty() )
|
2014-07-17 17:57:55 +02:00
|
|
|
{
|
|
|
|
QSKIP( "Skipping test, CALAMARES_TEST_DISK is not set. It should point to a disk which can be safely formatted" );
|
|
|
|
}
|
|
|
|
|
|
|
|
QVERIFY( CalaPM::init() );
|
2014-08-05 14:57:00 +02:00
|
|
|
FileSystemFactory::init();
|
|
|
|
|
|
|
|
refreshDevice();
|
|
|
|
}
|
2014-07-17 17:57:55 +02:00
|
|
|
|
2014-08-05 14:57:00 +02:00
|
|
|
void
|
|
|
|
JobTests::refreshDevice()
|
|
|
|
{
|
|
|
|
QString devicePath = qgetenv( "CALAMARES_TEST_DISK" );
|
2014-07-17 17:57:55 +02:00
|
|
|
CoreBackend* backend = CoreBackendManager::self()->backend();
|
2014-07-23 14:34:06 +02:00
|
|
|
m_device.reset( backend->scanDevice( devicePath ) );
|
2014-07-17 17:57:55 +02:00
|
|
|
QVERIFY( !m_device.isNull() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JobTests::testPartitionTable()
|
|
|
|
{
|
2014-07-18 18:37:44 +02:00
|
|
|
queuePartitionTableCreation( PartitionTable::msdos );
|
|
|
|
QVERIFY( m_runner.run() );
|
|
|
|
QVERIFY( m_device->partitionTable() );
|
|
|
|
QVERIFY( firstFreePartition( m_device->partitionTable() ) );
|
|
|
|
|
2014-07-18 10:51:05 +02:00
|
|
|
queuePartitionTableCreation( PartitionTable::gpt );
|
2014-07-18 18:37:44 +02:00
|
|
|
QVERIFY( m_runner.run() );
|
|
|
|
QVERIFY( m_device->partitionTable() );
|
|
|
|
QVERIFY( firstFreePartition( m_device->partitionTable() ) );
|
2014-07-17 17:57:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-18 10:51:05 +02:00
|
|
|
JobTests::queuePartitionTableCreation( PartitionTable::TableType type)
|
2014-07-17 17:57:55 +02:00
|
|
|
{
|
2014-07-18 10:51:05 +02:00
|
|
|
auto job = new CreatePartitionTableJob( m_device.data(), type );
|
2014-07-17 17:57:55 +02:00
|
|
|
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();
|
2014-07-18 12:11:42 +02:00
|
|
|
qint64 lastSector;
|
|
|
|
|
|
|
|
if ( size > 0 )
|
|
|
|
lastSector = firstSector + size / m_device->logicalSectorSize();
|
|
|
|
else
|
|
|
|
lastSector = freeSpacePartition->lastSector();
|
2014-07-17 17:57:55 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JobTests::testCreatePartition()
|
|
|
|
{
|
2014-07-18 10:51:05 +02:00
|
|
|
queuePartitionTableCreation( PartitionTable::gpt );
|
2014-07-17 17:57:55 +02:00
|
|
|
CreatePartitionJob* job;
|
2014-07-23 14:34:06 +02:00
|
|
|
Partition* freePartition;
|
2014-07-17 17:57:55 +02:00
|
|
|
|
2014-07-23 14:34:06 +02:00
|
|
|
freePartition = firstFreePartition( m_device->partitionTable() );
|
|
|
|
QVERIFY( freePartition );
|
|
|
|
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 1 * MB);
|
|
|
|
Partition* partition1 = job->partition();
|
|
|
|
QVERIFY( partition1 );
|
2014-07-17 17:57:55 +02:00
|
|
|
job->updatePreview();
|
|
|
|
m_queue.enqueue( job_ptr( job ) );
|
|
|
|
|
2014-07-23 14:34:06 +02:00
|
|
|
freePartition = firstFreePartition( m_device->partitionTable() );
|
|
|
|
QVERIFY( freePartition );
|
|
|
|
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::LinuxSwap, 1 * MB);
|
|
|
|
Partition* partition2 = job->partition();
|
|
|
|
QVERIFY( partition2 );
|
2014-07-17 17:57:55 +02:00
|
|
|
job->updatePreview();
|
|
|
|
m_queue.enqueue( job_ptr( job ) );
|
|
|
|
|
2014-07-23 14:34:06 +02:00
|
|
|
freePartition = firstFreePartition( m_device->partitionTable() );
|
|
|
|
QVERIFY( freePartition );
|
|
|
|
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Fat32, 1 * MB);
|
|
|
|
Partition* partition3 = job->partition();
|
|
|
|
QVERIFY( partition3 );
|
2014-07-17 17:57:55 +02:00
|
|
|
job->updatePreview();
|
|
|
|
m_queue.enqueue( job_ptr( job ) );
|
|
|
|
|
2014-07-18 11:27:03 +02:00
|
|
|
QVERIFY( m_runner.run() );
|
2014-07-23 14:34:06 +02:00
|
|
|
|
|
|
|
// Check partitionPath has been set. It is not known until the job has
|
|
|
|
// executed.
|
|
|
|
QString devicePath = m_device->deviceNode();
|
|
|
|
QCOMPARE( partition1->partitionPath(), devicePath + "1" );
|
|
|
|
QCOMPARE( partition2->partitionPath(), devicePath + "2" );
|
|
|
|
QCOMPARE( partition3->partitionPath(), devicePath + "3" );
|
2014-07-17 17:57:55 +02:00
|
|
|
}
|
2014-07-18 10:51:05 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
JobTests::testCreatePartitionExtended()
|
|
|
|
{
|
|
|
|
queuePartitionTableCreation( PartitionTable::msdos );
|
|
|
|
CreatePartitionJob* job;
|
2014-07-23 14:34:06 +02:00
|
|
|
Partition* freePartition;
|
2014-07-18 10:51:05 +02:00
|
|
|
|
2014-07-23 14:34:06 +02:00
|
|
|
freePartition = firstFreePartition( m_device->partitionTable() );
|
|
|
|
QVERIFY( freePartition );
|
|
|
|
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 10 * MB);
|
|
|
|
Partition* partition1 = job->partition();
|
|
|
|
QVERIFY( partition1 );
|
2014-07-18 10:51:05 +02:00
|
|
|
job->updatePreview();
|
|
|
|
m_queue.enqueue( job_ptr( job ) );
|
|
|
|
|
2014-07-23 14:34:06 +02:00
|
|
|
freePartition = firstFreePartition( m_device->partitionTable() );
|
|
|
|
QVERIFY( freePartition );
|
|
|
|
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Extended ), FileSystem::Extended, 10 * MB);
|
2014-07-18 10:51:05 +02:00
|
|
|
job->updatePreview();
|
|
|
|
m_queue.enqueue( job_ptr( job ) );
|
|
|
|
Partition* extendedPartition = job->partition();
|
|
|
|
|
2014-07-23 14:34:06 +02:00
|
|
|
freePartition = firstFreePartition( extendedPartition );
|
|
|
|
QVERIFY( freePartition );
|
|
|
|
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Logical ), FileSystem::Ext4, 0);
|
|
|
|
Partition* partition2 = job->partition();
|
|
|
|
QVERIFY( partition2 );
|
2014-07-18 10:51:05 +02:00
|
|
|
job->updatePreview();
|
|
|
|
m_queue.enqueue( job_ptr( job ) );
|
|
|
|
|
2014-07-18 11:27:03 +02:00
|
|
|
QVERIFY( m_runner.run() );
|
2014-07-23 14:34:06 +02:00
|
|
|
|
|
|
|
// Check partitionPath has been set. It is not known until the job has
|
|
|
|
// executed.
|
|
|
|
QString devicePath = m_device->deviceNode();
|
|
|
|
QCOMPARE( partition1->partitionPath(), devicePath + "1" );
|
|
|
|
QCOMPARE( extendedPartition->partitionPath(), devicePath + "2" );
|
|
|
|
QCOMPARE( partition2->partitionPath(), devicePath + "5" );
|
2014-07-18 10:51:05 +02:00
|
|
|
}
|
2014-08-05 14:57:00 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
JobTests::testResizePartition_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn< int >( "oldStartMB" );
|
|
|
|
QTest::addColumn< int >( "oldSizeMB" );
|
|
|
|
QTest::addColumn< int >( "newStartMB" );
|
|
|
|
QTest::addColumn< int >( "newSizeMB" );
|
|
|
|
|
|
|
|
QTest::newRow("grow") << 10 << 5 << 10 << 7;
|
|
|
|
QTest::newRow("shrink") << 10 << 6 << 10 << 3;
|
|
|
|
QTest::newRow("moveLeft") << 10 << 5 << 8 << 5;
|
|
|
|
QTest::newRow("moveRight") << 10 << 5 << 12 << 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JobTests::testResizePartition()
|
|
|
|
{
|
|
|
|
QFETCH( int, oldStartMB );
|
|
|
|
QFETCH( int, oldSizeMB );
|
|
|
|
QFETCH( int, newStartMB );
|
|
|
|
QFETCH( int, newSizeMB );
|
|
|
|
|
|
|
|
const qint64 sectorSize = m_device->logicalSectorSize();
|
|
|
|
const qint64 sectorForMB = MB / sectorSize;
|
|
|
|
|
|
|
|
qint64 oldFirst = sectorForMB * oldStartMB;
|
|
|
|
qint64 oldLast = oldFirst + sectorForMB * oldSizeMB - 1;
|
|
|
|
qint64 newFirst = sectorForMB * newStartMB;
|
|
|
|
qint64 newLast = newFirst + sectorForMB * newSizeMB - 1;
|
|
|
|
|
|
|
|
// Setup: create the test partition
|
|
|
|
{
|
|
|
|
queuePartitionTableCreation( PartitionTable::msdos );
|
|
|
|
|
|
|
|
Partition* freePartition = firstFreePartition( m_device->partitionTable() );
|
|
|
|
QVERIFY( freePartition );
|
|
|
|
Partition* partition = PMUtils::createNewPartition( freePartition->parent(), *m_device, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, oldFirst, oldLast );
|
|
|
|
CreatePartitionJob* job = new CreatePartitionJob( m_device.data(), partition );
|
|
|
|
job->updatePreview();
|
|
|
|
m_queue.enqueue( job_ptr( job ) );
|
|
|
|
|
|
|
|
QVERIFY( m_runner.run() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize
|
|
|
|
{
|
|
|
|
refreshDevice();
|
|
|
|
QVERIFY( m_device->partitionTable() );
|
|
|
|
Partition* partition = m_device->partitionTable()->findPartitionBySector( oldFirst, PartitionRole( PartitionRole::Primary ) );
|
|
|
|
QVERIFY( partition );
|
|
|
|
|
|
|
|
ResizePartitionJob* job = new ResizePartitionJob( m_device.data(), partition, newFirst, newLast );
|
|
|
|
QVERIFY( m_runner.run() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
{
|
|
|
|
refreshDevice();
|
|
|
|
QVERIFY( m_device->partitionTable() );
|
|
|
|
Partition* partition = m_device->partitionTable()->findPartitionBySector( newFirst, PartitionRole( PartitionRole::Primary ) );
|
|
|
|
QVERIFY( partition );
|
|
|
|
QCOMPARE( partition->firstSector(), newFirst );
|
|
|
|
QCOMPARE( partition->lastSector(), newLast );
|
|
|
|
QCOMPARE( partition->fileSystem().firstSector(), newFirst );
|
|
|
|
QCOMPARE( partition->fileSystem().lastSector(), newLast );
|
|
|
|
}
|
|
|
|
}
|