diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -62,3 +62,7 @@ # Test SMART support kpm_test(testsmart testsmart.cpp ${SMARTPARSER}) add_test(NAME testsmart COMMAND testsmart ${BACKEND}) + +# Test RAID support +kpm_test(testraid testraid.cpp) +add_test(NAME testraid COMMAND testraid ${BACKEND}) diff --git a/test/testraid.h b/test/testraid.h new file mode 100644 --- /dev/null +++ b/test/testraid.h @@ -0,0 +1,41 @@ + /************************************************************************* + * Copyright (C) 2019 by Shubham * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of * + * the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + + // SPDX-License-Identifier: GPL-3.0+ + +#ifndef TESTRAID_H +#define TESTRAID_H + +#include + +class TestRAID +{ +public: + TestRAID(); + ~TestRAID(); + + bool testIsmdadmInstalled(); + bool testRAIDCreation(); + bool testPartitionSize(); + + //QString getDefaultPartitionPath(); + +private: + QProcess *m_mdadm; +}; + +#endif // TESTRAID_H diff --git a/test/testraid.cpp b/test/testraid.cpp new file mode 100644 --- /dev/null +++ b/test/testraid.cpp @@ -0,0 +1,119 @@ + /************************************************************************* + * Copyright (C) 2019 by Shubham * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of * + * the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + + // SPDX-License-Identifier: GPL-3.0+ + +#include "helpers.h" +#include "testraid.h" + +#include "util/externalcommand.h" +#include "backend/corebackend.h" +#include "backend/corebackendmanager.h" +#include "core/raid/softwareraid.h" + +#include +#include + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + KPMCoreInitializer init; + + if (argc == 2) + init = KPMCoreInitializer(argv[1]); + + return init.isValid() ? true : false; + + CoreBackend *backend = CoreBackendManager::self()->backend(); + + if (!backend) { + qWarning() << "Failed to load backend plugin"; + return false; + } + + TestRAID raid; + + if (!raid.testIsmdadmInstalled() || !testRAIDCreation() /*|| !testPartitionSize()*/) + return false; + + return app.exec(); +} + +TestRAID::TestRAID() : + m_mdadm(nullptr) +{ + +} + +TestRAID::~TestRAID() +{ + m_mdadm->closeWriteChannel(); + m_mdadm->kill(); +} + +bool TestRAID::testIsmdadmInstalled() +{ + m_mdadm->start( QStringLiteral("whereis"), QStringList() << QStringLiteral("mdadm") ); + m_mdadm->waitForFinished(); + + // store QProcess output to later parse it + QByteArray output(m_mdadm->readAllStandardOutput()); + + // Parse "whereis mdadm" output + if (output == QByteArray("mdadm:")) { + qWarning() << "mdadm tool is not installed"; + return false; + } + + return true; +} + +bool TestRAID::testRAIDCreation() +{ + ExternalCommand command(QStringLiteral("mdadm"), { QStringLiteral("--create") }); + + if (!command.run() || command.exitCode() != 0) + return false; + + return true; +} + +/*QString TestRAID::getDefaultPartitionPath() +{ + ExternalCommand cmd(QStringLiteral("df"), { QStringLiteral("-P"), QStringLiteral("/home"), QStringLiteral("|"), + QStringLiteral("awk"), QStringLiteral("\'END{print $1}\'") }); + + if (cmd.run() && cmd.exitCode() == 0) { + return cmd.output(); + } + + return QString(); +} + +bool TestRAID::testPartitionSize() +{ + QString partitionPath = getDefaultPartitionPath(); + + SoftwareRAID sr(QStringLiteral("md")); + + if (sr.partitionSize(partitionPath) == static_cast(-1)) + return false; + + + return true; +}*/