aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/devcontainer/devcontainer_test.cpp
blob: 63b35bd5550a770ea6337eb623c315203914af03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "devcontainerplugin_constants.h"

#include <coreplugin/messagemanager.h>

#include <devcontainer/devcontainer.h>

#include <projectexplorer/kitmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectmanager.h>

#include <utils/algorithm.h>
#include <utils/filepath.h>
#include <utils/infobar.h>
#include <utils/mimeutils.h>
#include <utils/qtcprocess.h>

#include <coreplugin/icore.h>

#include <QTest>
#include <QSignalSpy>

using namespace Utils;

namespace DevContainer::Internal {

static bool testDocker(const FilePath &executable)
{
    Process p;
    p.setCommand({executable, {"info", "--format", "{{.OSType}}"}});
    p.runBlocking();
    const QString platform = p.cleanedStdOut().trimmed();
    return p.result() == ProcessResult::FinishedWithSuccess && platform == "linux";
}

static bool testDockerMount(const FilePath &executable, const FilePath &testDir)
{
    Process p;
    p.setCommand(
        {executable,
         {"run",
          "--rm",
          "--mount",
          "type=bind,source=" + testDir.path() + ",target=/mnt/test",
          "alpine:latest",
          "ls",
          "/mnt/test"}});
    p.runBlocking();
    if (p.result() != ProcessResult::FinishedWithSuccess) {
        qWarning() << "Docker mount test failed:" << p.verboseExitMessage();
        return false;
    }
    return p.result() == ProcessResult::FinishedWithSuccess;
}

class Tests : public QObject
{
    Q_OBJECT

    const FilePath testData{TESTDATA};

signals:
    void deviceUpDone();

private slots:
    void initTestCase()
    {
        if (!testDocker("docker"))
            QSKIP("Docker is not set up correctly, skipping tests.");

        if (!testDockerMount("docker", testData))
            QSKIP("Docker mount test failed, skipping tests.");

        Core::MessageManager::writeDisrupting("Starting DevContainer tests...");
    }

    void testSimpleProject()
    {
        const auto cmakelists = testData / "simpleproject" / "CMakeLists.txt";
        ProjectExplorer::OpenProjectResult opr
            = ProjectExplorer::ProjectExplorerPlugin::openProject(cmakelists);

        QVERIFY(opr);

        QSignalSpy deviceAddedSpy(this, &Tests::deviceUpDone);

        InstanceConfig instanceConfig;
        instanceConfig.configFilePath = testData / "simpleproject" / ".devcontainer"
                                        / "devcontainer.json";
        instanceConfig.workspaceFolder = opr.project()->projectDirectory();

        const auto infoBarEntryId = Utils::Id::fromString(QString(
            "DevContainer.Instantiate.InfoBar." + instanceConfig.workspaceFolder.toUrlishString()));

        Utils::InfoBar *infoBar = Core::ICore::popupInfoBar();
        QVERIFY(infoBar->containsInfo(infoBarEntryId));
        Utils::InfoBarEntry entry
            = Utils::findOrDefault(infoBar->entries(), [infoBarEntryId](const Utils::InfoBarEntry &e) {
                  return e.id() == infoBarEntryId;
              });

        QCOMPARE(entry.id(), infoBarEntryId);
        QCOMPARE(entry.buttons().size(), 1);
        auto yesButton = entry.buttons().first();

        // Trigger loading the DevContainer instance
        yesButton.callback();

        using namespace std::chrono_literals;
        QVERIFY(deviceAddedSpy.wait(
            std::chrono::duration_cast<std::chrono::milliseconds>(1min).count()));

        FilePath expectedRootPath = FilePath::fromParts(
            Constants::DEVCONTAINER_FS_SCHEME, instanceConfig.devContainerId(), u"/");
        QVERIFY(expectedRootPath.exists());
        QVERIFY(!infoBar->containsInfo(infoBarEntryId));

        FilePath expectedLibExecMountPoint = FilePath::fromParts(
            Constants::DEVCONTAINER_FS_SCHEME,
            instanceConfig.devContainerId(),
            u"/custom/libexec/mnt/point");
        QVERIFY(expectedLibExecMountPoint.exists());
        QVERIFY(expectedLibExecMountPoint.isReadableDir());

        FilePath expectedWorkspaceFolder = FilePath::fromParts(
            Constants::DEVCONTAINER_FS_SCHEME,
            instanceConfig.devContainerId(),
            u"/custom/workspace");
        QVERIFY(expectedWorkspaceFolder.exists());
        QVERIFY(expectedWorkspaceFolder.isReadableDir());

        FilePath expectedMainCpp = expectedWorkspaceFolder / "main.cpp";
        QVERIFY(expectedMainCpp.exists());
        QVERIFY(expectedMainCpp.isReadableFile());

        QCOMPARE(
            expectedRootPath.withNewMappedPath(testData / "simpleproject" / "main.cpp"),
            expectedMainCpp);
        QVERIFY(expectedRootPath.ensureReachable(expectedMainCpp));
        QCOMPARE(expectedMainCpp.localSource(), testData / "simpleproject" / "main.cpp");

        ProjectExplorer::ProjectManager::removeProject(opr.project());
        QVERIFY(!expectedRootPath.exists());
    }

