-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_bmi_cache.cpp
More file actions
419 lines (355 loc) · 15.2 KB
/
Copy pathtest_bmi_cache.cpp
File metadata and controls
419 lines (355 loc) · 15.2 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <gtest/gtest.h>
#include <fcntl.h>
#if !defined(_WIN32)
#include <sys/file.h>
#include <unistd.h>
#endif
import std;
import mcpp.bmi_cache;
import mcpp.libs.json;
using namespace mcpp::bmi_cache;
namespace {
struct Tmp {
std::filesystem::path path;
Tmp() {
auto base = std::filesystem::temp_directory_path()
/ std::format("mcpp_bmi_cache_test_{}", std::random_device{}());
std::filesystem::create_directories(base);
path = base;
}
~Tmp() {
std::error_code ec;
std::filesystem::remove_all(path, ec);
}
};
nlohmann::json makeInputs(std::string_view flavor = "base") {
nlohmann::json j;
j["epoch"] = 1;
j["toolchain"] = {{"compiler", "gcc"}, {"compiler_version", "16.1.0"}};
j["profile"] = {{"opt_level", "2"}, {"debug", false}};
j["package"] = {{"index", "mcpplibs"}, {"name", "mcpplibs.cmdline"},
{"version", "0.0.1"}};
j["config"] = {{"cxxflags", nlohmann::json::array({std::string(flavor)})}};
return j;
}
CacheKey makeKey(const std::filesystem::path& root,
std::string_view key = "deadbeef0123abcd",
std::string_view flavor = "base") {
return CacheKey{
.cacheRoot = root,
.indexName = "mcpplibs",
.packageName = "mcpplibs.cmdline",
.version = "0.0.1",
.keyHex = std::string(key),
.inputs = makeInputs(flavor),
};
}
void writeFile(const std::filesystem::path& p, std::string_view body) {
std::filesystem::create_directories(p.parent_path());
std::ofstream(p) << body;
}
std::string readFile(const std::filesystem::path& p) {
std::ifstream is(p);
return std::string((std::istreambuf_iterator<char>(is)), {});
}
nlohmann::json readJson(const std::filesystem::path& p) {
std::ifstream is(p);
nlohmann::json j;
is >> j;
return j;
}
// `cacheRel` is the entry-internal address, `buildRel` is where this "build"
// produced the file (relative to the project target dir). They are separate on
// purpose — see mcpp#344.
DepArtifacts oneOfEach() {
return DepArtifacts{ .bmiFiles = {"lib.gcm"},
.objFiles = {{"lib.m.o", "obj/lib.m.o"}} };
}
void seedProject(const std::filesystem::path& project) {
writeFile(project / "gcm.cache" / "lib.gcm", "G");
writeFile(project / "obj" / "lib.m.o", "O");
}
} // namespace
TEST(BmiCache, EntryDirIsKeyedByPackageIdentityAndKeyNotByProject) {
auto k = makeKey("/home/u/.mcpp/build-cache/v1");
auto expected = std::filesystem::path(
"/home/u/.mcpp/build-cache/v1/pkg/mcpplibs/mcpplibs.cmdline@0.0.1/deadbeef0123abcd");
EXPECT_EQ(k.dir(), expected);
EXPECT_EQ(k.entryFile().filename().string(), "entry.json");
EXPECT_EQ(k.bmiDir().filename().string(), "bmi");
EXPECT_EQ(k.objDir().filename().string(), "obj");
}
// The layout version must be a path segment: replacing the layout has to be a
// new tree, never a migration of (or a deletion of) the old one.
TEST(BmiCache, EntryDirCarriesTheLayoutVersion) {
auto k = makeKey("/home/u/.mcpp/build-cache/v1");
auto s = k.dir().generic_string();
EXPECT_NE(s.find("/build-cache/v1/pkg/"), std::string::npos) << s;
// And nothing may land under the pre-v1 root.
EXPECT_EQ(s.find("/.mcpp/bmi/"), std::string::npos) << s;
}
TEST(BmiCache, IsCachedFalseWhenEntryMissing) {
Tmp t;
EXPECT_FALSE(is_cached(makeKey(t.path), oneOfEach()));
}
TEST(BmiCache, PopulateWritesSelfDescribingEntry) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto k = makeKey(home);
auto pop = populate_from(k, project, oneOfEach());
ASSERT_TRUE(pop) << pop.error();
ASSERT_TRUE(std::filesystem::exists(k.entryFile()));
EXPECT_TRUE(std::filesystem::exists(k.bmiDir() / "lib.gcm"));
EXPECT_TRUE(std::filesystem::exists(k.objDir() / "lib.m.o"));
EXPECT_TRUE(is_cached(k, oneOfEach()));
// The entry has to carry the key AND the full inputs: a hit is validated
// field by field, so a cache whose entries only listed files could never be
// audited when a wrong hit was suspected.
auto j = readJson(k.entryFile());
EXPECT_EQ(j.value("key", std::string{}), "deadbeef0123abcd");
EXPECT_EQ(j["inputs"], makeInputs());
EXPECT_EQ(j["bmi"].size(), 1u);
EXPECT_EQ(j["obj"].size(), 1u);
EXPECT_TRUE(j.contains("created"));
EXPECT_TRUE(j.contains("accessed"));
}
// The whole point of recording inputs: equal hashes are never trusted on their
// own. Same key directory, different inputs ⇒ miss.
TEST(BmiCache, IsCachedFalseWhenRecordedInputsDiffer) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto written = makeKey(home, "samekey00000000", "base");
ASSERT_TRUE(populate_from(written, project, oneOfEach()));
EXPECT_TRUE(is_cached(written, oneOfEach()));
auto probed = makeKey(home, "samekey00000000", "different-flag");
EXPECT_FALSE(is_cached(probed, oneOfEach()));
}
// An entry written by an older mcpp may carry extra keys, but every field the
// current build cares about must be present. Missing ⇒ mismatch, never a pass.
TEST(BmiCache, IsCachedFalseWhenARequiredInputFieldIsAbsent) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto k = makeKey(home);
ASSERT_TRUE(populate_from(k, project, oneOfEach()));
auto j = readJson(k.entryFile());
j["inputs"].erase("profile");
std::ofstream(k.entryFile()) << j.dump(2);
EXPECT_FALSE(is_cached(k, oneOfEach()));
}
TEST(BmiCache, IsCachedFalseWhenSchemaDiffers) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto k = makeKey(home);
ASSERT_TRUE(populate_from(k, project, oneOfEach()));
auto j = readJson(k.entryFile());
j["schema"] = kEntrySchema + 1;
std::ofstream(k.entryFile()) << j.dump(2);
EXPECT_FALSE(is_cached(k, oneOfEach()));
}
TEST(BmiCache, IsCachedFalseWhenSentinelExistsButFileMissing) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto k = makeKey(home);
ASSERT_TRUE(populate_from(k, project, oneOfEach()));
ASSERT_TRUE(is_cached(k, oneOfEach()));
std::filesystem::remove(k.objDir() / "lib.m.o");
EXPECT_FALSE(is_cached(k, oneOfEach()));
}
TEST(BmiCache, ResolveCachedReportsTheArtifactsWithoutCopying) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto k = makeKey(home);
ASSERT_TRUE(populate_from(k, project, oneOfEach()));
auto arts = resolve_cached(k);
ASSERT_TRUE(arts) << arts.error();
EXPECT_EQ(arts->bmiFiles, std::vector<std::string>{"lib.gcm"});
ASSERT_EQ(arts->objFiles.size(), 1u);
EXPECT_EQ(arts->objFiles[0].cacheRel, "lib.m.o");
// Read-back never yields a build path: the entry does not record one, which
// is the whole point of mcpp#344.
EXPECT_TRUE(arts->objFiles[0].buildRel.empty());
// resolve_cached must not write into a project dir. Staging is a ninja edge
// now: copying artifacts in from outside the graph is exactly what made the
// old cache a no-op — ninja rebuilds any output it has no command-line
// record for, so the copies were recompiled over on every fresh build dir.
auto project2 = t.path / "proj2" / "target";
(void)resolve_cached(k);
EXPECT_FALSE(std::filesystem::exists(project2));
}
TEST(BmiCache, CachedPathsPointIntoTheEntry) {
auto k = makeKey("/home/u/.mcpp/build-cache/v1");
EXPECT_EQ(cached_bmi_path(k, "lib.gcm"), k.bmiDir() / "lib.gcm");
EXPECT_EQ(cached_obj_path(k, "sub/lib.m.o"), k.objDir() / "sub" / "lib.m.o");
}
// Nested object paths (mcpp#233's per-package object prefixes) must round-trip.
TEST(BmiCache, PopulateHandlesNestedObjectPaths) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
writeFile(project / "obj" / "pkg_zlib" / "zlib-1.3" / "compress.o", "NESTED");
auto k = makeKey(home);
DepArtifacts arts {
.objFiles = {{"zlib-1.3/compress.o", "obj/pkg_zlib/zlib-1.3/compress.o"}} };
ASSERT_TRUE(populate_from(k, project, arts));
// Stored at the ENTRY address, read from the BUILD path. The entry knows
// nothing about the `pkg_zlib/` partition, which is consumer-side.
EXPECT_EQ(readFile(k.objDir() / "zlib-1.3" / "compress.o"), "NESTED");
EXPECT_TRUE(is_cached(k, arts));
auto j = readJson(k.entryFile());
EXPECT_EQ(j["obj"][0].get<std::string>(), "zlib-1.3/compress.o");
}
// mcpp#344, the load-bearing case. Two consumers of ONE key may place the same
// object at different build-dir paths (#233's basename disambiguation fires on
// a census over the whole build dir, which varies with the consumer's package
// mix). The entry is written by whoever runs first; whoever runs second must
// get a clean MISS, with the divergence named — never a hit that goes on to
// stage a file the entry never had, which is what ninja reported as
// "missing and no known rule to make it" at graph load.
TEST(BmiCache, ProbeMissesWhenTheEntryDoesNotHoldTheRequestedArtifacts) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
writeFile(project / "obj" / "compress.o", "FLAT");
auto k = makeKey(home);
// The first consumer populated the entry under a DIFFERENT internal
// address than the one this build is going to ask for.
DepArtifacts written { .objFiles = {{"other/compress.o", "obj/compress.o"}} };
ASSERT_TRUE(populate_from(k, project, written));
DepArtifacts wanted { .objFiles = {{"zlib-1.3/compress.o", "obj/compress.o"}} };
auto probe = probe_cached(k, wanted);
EXPECT_FALSE(probe.ok);
ASSERT_EQ(probe.layoutMismatch.size(), 1u);
EXPECT_EQ(probe.layoutMismatch[0], "zlib-1.3/compress.o");
}
// The same asymmetry on the read side: an entry that lists a file it does not
// have on disk must not be reported as a layout divergence OR as a hit.
TEST(BmiCache, ProbeMissesWhenARequestedFileIsListedButAbsent) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto k = makeKey(home);
ASSERT_TRUE(populate_from(k, project, oneOfEach()));
std::filesystem::remove(k.objDir() / "lib.m.o");
auto probe = probe_cached(k, oneOfEach());
EXPECT_FALSE(probe.ok);
ASSERT_EQ(probe.layoutMismatch.size(), 1u);
EXPECT_EQ(probe.layoutMismatch[0], "lib.m.o");
}
// An entry whose own validation fails (wrong inputs) is an ordinary miss and
// carries NO layout complaint — the caller warns on layout divergence only, and
// warning on every routine miss would make the signal worthless.
TEST(BmiCache, ProbeReportsNoLayoutMismatchWhenTheEntryItselfIsInvalid) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
ASSERT_TRUE(populate_from(makeKey(home, "samekey00000000", "base"),
project, oneOfEach()));
auto probe = probe_cached(makeKey(home, "samekey00000000", "other"),
oneOfEach());
EXPECT_FALSE(probe.ok);
EXPECT_TRUE(probe.layoutMismatch.empty());
}
// touch_accessed is what makes `cache gc` an LRU rather than "drop what was
// populated long ago". It must move the stamp and leave the artifacts alone —
// their mtimes participate in ninja's restat handling.
TEST(BmiCache, TouchAccessedMovesTheStampAndNotTheArtifacts) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto k = makeKey(home);
ASSERT_TRUE(populate_from(k, project, oneOfEach()));
auto j0 = readJson(k.entryFile());
auto created0 = j0.value("created", std::string{});
auto objTime0 = std::filesystem::last_write_time(k.objDir() / "lib.m.o");
auto bmiTime0 = std::filesystem::last_write_time(k.bmiDir() / "lib.gcm");
// Rewrite the stamp to something clearly old, then touch.
j0["accessed"] = "1000";
std::ofstream(k.entryFile()) << j0.dump(2);
touch_accessed(k);
auto j1 = readJson(k.entryFile());
EXPECT_NE(j1.value("accessed", std::string{}), "1000");
EXPECT_EQ(j1.value("created", std::string{}), created0)
<< "touch must not reset the creation stamp";
EXPECT_EQ(std::filesystem::last_write_time(k.objDir() / "lib.m.o"), objTime0);
EXPECT_EQ(std::filesystem::last_write_time(k.bmiDir() / "lib.gcm"), bmiTime0);
EXPECT_TRUE(is_cached(k, oneOfEach())) << "touching must not invalidate the entry";
}
TEST(BmiCache, RepopulatePreservesCreatedStamp) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto k = makeKey(home);
ASSERT_TRUE(populate_from(k, project, oneOfEach()));
auto j0 = readJson(k.entryFile());
j0["created"] = "12345";
std::ofstream(k.entryFile()) << j0.dump(2);
ASSERT_TRUE(populate_from(k, project, oneOfEach()));
EXPECT_EQ(readJson(k.entryFile()).value("created", std::string{}), "12345");
}
TEST(BmiCache, PopulateFailsIfBuildOutputMissing) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
std::filesystem::create_directories(project / "gcm.cache");
DepArtifacts arts { .bmiFiles = {"missing.gcm"} };
auto k = makeKey(home);
auto pop = populate_from(k, project, arts);
EXPECT_FALSE(pop);
EXPECT_NE(pop.error().find("expected build output missing"), std::string::npos);
}
// Two keys for the same package@version are independent entries — that is what
// lets one machine hold a dev-profile and a release-profile build of the same
// dependency at once.
TEST(BmiCache, DifferentKeysAreIndependentEntries) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto a = makeKey(home, "aaaaaaaaaaaaaaaa", "opt");
auto b = makeKey(home, "bbbbbbbbbbbbbbbb", "debug");
ASSERT_TRUE(populate_from(a, project, oneOfEach()));
ASSERT_TRUE(populate_from(b, project, oneOfEach()));
EXPECT_NE(a.dir(), b.dir());
EXPECT_TRUE(is_cached(a, oneOfEach()));
EXPECT_TRUE(is_cached(b, oneOfEach()));
}
#if !defined(_WIN32)
// When an external holder takes the .lock, populate_from must skip (returns
// success but does NOT clobber the entry). Uses flock(), POSIX-only.
TEST(BmiCache, PopulateSkipsWhenLockHeld) {
Tmp t;
auto home = t.path / "home";
auto project = t.path / "proj" / "target";
seedProject(project);
auto k = makeKey(home);
std::filesystem::create_directories(k.dir());
auto lockPath = k.dir() / ".lock";
int fd = ::open(lockPath.c_str(), O_CREAT | O_RDWR, 0644);
ASSERT_GE(fd, 0);
ASSERT_EQ(::flock(fd, LOCK_EX | LOCK_NB), 0);
auto pop = populate_from(k, project, oneOfEach());
EXPECT_TRUE(pop) << "should silently skip when lock is held";
EXPECT_FALSE(std::filesystem::exists(k.entryFile()));
::flock(fd, LOCK_UN);
::close(fd);
auto pop2 = populate_from(k, project, oneOfEach());
ASSERT_TRUE(pop2) << pop2.error();
EXPECT_TRUE(std::filesystem::exists(k.entryFile()));
}
#endif // !defined(_WIN32)