package agentserver import "testing" func TestRoleAllowed(t *testing.T) { cases := []struct { roles []string allowed string want bool }{ {nil, "", true}, {[]string{"dev"}, " ", true}, {[]string{"dev"}, "ops,admin", false}, {[]string{"dev", "ops"}, "ops, admin", true}, {nil, "ops", false}, } for _, c := range cases { if got := RoleAllowed(c.roles, c.allowed); got != c.want { t.Errorf("RoleAllowed(%v, %q)=%v, want %v", c.roles, c.allowed, got, c.want) } } } func TestSelectProfile(t *testing.T) { byRole := "ops:prf_ops, admin:prf_admin,bad, :prf_x,dev:" cases := []struct { roles []string want string }{ {[]string{"dev"}, "prf_default"}, // dev 映射值为空,忽略 {[]string{"admin"}, "prf_admin"}, {[]string{"admin", "ops"}, "prf_ops"}, // 按配置顺序取第一个命中 {nil, "prf_default"}, } for _, c := range cases { if got := SelectProfile(c.roles, byRole, " prf_default "); got != c.want { t.Errorf("SelectProfile(%v)=%q, want %q", c.roles, got, c.want) } } if got := SelectProfile([]string{"x"}, "", ""); got != "" { t.Errorf("没有默认值时应返回空,得到 %q", got) } }