isothrum-machine 3.0.3

Machine-setup domain for Isothrum: check this computer's tools, Docker engines and WSL 2, and detect the Vagrant providers (hypervisors) it can drive.
Documentation
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
use super::*;

/// A machine where nothing could be read.
fn blank() -> Probes {
    Probes {
        status: Run::Failed,
        version: Run::Failed,
        lxss_service: None,
        wsl_service: None,
        vm_platform: None,
        wsl_feature: None,
        hypervisor: None,
        reboot_pending: None,
        renames_pending: None,
        installed_this_boot: false,
        restarted_for_wsl: false,
    }
}

/// Windows 11 without WSL: wsl.exe is there, and only offers to install.
fn win11_without_wsl() -> Probes {
    Probes {
        lxss_service: Some(false),
        wsl_service: Some(false),
        vm_platform: Some(Feature::Disabled),
        wsl_feature: Some(Feature::Disabled),
        hypervisor: Some(false),
        reboot_pending: Some(false),
        renames_pending: Some(false),
        ..blank()
    }
}

/// Windows 11 with the Store WSL and no distribution, after the restart.
fn win11_store_ready() -> Probes {
    Probes {
        status: Run::Ok,
        version: Run::Ok,
        wsl_service: Some(true),
        vm_platform: Some(Feature::Enabled),
        hypervisor: Some(true),
        ..win11_without_wsl()
    }
}

/// Windows 10 21H2 with the WSL feature (no Store WSL: `--version` is unknown to it).
fn win10_inbox_ready() -> Probes {
    Probes {
        status: Run::Ok,
        version: Run::Failed,
        lxss_service: Some(true),
        wsl_service: Some(false),
        vm_platform: Some(Feature::Enabled),
        wsl_feature: Some(Feature::Enabled),
        hypervisor: Some(true),
        reboot_pending: Some(false),
        renames_pending: Some(false),
        installed_this_boot: false,
        restarted_for_wsl: false,
    }
}

/// The Store WSL turned on, virtualization off in the firmware: the platform reads as on,
/// no hypervisor ever starts, and something re-adds file renames at every boot.
fn firmware_off() -> Probes {
    Probes {
        status: Run::Failed,
        hypervisor: Some(false),
        renames_pending: Some(true),
        ..win11_store_ready()
    }
}

#[test]
fn a_machine_without_wsl_is_missing() {
    assert_eq!(classify(&win11_without_wsl()), Missing);
    // Windows 10, whose wsl.exe may not exist at all.
    assert_eq!(
        classify(&Probes {
            status: Run::Missing,
            version: Run::Missing,
            ..win11_without_wsl()
        }),
        Missing
    );
    // The features couldn't be read: the services and wsl.exe still agree.
    assert_eq!(
        classify(&Probes {
            vm_platform: None,
            wsl_feature: None,
            hypervisor: None,
            ..win11_without_wsl()
        }),
        Missing
    );
    // A restart for an update, or renames, are no sign of WSL.
    assert_eq!(
        classify(&Probes {
            reboot_pending: Some(true),
            renames_pending: Some(true),
            ..win11_without_wsl()
        }),
        Missing
    );
}

#[test]
fn wsl_on_and_running_is_ready() {
    assert_eq!(classify(&win11_store_ready()), Ready);
    assert_eq!(classify(&win10_inbox_ready()), Ready);
    // Without a distribution `wsl --status` may fail: the platform running is enough.
    assert_eq!(
        classify(&Probes {
            status: Run::Failed,
            ..win11_store_ready()
        }),
        Ready
    );
    // A hung wsl.exe doesn't hide the rest.
    assert_eq!(
        classify(&Probes {
            status: Run::TimedOut,
            version: Run::TimedOut,
            ..win11_store_ready()
        }),
        Ready
    );
    // WMI unavailable: WSL answering, with nothing waiting for a restart, is enough.
    assert_eq!(
        classify(&Probes {
            vm_platform: None,
            wsl_feature: None,
            hypervisor: None,
            ..win11_store_ready()
        }),
        Ready
    );
    // An update waiting for a restart, while WSL answers.
    assert_eq!(
        classify(&Probes {
            reboot_pending: Some(true),
            renames_pending: Some(true),
            ..win11_store_ready()
        }),
        Ready
    );
    // Renames left by others never hold a working WSL back.
    assert_eq!(
        classify(&Probes {
            renames_pending: Some(true),
            ..win10_inbox_ready()
        }),
        Ready
    );
}

