2025-09-18 18:42:13 -04:00
package settings
import (
"fmt"
"os"
"reflect"
"regexp"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
2025-12-05 11:57:09 -06:00
"k8s.io/utils/ptr"
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
2025-09-18 18:42:13 -04:00
)
// allEnvVarsSet returns a map which contains keys corresponding to every ENV var that can be used to configure settings,
// with values set to a non-default value.
func allEnvVarsSet ( ) map [ string ] string {
return map [ string ] string {
2026-02-06 13:38:41 -08:00
"AGW_DNS_LOOKUP_FAMILY" : string ( DnsLookupFamilyV4Only ) ,
"AGW_LISTENER_BIND_IPV6" : "false" ,
"AGW_ISTIO_NAMESPACE" : "my-istio-namespace" ,
2026-03-02 18:09:59 +02:00
"AGW_ISTIO_REVISION" : "my-istio-revision" ,
2026-02-06 13:38:41 -08:00
"AGW_XDS_SERVICE_HOST" : "my-xds-host" ,
"AGW_XDS_SERVICE_NAME" : "custom-svc" ,
"AGW_AGENTGATEWAY_XDS_SERVICE_PORT" : "5678" ,
2026-02-19 08:28:53 -08:00
"AGW_NO_LISTENERS_DUMMY_PORT" : "8443" ,
2026-02-06 13:38:41 -08:00
"AGW_ENABLE_INFER_EXT" : "true" ,
"AGW_LOG_LEVEL" : "debug" ,
2026-02-16 11:05:34 -05:00
"AGW_DISCOVERY_NAMESPACE_SELECTORS" : ` [ { "matchLabels": { "app":"test"}}] ` ,
2026-02-06 13:38:41 -08:00
"AGW_ENABLE_BUILTIN_DEFAULT_METRICS" : "true" ,
2026-02-16 11:05:34 -05:00
"AGW_GLOBAL_POLICY_NAMESPACE" : "policy-ns" ,
2026-02-06 13:38:41 -08:00
"AGW_DISABLE_LEADER_ELECTION" : "true" ,
"AGW_GATEWAY_CLASS_PARAMETERS_REFS" : ` { "kgateway": { "name":"custom-gwp","namespace":"infra"},"agentgateway": { "name":"custom-gwp-agw","namespace":"infra"}} ` ,
"AGW_XDS_AUTH" : "false" ,
"AGW_XDS_TLS" : "true" ,
"AGW_ENABLE_EXPERIMENTAL_GATEWAY_API_FEATURES" : "false" ,
2026-02-12 09:52:06 -08:00
"AGW_PROXY_IMAGE_REGISTRY" : "my-registry" ,
"AGW_PROXY_IMAGE_REPOSITORY" : "my-repo" ,
"AGW_PROXY_IMAGE_TAG" : "my-tag" ,
2025-09-18 18:42:13 -04:00
}
}
func TestSettings ( t * testing . T ) {
testCases := [ ] struct {
// name of the test case
name string
// env vars that are set at the beginning of test (and removed after test)
envVars map [ string ] string
// if set, then these are the expected populated settings
expectedSettings * Settings
// if set, then an error parsing the settings is expected to occur
expectedErrorStr string
} {
{
// This test will pass if a new field is added to Settings and the default value is null value for the type.
// In this case the test will still be testing that expected default values are set, though our convention is to set it explicitly.
name : "defaults to empty or default values" ,
envVars : map [ string ] string { } ,
expectedSettings : & Settings {
2025-11-05 11:39:44 -05:00
DnsLookupFamily : DnsLookupFamilyV4Preferred ,
ListenerBindIpv6 : true ,
IstioNamespace : "istio-system" ,
2026-03-02 18:09:59 +02:00
IstioRevision : "default" ,
2025-11-05 11:39:44 -05:00
XdsServiceHost : "" ,
2026-02-06 13:38:41 -08:00
XdsServiceName : "agentgateway" ,
AgentgatewayXdsServicePort : 9978 ,
2026-02-19 08:28:53 -08:00
NoListenersDummyPort : 443 ,
2025-11-05 11:39:44 -05:00
EnableInferExt : false ,
LogLevel : "info" ,
2026-02-16 11:05:34 -05:00
DiscoveryNamespaceSelectors : "[]" ,
2025-11-05 11:39:44 -05:00
EnableBuiltinDefaultMetrics : false ,
2026-02-16 11:05:34 -05:00
GlobalPolicyNamespace : "" ,
2025-11-05 11:39:44 -05:00
DisableLeaderElection : false ,
XdsAuth : true ,
XdsTLS : false ,
2025-11-13 11:37:28 -05:00
EnableExperimentalGatewayAPIFeatures : true ,
2025-12-05 11:57:09 -06:00
GatewayClassParametersRefs : GatewayClassParametersRefs { } ,
2026-02-12 09:52:06 -08:00
ProxyImageRegistry : "cr.agentgateway.dev" ,
ProxyImageRepository : "agentgateway" ,
2025-09-18 18:42:13 -04:00
} ,
} ,
{
// This test will pass if a new field is added to Settings and the default value is null value for the type.
// However, a separate test will fail if a new field with a non-default value is not added to the map returned by allEnvVarsSet()
name : "all values set" ,
envVars : allEnvVarsSet ( ) ,
expectedSettings : & Settings {
2025-11-05 11:39:44 -05:00
DnsLookupFamily : DnsLookupFamilyV4Only ,
ListenerBindIpv6 : false ,
IstioNamespace : "my-istio-namespace" ,
2026-03-02 18:09:59 +02:00
IstioRevision : "my-istio-revision" ,
2025-11-05 11:39:44 -05:00
XdsServiceHost : "my-xds-host" ,
XdsServiceName : "custom-svc" ,
AgentgatewayXdsServicePort : 5678 ,
2026-02-19 08:28:53 -08:00
NoListenersDummyPort : 8443 ,
2025-11-05 11:39:44 -05:00
EnableInferExt : true ,
LogLevel : "debug" ,
2026-02-16 11:05:34 -05:00
DiscoveryNamespaceSelectors : ` [ { "matchLabels": { "app":"test"}}] ` ,
2025-11-05 11:39:44 -05:00
EnableBuiltinDefaultMetrics : true ,
2026-02-16 11:05:34 -05:00
GlobalPolicyNamespace : "policy-ns" ,
2025-11-05 11:39:44 -05:00
DisableLeaderElection : true ,
XdsAuth : false ,
XdsTLS : true ,
2025-11-13 11:37:28 -05:00
EnableExperimentalGatewayAPIFeatures : false ,
2026-02-12 09:52:06 -08:00
ProxyImageRegistry : "my-registry" ,
ProxyImageRepository : "my-repo" ,
ProxyImageTag : ptr . To ( "my-tag" ) ,
2025-12-05 11:57:09 -06:00
GatewayClassParametersRefs : GatewayClassParametersRefs {
"kgateway" : {
Name : "custom-gwp" ,
Namespace : ptr . To ( gwv1 . Namespace ( "infra" ) ) ,
} ,
"agentgateway" : {
Name : "custom-gwp-agw" ,
Namespace : ptr . To ( gwv1 . Namespace ( "infra" ) ) ,
} ,
} ,
2025-09-18 18:42:13 -04:00
} ,
} ,
{
name : "errors on invalid bool" ,
envVars : map [ string ] string {
2026-02-06 13:38:41 -08:00
"AGW_ENABLE_INFER_EXT" : "true123" ,
2025-09-18 18:42:13 -04:00
} ,
expectedErrorStr : "invalid syntax" ,
} ,
{
2026-02-16 11:05:34 -05:00
name : "errors on invalid bool" ,
2025-09-18 18:42:13 -04:00
envVars : map [ string ] string {
2026-02-16 11:05:34 -05:00
"AGW_LISTENER_BIND_IPV6" : "true123" ,
2025-09-18 18:42:13 -04:00
} ,
expectedErrorStr : "invalid syntax" ,
} ,
2025-09-19 12:30:48 -04:00
{
name : "errors on invalid port" ,
envVars : map [ string ] string {
2026-02-06 13:38:41 -08:00
"AGW_AGENTGATEWAY_XDS_SERVICE_PORT" : "a123" ,
2025-09-19 12:30:48 -04:00
} ,
expectedErrorStr : "invalid syntax" ,
} ,
2025-09-18 18:42:13 -04:00
{
name : "errors on invalid dns lookup family" ,
envVars : map [ string ] string {
2026-02-06 13:38:41 -08:00
"AGW_DNS_LOOKUP_FAMILY" : "invalid" ,
2025-09-18 18:42:13 -04:00
} ,
expectedErrorStr : ` invalid DNS lookup family: "invalid" ` ,
} ,
2025-12-05 11:57:09 -06:00
{
name : "errors on invalid gatewayclass parameters refs: missing name" ,
envVars : map [ string ] string {
2026-02-06 13:38:41 -08:00
"AGW_GATEWAY_CLASS_PARAMETERS_REFS" : ` { "kgateway": { "namespace":"missing-name"}} ` ,
2025-12-05 11:57:09 -06:00
} ,
expectedErrorStr : ` gateway class "kgateway" parametersRef.name must be set ` ,
} ,
{
name : "errors on invalid gatewayclass parameters refs: missing namespace" ,
envVars : map [ string ] string {
2026-02-06 13:38:41 -08:00
"AGW_GATEWAY_CLASS_PARAMETERS_REFS" : ` { "kgateway": { "name":"custom-gwp"}} ` ,
2025-12-05 11:57:09 -06:00
} ,
expectedErrorStr : ` gateway class "kgateway" parametersRef.namespace must be set ` ,
} ,
2025-09-18 18:42:13 -04:00
{
name : "ignores other env vars" ,
envVars : map [ string ] string {
2026-02-06 13:38:41 -08:00
"AGW_DOES_NOT_EXIST" : "true" ,
2025-09-18 18:42:13 -04:00
"ANOTHER_VAR" : "abc" ,
2026-02-06 13:38:41 -08:00
"AGW_ENABLE_ISTIO_AUTO_MTLS" : "true" ,
2025-09-18 18:42:13 -04:00
} ,
expectedSettings : & Settings {
2025-11-13 11:37:28 -05:00
DnsLookupFamily : DnsLookupFamilyV4Preferred ,
ListenerBindIpv6 : true ,
IstioNamespace : "istio-system" ,
2026-03-02 18:09:59 +02:00
IstioRevision : "default" ,
2026-02-06 13:38:41 -08:00
XdsServiceName : "agentgateway" ,
AgentgatewayXdsServicePort : 9978 ,
2026-02-16 11:05:34 -05:00
EnableInferExt : false ,
2026-02-19 08:28:53 -08:00
NoListenersDummyPort : 443 ,
2025-11-13 11:37:28 -05:00
LogLevel : "info" ,
2026-02-16 11:05:34 -05:00
DiscoveryNamespaceSelectors : "[]" ,
EnableBuiltinDefaultMetrics : false ,
GlobalPolicyNamespace : "" ,
DisableLeaderElection : false ,
2025-11-13 11:37:28 -05:00
XdsAuth : true ,
XdsTLS : false ,
EnableExperimentalGatewayAPIFeatures : true ,
2025-12-05 11:57:09 -06:00
GatewayClassParametersRefs : GatewayClassParametersRefs { } ,
2026-02-12 09:52:06 -08:00
ProxyImageRegistry : "cr.agentgateway.dev" ,
ProxyImageRepository : "agentgateway" ,
2025-09-18 18:42:13 -04:00
} ,
} ,
}
for _ , tc := range testCases {
t . Run ( tc . name , func ( t * testing . T ) {
t . Cleanup ( func ( ) {
cleanupEnvVars ( t , tc . envVars )
} )
for k , v := range tc . envVars {
if err := os . Setenv ( k , v ) ; err != nil {
t . Fatalf ( "Failed to set environment variable %s=%s: %v" , k , v , err )
}
}
s , err := BuildSettings ( )
if tc . expectedErrorStr != "" {
require . ErrorContains ( t , err , tc . expectedErrorStr )
return
}
require . NoError ( t , err )
diff := cmp . Diff ( tc . expectedSettings , s )
require . Emptyf ( t , diff , "Settings do not match expected values (-expected +got):\n%s" , diff )
} )
}
}
// TestEnvVarCoverage tests that all settings are tested with non-default values.
func TestEnvVarCoverage ( t * testing . T ) {
s := Settings { }
settingsValue := reflect . ValueOf ( s )
allEnvVars := allEnvVarsSet ( )
// Check for the right number of env vars defined
require . Equal ( t , settingsValue . NumField ( ) , len ( allEnvVars ) , "Number of fields in Settings does not match number of tested env vars" )
// Check that each field of Settings has a corresponding env var set in the test map
// This protects against typos when adding new settings to the test map.
for envVar , defaultValue := range expectedEnvVars ( settingsValue ) {
require . Contains ( t , allEnvVars , envVar , "Env var %s is not tested" , envVar )
require . NotEqual ( t , allEnvVars [ envVar ] , defaultValue , "Env var %s is set to the default value" , envVar )
}
}
// TestExpectedEnvVars tests that the expectedEnvVars function, which is used only in this test file, parses the field tags correctly to calculate the env var names.
func TestExpectedEnvVars ( t * testing . T ) {
validateValue := reflect . ValueOf ( validateExpectedEnvs { } )
expectedEnvVars := expectedEnvVars ( validateValue )
require . Equal ( t , len ( expectedEnvVars ) , validateValue . NumField ( ) )
// Check that the env vars are correct
2026-02-06 13:38:41 -08:00
require . Contains ( t , expectedEnvVars , "AGW_FIELD_ONE" , "Env var KGW_FIELD_ONE is not set" )
require . Equal ( t , expectedEnvVars [ "AGW_FIELD_ONE" ] , "default_value_1" , "Env var KGW_FIELD_ONE is not set to the default value" )
require . Contains ( t , expectedEnvVars , "AGW_FIELDTWO" , "Env var KGW_FIELDTWO is not set" )
require . Equal ( t , expectedEnvVars [ "AGW_FIELDTWO" ] , "" , "Env var KGW_FIELDTWO is not set to the default value" )
2025-09-18 18:42:13 -04:00
require . Contains ( t , expectedEnvVars , "ALT_FIELD_3" , "Env var ALT_FIELD_3 is not set" )
require . Contains ( t , expectedEnvVars , "ALT_FIELD_4" , "Env var ALT_FIELD_4 is not set" )
2026-02-06 13:38:41 -08:00
require . Contains ( t , expectedEnvVars , "AGW_FIELD_SSL_CONFIG" , "Env var KGW_FIELD_SSL_CONFIG is not set" )
require . Contains ( t , expectedEnvVars , "AGW_FIELDHTTPCONFIG" , "Env var KGW_FIELDHTTPCONFIG is not set" )
2025-09-18 18:42:13 -04:00
}
func cleanupEnvVars ( t * testing . T , envVars map [ string ] string ) {
t . Helper ( )
for k := range envVars {
if err := os . Unsetenv ( k ) ; err != nil {
t . Errorf ( "Failed to unset environment variable %s: %v" , k , err )
}
}
}
var (
gatherRegexp = regexp . MustCompile ( "([^A-Z]+|[A-Z]+[^A-Z]+|[A-Z]+)" )
acronymRegexp = regexp . MustCompile ( "([A-Z]+)([A-Z][^A-Z]+)" )
)
// expectedEnvVars returns a map of all the env vars that should be set for the given Settings value.
// The value of the map is the default value of the field.
2025-11-06 15:51:29 -05:00
func expectedEnvVars ( settingsValue reflect . Value ) map [ string ] any {
2025-09-18 18:42:13 -04:00
// This is a modified version of the code in https://github.com/kelseyhightower/envconfig/blob/7834011875d613aec60c606b52c2b0fe8949fe91/envconfig.go#L102-L128
2025-11-06 15:51:29 -05:00
expectedEnvVars := make ( map [ string ] any , settingsValue . NumField ( ) )
2025-09-18 18:42:13 -04:00
for i := 0 ; i < settingsValue . NumField ( ) ; i ++ {
fieldType := settingsValue . Type ( ) . Field ( i )
splitWords := fieldType . Tag . Get ( "split_words" ) == "true"
envVarName := fieldType . Name
if splitWords {
words := gatherRegexp . FindAllStringSubmatch ( fieldType . Name , - 1 )
if len ( words ) > 0 {
var name [ ] string
for _ , words := range words {
if m := acronymRegexp . FindStringSubmatch ( words [ 0 ] ) ; len ( m ) == 3 {
name = append ( name , m [ 1 ] , m [ 2 ] )
} else {
name = append ( name , words [ 0 ] )
}
}
envVarName = strings . Join ( name , "_" )
}
}
envVarName = strings . ToUpper ( envVarName )
// Always have a prefix
2026-02-06 13:38:41 -08:00
envVarName = fmt . Sprintf ( "AGW_%s" , envVarName )
2025-09-18 18:42:13 -04:00
// If the field has an alt tag, use that as the env var name
if fieldType . Tag . Get ( "alt" ) != "" {
envVarName = fieldType . Tag . Get ( "alt" )
}
expectedEnvVars [ envVarName ] = fieldType . Tag . Get ( "default" )
}
return expectedEnvVars
}
// validateExpectedEnvs is used to validate the expectedEnvVars function.
type validateExpectedEnvs struct {
// Field with split_words:true
FieldOne string ` split_words:"true" default:"default_value_1" `
// Field with split_words:false
FieldTwo string ` split_words:"false" `
// Field with split_words:true and alt name
FieldThree string ` split_words:"true" alt:"ALT_FIELD_3" `
// Field with split_words:false and alt name
FieldFour string ` split_words:"false" alt:"ALT_FIELD_4" `
// Field with acronym and split_words:true
FieldSSLConfig string ` split_words:"true" `
// Field with acronym and split_words:false
FieldHTTPConfig string ` split_words:"false" `
}