mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-08 02:41:49 +01:00
util/syspolicy/policytest: move policy test helper to its own package
Updates #16998 Updates #12614 Change-Id: I9fd27d653ebee547951705dc5597481e85b60747 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
parent
2b3e533048
commit
24b8a57b1e
@ -64,7 +64,7 @@ import (
|
|||||||
"tailscale.com/util/set"
|
"tailscale.com/util/set"
|
||||||
"tailscale.com/util/syspolicy"
|
"tailscale.com/util/syspolicy"
|
||||||
"tailscale.com/util/syspolicy/pkey"
|
"tailscale.com/util/syspolicy/pkey"
|
||||||
"tailscale.com/util/syspolicy/policyclient"
|
"tailscale.com/util/syspolicy/policytest"
|
||||||
"tailscale.com/util/syspolicy/setting"
|
"tailscale.com/util/syspolicy/setting"
|
||||||
"tailscale.com/util/syspolicy/source"
|
"tailscale.com/util/syspolicy/source"
|
||||||
"tailscale.com/wgengine"
|
"tailscale.com/wgengine"
|
||||||
@ -1183,7 +1183,7 @@ func TestConfigureExitNode(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
var pol testPolicy
|
var pol policytest.Config
|
||||||
// Configure policy settings, if any.
|
// Configure policy settings, if any.
|
||||||
if tt.exitNodeIDPolicy != nil {
|
if tt.exitNodeIDPolicy != nil {
|
||||||
pol.Set(pkey.ExitNodeID, string(*tt.exitNodeIDPolicy))
|
pol.Set(pkey.ExitNodeID, string(*tt.exitNodeIDPolicy))
|
||||||
@ -5539,62 +5539,6 @@ func TestReadWriteRouteInfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// testPolicy is a [policyclient.Client] with a static mapping of values.
|
|
||||||
// The map value must be of the correct type (string, []string, bool, etc).
|
|
||||||
//
|
|
||||||
// It is used for testing purposes to simulate policy client behavior.
|
|
||||||
// It panics if the values are the wrong type.
|
|
||||||
type testPolicy struct {
|
|
||||||
v map[pkey.Key]any
|
|
||||||
policyclient.NoPolicyClient
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sp *testPolicy) Set(key pkey.Key, value any) {
|
|
||||||
if sp.v == nil {
|
|
||||||
sp.v = make(map[pkey.Key]any)
|
|
||||||
}
|
|
||||||
sp.v[key] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sp testPolicy) GetStringArray(key pkey.Key, defaultVal []string) ([]string, error) {
|
|
||||||
if val, ok := sp.v[key]; ok {
|
|
||||||
if arr, ok := val.([]string); ok {
|
|
||||||
return arr, nil
|
|
||||||
}
|
|
||||||
panic(fmt.Sprintf("key %s is not a []string", key))
|
|
||||||
}
|
|
||||||
return defaultVal, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sp testPolicy) GetString(key pkey.Key, defaultVal string) (string, error) {
|
|
||||||
if val, ok := sp.v[key]; ok {
|
|
||||||
if str, ok := val.(string); ok {
|
|
||||||
return str, nil
|
|
||||||
}
|
|
||||||
panic(fmt.Sprintf("key %s is not a string", key))
|
|
||||||
}
|
|
||||||
return defaultVal, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sp testPolicy) GetBoolean(key pkey.Key, defaultVal bool) (bool, error) {
|
|
||||||
if val, ok := sp.v[key]; ok {
|
|
||||||
if b, ok := val.(bool); ok {
|
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
panic(fmt.Sprintf("key %s is not a bool", key))
|
|
||||||
}
|
|
||||||
return defaultVal, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sp testPolicy) HasAnyOf(keys ...pkey.Key) (bool, error) {
|
|
||||||
for _, key := range keys {
|
|
||||||
if _, ok := sp.v[key]; ok {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFillAllowedSuggestions(t *testing.T) {
|
func TestFillAllowedSuggestions(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@ -5628,7 +5572,7 @@ func TestFillAllowedSuggestions(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
var pol testPolicy
|
var pol policytest.Config
|
||||||
pol.Set(pkey.AllowedSuggestedExitNodes, tt.allowPolicy)
|
pol.Set(pkey.AllowedSuggestedExitNodes, tt.allowPolicy)
|
||||||
|
|
||||||
got := fillAllowedSuggestions(pol)
|
got := fillAllowedSuggestions(pol)
|
||||||
|
|||||||
117
util/syspolicy/policytest/policytest.go
Normal file
117
util/syspolicy/policytest/policytest.go
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
// Package policytest contains test helpers for the syspolicy packages.
|
||||||
|
package policytest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"tailscale.com/util/syspolicy/pkey"
|
||||||
|
"tailscale.com/util/syspolicy/policyclient"
|
||||||
|
"tailscale.com/util/syspolicy/ptype"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config is a [policyclient.Client] implementation with a static mapping of
|
||||||
|
// values.
|
||||||
|
//
|
||||||
|
// It is used for testing purposes to simulate policy client behavior.
|
||||||
|
//
|
||||||
|
// It panics if a value is Set with one type and then accessed with a different
|
||||||
|
// expected type.
|
||||||
|
type Config map[pkey.Key]any
|
||||||
|
|
||||||
|
var _ policyclient.Client = Config{}
|
||||||
|
|
||||||
|
func (c *Config) Set(key pkey.Key, value any) {
|
||||||
|
if *c == nil {
|
||||||
|
*c = make(map[pkey.Key]any)
|
||||||
|
}
|
||||||
|
(*c)[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) GetStringArray(key pkey.Key, defaultVal []string) ([]string, error) {
|
||||||
|
if val, ok := c[key]; ok {
|
||||||
|
if arr, ok := val.([]string); ok {
|
||||||
|
return arr, nil
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("key %s is not a []string", key))
|
||||||
|
}
|
||||||
|
return defaultVal, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) GetString(key pkey.Key, defaultVal string) (string, error) {
|
||||||
|
if val, ok := c[key]; ok {
|
||||||
|
if str, ok := val.(string); ok {
|
||||||
|
return str, nil
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("key %s is not a string", key))
|
||||||
|
}
|
||||||
|
return defaultVal, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) GetBoolean(key pkey.Key, defaultVal bool) (bool, error) {
|
||||||
|
if val, ok := c[key]; ok {
|
||||||
|
if b, ok := val.(bool); ok {
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("key %s is not a bool", key))
|
||||||
|
}
|
||||||
|
return defaultVal, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) GetUint64(key pkey.Key, defaultVal uint64) (uint64, error) {
|
||||||
|
if val, ok := c[key]; ok {
|
||||||
|
if u, ok := val.(uint64); ok {
|
||||||
|
return u, nil
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("key %s is not a uint64", key))
|
||||||
|
}
|
||||||
|
return defaultVal, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) GetDuration(key pkey.Key, defaultVal time.Duration) (time.Duration, error) {
|
||||||
|
if val, ok := c[key]; ok {
|
||||||
|
if d, ok := val.(time.Duration); ok {
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("key %s is not a time.Duration", key))
|
||||||
|
}
|
||||||
|
return defaultVal, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) GetPreferenceOption(key pkey.Key, defaultVal ptype.PreferenceOption) (ptype.PreferenceOption, error) {
|
||||||
|
if val, ok := c[key]; ok {
|
||||||
|
if p, ok := val.(ptype.PreferenceOption); ok {
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("key %s is not a ptype.PreferenceOption", key))
|
||||||
|
}
|
||||||
|
return defaultVal, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) GetVisibility(key pkey.Key) (ptype.Visibility, error) {
|
||||||
|
if val, ok := c[key]; ok {
|
||||||
|
if p, ok := val.(ptype.Visibility); ok {
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("key %s is not a ptype.Visibility", key))
|
||||||
|
}
|
||||||
|
return ptype.Visibility(ptype.ShowChoiceByPolicy), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) HasAnyOf(keys ...pkey.Key) (bool, error) {
|
||||||
|
for _, key := range keys {
|
||||||
|
if _, ok := c[key]; ok {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sp Config) RegisterChangeCallback(callback func(policyclient.PolicyChange)) (func(), error) {
|
||||||
|
return func() {}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sp Config) SetDebugLoggingEnabled(enabled bool) {}
|
||||||
Loading…
x
Reference in New Issue
Block a user