myxce/src/frontend/apiserver/models/hosts_test.go
Rongsong Shen febb4a6c4d fix: preserve admin-set host state across worker re-registration in makeWorker_
When a worker re-joins and already exists in workermgr, makeWorker_()
now overrides the existing slot instead of returning EEXIST. Admin-set
flags (LOCKEDU, CLOSED, HIDDEN, CLOSED_BY_ADMIN) survive the override.

Add XCE_WORKER_CLOSED_BY_ADMIN flag to distinguish admin-closed hosts
from workload-closed ones. worker_ctrl sets/clears it alongside CLOSED.

Refactor worker_init/worker_destroy into copy_worker/kill_worker helpers
shared by both fresh creation and override paths.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-13 10:38:39 +00:00

71 lines
No EOL
1.4 KiB
Go

package models
import "testing"
func TestHostStateStr(t *testing.T) {
tests := []struct {
name string
state int
want string
}{
{
name: "zero value (no bad flags set) returns OK — C status=0 means healthy",
state: 0,
want: "OK",
},
{
name: "UNAVAIL flag",
state: HOSTSTAT["UNAVAIL"],
want: "UNAVAIL",
},
{
name: "CLOSED flag",
state: HOSTSTAT["CLOSED"],
want: "CLOSED",
},
{
name: "CLOSED_BY_ADMIN flag",
state: HOSTSTAT["CLOSED_BY_ADMIN"],
want: "CLOSED_BY_ADMIN",
},
{
name: "BUSY flag",
state: HOSTSTAT["BUSY"],
want: "BUSY",
},
{
name: "LOCKEDU flag",
state: HOSTSTAT["LOCKEDU"],
want: "LOCKU",
},
{
name: "LOCKEDW flag",
state: HOSTSTAT["LOCKEDW"],
want: "LOCKW",
},
{
name: "no flags set returns OK for real host status",
state: 0x00000001, // some non-zero, non-flag value
want: "OK",
},
{
name: "combined UNAVAIL+CLOSED flags",
state: HOSTSTAT["UNAVAIL"] | HOSTSTAT["CLOSED"],
want: "UNAVAIL", // UNAVAIL takes priority
},
{
name: "combined CLOSED+CLOSED_BY_ADMIN flags",
state: HOSTSTAT["CLOSED"] | HOSTSTAT["CLOSED_BY_ADMIN"],
want: "CLOSED_BY_ADMIN", // CLOSED_BY_ADMIN overrides generic CLOSED
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := HostStateStr(tt.state)
if got != tt.want {
t.Errorf("HostStateStr(%d) = %q, want %q", tt.state, got, tt.want)
}
})
}
}