#[test]
fn a_pending_update_doesnt_hide_a_store_wsl_without_a_distribution() {
    // The usual state after `wsl --install --no-distribution` and its restart: `--status`
    // fails (no distribution), and Windows Update waits for a restart of its own.
    let store = Probes {
        status: Run::Failed,
        reboot_pending: Some(true),
        ..win11_store_ready()
    };
    assert_eq!(classify(&store), Ready);
    // Known by its service alone (`--version` timed out).
    assert_eq!(
        classify(&Probes {
            version: Run::TimedOut,
            ..store
        }),
        Ready
    );
    // The WSL feature (no Store WSL) gives no such proof: the restart may be its own.
    let inbox = Probes {
        status: Run::Failed,
        reboot_pending: Some(true),
        ..win10_inbox_ready()
    };
    assert_eq!(classify(&inbox), NeedsReboot);
}

#[test]
fn unreadable_servicing_is_no_proof_nothing_is_pending() {
    let unread = Probes {
        vm_platform: None,
        wsl_feature: None,
        hypervisor: None,
        reboot_pending: None,
        ..win11_store_ready()
    };
    assert_eq!(classify(&unread), Unknown);
    assert_eq!(
        classify(&Probes {
            reboot_pending: Some(false),
            ..unread
        }),
        Ready
    );
}

#[test]
fn turned_on_but_not_restarted_needs_a_reboot() {
    // Right after the caller's own install: the Store WSL is in, the platform reads as on, no
    // hypervisor yet.
    let after_install = Probes {
        status: Run::Failed,
        version: Run::Ok,
        wsl_service: Some(true),
        vm_platform: Some(Feature::Enabled),
        hypervisor: Some(false),
        reboot_pending: Some(true),
        installed_this_boot: true,
        ..win11_without_wsl()
    };
    assert_eq!(classify(&after_install), NeedsReboot);
    // Hyper-V already ran a hypervisor, and WSL answers: still the restart first.
    assert_eq!(
        classify(&Probes {
            hypervisor: Some(true),
            status: Run::Ok,
            ..after_install
        }),
        NeedsReboot
    );
    // Servicing says nothing, but the caller's install left renames.
    assert_eq!(
        classify(&Probes {
            hypervisor: Some(true),
            reboot_pending: Some(false),
            renames_pending: Some(true),
            ..after_install
        }),
        NeedsReboot
    );
    // The app was reopened before the restart, and WMI still reads the features as off.
    let reopened = Probes {
        vm_platform: Some(Feature::Disabled),
        wsl_feature: Some(Feature::Disabled),
        wsl_service: Some(false),
        version: Run::Failed,
        ..after_install
    };
    assert_eq!(classify(&reopened), NeedsReboot);
    // The caller's install ran this boot and the servicing state can't be read.
    assert_eq!(
        classify(&Probes {
            reboot_pending: None,
            ..reopened
        }),
        NeedsReboot
    );
    // Turned on by hand (`wsl --install` in PowerShell) on Windows 10: no mark of the caller's.
    let by_hand = Probes {
        status: Run::Failed,
        vm_platform: Some(Feature::Enabled),
        wsl_feature: Some(Feature::Enabled),
        lxss_service: Some(false),
        hypervisor: Some(false),
        reboot_pending: Some(true),
        ..win11_without_wsl()
    };
    assert_eq!(classify(&by_hand), NeedsReboot);
}

#[test]
fn our_install_that_changed_nothing_is_judged_on_the_rest() {
    // It ran this boot, yet no restart or rename is pending: WSL was ready already.
    assert_eq!(
        classify(&Probes {
            installed_this_boot: true,
            ..win11_store_ready()
        }),
        Ready
    );
}

#[test]
fn virtualization_off_in_the_firmware_never_loops_on_restarts() {
    // Renames re-added at every boot are no reason to restart (again).
    let off = firmware_off();
    assert_eq!(classify(&off), Unknown);
    assert!(virtualization_off(&off, classify(&off)));
    // After the caller's restart, even with servicing waiting for another one: no second ask.
    let after_ours = Probes {
        restarted_for_wsl: true,
        reboot_pending: Some(true),
        ..off
    };
    assert_eq!(classify(&after_ours), Unknown);
    assert!(virtualization_off(&after_ours, classify(&after_ours)));
    // The same pending restart without one of the caller's yet: that one is worth trying, once.
    let before_ours = Probes {
        reboot_pending: Some(true),
        ..off
    };
    assert_eq!(classify(&before_ours), NeedsReboot);
    assert!(!virtualization_off(&before_ours, NeedsReboot));
    // A new install of the caller's starts over.
    assert_eq!(
        classify(&Probes {
            installed_this_boot: true,
            ..after_ours
        }),
        NeedsReboot
    );
    // A working machine is never told virtualization is off.
    assert!(!virtualization_off(&win11_store_ready(), Ready));
}

#[test]
fn what_the_signals_cant_settle_is_unknown() {
    assert_eq!(classify(&blank()), Unknown);
    // Everything timed out.
    assert_eq!(
        classify(&Probes {
            status: Run::TimedOut,
            version: Run::TimedOut,
            ..blank()
        }),
        Unknown
    );
    // A hung wsl.exe with no other sign of WSL: maybe it's there and stuck.
    assert_eq!(
        classify(&Probes {
            status: Run::TimedOut,
            ..win11_without_wsl()
        }),
        Unknown
    );
    // On but no hypervisor and nothing pending: perhaps virtualization is off in the firmware.
    let no_hypervisor = Probes {
        hypervisor: Some(false),
        ..win10_inbox_ready()
    };
    assert_eq!(classify(&no_hypervisor), Unknown);
    assert!(virtualization_off(&no_hypervisor, Unknown));
    // Only the platform on, nothing of WSL: neither ready nor plainly missing.
    assert_eq!(
        classify(&Probes {
            vm_platform: Some(Feature::Enabled),
            ..win11_without_wsl()
        }),
        Unknown
    );
}

#[test]
fn wsl_1_without_the_platform_is_missing_wsl_2() {
    let wsl1 = Probes {
        vm_platform: Some(Feature::Disabled),
        hypervisor: Some(false),
        ..win10_inbox_ready()
    };
    assert_eq!(classify(&wsl1), Missing);
    // A restart pending for something else (an update) doesn't turn the platform on: no restart
    // is asked for, "Turn on WSL 2" is.
    assert_eq!(
        classify(&Probes {
            reboot_pending: Some(true),
            ..wsl1
        }),
        Missing
    );
    // The caller's own install this boot is what the restart finishes, whatever the platform reads.
    assert_eq!(
        classify(&Probes {
            reboot_pending: Some(true),
            installed_this_boot: true,
            ..wsl1
        }),
        NeedsReboot
    );
}

#[test]
fn a_store_wsl_without_the_platform_and_an_updates_restart_pending_is_missing() {
    // Docker Desktop's "Virtual Machine Platform is not enabled": the Store WSL is installed, the
    // platform reads off, and Windows waits for an update's restart.
    let store_without_platform = Probes {
        version: Run::Ok,
        wsl_service: Some(true),
        reboot_pending: Some(true),
        renames_pending: Some(true),
        ..win11_without_wsl()
    };
    assert_eq!(classify(&store_without_platform), Missing);
}

#[test]
fn the_wmi_output_is_read_by_key() {
    let out = "features=ok\r\nfeature:VirtualMachinePlatform=1\r\nfeature:Microsoft-Windows-Subsystem-Linux=2\r\nhypervisor=True\r\n";
    assert_eq!(parse_cim(out), (Some(Feature::Enabled), Some(Feature::Disabled), Some(true)));
    // An edition without the feature doesn't list it.
    assert_eq!(
        parse_cim("features=ok\nfeature:VirtualMachinePlatform=3\nhypervisor=False\n"),
        (Some(Feature::Absent), Some(Feature::Absent), Some(false))
    );
    // The feature query failed; the hypervisor one didn't.
    assert_eq!(parse_cim("hypervisor=False\n"), (None, None, Some(false)));
    // InstallState 4 (unknown), and nothing at all.
    assert_eq!(
        parse_cim("features=ok\nfeature:VirtualMachinePlatform=4\nfeature:Microsoft-Windows-Subsystem-Linux=1"),
        (None, Some(Feature::Enabled), None)
    );
    assert_eq!(parse_cim(""), (None, None, None));
}