mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-24 01:26:39 +00:00
ipn/ipnlocal, util/syspolicy: convert last RegisterWellKnownSettingsForTest caller, remove
Updates #16998 Change-Id: I735d75129a97a929092e9075107e41cdade18944 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
624cdd2961
commit
b034f7cca9
@@ -6,8 +6,12 @@ package policytest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"tailscale.com/util/set"
|
||||
"tailscale.com/util/syspolicy/pkey"
|
||||
"tailscale.com/util/syspolicy/policyclient"
|
||||
"tailscale.com/util/syspolicy/ptype"
|
||||
@@ -29,11 +33,85 @@ type Config map[pkey.Key]any
|
||||
|
||||
var _ policyclient.Client = Config{}
|
||||
|
||||
// Set sets key to value. The value should be of the correct type that it will
|
||||
// be read as later. For PreferenceOption and Visibility, you may also set them
|
||||
// to 'string' values and they'll be UnmarshalText'ed into their correct value
|
||||
// at Get time.
|
||||
//
|
||||
// As a special case, the value can also be of type error to make the accessors
|
||||
// return that error value.
|
||||
func (c *Config) Set(key pkey.Key, value any) {
|
||||
if *c == nil {
|
||||
*c = make(map[pkey.Key]any)
|
||||
}
|
||||
(*c)[key] = value
|
||||
|
||||
if w, ok := (*c)[watchersKey].(*watchers); ok && key != watchersKey {
|
||||
w.mu.Lock()
|
||||
vals := slices.Collect(maps.Values(w.s))
|
||||
w.mu.Unlock()
|
||||
for _, f := range vals {
|
||||
f(policyChange(key))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetMultiple is a batch version of [Config.Set]. It copies the contents of o
|
||||
// into c and does at most one notification wake-up for the whole batch.
|
||||
func (c *Config) SetMultiple(o Config) {
|
||||
if *c == nil {
|
||||
*c = make(map[pkey.Key]any)
|
||||
}
|
||||
|
||||
maps.Copy(*c, o)
|
||||
|
||||
if w, ok := (*c)[watchersKey].(*watchers); ok {
|
||||
w.mu.Lock()
|
||||
vals := slices.Collect(maps.Values(w.s))
|
||||
w.mu.Unlock()
|
||||
for _, f := range vals {
|
||||
f(policyChanges(o))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type policyChange pkey.Key
|
||||
|
||||
func (pc policyChange) HasChanged(v pkey.Key) bool { return pkey.Key(pc) == v }
|
||||
func (pc policyChange) HasChangedAnyOf(keys ...pkey.Key) bool {
|
||||
return slices.Contains(keys, pkey.Key(pc))
|
||||
}
|
||||
|
||||
type policyChanges map[pkey.Key]any
|
||||
|
||||
func (pc policyChanges) HasChanged(v pkey.Key) bool {
|
||||
_, ok := pc[v]
|
||||
return ok
|
||||
}
|
||||
func (pc policyChanges) HasChangedAnyOf(keys ...pkey.Key) bool {
|
||||
for _, k := range keys {
|
||||
if pc.HasChanged(k) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const watchersKey = "_policytest_watchers"
|
||||
|
||||
type watchers struct {
|
||||
mu sync.Mutex
|
||||
s set.HandleSet[func(policyclient.PolicyChange)]
|
||||
}
|
||||
|
||||
// EnableRegisterChangeCallback makes c support the RegisterChangeCallback
|
||||
// for testing. Without calling this, the RegisterChangeCallback does nothing.
|
||||
// For watchers to be notified, use the [Config.Set] method. Changing the map
|
||||
// directly obviously wouldn't work.
|
||||
func (c *Config) EnableRegisterChangeCallback() {
|
||||
if _, ok := (*c)[watchersKey]; !ok {
|
||||
c.Set(watchersKey, new(watchers))
|
||||
}
|
||||
}
|
||||
|
||||
func (c Config) GetStringArray(key pkey.Key, defaultVal []string) ([]string, error) {
|
||||
@@ -153,8 +231,19 @@ func (c Config) HasAnyOf(keys ...pkey.Key) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (sp Config) RegisterChangeCallback(callback func(policyclient.PolicyChange)) (func(), error) {
|
||||
return func() {}, nil
|
||||
func (c Config) RegisterChangeCallback(callback func(policyclient.PolicyChange)) (func(), error) {
|
||||
w, ok := c[watchersKey].(*watchers)
|
||||
if !ok {
|
||||
return func() {}, nil
|
||||
}
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
h := w.s.Add(callback)
|
||||
return func() {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
delete(w.s, h)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (sp Config) SetDebugLoggingEnabled(enabled bool) {}
|
||||
|
||||
Reference in New Issue
Block a user