    void testWithKit()
    {
        const auto cmakelists = testData / "withkit" / "CMakeLists.txt";
        ProjectExplorer::OpenProjectResult opr
            = ProjectExplorer::ProjectExplorerPlugin::openProject(cmakelists);

        QVERIFY(opr);

        QSignalSpy deviceAddedSpy(this, &Tests::deviceUpDone);

        InstanceConfig instanceConfig;
        instanceConfig.configFilePath = testData / "withkit" / ".devcontainer"
                                        / "devcontainer.json";
        instanceConfig.workspaceFolder = opr.project()->projectDirectory();

        const auto infoBarEntryId = Utils::Id::fromString(QString(
            "DevContainer.Instantiate.InfoBar." + instanceConfig.workspaceFolder.toUrlishString()));

        Utils::InfoBar *infoBar = Core::ICore::popupInfoBar();
        QVERIFY(infoBar->containsInfo(infoBarEntryId));
        Utils::InfoBarEntry entry
            = Utils::findOrDefault(infoBar->entries(), [infoBarEntryId](const Utils::InfoBarEntry &e) {
                  return e.id() == infoBarEntryId;
              });

        QCOMPARE(entry.id(), infoBarEntryId);
        QCOMPARE(entry.buttons().size(), 1);
        auto yesButton = entry.buttons().first();

        // Trigger loading the DevContainer instance
        yesButton.callback();

        using namespace std::chrono_literals;
        QVERIFY(deviceAddedSpy.wait(
            std::chrono::duration_cast<std::chrono::milliseconds>(10min).count()));

        FilePath expectedRootPath
            = FilePath::fromParts(u"devcontainer", instanceConfig.devContainerId(), u"/");
        QVERIFY(expectedRootPath.exists());
        QVERIFY(!infoBar->containsInfo(infoBarEntryId));

        auto kit1 = Utils::findOrDefault(
            ProjectExplorer::KitManager::instance()->kits(),
            [](ProjectExplorer::Kit *k) { return k->displayName() == QLatin1String("DevKit1"); });
        QVERIFY(kit1);
        auto kit2 = Utils::findOrDefault(
            ProjectExplorer::KitManager::instance()->kits(),
            [](ProjectExplorer::Kit *k) { return k->displayName() == QLatin1String("DevKit2"); });
        QVERIFY(kit2);
    }
};

QObject *createDevcontainerTest()
{
    return new Tests;
}

} // namespace DevContainer::Internal

#include "devcontainer_test.moc"