From 0e5ca74b983f3a60c3e251f7794a1cad85d296fa Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sun, 2 Nov 2025 16:15:09 +0900 Subject: [PATCH] cmd/hakurei/print: serialise array for ps Wanted to do this for a long time, since the key is redundant. This also makes it easier to migrate to the new store interface. Signed-off-by: Ophestra --- cmd/hakurei/print.go | 8 +++---- cmd/hakurei/print_test.go | 50 ++++++++++++++++++++++++++++++++++++--- cmd/hpkg/test/test.py | 2 +- test/configuration.nix | 4 ++-- test/test.py | 2 +- 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/cmd/hakurei/print.go b/cmd/hakurei/print.go index 6e5c690..eaa8661 100644 --- a/cmd/hakurei/print.go +++ b/cmd/hakurei/print.go @@ -1,9 +1,11 @@ package main import ( + "bytes" "fmt" "io" "log" + "maps" "slices" "strconv" "strings" @@ -177,10 +179,8 @@ func printPs(output io.Writer, now time.Time, s store.Compat, short, flagJSON bo } if !short && flagJSON { - es := make(map[string]*hst.State, len(entries)) - for id, instance := range entries { - es[id.String()] = instance - } + es := slices.Collect(maps.Values(entries)) + slices.SortFunc(es, func(a, b *hst.State) int { return bytes.Compare(a.ID[:], b.ID[:]) }) encodeJSON(log.Fatal, output, short, es) return } diff --git a/cmd/hakurei/print_test.go b/cmd/hakurei/print_test.go index 3db06ec..5309e37 100644 --- a/cmd/hakurei/print_test.go +++ b/cmd/hakurei/print_test.go @@ -1,10 +1,12 @@ package main import ( + "bytes" "strings" "testing" "time" + "hakurei.app/container/check" "hakurei.app/hst" "hakurei.app/internal/store" ) @@ -530,8 +532,25 @@ func TestPrintPs(t *testing.T) { 4cf073bd 3405691582 9 (org.chromium.Chromium) 1h2m32s `}, {"valid short", map[hst.ID]*hst.State{testID: testState}, true, false, "4cf073bd\n"}, - {"valid json", map[hst.ID]*hst.State{testID: testState}, false, true, `{ - "8e2c76b066dabe574cf073bdb46eb5c1": { + {"valid json", map[hst.ID]*hst.State{testID: testState, (hst.ID)(bytes.Repeat([]byte{0xaa}, len(hst.ID{}))): { + ID: (hst.ID)(bytes.Repeat([]byte{0xaa}, len(hst.ID{}))), + PID: 0xbeef, + ShimPID: 0xcafe, + Config: &hst.Config{ + ID: "uk.gensokyo.cat", + Enablements: hst.NewEnablements(hst.EWayland | hst.EPulse), + Identity: 1, + Container: &hst.ContainerConfig{ + Shell: check.MustAbs("/bin/sh"), + Home: check.MustAbs("/data/data/uk.gensokyo.cat"), + Path: check.MustAbs("/usr/bin/cat"), + Args: []string{"cat"}, + Flags: hst.FUserns, + }, + }, + Time: time.Unix(0, 0xdeadbeef).UTC(), + }}, false, true, `[ + { "instance": "8e2c76b066dabe574cf073bdb46eb5c1", "pid": 3405691582, "shim_pid": 3735928559, @@ -683,8 +702,33 @@ func TestPrintPs(t *testing.T) { "share_tmpdir": true }, "time": "1970-01-01T00:00:00.000000009Z" + }, + { + "instance": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "pid": 48879, + "shim_pid": 51966, + "id": "uk.gensokyo.cat", + "enablements": { + "wayland": true, + "pulse": true + }, + "identity": 1, + "groups": null, + "container": { + "env": null, + "filesystem": null, + "shell": "/bin/sh", + "home": "/data/data/uk.gensokyo.cat", + "path": "/usr/bin/cat", + "args": [ + "cat" + ], + "userns": true, + "map_real_uid": false + }, + "time": "1970-01-01T00:00:03.735928559Z" } -} +] `}, {"valid short json", map[hst.ID]*hst.State{testID: testState}, true, true, `["8e2c76b066dabe574cf073bdb46eb5c1"] `}, diff --git a/cmd/hpkg/test/test.py b/cmd/hpkg/test/test.py index 6f16729..dcfe4e9 100644 --- a/cmd/hpkg/test/test.py +++ b/cmd/hpkg/test/test.py @@ -58,7 +58,7 @@ def check_state(name, enablements): instances = json.loads(machine.succeed("sudo -u alice -i XDG_RUNTIME_DIR=/run/user/1000 hakurei --json ps")) if len(instances) != 1: raise Exception(f"unexpected state length {len(instances)}") - instance = next(iter(instances.values())) + instance = instances[0] if len(instance['container']['args']) != 1 or not (instance['container']['args'][0].startswith("/nix/store/")) or f"hakurei-{name}-" not in (instance['container']['args'][0]): raise Exception(f"unexpected args {instance['container']['args']}") diff --git a/test/configuration.nix b/test/configuration.nix index 39fd166..4ac8f76 100644 --- a/test/configuration.nix +++ b/test/configuration.nix @@ -84,14 +84,14 @@ virtualisation = { # Hopefully reduces spurious test failures: - memorySize = 4096; + memorySize = 8192; qemu.options = [ # Need to switch to a different GPU driver than the default one (-vga std) so that Sway can launch: "-vga none -device virtio-gpu-pci" # Increase Go test compiler performance: - "-smp 8" + "-smp 16" ]; }; diff --git a/test/test.py b/test/test.py index cfc2f84..839a2b4 100644 --- a/test/test.py +++ b/test/test.py @@ -56,7 +56,7 @@ def check_state(name, enablements): instances = json.loads(machine.succeed("sudo -u alice -i XDG_RUNTIME_DIR=/run/user/1000 hakurei --json ps")) if len(instances) != 1: raise Exception(f"unexpected state length {len(instances)}") - instance = next(iter(instances.values())) + instance = instances[0] command = f"{name}-start" if not (instance['container']['path'].startswith("/nix/store/")) or not (instance['container']['path'].endswith(command)):