check the registration
|
assert . NotNil ( t , differ . FetchContentErrors )
assert . NotNil ( t , differ . ReadClusterListErrors )
assert . NotNil ( t , differ . ReadReportedErrors )
assert . NotNil ( t , differ . ProducerSetupErrors )
assert . NotNil ( t , differ . StorageSetupErrors )
assert . NotNil ( t , differ . ReadReportForClusterErrors )
assert . NotNil ( t , differ . DeserializeReportErrors )
assert . NotNil ( t , differ . ReportWithHighImpact )
assert . NotNil ( t , differ . NotificationNotSentSameState )
assert . NotNil ( t , differ . NotificationNotSentErrorState )
assert . NotNil ( t , differ . NotificationSent )
assert . NotNil ( t , differ . NoSeverityTotalRisk )
}
func TestPushMetricsNoNamespaceConfig ( t * testing . T ) {
var expectedPushes = 0
testServer := httptest . NewServer (
http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
w . Header ( ) . Set ( "Content-Type" , `text/plain; charset=utf-8` )
w . WriteHeader ( http . StatusOK )
expectedPushes ++
} ) ,
)
defer testServer . Close ( )
metricsConf := conf . MetricsConfiguration {
Job : "ccx_notification_service" ,
GatewayURL : testServer . URL ,
GatewayAuthToken : "some token" ,
}
err := differ . PushMetrics ( & metricsConf )
assert . Nil ( t , err )
assert . Zero ( t , expectedPushes ,
fmt . Sprintf ( "expected exactly %d pushes" , expectedPushes ) )
}
func TestPushMetricsGatewayNoAuthConfig ( t * testing . T ) {
var expectedPushes = 0
testServer := httptest . NewServer (
http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
w . Header ( ) . Set ( "Content-Type" , `text/plain; charset=utf-8` )
w . WriteHeader ( http . StatusOK )
expectedPushes ++
} ) ,
)
defer testServer . Close ( )
metricsConf := conf . MetricsConfiguration {
Job : "ccx_notification_service" ,
Namespace : "ccx_notification_service" ,
GatewayURL : testServer . URL ,
}
err := differ . PushMetrics ( & metricsConf )
assert . Nil ( t , err )
assert . Zero ( t , expectedPushes ,
fmt . Sprintf ( "expected exactly %d pushes" , expectedPushes ) )
}
func TestPushMetricsGatewayNotFailingWithRetriesThenOk ( t * testing . T ) {
var (
pushes int
expectedPushes = 6
timeBetweenRetries = 200 * time . Millisecond
totalTime = 2 * time . Second
)
testServer := httptest . NewServer (
http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
w . Header ( ) . Set ( "Content-Type" , `text/plain; charset=utf-8` )
if pushes >= 5 {
w . WriteHeader ( http . StatusOK )
} else {
w . WriteHeader ( http . StatusBadGateway )
}
pushes ++
} ) ,
)
defer testServer . Close ( )
_ , cancel := context . WithTimeout ( context . Background ( ) , totalTime )
metricsConf := conf . MetricsConfiguration {
Job : "ccx_notification_service" ,
Namespace : "ccx_notification_service" ,
GatewayURL : testServer . URL ,
GatewayAuthToken : "some_token" ,
RetryAfter : timeBetweenRetries ,
Retries : 10 ,
}
err := differ . PushMetrics ( & metricsConf )
assert . Nil ( t , err )
cancel ( )
log . Info ( ) . Int ( "pushes" , pushes ) . Msg ( "debug" )
assert . Equal ( t , expectedPushes , pushes ,
fmt . Sprintf ( "expected exactly %d retries, but received %d" , expectedPushes , pushes ) )
}
func TestPushMetricsGatewayNotFailingWithRetries ( t * testing . T ) {
var (
pushes int
expectedPushes = 1
timeBetweenRetries = 100 * time . Millisecond
totalTime = 500 * time . Millisecond
)
testServer := httptest . NewServer (
http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
w . Header ( ) . Set ( "Content-Type" , `text/plain; charset=utf-8` )
w . WriteHeader ( http . StatusOK )
pushes ++
} ) ,
)
defer testServer . Close ( )
_ , cancel := context . WithTimeout ( context . Background ( ) , totalTime )
metricsConf := conf . MetricsConfiguration {
Job : "ccx_notification_service" ,
Namespace : "ccx_notification_service" ,
GatewayURL : testServer . URL ,
GatewayAuthToken : "some_token" ,
RetryAfter : timeBetweenRetries ,
Retries : 10 ,
}
err := differ . PushMetrics ( & metricsConf )
assert . Nil ( t , err )
cancel ( )
log . Info ( ) . Int ( "pushes" , pushes ) . Msg ( "debug" )
assert . Equal ( t , expectedPushes , pushes ,
fmt . Sprintf ( "expected exactly %d retries, but received %d" , expectedPushes , pushes ) )
}
func TestPushMetricsGatewayFailing ( t * testing . T ) {
var (
timeBetweenRetries = 100 * time . Millisecond
totalTime = 1 * time . Second
)
testServer := httptest . NewServer (
http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
w . Header ( ) . Set ( "Content-Type" , `text/plain; charset=utf-8` )
w . WriteHeader ( http . StatusBadGateway )
} ) ,
)
defer testServer . Close ( )
_ , cancel := context . WithTimeout ( context . Background ( ) , totalTime )
metricsConf := conf . MetricsConfiguration {
Job : "ccx_notification_service" ,
Namespace : "ccx_notification_service" ,
GatewayURL : testServer . URL ,
GatewayAuthToken : "some_token" ,
RetryAfter : timeBetweenRetries ,
Retries : 10 ,
}
err := differ . PushMetrics ( & metricsConf )
assert . ErrorIs ( t , err , & differ . StatusMetricsError { } )
cancel ( )
}
func TestPushMetricsInLoop ( t * testing . T ) {
|
Fake a Pushgateway that responds with 202 to DELETE and with 200 in
all other cases and counts the number of pushes received
|
var (
pushes int
expectedPushes = 5
timeBetweenPush = 100 * time . Millisecond
totalTime = 1 * time . Second
)
pgwOK := httptest . NewServer (
http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
w . Header ( ) . Set ( "Content-Type" , `text/plain; charset=utf-8` )
w . WriteHeader ( http . StatusOK )
pushes ++
} ) ,
)
defer pgwOK . Close ( )
metricsConf := conf . MetricsConfiguration {
Job : "ccx_notification_service" ,
Namespace : "ccx_notification_service" ,
GatewayURL : pgwOK . URL ,
GatewayAuthToken : "some_token" ,
GatewayTimeBetweenPush : timeBetweenPush ,
}
ctx , cancel := context . WithTimeout ( context . Background ( ) , totalTime )
go differ . PushMetricsInLoop ( ctx , & metricsConf )
time . Sleep ( totalTime )
cancel ( )
assert . GreaterOrEqual ( t , pushes , expectedPushes , fmt . Sprintf ( "expected more than %d pushes but found %d" , expectedPushes , pushes ) )
}
|