Skip to content

Commit a3075c0

Browse files
author
Uwe Schwaeke
committed
test(hypervisor): make the console socket dir tests observe the chown
TestEnsureOwnedDir and TestPrepareConsoleSocketDir chowned the console socket directory to this process's own uid:gid, which a new directory already has, so they passed with the chown removed. New subtests ask for an owner the directory does not have (nobody:nogroup as root, otherwise a second group of this user) and check that the owner changed. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Uwe Schwaeke <uwe.schwaeke@sap.com>
1 parent 0f9b850 commit a3075c0

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

‎pkg/hypervisor/console_dir_test.go‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ func statOwnerMode(t *testing.T, path string) (uid, gid int, mode os.FileMode) {
7676

7777
// The socket directory is QEMU's and closed to everyone else (0750). A missing parent is
7878
// created traversable (0755), because QEMU must still reach its disks under the storage
79-
// root. An existing directory with a wider mode is corrected.
79+
// root. An existing directory with a wider mode is corrected, and one with another owner
80+
// becomes QEMU's.
8081
func TestEnsureOwnedDir(t *testing.T) {
8182
uid, gid := os.Getuid(), os.Getgid() // chown to oneself needs no privilege
8283

@@ -106,6 +107,22 @@ func TestEnsureOwnedDir(t *testing.T) {
106107
t.Errorf("mode = %o; want 750", mode)
107108
}
108109
})
110+
111+
// The subtests above chown to this process's own owner, which a new directory already
112+
// has; this one asks for an owner it does not have, so the chown is seen.
113+
t.Run("existing and someone else's", func(t *testing.T) {
114+
dir := filepath.Join(t.TempDir(), "console-sockets")
115+
if err := os.Mkdir(dir, 0o750); err != nil {
116+
t.Fatal(err)
117+
}
118+
qemuUID, qemuGID := otherOwner(t)
119+
if err := ensureOwnedDir(dir, qemuUID, qemuGID); err != nil {
120+
t.Fatalf("ensureOwnedDir: %v", err)
121+
}
122+
if gotUID, gotGID, _ := statOwnerMode(t, dir); gotUID != qemuUID || gotGID != qemuGID {
123+
t.Errorf("owner = %d:%d; want %d:%d", gotUID, gotGID, qemuUID, qemuGID)
124+
}
125+
})
109126
}
110127

111128
// PrepareConsoleSocketDir reads QEMU's owner from libvirt and records the outcome:
@@ -127,6 +144,21 @@ func TestPrepareConsoleSocketDir(t *testing.T) {
127144
}
128145
})
129146

147+
// The label above names this process's own owner, which a new directory already has;
148+
// this one names an owner it does not have, so the chown to it is seen.
149+
t.Run("prepared for someone else", func(t *testing.T) {
150+
uid, gid := otherOwner(t)
151+
qemuLabel := fmt.Sprintf("<baselabel type='kvm'>+%d:+%d</baselabel>", uid, gid)
152+
m := newConnectedManagerWith(t, &fakeLibvirtd{caps: capsWith(apparmorModel + dacModel(qemuLabel))})
153+
dir := filepath.Join(t.TempDir(), "console-sockets")
154+
if err := m.PrepareConsoleSocketDir(t.Context(), dir); err != nil {
155+
t.Fatalf("PrepareConsoleSocketDir: %v", err)
156+
}
157+
if gotUID, gotGID, _ := statOwnerMode(t, dir); gotUID != uid || gotGID != gid {
158+
t.Errorf("owner = %d:%d; want %d:%d", gotUID, gotGID, uid, gid)
159+
}
160+
})
161+
130162
t.Run("no owner reported", func(t *testing.T) {
131163
m := newConnectedManagerWith(t, &fakeLibvirtd{caps: capsWith(apparmorModel)})
132164
dir := filepath.Join(t.TempDir(), "console-sockets")

0 commit comments

Comments
 (0)