File indexing completed on 2024-05-12 04:39:49

0001 /*
0002     This file was partly taken from KDevelop's cvs plugin
0003     SPDX-FileCopyrightText: 2017 Aleix Pol Gonzalez <aleixpol@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include <tests/autotestshell.h>
0009 #include <tests/testproject.h>
0010 #include <tests/testcore.h>
0011 #include <interfaces/iplugincontroller.h>
0012 #include <interfaces/iruntimecontroller.h>
0013 #include <interfaces/iruntime.h>
0014 #include <project/projectmodel.h>
0015 
0016 #include <QJsonObject>
0017 #include <QLoggingCategory>
0018 #include <QProcess>
0019 #include <QSignalSpy>
0020 #include <QTest>
0021 
0022 using namespace KDevelop;
0023 
0024 static QString s_testedImage = QStringLiteral("ubuntu:17.04");
0025 
0026 class DockerTest: public QObject
0027 {
0028     Q_OBJECT
0029 public:
0030 
0031     DockerTest() {
0032         QLoggingCategory::setFilterRules(QStringLiteral("*.debug=false\ndefault.debug=true\nkdevelop.plugins.docker=true\n"));
0033     }
0034 
0035     IRuntime* m_initialRuntime = nullptr;
0036 
0037 private Q_SLOTS:
0038     void initTestCase() {
0039         auto ret = QProcess::execute("docker", {"pull", s_testedImage});
0040         if (ret != 0) {
0041             QSKIP("Couldn't successfully call docker");
0042             return;
0043         }
0044 
0045         AutoTestShell::init({QStringLiteral("kdevdocker"), QStringLiteral("KDevGenericManager")});
0046         TestCore::initialize();
0047 
0048         m_initialRuntime = ICore::self()->runtimeController()->currentRuntime();
0049 
0050         auto plugin = ICore::self()->pluginController()->loadPlugin("kdevdocker");
0051         QVERIFY(plugin);
0052 
0053         QSignalSpy spy(plugin, SIGNAL(imagesListed()));
0054         QVERIFY(spy.wait());
0055 
0056         auto projectPath = QUrl::fromLocalFile(QFINDTESTDATA("testproject/test.kdev4"));
0057         TestCore::self()->projectController()->openProject(projectPath);
0058         QSignalSpy spy2(TestCore::self()->projectController(), &IProjectController::projectOpened);
0059         QVERIFY(spy2.wait());
0060     }
0061 
0062     void init()
0063     {
0064         QVERIFY(ICore::self()->runtimeController()->currentRuntime() == m_initialRuntime);
0065         const auto& availableRuntimes = ICore::self()->runtimeController()->availableRuntimes();
0066         for (IRuntime* runtime : availableRuntimes) {
0067             if (s_testedImage == runtime->name()) {
0068                 ICore::self()->runtimeController()->setCurrentRuntime(runtime);
0069             }
0070         }
0071         QVERIFY(ICore::self()->runtimeController()->currentRuntime() != m_initialRuntime);
0072     }
0073 
0074     void paths() {
0075         auto rt = ICore::self()->runtimeController()->currentRuntime();
0076         QVERIFY(rt);
0077 
0078         const Path root("/");
0079         const Path hostDir = rt->pathInHost(root);
0080         QCOMPARE(root, rt->pathInRuntime(hostDir));
0081     }
0082 
0083     void projectPath() {
0084         auto rt = ICore::self()->runtimeController()->currentRuntime();
0085         QVERIFY(rt);
0086         auto project = ICore::self()->projectController()->projects().first();
0087         QVERIFY(project);
0088 
0089         const Path file = project->projectItem()->folder()->fileList().first()->path();
0090         const Path fileRuntime = rt->pathInRuntime(file);
0091         QCOMPARE(fileRuntime, Path("/src/test/testfile.sh"));
0092         QCOMPARE(rt->pathInHost(fileRuntime), file);
0093         QCOMPARE(project->path(), rt->pathInHost(rt->pathInRuntime(project->path())));
0094     }
0095 
0096     void projectDirectory() {
0097         auto rt = ICore::self()->runtimeController()->currentRuntime();
0098         QVERIFY(rt);
0099         auto project = ICore::self()->projectController()->projects().first();
0100         QVERIFY(project);
0101 
0102         const Path projectDir = project->path();
0103         const Path dirRuntime = rt->pathInRuntime(projectDir);
0104         QCOMPARE(dirRuntime, Path("/src/test/"));
0105         QCOMPARE(rt->pathInHost(dirRuntime), projectDir);
0106         QCOMPARE(project->path(), rt->pathInHost(rt->pathInRuntime(project->path())));
0107     }
0108 
0109     void envs() {
0110         auto rt = ICore::self()->runtimeController()->currentRuntime();
0111         QVERIFY(rt);
0112 
0113         QCOMPARE(rt->getenv("PATH"), QByteArray("/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"));
0114     }
0115 
0116     void runProcess() {
0117         auto rt = ICore::self()->runtimeController()->currentRuntime();
0118         QVERIFY(rt);
0119         auto project = ICore::self()->projectController()->projects().first();
0120         QVERIFY(project);
0121 
0122         const Path projectPath = rt->pathInRuntime(project->path());
0123         QProcess process;
0124         process.setProgram("ls");
0125         process.setArguments({projectPath.toLocalFile()});
0126         rt->startProcess(&process);
0127         QVERIFY(process.waitForFinished());
0128         QCOMPARE(process.exitCode(), 0);
0129         QCOMPARE(process.readAll(), QByteArray("test.kdev4\ntestfile.sh\n"));
0130     }
0131 
0132     void cleanup() {
0133         ICore::self()->runtimeController()->setCurrentRuntime(m_initialRuntime);
0134     }
0135 
0136 };
0137 
0138 QTEST_MAIN( DockerTest )
0139 
0140 #include "test_docker.moc"