-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_runtime_selection.cpp
More file actions
404 lines (362 loc) · 16.5 KB
/
Copy pathtest_runtime_selection.cpp
File metadata and controls
404 lines (362 loc) · 16.5 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <gtest/gtest.h>
import std;
import mcpp.config;
import mcpp.manifest;
import mcpp.platform;
import mcpp.platform.runtime_binding;
import mcpp.platform.xlings.runtime_selection;
namespace runtime = mcpp::xlings::runtime;
namespace {
mcpp::manifest::Manifest named(std::string name) {
mcpp::manifest::Manifest m;
m.xlings.subosDeclared = true;
m.xlings.subos = std::move(name);
return m;
}
struct RuntimeHome {
std::filesystem::path dir;
mcpp::config::GlobalConfig cfg;
RuntimeHome() {
dir = std::filesystem::temp_directory_path()
/ std::format("mcpp_runtime_selection_{}", std::random_device{}());
cfg.registryDir = dir / "xlings-home";
std::filesystem::create_directories(cfg.registryDir);
}
~RuntimeHome() {
std::error_code ec;
std::filesystem::remove_all(dir, ec);
}
void write(const std::filesystem::path& subos,
std::string_view runtimeName = "glibc@2.39",
std::string_view envValue = "${subosdir}/drivers") const {
std::filesystem::create_directories(subos);
std::ofstream(subos / ".xlings.json") << std::format(R"({{
"subos_info": {{
"schema_version": 1,
"runtime": "{}",
"host_glibc": "2.43",
"envs": {{
"mesa@25": [{{"var":"DRIVERS","op":"prepend","value":"{}"}}]
}},
"runtime_contract": {{
"providers": [{{
"capability": "render.demo",
"provider": {{"namespace":"xim","name":"renderer",
"version":"4.0.0","source":"xim-pkgindex@rev"}}
}}],
"artifacts": [{{
"role":"driver",
"provider": {{"namespace":"xim","name":"renderer",
"version":"4.0.0","source":"xim-pkgindex@rev"}},
"path":"${{subosdir}}/runtime/renderer.bin",
"provenance":"subos_view","abi":"fixture-v1",
"digest":"sha256:def","host_fingerprint":"host-2"
}}]
}}
}},
"workspace": {{}}
}})", runtimeName, envValue);
}
void point_default_glibc_view_at(std::string_view version) const {
auto payload = install_glibc_payload(version);
auto view = cfg.xlingsHome() / "subos" / "default" / "lib";
std::filesystem::create_directories(view);
std::filesystem::create_symlink(
payload / "libc.so.6", view / "libc.so.6");
std::filesystem::create_symlink(
payload / "ld-linux-x86-64.so.2",
view / "ld-linux-x86-64.so.2");
}
std::filesystem::path install_glibc_payload(
std::string_view version) const {
auto payload = cfg.xlingsHome() / "data" / "xpkgs"
/ "xim-x-glibc" / version / "lib";
std::filesystem::create_directories(payload);
std::ofstream(payload / "libc.so.6") << "fixture libc";
std::ofstream(payload / "ld-linux-x86-64.so.2") << "fixture loader";
return payload;
}
};
TEST(RuntimeSelection, AbsenceMeansMcppDefault) {
mcpp::manifest::Manifest root;
auto selected = runtime::select_runtime(root, std::nullopt, "/repo");
ASSERT_TRUE(selected.has_value()) << selected.error();
EXPECT_EQ(selected->mode, runtime::RuntimeSelection::Mode::McppDefault);
EXPECT_EQ(selected->source, runtime::RuntimeSelection::Source::DefaultPolicy);
EXPECT_EQ(selected->subosName, "default");
EXPECT_EQ(selected->ownerRoot, std::filesystem::path("/repo"));
}
TEST(RuntimeSelection, ExplicitDefaultRemainsNamed) {
auto root = named("default");
auto selected = runtime::select_runtime(root, std::nullopt, "/repo");
ASSERT_TRUE(selected.has_value()) << selected.error();
EXPECT_EQ(selected->mode, runtime::RuntimeSelection::Mode::NamedSubos);
EXPECT_EQ(selected->source, runtime::RuntimeSelection::Source::Manifest);
EXPECT_EQ(selected->subosName, "default");
}
TEST(RuntimeSelection, WorkspaceRootOverridesMember) {
auto member = named("member-dev");
auto workspace = named("workspace-el8");
auto selected = runtime::select_runtime(
member, std::cref(workspace), "/repo/member", "/repo");
ASSERT_TRUE(selected.has_value()) << selected.error();
EXPECT_EQ(selected->subosName, "workspace-el8");
EXPECT_EQ(selected->ownerRoot, std::filesystem::path("/repo"));
}
TEST(RuntimeSelection, MemberAppliesWhenItIsTheIndependentRoot) {
auto member = named("member-dev");
auto selected = runtime::select_runtime(member, std::nullopt, "/repo/member");
ASSERT_TRUE(selected.has_value()) << selected.error();
EXPECT_EQ(selected->subosName, "member-dev");
EXPECT_EQ(selected->ownerRoot, std::filesystem::path("/repo/member"));
}
TEST(RuntimeSelection, EmptyAndPathLikeNamesAreRejected) {
for (auto bad : {"", ".", "..", "a/b", "a\\b", "a:b"}) {
auto root = named(bad);
auto selected = runtime::select_runtime(root, std::nullopt, "/repo");
EXPECT_FALSE(selected.has_value()) << bad;
}
}
TEST(RuntimeSelection, DependencyDeclarationCannotAffectConsumerSelection) {
mcpp::manifest::Manifest consumer;
auto dependency = named("dependency-only");
(void)dependency;
auto selected = runtime::select_runtime(consumer, std::nullopt, "/consumer");
ASSERT_TRUE(selected.has_value()) << selected.error();
EXPECT_EQ(selected->mode, runtime::RuntimeSelection::Mode::McppDefault);
EXPECT_EQ(selected->subosName, "default");
}
TEST(RuntimeBinding, DefaultAlwaysUsesConfiguredMcppHomeDefault) {
RuntimeHome h;
const auto expected = h.cfg.xlingsHome() / "subos" / "default";
h.write(expected);
mcpp::manifest::Manifest root;
auto selection = runtime::select_runtime(root, std::nullopt, h.dir / "repo");
ASSERT_TRUE(selection);
auto binding = mcpp::platform::runtime::resolve_runtime_binding(
*selection, {}, h.cfg);
ASSERT_TRUE(binding.has_value()) << binding.error();
EXPECT_EQ(binding->subosDir, expected);
EXPECT_EQ(binding->runtimeId, "glibc@2.39");
if constexpr (mcpp::platform::is_linux)
EXPECT_EQ(binding->libc, std::optional<std::string>("glibc@2.39"));
else
EXPECT_FALSE(binding->libc.has_value());
EXPECT_EQ(binding->provenance, "mcpp_default");
EXPECT_FALSE(binding->contractHash.empty());
ASSERT_EQ(binding->runtimeProviders.size(), 1u);
EXPECT_EQ(binding->runtimeProviders[0].provider.name, "renderer");
ASSERT_EQ(binding->runtimeArtifacts.size(), 1u);
EXPECT_EQ(binding->runtimeArtifacts[0].path,
(expected / "runtime/renderer.bin").lexically_normal());
}
TEST(RuntimeBinding, ExplicitDefaultUsesGlobalDefaultButKeepsNamedIdentity) {
RuntimeHome h;
const auto expected = h.cfg.xlingsHome() / "subos" / "default";
h.write(expected);
auto manifest = named("default");
auto selection = runtime::select_runtime(manifest, std::nullopt, h.dir / "repo");
ASSERT_TRUE(selection);
auto binding = mcpp::platform::runtime::resolve_runtime_binding(
*selection, {}, h.cfg);
ASSERT_TRUE(binding.has_value()) << binding.error();
EXPECT_EQ(binding->subosDir, expected);
EXPECT_EQ(binding->provenance, "named_subos");
EXPECT_EQ(binding->selection.mode, runtime::RuntimeSelection::Mode::NamedSubos);
}
TEST(RuntimeBinding, PhysicalSubosViewReconcilesAStaleDeclaredGlibcIdentity) {
if constexpr (!mcpp::platform::is_linux)
GTEST_SKIP() << "glibc SubOS views only exist on Linux";
RuntimeHome h;
const auto subos = h.cfg.xlingsHome() / "subos" / "default";
h.write(subos, "glibc@2.39");
h.point_default_glibc_view_at("2.44");
mcpp::manifest::Manifest root;
auto selection = runtime::select_runtime(root, std::nullopt, h.dir / "repo");
ASSERT_TRUE(selection);
auto binding = mcpp::platform::runtime::resolve_runtime_binding(
*selection, {}, h.cfg);
ASSERT_TRUE(binding.has_value()) << binding.error();
EXPECT_EQ(binding->runtimeId, "glibc@2.44");
EXPECT_EQ(binding->libc, std::optional<std::string>("glibc@2.44"));
ASSERT_EQ(binding->libraryDirs.size(), 1u);
EXPECT_EQ(binding->libraryDirs.front(),
h.cfg.xlingsHome() / "data" / "xpkgs"
/ "xim-x-glibc" / "2.44" / "lib");
}
TEST(RuntimeBinding, BrokenSubosViewFallsBackOnlyToTheExactDeclaredPayload) {
if constexpr (!mcpp::platform::is_linux)
GTEST_SKIP() << "glibc SubOS views only exist on Linux";
RuntimeHome h;
const auto subos = h.cfg.xlingsHome() / "subos" / "default";
h.write(subos, "glibc@2.44");
auto payload = h.install_glibc_payload("2.44");
auto view = subos / "lib";
std::filesystem::create_directories(view);
std::filesystem::create_symlink(
payload.parent_path() / "lib64" / "libc.so.6",
view / "libc.so.6");
mcpp::manifest::Manifest root;
auto selection = runtime::select_runtime(root, std::nullopt, h.dir / "repo");
ASSERT_TRUE(selection);
auto binding = mcpp::platform::runtime::resolve_runtime_binding(
*selection, {}, h.cfg);
ASSERT_TRUE(binding.has_value()) << binding.error();
EXPECT_EQ(binding->runtimeId, "glibc@2.44");
ASSERT_EQ(binding->libraryDirs.size(), 1u);
EXPECT_EQ(binding->libraryDirs.front(), payload);
EXPECT_EQ(binding->loader,
std::optional<std::filesystem::path>(
payload / "ld-linux-x86-64.so.2"));
}
TEST(RuntimeBinding, NamedSubosIsOwnedByTheRootAndMissingIsHardError) {
RuntimeHome h;
auto manifest = named("el8");
auto selection = runtime::select_runtime(manifest, std::nullopt, h.dir / "repo");
ASSERT_TRUE(selection);
auto missing = mcpp::platform::runtime::resolve_runtime_binding(
*selection, {}, h.cfg);
ASSERT_FALSE(missing.has_value());
EXPECT_NE(missing.error().find("el8"), std::string::npos);
const auto expected = h.dir / "repo" / ".mcpp" / ".xlings" / "subos" / "el8";
h.write(expected);
auto binding = mcpp::platform::runtime::resolve_runtime_binding(
*selection, {}, h.cfg);
ASSERT_TRUE(binding.has_value()) << binding.error();
EXPECT_EQ(binding->subosDir, expected);
}
TEST(RuntimeBinding, NamedEnvironmentsHaveDistinctContractsAndRoundTrip) {
RuntimeHome h;
auto el8Manifest = named("el8");
auto devManifest = named("dev");
auto el8Selection = runtime::select_runtime(
el8Manifest, std::nullopt, h.dir / "repo");
auto devSelection = runtime::select_runtime(
devManifest, std::nullopt, h.dir / "repo");
ASSERT_TRUE(el8Selection);
ASSERT_TRUE(devSelection);
h.write(h.dir / "repo" / ".mcpp" / ".xlings" / "subos" / "el8");
h.write(h.dir / "repo" / ".mcpp" / ".xlings" / "subos" / "dev");
auto el8 = mcpp::platform::runtime::resolve_runtime_binding(
*el8Selection, {}, h.cfg);
auto dev = mcpp::platform::runtime::resolve_runtime_binding(
*devSelection, {}, h.cfg);
ASSERT_TRUE(el8);
ASSERT_TRUE(dev);
EXPECT_NE(el8->contractHash, dev->contractHash);
auto encoded = mcpp::platform::runtime::serialize_runtime_binding(*el8);
auto decoded = mcpp::platform::runtime::deserialize_runtime_binding(encoded);
ASSERT_TRUE(decoded.has_value()) << decoded.error();
EXPECT_EQ(decoded->contractHash, el8->contractHash);
if constexpr (mcpp::platform::is_linux) {
ASSERT_TRUE(decoded->hostLibc.has_value());
EXPECT_EQ(*decoded->hostLibc, "2.43");
} else {
EXPECT_FALSE(decoded->hostLibc.has_value());
}
EXPECT_EQ(decoded->subosDir, el8->subosDir);
ASSERT_EQ(decoded->environment.size(), 1u);
EXPECT_EQ(decoded->environment[0].var, "DRIVERS");
ASSERT_EQ(decoded->runtimeProviders.size(), 1u);
EXPECT_EQ(decoded->runtimeProviders[0].provider.namespace_, "xim");
ASSERT_EQ(decoded->runtimeArtifacts.size(), 1u);
EXPECT_EQ(decoded->runtimeArtifacts[0].hostFingerprint, "host-2");
EXPECT_TRUE(decoded->declared);
}
// ── absence degrades, contradiction fails (openxlings/xlings#543) ──────────
//
// A SubOS that says nothing used to make `resolve_runtime_binding` return an
// error, which every caller of `mcpp build`/`mcpp test` propagated — stopping
// the tool on Windows with a message about GL drivers, on a platform that has
// no ELF, no PT_INTERP and no private libc. The rule the index-floor incident
// already paid for applies here too: data that is missing or newer must not
// invalidate the program that reads it.
TEST(RuntimeBindingDegradation, UndeclaredSubosStillResolves) {
RuntimeHome h;
auto manifest = named("bare");
auto selection = runtime::select_runtime(manifest, std::nullopt,
h.dir / "repo");
ASSERT_TRUE(selection);
// Exists, describes nothing. The common state of a freshly created SubOS,
// and the permanent state on a platform whose xlings does not write the
// block at all.
std::filesystem::create_directories(
h.dir / "repo" / ".mcpp" / ".xlings" / "subos" / "bare");
auto binding = mcpp::platform::runtime::resolve_runtime_binding(
*selection, {}, h.cfg);
ASSERT_TRUE(binding.has_value())
<< "absence must degrade, not fail: " << binding.error();
EXPECT_FALSE(binding->declared);
EXPECT_FALSE(binding->note.empty())
<< "a degradation nobody reports is indistinguishable from none";
EXPECT_TRUE(binding->runtimeId.empty());
EXPECT_FALSE(binding->hermetic());
}
// The cache must survive it too. A degraded binding legitimately has schema 0
// and no runtime identity, and a completeness check that demands those fields
// would make every cached degraded binding undecodable — sending the build
// back down the slow path on every single invocation, forever.
TEST(RuntimeBindingDegradation, UndeclaredBindingRoundTrips) {
RuntimeHome h;
auto manifest = named("bare");
auto selection = runtime::select_runtime(manifest, std::nullopt,
h.dir / "repo");
ASSERT_TRUE(selection);
std::filesystem::create_directories(
h.dir / "repo" / ".mcpp" / ".xlings" / "subos" / "bare");
auto binding = mcpp::platform::runtime::resolve_runtime_binding(
*selection, {}, h.cfg);
ASSERT_TRUE(binding.has_value());
auto decoded = mcpp::platform::runtime::deserialize_runtime_binding(
mcpp::platform::runtime::serialize_runtime_binding(*binding));
ASSERT_TRUE(decoded.has_value()) << decoded.error();
EXPECT_EQ(decoded->contractHash, binding->contractHash);
EXPECT_FALSE(decoded->declared);
EXPECT_EQ(decoded->note, binding->note);
}
// A newer schema is read, not refused. `subos_info::read` already documents
// this ("a HIGHER one on disk is still read"); the consumer used to demand
// equality, so the day xlings writes schema 2 every build on every platform
// would have stopped at once.
TEST(RuntimeBindingDegradation, NewerSchemaIsReadNotRefused) {
RuntimeHome h;
auto manifest = named("future");
auto selection = runtime::select_runtime(manifest, std::nullopt,
h.dir / "repo");
ASSERT_TRUE(selection);
auto subos = h.dir / "repo" / ".mcpp" / ".xlings" / "subos" / "future";
std::filesystem::create_directories(subos);
std::ofstream(subos / ".xlings.json") << R"({
"subos_info": {
"schema_version": 99,
"runtime": "glibc@2.39",
"envs": {},
"a_field_from_the_future": true
}
})";
auto binding = mcpp::platform::runtime::resolve_runtime_binding(
*selection, {}, h.cfg);
ASSERT_TRUE(binding.has_value())
<< "a newer contract must not invalidate an older mcpp: "
<< binding.error();
EXPECT_TRUE(binding->declared);
EXPECT_EQ(binding->runtimeId, "glibc@2.39");
EXPECT_NE(binding->note.find("newer"), std::string::npos)
<< "reading a newer schema silently is the other half of the defect";
}
// The boundary. A SubOS the user NAMED and that is not there cannot be
// satisfied under any interpretation, and picking a different one would make
// one mcpp.toml mean different ABIs on different machines.
TEST(RuntimeBindingDegradation, MissingSubosStillFails) {
RuntimeHome h;
auto manifest = named("never-created");
auto selection = runtime::select_runtime(manifest, std::nullopt,
h.dir / "repo");
ASSERT_TRUE(selection);
auto binding = mcpp::platform::runtime::resolve_runtime_binding(
*selection, {}, h.cfg);
ASSERT_FALSE(binding.has_value())
<< "a contradiction must be reported, not degraded";
EXPECT_NE(binding.error().find("does not exist"), std::string::npos);
}
} // namespace