2022-07-05 04:43:12 +00:00
package validator
import (
"testing"
2023-05-07 05:48:26 +00:00
"time"
2022-07-05 04:43:12 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/authelia/authelia/v4/internal/configuration/schema"
)
func TestValidateTelemetry ( t * testing . T ) {
2023-05-07 05:48:26 +00:00
mustParseAddress := func ( a string ) * schema . AddressTCP {
addr , err := schema . NewAddress ( a )
2022-07-05 04:43:12 +00:00
if err != nil {
panic ( err )
}
2023-05-07 05:48:26 +00:00
return & schema . AddressTCP { Address : * addr }
2022-07-05 04:43:12 +00:00
}
testCases := [ ] struct {
name string
have * schema . Configuration
expected * schema . Configuration
expectedWrns , expectedErrs [ ] string
} {
{
"ShouldSetDefaults" ,
& schema . Configuration { } ,
& schema . Configuration { Telemetry : schema . DefaultTelemetryConfig } ,
nil ,
nil ,
} ,
{
"ShouldSetDefaultPort" ,
& schema . Configuration { Telemetry : schema . TelemetryConfig { Metrics : schema . TelemetryMetricsConfig { Address : mustParseAddress ( "tcp://0.0.0.0" ) } } } ,
2023-05-07 05:48:26 +00:00
& schema . Configuration { Telemetry : schema . TelemetryConfig {
Metrics : schema . TelemetryMetricsConfig {
Address : & schema . AddressTCP { Address : schema . NewAddressFromNetworkValues ( schema . AddressSchemeTCP , "0.0.0.0" , schema . DefaultTelemetryConfig . Metrics . Address . Port ( ) ) } ,
Buffers : schema . ServerBuffers {
Read : 4096 ,
Write : 4096 ,
} ,
Timeouts : schema . ServerTimeouts {
Read : time . Second * 6 ,
Write : time . Second * 6 ,
Idle : time . Second * 30 ,
} ,
} ,
} } ,
2022-07-05 04:43:12 +00:00
nil ,
nil ,
} ,
{
"ShouldSetDefaultPortAlt" ,
2023-05-07 05:48:26 +00:00
& schema . Configuration { Telemetry : schema . TelemetryConfig { Metrics : schema . TelemetryMetricsConfig { Address : mustParseAddress ( "tcp://:0" ) } } } ,
2022-07-05 04:43:12 +00:00
& schema . Configuration { Telemetry : schema . DefaultTelemetryConfig } ,
nil ,
nil ,
} ,
{
"ShouldSetDefaultPortWithCustomIP" ,
& schema . Configuration { Telemetry : schema . TelemetryConfig { Metrics : schema . TelemetryMetricsConfig { Address : mustParseAddress ( "tcp://127.0.0.1" ) } } } ,
& schema . Configuration { Telemetry : schema . TelemetryConfig { Metrics : schema . TelemetryMetricsConfig { Address : mustParseAddress ( "tcp://127.0.0.1:9959" ) } } } ,
nil ,
nil ,
} ,
{
"ShouldNotValidateUDP" ,
& schema . Configuration { Telemetry : schema . TelemetryConfig { Metrics : schema . TelemetryMetricsConfig { Address : mustParseAddress ( "udp://0.0.0.0" ) } } } ,
& schema . Configuration { Telemetry : schema . TelemetryConfig { Metrics : schema . TelemetryMetricsConfig { Address : mustParseAddress ( "udp://0.0.0.0:9959" ) } } } ,
nil ,
2023-05-07 05:48:26 +00:00
[ ] string { "telemetry: metrics: option 'address' with value 'udp://0.0.0.0:0' is invalid: scheme must be one of 'tcp', 'tcp4', 'tcp6', or 'unix' but is configured as 'udp'" } ,
2022-07-05 04:43:12 +00:00
} ,
}
for _ , tc := range testCases {
t . Run ( tc . name , func ( t * testing . T ) {
v := schema . NewStructValidator ( )
ValidateTelemetry ( tc . have , v )
assert . Equal ( t , tc . expected . Telemetry . Metrics . Enabled , tc . have . Telemetry . Metrics . Enabled )
assert . Equal ( t , tc . expected . Telemetry . Metrics . Address , tc . have . Telemetry . Metrics . Address )
lenWrns := len ( tc . expectedWrns )
wrns := v . Warnings ( )
if lenWrns == 0 {
assert . Len ( t , wrns , 0 )
} else {
require . Len ( t , wrns , lenWrns )
for i , expectedWrn := range tc . expectedWrns {
assert . EqualError ( t , wrns [ i ] , expectedWrn )
}
}
lenErrs := len ( tc . expectedErrs )
errs := v . Errors ( )
if lenErrs == 0 {
assert . Len ( t , errs , 0 )
} else {
require . Len ( t , errs , lenErrs )
for i , expectedErr := range tc . expectedErrs {
assert . EqualError ( t , errs [ i ] , expectedErr )
}
}
} )
}
}
2023-05-07 05:48:26 +00:00
func TestValidateTelemetryShouldCorrectlyIdentifyValidAddressSchemes ( t * testing . T ) {
testCases := [ ] struct {
have string
expected string
} {
{ schema . AddressSchemeTCP , "" } ,
{ schema . AddressSchemeTCP4 , "" } ,
{ schema . AddressSchemeTCP6 , "" } ,
{ schema . AddressSchemeUDP , "telemetry: metrics: option 'address' with value 'udp://:9091' is invalid: scheme must be one of 'tcp', 'tcp4', 'tcp6', or 'unix' but is configured as 'udp'" } ,
{ schema . AddressSchemeUDP4 , "telemetry: metrics: option 'address' with value 'udp4://:9091' is invalid: scheme must be one of 'tcp', 'tcp4', 'tcp6', or 'unix' but is configured as 'udp4'" } ,
{ schema . AddressSchemeUDP6 , "telemetry: metrics: option 'address' with value 'udp6://:9091' is invalid: scheme must be one of 'tcp', 'tcp4', 'tcp6', or 'unix' but is configured as 'udp6'" } ,
{ schema . AddressSchemeUnix , "" } ,
{ "http" , "telemetry: metrics: option 'address' with value 'http://:9091' is invalid: scheme must be one of 'tcp', 'tcp4', 'tcp6', or 'unix' but is configured as 'http'" } ,
}
have := & schema . Configuration { }
validator := schema . NewStructValidator ( )
for _ , tc := range testCases {
t . Run ( tc . have , func ( t * testing . T ) {
validator . Clear ( )
switch tc . have {
case schema . AddressSchemeUnix :
have . Telemetry . Metrics . Address = & schema . AddressTCP { Address : schema . NewAddressUnix ( "/path/to/authelia.sock" ) }
default :
have . Telemetry . Metrics . Address = & schema . AddressTCP { Address : schema . NewAddressFromNetworkValues ( tc . have , "" , 9091 ) }
}
ValidateTelemetry ( have , validator )
assert . Len ( t , validator . Warnings ( ) , 0 )
if tc . expected == "" {
assert . Len ( t , validator . Errors ( ) , 0 )
} else {
require . Len ( t , validator . Errors ( ) , 1 )
assert . EqualError ( t , validator . Errors ( ) [ 0 ] , tc . expected )
}
} )
}
}