Commit b2fd8e23 authored by David Redondo's avatar David Redondo 🏎
Browse files

Add migration for old disk and partitions sensors

Because the ids of the objects changed we cannot do it with just regexes.
Also make the test data driven.
parent 320686e2
......@@ -45,6 +45,7 @@ ecm_add_test(sensorfacecontrollertest.cpp LINK_LIBRARIES
KF5::I18n
KF5::Package
KF5::Declarative
KF5::Solid
KSysGuard::SensorFaces
)
......
......@@ -22,7 +22,13 @@
#include <QJsonDocument>
#include <QJsonArray>
#include <KConfig>
#include <Solid/Block>
#include <Solid/Device>
#include <Solid/Predicate>
#include <Solid/StorageAccess>
#include <Solid/StorageVolume>
#include "SensorFaceController.h"
......@@ -32,37 +38,80 @@ class SensorFaceControllerTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testSensorIdConversion()
void testSensorIdConversion_data()
{
QJsonArray originalTotalSensors = {
QTest::addColumn<QJsonArray>("oldSensors");
QTest::addColumn<QJsonArray>("expectedSensors");
QTest::addRow("network")
<< QJsonArray {
QStringLiteral("network/all/sentDataRate"),
QStringLiteral("network/all/totalReceivedData"),
QStringLiteral("network/interfaces/test/receiver/data"),
QStringLiteral("network/interfaces/test/transmitter/dataTotal"),
};
QJsonArray expectedTotalSensors = {
}
<< QJsonArray {
QStringLiteral("network/all/upload"),
QStringLiteral("network/all/totalDownload"),
QStringLiteral("network/test/download"),
QStringLiteral("network/test/totalUpload"),
};
const auto storageAccesses = Solid::Device::listFromQuery(Solid::Predicate(Solid::DeviceInterface::StorageAccess, QStringLiteral("accessible"), true));
for (int i = 0; i < storageAccesses.size(); ++i) {
const auto storageAccess = storageAccesses[i].as<Solid::StorageAccess>();
const auto blockDevice = storageAccesses[i].as<Solid::Block>();
const auto storageVolume = storageAccesses[i].as<Solid::StorageVolume>();
const QString newPrefix = QStringLiteral("disk/") + (storageVolume->uuid().isEmpty() ? storageVolume->label() : storageVolume->uuid());
// Old code uses "disk/sdc2_(8:34)/..."
QString device = blockDevice->device().mid(strlen("/dev/"));
const QString diskPrefix = QStringLiteral("disk/%1_(%2:%3)").arg(device).arg(blockDevice->deviceMajor()).arg(blockDevice->deviceMinor());
QTest::addRow("disk%d",i)
<< QJsonArray {
{diskPrefix + QStringLiteral("/Rate/rio")},
{diskPrefix + QStringLiteral("/Rate/wio")},
}
<< QJsonArray {
{newPrefix + QStringLiteral("/read")},
{newPrefix + QStringLiteral("/write")},
};
// Old code uses "partitions/mountPath/..."
const QString mountPath = storageAccess->filePath() == QLatin1String("/") ? QStringLiteral("/__root__") : storageAccess->filePath();
QString partitionPrefix = QStringLiteral("partitions") + mountPath;
QTest::addRow("partition%d", i)
<< QJsonArray {
{partitionPrefix + QStringLiteral("/total")},
{partitionPrefix + QStringLiteral("/freespace")},
{partitionPrefix + QStringLiteral("/filllevel")},
{partitionPrefix + QStringLiteral("/usedspace")},
} << QJsonArray {
{newPrefix + QStringLiteral("/total")},
{newPrefix + QStringLiteral("/free")},
{newPrefix + QStringLiteral("/usedPercent")},
{newPrefix + QStringLiteral("/used")},
};
}
}
void testSensorIdConversion()
{
QFETCH(QJsonArray, oldSensors);
QFETCH(QJsonArray, expectedSensors);
KConfig config;
auto sensorsGroup = config.group("Sensors");
sensorsGroup.writeEntry("totalSensors", QJsonDocument{originalTotalSensors}.toJson(QJsonDocument::Compact));
sensorsGroup.writeEntry("sensors", QJsonDocument{oldSensors}.toJson(QJsonDocument::Compact));
KSysGuard::SensorFaceControllerPrivate d;
auto sensors = d.readAndUpdateSensors(sensorsGroup, QStringLiteral("totalSensors"));
auto sensors = d.readAndUpdateSensors(sensorsGroup, QStringLiteral("sensors"));
QCOMPARE(sensors.size(), expectedSensors.size());
QCOMPARE(sensors.size(), 4);
QCOMPARE(sensors.at(0), expectedTotalSensors.at(0));
QCOMPARE(sensors.at(1), expectedTotalSensors.at(1));
QCOMPARE(sensors.at(2), expectedTotalSensors.at(2));
QCOMPARE(sensors.at(3), expectedTotalSensors.at(3));
for (int i = 0; i < sensors.size(); ++i) {
QCOMPARE(sensors.at(i), expectedSensors.at(i));
}
auto newEntry = sensorsGroup.readEntry("totalSensors");
QCOMPARE(newEntry.toUtf8(), QJsonDocument{expectedTotalSensors}.toJson(QJsonDocument::Compact));
auto newEntry = sensorsGroup.readEntry("sensors");
QCOMPARE(newEntry.toUtf8(), QJsonDocument{expectedSensors}.toJson(QJsonDocument::Compact));
}
};
......
......@@ -50,6 +50,7 @@ target_link_libraries(SensorFaces
KF5::I18n
KF5::Package
KF5::Declarative
KF5::Solid
)
set_target_properties(SensorFaces PROPERTIES
......
......@@ -31,6 +31,11 @@
#include <KLocalizedString>
#include <KConfigLoader>
#include <KPluginMetaData>
#include <Solid/Block>
#include <Solid/Device>
#include <Solid/Predicate>
#include <Solid/StorageAccess>
#include <Solid/StorageVolume>
using namespace KSysGuard;
......@@ -133,6 +138,8 @@ QHash<int, QByteArray> PresetsModel::roleNames() const
}
QVector<QPair<QRegularExpression, QString>> KSysGuard::SensorFaceControllerPrivate::sensorIdReplacements;
QRegularExpression SensorFaceControllerPrivate::oldDiskSensor = QRegularExpression(QStringLiteral("^disk\\/(.+)_\\(\\d+:\\d+\\)"));
QRegularExpression SensorFaceControllerPrivate::oldPartitionSensor = QRegularExpression(QStringLiteral("^partitions(\\/.+)\\/"));
SensorFaceControllerPrivate::SensorFaceControllerPrivate()
{
......@@ -151,10 +158,52 @@ SensorFaceControllerPrivate::SensorFaceControllerPrivate()
{ QRegularExpression(QStringLiteral("(.*)/transmitter/data$")), QStringLiteral("\\1/upload")},
{ QRegularExpression(QStringLiteral("(.*)/receiver/dataTotal$")), QStringLiteral("\\1/totalDownload")},
{ QRegularExpression(QStringLiteral("(.*)/transmitter/dataTotal$")), QStringLiteral("\\1/totalUpload")},
{ QRegularExpression(QStringLiteral("(.*)/Rate/rio")), QStringLiteral("\\1/read")},
{ QRegularExpression(QStringLiteral("(.*)/Rate/wio$")), QStringLiteral("\\1/write")},
{ QRegularExpression(QStringLiteral("(.*)/freespace$")), QStringLiteral("\\1/free")},
{ QRegularExpression(QStringLiteral("(.*)/filllevel$")), QStringLiteral("\\1/usedPercent")},
{ QRegularExpression(QStringLiteral("(.*)/usedspace$")), QStringLiteral("\\1/used")},
};
}
}
QString SensorFaceControllerPrivate::replaceDiskId(const QString &entryName) const
{
const auto match = oldDiskSensor.match(entryName);
if (!match.hasMatch()) {
return entryName;
}
const QString device = match.captured(1);
Solid::Predicate predicate(Solid::DeviceInterface::StorageAccess);
predicate &= Solid::Predicate(Solid::DeviceInterface::Block, QStringLiteral("device"), QStringLiteral("/dev/%1").arg(device));
const auto devices = Solid::Device::listFromQuery(predicate);
if (devices.empty()) {
return QString();
}
QString sensorId = entryName;
const auto volume = devices[0].as<Solid::StorageVolume>();
const QString id = volume->uuid().isEmpty() ? volume->label() : volume->uuid();
return sensorId.replace(match.captured(0), QStringLiteral("disk/") + id);
}
QString SensorFaceControllerPrivate::replacePartitionId(const QString &entryName) const
{
const auto match = oldPartitionSensor.match(entryName);
if (!match.hasMatch()) {
return entryName;
}
const QString filePath = match.captured(1) == QLatin1String("/__root__") ? QStringLiteral("/") : match.captured(1);
const Solid::Predicate predicate(Solid::DeviceInterface::StorageAccess, QStringLiteral("filePath"), filePath);
const auto devices = Solid::Device::listFromQuery(predicate);
if (devices.empty()) {
return entryName;
}
QString sensorId = entryName;
const auto volume = devices[0].as<Solid::StorageVolume>();
const QString id = volume->uuid().isEmpty() ? volume->label() : volume->uuid();
return sensorId.replace(match.captured(0), QStringLiteral("disk/%1/").arg(id));
}
QJsonArray SensorFaceControllerPrivate::readSensors(const KConfigGroup &read, const QString &entryName)
{
auto original = QJsonDocument::fromJson(read.readEntry(entryName, QString()).toUtf8()).array();
......@@ -167,6 +216,8 @@ QJsonArray SensorFaceControllerPrivate::readSensors(const KConfigGroup &read, co
sensorId.replace(replacement.first, replacement.second);
}
}
sensorId = replaceDiskId(sensorId);
sensorId = replacePartitionId(sensorId);
newSensors.append(sensorId);
}
......
......@@ -82,6 +82,8 @@ public:
QJsonArray readSensors(const KConfigGroup &config, const QString &entryName);
QJsonArray readAndUpdateSensors(KConfigGroup &config, const QString &entryName);
QString replaceDiskId(const QString &entryname) const;
QString replacePartitionId(const QString &entryname) const;
QJsonArray resolveSensors(const QJsonArray &partialEntries);
SensorFace *createGui(const QString &qmlPath);
QQuickItem *createConfigUi(const QString &file, const QVariantMap &initialProperties);
......@@ -118,6 +120,8 @@ public:
PresetsModel *availablePresetsModel = nullptr;
static QVector<QPair<QRegularExpression, QString>> sensorIdReplacements;
static QRegularExpression oldDiskSensor;
static QRegularExpression oldPartitionSensor;
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment