Skip to content

Commit 090246f

Browse files
authored
Merge pull request #73 from numtide/feat/capture-swap
feat: capture swap entries
2 parents 6532af4 + 6b16886 commit 090246f

File tree

14 files changed

+522
-22
lines changed

14 files changed

+522
-22
lines changed

cmd/root.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ var (
1919
cfgFile string
2020
outputPath string
2121
hardwareFeatures []string
22+
23+
scanner = facter.Scanner{}
2224
)
2325

2426
// rootCmd represents the base command when called without any subcommands
@@ -29,16 +31,15 @@ var rootCmd = &cobra.Command{
2931
// todo add Long description
3032
RunE: func(cmd *cobra.Command, args []string) error {
3133
// convert the hardware features into probe features
32-
var hardwareProbes []hwinfo.ProbeFeature
3334
for _, str := range hardwareFeatures {
3435
probe, err := hwinfo.ProbeFeatureString(str)
3536
if err != nil {
3637
return fmt.Errorf("invalid hardware feature: %w", err)
3738
}
38-
hardwareProbes = append(hardwareProbes, probe)
39+
scanner.Features = append(scanner.Features, probe)
3940
}
4041

41-
report, err := facter.GenerateReport(hardwareProbes)
42+
report, err := scanner.Scan()
4243
if err != nil {
4344
return err
4445
}
@@ -81,7 +82,11 @@ func init() {
8182

8283
// Cobra also supports local flags, which will only run when this action is called directly.
8384
f := rootCmd.Flags()
84-
f.StringVarP(&outputPath, "output", "o", "", "Path to write the report")
85+
f.StringVarP(&outputPath, "output", "o", "", "path to write the report")
86+
87+
// Options for optional ephemeral system properties.
88+
f.BoolVarP(&scanner.Swap, "swap", "s", false, "capture swap entries")
89+
f.BoolVarP(&scanner.Ephemeral, "ephemeral", "e", false, "capture all ephemeral properties e.g. swap, filesystems and so on")
8590

8691
// We currently support all probe features at a high level as they share some generic information,
8792
// but we do not have mappings for all of their detail sections.
@@ -105,11 +110,11 @@ func init() {
105110

106111
f.StringSliceVarP(
107112
&hardwareFeatures,
108-
"hardware-features",
113+
"hardware",
109114
"f",
110115
defaultFeatures,
111116
fmt.Sprintf(
112-
"Hardware features to probe. Possible values are %s",
117+
"Hardware items to probe. Possible values are %s",
113118
strings.Join(allFeatures, ","),
114119
),
115120
)

flake.lock

Lines changed: 39 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
url = "github:numtide/treefmt-nix";
2222
inputs.nixpkgs.follows = "nixpkgs";
2323
};
24+
disko.url = "github:nix-community/disko";
2425
};
2526

2627
# Keep the magic invocations to minimum.

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ require (
77
github.com/klauspost/cpuid/v2 v2.2.9-0.20240805145549-92d5326f011e
88
github.com/spf13/cobra v1.8.1
99
github.com/spf13/viper v1.19.0
10+
github.com/stretchr/testify v1.9.0
1011
)
1112

1213
require (
1314
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
1415
github.com/charmbracelet/lipgloss v0.10.0 // indirect
16+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
1517
github.com/fsnotify/fsnotify v1.7.0 // indirect
1618
github.com/go-logfmt/logfmt v0.6.0 // indirect
1719
github.com/hashicorp/hcl v1.0.0 // indirect
@@ -24,6 +26,7 @@ require (
2426
github.com/muesli/reflow v0.3.0 // indirect
2527
github.com/muesli/termenv v0.15.2 // indirect
2628
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
29+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
2730
github.com/rivo/uniseg v0.4.7 // indirect
2831
github.com/sagikazarmark/locafero v0.4.0 // indirect
2932
github.com/sagikazarmark/slog-shim v0.1.0 // indirect

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
33
*/
44
package main
55

6-
import "github.com/numtide/nixos-facter/cmd"
6+
import (
7+
"github.com/numtide/nixos-facter/cmd"
8+
)
79

810
func main() {
911
cmd.Execute()

nix/packages/nixos-facter/default.nix

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
args @ {
1+
{
22
flake,
3+
# We need the following pragma to ensure deadnix doesn't remove inputs.
4+
# This package is being called with newScope/callPackage, which means it is only being passed args it defines.
5+
# We do not use inputs directly in this file, but need it for passing to the tests.
6+
# deadnix: skip
7+
inputs,
38
perSystem,
49
system,
510
pkgs,
611
pname,
712
...
8-
}: let
13+
} @ args: let
914
inherit (pkgs) go lib;
1015
fs = lib.fileset;
1116
in

nix/packages/nixos-facter/gomod2nix.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ schema = 3
1010
[mod."github.com/charmbracelet/log"]
1111
version = "v0.4.0"
1212
hash = "sha256-VQerB44vC646n3fe3haJ3DHa9L5+GRhCfDfm1p3QnZk="
13+
[mod."github.com/davecgh/go-spew"]
14+
version = "v1.1.2-0.20180830191138-d8f796af33cc"
15+
hash = "sha256-fV9oI51xjHdOmEx6+dlq7Ku2Ag+m/bmbzPo6A4Y74qc="
1316
[mod."github.com/fsnotify/fsnotify"]
1417
version = "v1.7.0"
1518
hash = "sha256-MdT2rQyQHspPJcx6n9ozkLbsktIOJutOqDuKpNAtoZY="
@@ -49,6 +52,9 @@ schema = 3
4952
[mod."github.com/pelletier/go-toml/v2"]
5053
version = "v2.2.2"
5154
hash = "sha256-ukxk1Cfm6cQW18g/aa19tLcUu5BnF7VmfAvrDHAOl6A="
55+
[mod."github.com/pmezard/go-difflib"]
56+
version = "v1.0.1-0.20181226105442-5d4384ee4fb2"
57+
hash = "sha256-XA4Oj1gdmdV/F/+8kMI+DBxKPthZ768hbKsO3d9Gx90="
5258
[mod."github.com/rivo/uniseg"]
5359
version = "v0.4.7"
5460
hash = "sha256-rDcdNYH6ZD8KouyyiZCUEy8JrjOQoAkxHBhugrfHjFo="
@@ -76,6 +82,9 @@ schema = 3
7682
[mod."github.com/spf13/viper"]
7783
version = "v1.19.0"
7884
hash = "sha256-MZ8EAvdgpGbw6kmUz8UOaAAAMdPPGd14TrCBAY+A1T4="
85+
[mod."github.com/stretchr/testify"]
86+
version = "v1.9.0"
87+
hash = "sha256-uUp/On+1nK+lARkTVtb5RxlW15zxtw2kaAFuIASA+J0="
7988
[mod."github.com/subosito/gotenv"]
8089
version = "v1.6.0"
8190
hash = "sha256-LspbjTniiq2xAICSXmgqP7carwlNaLqnCTQfw2pa80A="
Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,76 @@
11
{
22
pkgs,
3-
flake,
3+
inputs,
44
system,
5+
perSystem,
56
...
67
}: let
7-
inherit (flake.packages.${system}) nixos-facter;
8+
# we have to import diskoLib like this because there are some impure default imports e.g. <nixpkgs>
9+
diskoLib = import "${inputs.disko}/lib" {
10+
inherit (pkgs) lib;
11+
makeTest = import "${inputs.nixpkgs}/nixos/tests/make-test-python.nix";
12+
eval-config = import "${inputs.nixpkgs}/nixos/lib/eval-config.nix";
13+
};
814
in
915
# for now we only run the tests in x86_64-linux since we don't have access to a bare-metal ARM box or a VM that supports nested
1016
# virtualization which makes the test take forever and ultimately fail
1117
pkgs.lib.optionalAttrs pkgs.stdenv.isx86_64 {
12-
basic = pkgs.nixosTest {
18+
basic = diskoLib.testLib.makeDiskoTest {
19+
inherit pkgs;
1320
name = "basic";
14-
nodes.machine = {
15-
environment.systemPackages = [nixos-facter];
21+
disko-config = ./disko.nix;
22+
extraSystemConfig = {
23+
environment.systemPackages = [
24+
perSystem.self.nixos-facter
25+
];
26+
27+
systemd.services = {
28+
create-swap-files = {
29+
serviceConfig.Type = "oneshot";
30+
wantedBy = ["multi-user.target"];
31+
path = with pkgs; [
32+
coreutils
33+
util-linux
34+
];
35+
script = ''
36+
# create some swap files
37+
mkdir -p /swap
38+
for (( i=1; i<=3; i++ )); do
39+
out="/swap/swapfile-$i"
40+
dd if=/dev/zero of="$out" bs=1MB count=10
41+
chmod 600 "$out"
42+
mkswap "$out"
43+
swapon "$out"
44+
done
45+
'';
46+
};
47+
};
1648
};
17-
testScript = ''
18-
machine.succeed("nixos-facter generate report -o /report.json")
49+
50+
extraTestScript = ''
51+
import json
52+
53+
report = json.loads(machine.succeed("nixos-facter -e"))
54+
55+
with subtest("Capture system"):
56+
assert report['system'] == '${system}'
57+
58+
with subtest("Capture virtualisation"):
59+
virt = report['virtualisation']
60+
# kvm for systems that support it, otherwise the vm test should present itself as qemu
61+
# todo double-check this is the same for intel
62+
assert virt in ("kvm", "qemu"), f"expected virtualisation to be either kvm or qemu, got {virt}"
63+
64+
with subtest("Capture swap entries"):
65+
assert 'swap' in report, "'swap' not found in the report"
66+
assert report['swap'] == [
67+
{ 'path': '/dev/vda4', 'type': 'partition', 'size': 1048572, 'used': 0, 'priority': -2 },
68+
{ 'path': '/dev/dm-0', 'type': 'partition', 'size': 10236, 'used': 0, 'priority': 100 },
69+
{ 'path': '/swap/swapfile-1', 'type': 'file', 'size': 9760, 'used': 0, 'priority': -3 },
70+
{ 'path': '/swap/swapfile-2', 'type': 'file', 'size': 9760, 'used': 0, 'priority': -4 },
71+
{ 'path': '/swap/swapfile-3', 'type': 'file', 'size': 9760, 'used': 0, 'priority': -5 }
72+
], "swap entries did not match what we expected"
73+
# todo is there a nice way of showing a diff?
1974
'';
2075
};
2176
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
disko = {
3+
devices = {
4+
disk = {
5+
main = {
6+
device = "/dev/vdb";
7+
type = "disk";
8+
content = {
9+
type = "gpt";
10+
partitions = {
11+
ESP = {
12+
size = "500M";
13+
type = "EF00";
14+
content = {
15+
type = "filesystem";
16+
format = "vfat";
17+
mountpoint = "/boot";
18+
};
19+
};
20+
root = {
21+
end = "-1G";
22+
content = {
23+
type = "filesystem";
24+
format = "ext4";
25+
mountpoint = "/";
26+
};
27+
};
28+
encryptedSwap = {
29+
size = "10M";
30+
content = {
31+
type = "swap";
32+
randomEncryption = true;
33+
priority = 100; # prefer to encrypt as long as we have space for it
34+
};
35+
};
36+
plainSwap = {
37+
size = "100%";
38+
content = {
39+
type = "swap";
40+
discardPolicy = "both";
41+
resumeDevice = true; # resume from hiberation from this device
42+
};
43+
};
44+
};
45+
};
46+
};
47+
};
48+
};
49+
};
50+
}

0 commit comments

Comments
 (0)