[partition] Code-formatting for tests

This commit is contained in:
Adriaan de Groot 2019-06-24 12:37:00 +02:00
parent ecdd09edcd
commit b1ccd573f8
2 changed files with 66 additions and 54 deletions

View File

@ -24,10 +24,10 @@
#include "utils/Logger.h"
#include "utils/Units.h"
#include <core/KPMHelpers.h>
#include <jobs/CreatePartitionJob.h>
#include <jobs/CreatePartitionTableJob.h>
#include <jobs/ResizePartitionJob.h>
#include <core/KPMHelpers.h>
// CalaPM
#include <backend/corebackend.h>
@ -59,15 +59,14 @@ public:
~PartitionMounter()
{
if ( !m_mounted )
{
return;
}
int ret = QProcess::execute( "umount", QStringList() << m_mountPointDir.path() );
QCOMPARE( ret, 0 );
}
QString mountPoint() const
{
return m_mounted ? m_mountPointDir.path() : QString();
}
QString mountPoint() const { return m_mounted ? m_mountPointDir.path() : QString(); }
private:
QString m_devicePath;
@ -80,9 +79,9 @@ static QByteArray
generateTestData( qint64 size )
{
QByteArray ba;
ba.resize( static_cast<int>( size ) );
ba.resize( static_cast< int >( size ) );
// Fill the array explicitly to keep Valgrind happy
for ( auto it = ba.data() ; it < ba.data() + size ; ++it )
for ( auto it = ba.data(); it < ba.data() + size; ++it )
{
*it = char( rand() & 0xff );
}
@ -105,9 +104,9 @@ writeFile( const QString& path, const QByteArray data )
if ( count < 0 )
{
QString msg = QString( "Writing file failed. Only %1 bytes written out of %2. Error: '%3'." )
.arg( ptr - data.constData() )
.arg( data.size() )
.arg( file.errorString() );
.arg( ptr - data.constData() )
.arg( data.size() )
.arg( file.errorString() );
QFAIL( qPrintable( msg ) );
}
ptr += count;
@ -117,9 +116,11 @@ writeFile( const QString& path, const QByteArray data )
static Partition*
firstFreePartition( PartitionNode* parent )
{
for( auto child : parent->children() )
for ( auto child : parent->children() )
if ( isPartitionFreeSpace( child ) )
{
return child;
}
return nullptr;
}
@ -146,7 +147,9 @@ QueueRunner::run()
m_queue->start();
QEventLoop loop;
while ( !m_finished )
{
loop.processEvents();
}
return m_success;
}
@ -169,7 +172,8 @@ CalamaresUtils::Partition::KPMManager* kpmcore = nullptr;
//- PartitionJobTests ------------------------------------------------------------------
PartitionJobTests::PartitionJobTests()
: m_runner( &m_queue )
{}
{
}
void
PartitionJobTests::initTestCase()
@ -178,7 +182,8 @@ PartitionJobTests::initTestCase()
if ( devicePath.isEmpty() )
{
// The 0 is to keep the macro parameters happy
QSKIP( "Skipping test, CALAMARES_TEST_DISK is not set. It should point to a disk which can be safely formatted", 0 );
QSKIP( "Skipping test, CALAMARES_TEST_DISK is not set. It should point to a disk which can be safely formatted",
0 );
}
kpmcore = new CalamaresUtils::Partition::KPMManager();
@ -216,7 +221,7 @@ PartitionJobTests::testPartitionTable()
}
void
PartitionJobTests::queuePartitionTableCreation( PartitionTable::TableType type)
PartitionJobTests::queuePartitionTableCreation( PartitionTable::TableType type )
{
auto job = new CreatePartitionTableJob( m_device.data(), type );
job->updatePreview();
@ -224,7 +229,10 @@ PartitionJobTests::queuePartitionTableCreation( PartitionTable::TableType type)
}
CreatePartitionJob*
PartitionJobTests::newCreatePartitionJob( Partition* freeSpacePartition, PartitionRole role, FileSystem::Type type, qint64 size )
PartitionJobTests::newCreatePartitionJob( Partition* freeSpacePartition,
PartitionRole role,
FileSystem::Type type,
qint64 size )
{
Q_ASSERT( freeSpacePartition );
@ -232,25 +240,27 @@ PartitionJobTests::newCreatePartitionJob( Partition* freeSpacePartition, Partiti
qint64 lastSector;
if ( size > 0 )
{
lastSector = firstSector + size / m_device->logicalSize();
}
else
{
lastSector = freeSpacePartition->lastSector();
FileSystem* fs = FileSystemFactory::create( type, firstSector, lastSector
,m_device->logicalSize()
);
}
FileSystem* fs = FileSystemFactory::create( type, firstSector, lastSector, m_device->logicalSize() );
Partition* partition = new Partition(
freeSpacePartition->parent(),
*m_device,
role,
fs, firstSector, lastSector,
QString() /* path */,
KPM_PARTITION_FLAG(None) /* availableFlags */,
QString() /* mountPoint */,
false /* mounted */,
KPM_PARTITION_FLAG(None) /* activeFlags */,
KPM_PARTITION_STATE(New)
);
Partition* partition = new Partition( freeSpacePartition->parent(),
*m_device,
role,
fs,
firstSector,
lastSector,
QString() /* path */,
KPM_PARTITION_FLAG( None ) /* availableFlags */,
QString() /* mountPoint */,
false /* mounted */,
KPM_PARTITION_FLAG( None ) /* activeFlags */,
KPM_PARTITION_STATE( New ) );
return new CreatePartitionJob( m_device.data(), partition );
}
@ -263,7 +273,7 @@ PartitionJobTests::testCreatePartition()
freePartition = firstFreePartition( m_device->partitionTable() );
QVERIFY( freePartition );
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 1_MiB);
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 1_MiB );
Partition* partition1 = job->partition();
QVERIFY( partition1 );
job->updatePreview();
@ -271,7 +281,7 @@ PartitionJobTests::testCreatePartition()
freePartition = firstFreePartition( m_device->partitionTable() );
QVERIFY( freePartition );
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::LinuxSwap, 1_MiB);
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::LinuxSwap, 1_MiB );
Partition* partition2 = job->partition();
QVERIFY( partition2 );
job->updatePreview();
@ -279,7 +289,7 @@ PartitionJobTests::testCreatePartition()
freePartition = firstFreePartition( m_device->partitionTable() );
QVERIFY( freePartition );
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Fat32, 1_MiB);
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Fat32, 1_MiB );
Partition* partition3 = job->partition();
QVERIFY( partition3 );
job->updatePreview();
@ -304,7 +314,7 @@ PartitionJobTests::testCreatePartitionExtended()
freePartition = firstFreePartition( m_device->partitionTable() );
QVERIFY( freePartition );
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 10_MiB);
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 10_MiB );
Partition* partition1 = job->partition();
QVERIFY( partition1 );
job->updatePreview();
@ -312,14 +322,15 @@ PartitionJobTests::testCreatePartitionExtended()
freePartition = firstFreePartition( m_device->partitionTable() );
QVERIFY( freePartition );
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Extended ), FileSystem::Extended, 10_MiB);
job = newCreatePartitionJob(
freePartition, PartitionRole( PartitionRole::Extended ), FileSystem::Extended, 10_MiB );
job->updatePreview();
m_queue.enqueue( job_ptr( job ) );
Partition* extendedPartition = job->partition();
freePartition = firstFreePartition( extendedPartition );
QVERIFY( freePartition );
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Logical ), FileSystem::Ext4, 0);
job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Logical ), FileSystem::Ext4, 0 );
Partition* partition2 = job->partition();
QVERIFY( partition2 );
job->updatePreview();
@ -343,10 +354,10 @@ PartitionJobTests::testResizePartition_data()
QTest::addColumn< unsigned int >( "newStartMiB" );
QTest::addColumn< unsigned int >( "newSizeMiB" );
QTest::newRow("grow") << 10 << 50 << 10 << 70;
QTest::newRow("shrink") << 10 << 70 << 10 << 50;
QTest::newRow("moveLeft") << 10 << 50 << 8 << 50;
QTest::newRow("moveRight") << 10 << 50 << 12 << 50;
QTest::newRow( "grow" ) << 10 << 50 << 10 << 70;
QTest::newRow( "shrink" ) << 10 << 70 << 10 << 50;
QTest::newRow( "moveLeft" ) << 10 << 50 << 8 << 50;
QTest::newRow( "moveRight" ) << 10 << 50 << 12 << 50;
}
void
@ -360,9 +371,9 @@ PartitionJobTests::testResizePartition()
const qint64 sectorsPerMiB = 1_MiB / m_device->logicalSize();
qint64 oldFirst = sectorsPerMiB * oldStartMiB;
qint64 oldLast = oldFirst + sectorsPerMiB * oldSizeMiB - 1;
qint64 oldLast = oldFirst + sectorsPerMiB * oldSizeMiB - 1;
qint64 newFirst = sectorsPerMiB * newStartMiB;
qint64 newLast = newFirst + sectorsPerMiB * newSizeMiB - 1;
qint64 newLast = newFirst + sectorsPerMiB * newSizeMiB - 1;
// Make the test data file smaller than the full size of the partition to
// accomodate for the file system overhead
@ -376,15 +387,13 @@ PartitionJobTests::testResizePartition()
Partition* freePartition = firstFreePartition( m_device->partitionTable() );
QVERIFY( freePartition );
Partition* partition = KPMHelpers::createNewPartition(
freePartition->parent(),
*m_device,
PartitionRole( PartitionRole::Primary ),
FileSystem::Ext4,
oldFirst,
oldLast,
KPM_PARTITION_FLAG(None)
);
Partition* partition = KPMHelpers::createNewPartition( freePartition->parent(),
*m_device,
PartitionRole( PartitionRole::Primary ),
FileSystem::Ext4,
oldFirst,
oldLast,
KPM_PARTITION_FLAG( None ) );
CreatePartitionJob* job = new CreatePartitionJob( m_device.data(), partition );
job->updatePreview();
m_queue.enqueue( job_ptr( job ) );
@ -396,7 +405,8 @@ PartitionJobTests::testResizePartition()
// Write a test file in the partition
refreshDevice();
QVERIFY( m_device->partitionTable() );
Partition* partition = m_device->partitionTable()->findPartitionBySector( oldFirst, PartitionRole( PartitionRole::Primary ) );
Partition* partition
= m_device->partitionTable()->findPartitionBySector( oldFirst, PartitionRole( PartitionRole::Primary ) );
QVERIFY( partition );
QCOMPARE( partition->firstSector(), oldFirst );
QCOMPARE( partition->lastSector(), oldLast );
@ -421,7 +431,8 @@ PartitionJobTests::testResizePartition()
{
refreshDevice();
QVERIFY( m_device->partitionTable() );
Partition* partition = m_device->partitionTable()->findPartitionBySector( newFirst, PartitionRole( PartitionRole::Primary ) );
Partition* partition
= m_device->partitionTable()->findPartitionBySector( newFirst, PartitionRole( PartitionRole::Primary ) );
QVERIFY( partition );
QCOMPARE( partition->firstSector(), newFirst );
QCOMPARE( partition->lastSector(), newLast );

View File

@ -72,7 +72,8 @@ private:
QueueRunner m_runner;
void queuePartitionTableCreation( PartitionTable::TableType type );
CreatePartitionJob* newCreatePartitionJob( Partition* freeSpacePartition, PartitionRole, FileSystem::Type type, qint64 size );
CreatePartitionJob*
newCreatePartitionJob( Partition* freeSpacePartition, PartitionRole, FileSystem::Type type, qint64 size );
void refreshDevice();
};