fleet/orbit/pkg/user/user_linux_test.go

84 lines
1.6 KiB
Go
Raw Normal View History

Fix orbit active GUI session detection to start Fleet Desktop and key escrowing on Linux (#39777) Resolves #36024 and #34501. Main change is about stop using the first user in the `users` command output [*] and instead use `loginctl` commands to pick the correct current active GUI user. [*] `users` was returning empty on some new distributions, and in multi-sessions we were always picking the first one (even if it wasn't active). - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. ## Testing - [x] QA'd all new/changed functionality manually ## fleetd/orbit/Fleet Desktop - [x] Verified compatibility with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)) - [x] If the change applies to only one platform, confirmed that `runtime.GOOS` is used as needed to isolate changes - [x] Verified that fleetd runs on macOS, Linux and Windows - [x] Verified auto-update works from the released version of component to the new version (see [tools/tuf/test](../tools/tuf/test/README.md)) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Bug Fixes** * Fixed Fleet Desktop startup to correctly detect and use the active GUI session on Linux systems. * Improved GUI user detection for dialog prompts, ensuring system dialogs run in the proper user context. * **Improvements** * Enhanced error reporting and logging clarity for GUI session detection failures. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-16 14:41:16 +00:00
//go:build linux
package user
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseLoginctlUsersOutput(t *testing.T) {
tests := []struct {
name string
input string
want []User
wantErr string
}{
{
name: "single user",
input: " 1000 alice\n",
want: []User{{Name: "alice", ID: 1000}},
},
{
name: "multiple users",
input: " 1000 alice\n 1001 bob\n",
want: []User{
{Name: "alice", ID: 1000},
{Name: "bob", ID: 1001},
},
},
{
name: "includes system user",
input: " 0 root\n 1000 alice\n 120 gdm\n",
want: []User{
{Name: "root", ID: 0},
{Name: "alice", ID: 1000},
{Name: "gdm", ID: 120},
},
},
{
name: "no leading whitespace",
input: "1000 alice\n1001 bob\n",
want: []User{
{Name: "alice", ID: 1000},
{Name: "bob", ID: 1001},
},
},
{
name: "extra columns (some systemd versions)",
input: "1000 alice extra-stuff\n",
want: []User{{Name: "alice", ID: 1000}},
},
{
name: "empty output",
input: "",
wantErr: "no user session found",
},
{
name: "whitespace only",
input: " \n \n",
wantErr: "no user session found",
},
{
name: "skips malformed lines",
input: "not-a-uid alice\n1000 bob\n",
want: []User{{Name: "bob", ID: 1000}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseLoginctlUsersOutput(tt.input)
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}