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
|
use alt_controller_image::compare::{Change, SemanticDifference, compare};
use alt_controller_image::archive::NativeTarWriter;
use alt_controller_image::manifest::{
ArchiveMemberRecord, ArtifactManifest, FileRecord, InitrdRecord, PackageRecord, ServiceRecord,
};
use std::fs;
use std::io::Cursor;
use std::os::unix::fs::symlink;
use tempfile::tempdir;
fn baseline() -> ArtifactManifest {
ArtifactManifest::new(
vec![PackageRecord::new("kernel-image-rt", "1.0")],
vec![FileRecord::file("etc/controller.conf", "aaa")],
Some(InitrdRecord::new("boot/initrd-rt.img", "old-initrd")),
vec![ServiceRecord::new("controller.service", true)],
vec![ArchiveMemberRecord::new("etc/controller.conf", "file")],
)
.expect("valid baseline manifest")
}
#[test]
fn compares_added_removed_and_changed_semantic_records() {
let left = baseline();
let right = ArtifactManifest::new(
vec![
PackageRecord::new("kernel-image-rt", "2.0"),
PackageRecord::new("controller-agent", "1.0"),
],
vec![FileRecord::symlink("etc/controller.conf", "controller.conf.real")],
Some(InitrdRecord::new("boot/initrd-rt.img", "new-initrd")),
vec![ServiceRecord::new("controller.service", false)],
vec![ArchiveMemberRecord::new("usr/bin/controller", "file")],
)
.expect("valid comparison manifest");
let report = compare(&left, &right);
assert_eq!(
report.differences(),
&[
SemanticDifference::Package { name: "controller-agent".into(), change: Change::Added },
SemanticDifference::Package { name: "kernel-image-rt".into(), change: Change::Changed },
SemanticDifference::File { path: "etc/controller.conf".into(), change: Change::Changed },
SemanticDifference::Initrd { change: Change::Changed },
SemanticDifference::Service { name: "controller.service".into(), change: Change::Changed },
SemanticDifference::ArchiveMember { path: "etc/controller.conf".into(), change: Change::Removed },
SemanticDifference::ArchiveMember { path: "usr/bin/controller".into(), change: Change::Added },
]
);
assert!(report.render().contains("changed initrd"));
}
#[test]
fn distinguishes_added_and_removed_initrd_records() {
let without_initrd = ArtifactManifest::new(vec![], vec![], None, vec![], vec![])
.expect("valid manifest without initrd");
let with_initrd = ArtifactManifest::new(
vec![],
vec![],
Some(InitrdRecord::new("boot/initrd-rt.img", "digest")),
vec![],
vec![],
)
.expect("valid manifest with initrd");
assert_eq!(
compare(&without_initrd, &with_initrd).differences(),
&[SemanticDifference::Initrd {
change: Change::Added
}]
);
assert_eq!(
compare(&with_initrd, &without_initrd).differences(),
&[SemanticDifference::Initrd {
change: Change::Removed
}]
);
}
#[test]
fn reads_and_writes_a_toml_manifest_beside_an_artifact() {
let fixture = tempdir().expect("temporary directory");
let artifact = fixture.path().join("controller.tar");
let manifest = baseline();
let manifest_path = manifest.write_beside(&artifact).expect("write manifest");
assert_eq!(manifest_path, fixture.path().join("artifact.manifest.toml"));
assert_eq!(ArtifactManifest::load(&manifest_path).expect("load manifest"), manifest);
}
#[test]
fn rejects_duplicate_semantic_keys() {
let error = ArtifactManifest::new(
vec![PackageRecord::new("controller", "1"), PackageRecord::new("controller", "2")],
vec![],
None,
vec![],
vec![],
)
.expect_err("duplicate package names must fail");
assert!(error.to_string().contains("duplicate package record"));
}
#[test]
fn collects_semantic_facts_from_native_rootfs_and_tar_artifact() {
let fixture = tempdir().expect("temporary directory");
let rootfs = fixture.path().join("rootfs");
fs::create_dir_all(rootfs.join("etc/systemd/system/multi-user.target.wants"))
.expect("create service state directory");
fs::write(rootfs.join("etc/controller.conf"), "controller\n").expect("write file");
symlink("controller.conf", rootfs.join("etc/controller-link")).expect("write symlink");
symlink(
"/usr/lib/systemd/system/controller.service",
rootfs.join("etc/systemd/system/multi-user.target.wants/controller.service"),
)
.expect("enable service");
let artifact = fixture.path().join("controller.tar");
NativeTarWriter::new()
.write(&rootfs, &artifact)
.expect("write native tar");
let manifest = ArtifactManifest::collect(
&rootfs,
vec![PackageRecord::new("controller", "1.0")],
Some(InitrdRecord::new("boot/initrd-rt.img", "digest")),
&artifact,
)
.expect("collect semantic facts");
assert_eq!(manifest.packages, vec![PackageRecord::new("controller", "1.0")]);
assert!(manifest.files.iter().any(|record| record == &FileRecord::file(
"etc/controller.conf",
"2d5c759b2b539229e09d362e8dbe0ae410ff8c9ece6038624458724520683f5b",
)));
assert!(manifest
.files
.iter()
.any(|record| record == &FileRecord::symlink("etc/controller-link", "controller.conf")));
assert_eq!(manifest.services, vec![ServiceRecord::new("controller.service", true)]);
assert!(manifest
.archive_members
.iter()
.any(|record| record == &ArchiveMemberRecord::new("etc/controller.conf", "file")));
assert!(manifest
.archive_members
.iter()
.any(|record| record == &ArchiveMemberRecord::new("etc/controller-link", "symlink")));
}
#[test]
fn collects_comparable_semantic_facts_directly_from_a_tar_artifact() {
let fixture = tempdir().expect("temporary directory");
let rootfs = fixture.path().join("rootfs");
fs::create_dir_all(rootfs.join("etc/systemd/system/multi-user.target.wants"))
.expect("create service state directory");
fs::create_dir_all(rootfs.join("boot")).expect("create boot directory");
fs::write(rootfs.join("etc/controller.conf"), "controller\n").expect("write file");
fs::write(rootfs.join("boot/initrd-rt.img"), "initrd\n").expect("write initrd");
symlink(
"/usr/lib/systemd/system/controller.service",
rootfs.join("etc/systemd/system/multi-user.target.wants/controller.service"),
)
.expect("enable service");
let artifact = fixture.path().join("controller.tar");
NativeTarWriter::new()
.write(&rootfs, &artifact)
.expect("write native tar");
let manifest = ArtifactManifest::collect_archive(&artifact).expect("collect archive facts");
assert_eq!(manifest.packages, Vec::<PackageRecord>::new());
assert!(manifest.files.iter().any(|record| record == &FileRecord::file(
"etc/controller.conf",
"2d5c759b2b539229e09d362e8dbe0ae410ff8c9ece6038624458724520683f5b",
)));
assert_eq!(
manifest.initrd,
Some(InitrdRecord::new(
"boot/initrd-rt.img",
"8f7ed204b9dfaa20aa484445f54233c4b407cb80ec0f8c07f1f0a59675fb44cf",
))
);
assert_eq!(manifest.services, vec![ServiceRecord::new("controller.service", true)]);
}
#[test]
fn normalizes_legacy_dot_prefixed_archive_paths_before_comparison() {
let fixture = tempdir().expect("temporary directory");
let artifact = fixture.path().join("legacy.tar");
let file = fs::File::create(&artifact).expect("create legacy archive");
let mut archive = tar::Builder::new(file);
let mut header = tar::Header::new_gnu();
header.set_size(11);
header.set_mode(0o644);
header.set_cksum();
archive
.append_data(&mut header, "./etc/controller.conf", Cursor::new(b"controller\n"))
.expect("append legacy-style member");
archive.finish().expect("finish legacy archive");
let manifest = ArtifactManifest::collect_archive(&artifact).expect("collect legacy archive facts");
let expected = ArtifactManifest::new(
vec![],
vec![FileRecord::file(
"etc/controller.conf",
"2d5c759b2b539229e09d362e8dbe0ae410ff8c9ece6038624458724520683f5b",
)],
None,
vec![],
vec![ArchiveMemberRecord::new("etc/controller.conf", "file")],
)
.expect("valid normalized manifest");
assert!(compare(&manifest, &expected).is_equivalent());
}
#[test]
fn normalizes_dot_prefixed_paths_from_a_legacy_tree_archive() {
let fixture = tempdir().expect("temporary directory");
let rootfs = fixture.path().join("rootfs");
fs::create_dir_all(rootfs.join("etc")).expect("create rootfs directory");
fs::write(rootfs.join("etc/controller.conf"), "controller\n").expect("write rootfs file");
let artifact = fixture.path().join("legacy.tar");
let mut archive = tar::Builder::new(fs::File::create(&artifact).expect("create legacy archive"));
archive.append_dir_all(".", &rootfs).expect("write legacy tree archive");
archive.finish().expect("finish legacy archive");
let manifest = ArtifactManifest::collect_archive(&artifact).expect("collect legacy archive facts");
assert!(manifest.files.iter().all(|record| !record.path.starts_with("./")));
assert!(manifest
.archive_members
.iter()
.all(|record| !record.path.starts_with("./")));
}
#[test]
fn excludes_legacy_internal_host_and_fakedata_archive_members() {
let fixture = tempdir().expect("temporary directory");
let rootfs = fixture.path().join("rootfs");
fs::create_dir_all(rootfs.join(".host/private")).expect("create host metadata");
fs::create_dir_all(rootfs.join(".fakedata")).expect("create fakeroot metadata");
fs::write(rootfs.join(".host/private/secret"), "metadata").expect("write host metadata");
fs::write(rootfs.join(".fakedata/owner"), "metadata").expect("write fakeroot metadata");
fs::create_dir_all(rootfs.join("etc")).expect("create visible directory");
fs::write(rootfs.join("etc/controller.conf"), "controller\n").expect("write visible file");
let legacy = fixture.path().join("legacy.tar");
let native = fixture.path().join("native.tar");
let mut legacy_writer = tar::Builder::new(fs::File::create(&legacy).expect("create legacy archive"));
legacy_writer
.append_dir_all(".", &rootfs)
.expect("write legacy archive with internal metadata");
legacy_writer.finish().expect("finish legacy archive");
NativeTarWriter::new().write(&rootfs, &native).expect("write native archive");
let legacy_manifest = ArtifactManifest::collect_archive(&legacy).expect("collect legacy facts");
let native_manifest = ArtifactManifest::collect_archive(&native).expect("collect native facts");
assert!(legacy_manifest
.archive_members
.iter()
.all(|record| !record.path.starts_with(".host/") && !record.path.starts_with(".fakedata/")));
let report = compare(&legacy_manifest, &native_manifest);
assert!(report.is_equivalent(), "{}", report.render());
}
#[test]
fn native_manifest_collection_excludes_internal_members_from_its_archive_facts() {
let fixture = tempdir().expect("temporary directory");
let rootfs = fixture.path().join("rootfs");
fs::create_dir_all(rootfs.join(".host/private")).expect("create host metadata");
fs::create_dir_all(rootfs.join(".fakedata")).expect("create fakeroot metadata");
fs::create_dir_all(rootfs.join("etc")).expect("create visible directory");
fs::write(rootfs.join(".host/private/secret"), "metadata").expect("write host metadata");
fs::write(rootfs.join(".fakedata/owner"), "metadata").expect("write fakeroot metadata");
fs::write(rootfs.join("etc/controller.conf"), "controller\n").expect("write visible file");
let artifact = fixture.path().join("legacy.tar");
let mut writer = tar::Builder::new(fs::File::create(&artifact).expect("create legacy archive"));
writer.append_dir_all(".", &rootfs).expect("write legacy archive");
writer.finish().expect("finish legacy archive");
let manifest = ArtifactManifest::collect(&rootfs, vec![], None, &artifact)
.expect("collect native filesystem and archive facts");
assert!(manifest
.archive_members
.iter()
.all(|record| !record.path.starts_with(".host/") && !record.path.starts_with(".fakedata/")));
}
#[test]
fn rootfs_collection_recognizes_services_enabled_by_non_default_targets() {
let fixture = tempdir().expect("temporary directory");
let rootfs = fixture.path().join("rootfs");
let wants = rootfs.join("etc/systemd/system/graphical.target.wants");
fs::create_dir_all(&wants).expect("create service state directory");
symlink(
"/usr/lib/systemd/system/controller.service",
wants.join("controller.service"),
)
.expect("enable service");
let artifact = fixture.path().join("controller.tar");
NativeTarWriter::new()
.write(&rootfs, &artifact)
.expect("write native archive");
let manifest = ArtifactManifest::collect(&rootfs, vec![], None, &artifact)
.expect("collect native filesystem and archive facts");
assert_eq!(manifest.services, vec![ServiceRecord::new("controller.service", true)]);
}
#[test]
fn archive_collection_coalesces_a_service_enabled_by_multiple_targets() {
let fixture = tempdir().expect("temporary directory");
let rootfs = fixture.path().join("rootfs");
for target in ["multi-user.target.wants", "graphical.target.wants"] {
let wants = rootfs.join("etc/systemd/system").join(target);
fs::create_dir_all(&wants).expect("create service state directory");
symlink(
"/usr/lib/systemd/system/controller.service",
wants.join("controller.service"),
)
.expect("enable service");
}
let artifact = fixture.path().join("controller.tar");
NativeTarWriter::new()
.write(&rootfs, &artifact)
.expect("write native archive");
let manifest = ArtifactManifest::collect_archive(&artifact).expect("collect archive facts");
assert_eq!(manifest.services, vec![ServiceRecord::new("controller.service", true)]);
}
|