2018-01-25 14:00:05 +03:00
package integration
import (
"net/http"
"os"
"time"
"github.com/go-check/check"
2020-09-16 16:46:04 +03:00
"github.com/traefik/traefik/v2/integration/try"
2018-01-25 14:00:05 +03:00
checker "github.com/vdemeester/shakers"
)
type TracingSuite struct {
BaseSuite
2021-11-25 13:10:06 +03:00
whoamiIP string
whoamiPort int
tracerIP string
2018-01-25 14:00:05 +03:00
}
type TracingTemplate struct {
2021-11-25 13:10:06 +03:00
WhoamiIP string
WhoamiPort int
2019-10-15 17:30:06 +03:00
IP string
TraceContextHeaderName string
2018-01-25 14:00:05 +03:00
}
func ( s * TracingSuite ) SetUpSuite ( c * check . C ) {
s . createComposeProject ( c , "tracing" )
2021-11-25 13:10:06 +03:00
s . composeUp ( c )
2018-01-25 14:00:05 +03:00
2021-11-25 13:10:06 +03:00
s . whoamiIP = s . getComposeServiceIP ( c , "whoami" )
s . whoamiPort = 80
2018-01-25 14:00:05 +03:00
}
func ( s * TracingSuite ) startZipkin ( c * check . C ) {
2021-11-25 13:10:06 +03:00
s . composeUp ( c , "zipkin" )
s . tracerIP = s . getComposeServiceIP ( c , "zipkin" )
2018-01-25 14:00:05 +03:00
// Wait for Zipkin to turn ready.
2021-11-25 13:10:06 +03:00
err := try . GetRequest ( "http://" + s . tracerIP + ":9411/api/v2/services" , 20 * time . Second , try . StatusCodeIs ( http . StatusOK ) )
2018-01-25 14:00:05 +03:00
c . Assert ( err , checker . IsNil )
}
func ( s * TracingSuite ) TestZipkinRateLimit ( c * check . C ) {
s . startZipkin ( c )
2021-11-25 13:10:06 +03:00
// defer s.composeStop(c, "zipkin")
2019-06-28 01:16:04 +03:00
file := s . adaptFile ( c , "fixtures/tracing/simple-zipkin.toml" , TracingTemplate {
2021-11-25 13:10:06 +03:00
WhoamiIP : s . whoamiIP ,
WhoamiPort : s . whoamiPort ,
IP : s . tracerIP ,
2018-01-25 14:00:05 +03:00
} )
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
2021-11-25 13:10:06 +03:00
2018-01-25 14:00:05 +03:00
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2018-01-25 14:00:05 +03:00
2019-05-16 11:58:06 +03:00
// wait for traefik
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , time . Second , try . BodyContains ( "basic-auth" ) )
c . Assert ( err , checker . IsNil )
2018-01-25 14:00:05 +03:00
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusTooManyRequests ) )
c . Assert ( err , checker . IsNil )
// sleep for 4 seconds to be certain the configured time period has elapsed
// then test another request and verify a 200 status code
time . Sleep ( 4 * time . Second )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
// continue requests at 3 second intervals to test the other rate limit time period
time . Sleep ( 3 * time . Second )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
time . Sleep ( 3 * time . Second )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
time . Sleep ( 3 * time . Second )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusTooManyRequests ) )
c . Assert ( err , checker . IsNil )
2021-11-25 13:10:06 +03:00
err = try . GetRequest ( "http://" + s . tracerIP + ":9411/api/v2/spans?serviceName=tracing" , 20 * time . Second , try . BodyContains ( "forward service1/router1@file" , "ratelimit-1@file" ) )
2018-01-25 14:00:05 +03:00
c . Assert ( err , checker . IsNil )
}
func ( s * TracingSuite ) TestZipkinRetry ( c * check . C ) {
s . startZipkin ( c )
2021-11-25 13:10:06 +03:00
defer s . composeStop ( c , "zipkin" )
2019-06-28 01:16:04 +03:00
file := s . adaptFile ( c , "fixtures/tracing/simple-zipkin.toml" , TracingTemplate {
2021-11-25 13:10:06 +03:00
WhoamiIP : s . whoamiIP ,
WhoamiPort : 81 ,
IP : s . tracerIP ,
2018-01-25 14:00:05 +03:00
} )
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2018-01-25 14:00:05 +03:00
2019-05-16 11:58:06 +03:00
// wait for traefik
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , time . Second , try . BodyContains ( "basic-auth" ) )
c . Assert ( err , checker . IsNil )
2018-01-25 14:00:05 +03:00
err = try . GetRequest ( "http://127.0.0.1:8000/retry" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusBadGateway ) )
c . Assert ( err , checker . IsNil )
2021-11-25 13:10:06 +03:00
err = try . GetRequest ( "http://" + s . tracerIP + ":9411/api/v2/spans?serviceName=tracing" , 20 * time . Second , try . BodyContains ( "forward service2/router2@file" , "retry@file" ) )
2018-01-25 14:00:05 +03:00
c . Assert ( err , checker . IsNil )
}
func ( s * TracingSuite ) TestZipkinAuth ( c * check . C ) {
s . startZipkin ( c )
2021-11-25 13:10:06 +03:00
defer s . composeStop ( c , "zipkin" )
2019-06-28 01:16:04 +03:00
file := s . adaptFile ( c , "fixtures/tracing/simple-zipkin.toml" , TracingTemplate {
2021-11-25 13:10:06 +03:00
WhoamiIP : s . whoamiIP ,
WhoamiPort : s . whoamiPort ,
IP : s . tracerIP ,
2018-01-25 14:00:05 +03:00
} )
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2018-01-25 14:00:05 +03:00
2019-05-16 11:58:06 +03:00
// wait for traefik
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , time . Second , try . BodyContains ( "basic-auth" ) )
c . Assert ( err , checker . IsNil )
2018-01-25 14:00:05 +03:00
err = try . GetRequest ( "http://127.0.0.1:8000/auth" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusUnauthorized ) )
c . Assert ( err , checker . IsNil )
2021-11-25 13:10:06 +03:00
err = try . GetRequest ( "http://" + s . tracerIP + ":9411/api/v2/spans?serviceName=tracing" , 20 * time . Second , try . BodyContains ( "entrypoint web" , "basic-auth@file" ) )
2019-06-28 01:16:04 +03:00
c . Assert ( err , checker . IsNil )
}
func ( s * TracingSuite ) startJaeger ( c * check . C ) {
2021-11-25 13:10:06 +03:00
s . composeUp ( c , "jaeger" , "whoami" )
s . tracerIP = s . getComposeServiceIP ( c , "jaeger" )
2019-06-28 01:16:04 +03:00
// Wait for Jaeger to turn ready.
2021-11-25 13:10:06 +03:00
err := try . GetRequest ( "http://" + s . tracerIP + ":16686/api/services" , 20 * time . Second , try . StatusCodeIs ( http . StatusOK ) )
2019-06-28 01:16:04 +03:00
c . Assert ( err , checker . IsNil )
}
func ( s * TracingSuite ) TestJaegerRateLimit ( c * check . C ) {
s . startJaeger ( c )
2021-11-25 13:10:06 +03:00
defer s . composeStop ( c , "jaeger" )
2019-06-28 01:16:04 +03:00
file := s . adaptFile ( c , "fixtures/tracing/simple-jaeger.toml" , TracingTemplate {
2021-11-25 13:10:06 +03:00
WhoamiIP : s . whoamiIP ,
WhoamiPort : s . whoamiPort ,
IP : s . tracerIP ,
2019-10-15 17:30:06 +03:00
TraceContextHeaderName : "uber-trace-id" ,
2019-06-28 01:16:04 +03:00
} )
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2019-06-28 01:16:04 +03:00
// wait for traefik
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , time . Second , try . BodyContains ( "basic-auth" ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusTooManyRequests ) )
c . Assert ( err , checker . IsNil )
// sleep for 4 seconds to be certain the configured time period has elapsed
// then test another request and verify a 200 status code
time . Sleep ( 4 * time . Second )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
// continue requests at 3 second intervals to test the other rate limit time period
time . Sleep ( 3 * time . Second )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
time . Sleep ( 3 * time . Second )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8000/ratelimit" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusTooManyRequests ) )
c . Assert ( err , checker . IsNil )
2021-11-25 13:10:06 +03:00
err = try . GetRequest ( "http://" + s . tracerIP + ":16686/api/traces?service=tracing" , 20 * time . Second , try . BodyContains ( "forward service1/router1@file" , "ratelimit-1@file" ) )
2019-06-28 01:16:04 +03:00
c . Assert ( err , checker . IsNil )
}
func ( s * TracingSuite ) TestJaegerRetry ( c * check . C ) {
s . startJaeger ( c )
2021-11-25 13:10:06 +03:00
defer s . composeStop ( c , "jaeger" )
2019-06-28 01:16:04 +03:00
file := s . adaptFile ( c , "fixtures/tracing/simple-jaeger.toml" , TracingTemplate {
2021-11-25 13:10:06 +03:00
WhoamiIP : s . whoamiIP ,
WhoamiPort : 81 ,
IP : s . tracerIP ,
2019-10-15 17:30:06 +03:00
TraceContextHeaderName : "uber-trace-id" ,
2019-06-28 01:16:04 +03:00
} )
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2019-06-28 01:16:04 +03:00
// wait for traefik
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , time . Second , try . BodyContains ( "basic-auth" ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8000/retry" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusBadGateway ) )
c . Assert ( err , checker . IsNil )
2021-11-25 13:10:06 +03:00
err = try . GetRequest ( "http://" + s . tracerIP + ":16686/api/traces?service=tracing" , 20 * time . Second , try . BodyContains ( "forward service2/router2@file" , "retry@file" ) )
2019-06-28 01:16:04 +03:00
c . Assert ( err , checker . IsNil )
}
func ( s * TracingSuite ) TestJaegerAuth ( c * check . C ) {
s . startJaeger ( c )
2021-11-25 13:10:06 +03:00
defer s . composeStop ( c , "jaeger" )
2019-06-28 01:16:04 +03:00
file := s . adaptFile ( c , "fixtures/tracing/simple-jaeger.toml" , TracingTemplate {
2021-11-25 13:10:06 +03:00
WhoamiIP : s . whoamiIP ,
WhoamiPort : s . whoamiPort ,
IP : s . tracerIP ,
2019-10-15 17:30:06 +03:00
TraceContextHeaderName : "uber-trace-id" ,
} )
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2019-10-15 17:30:06 +03:00
// wait for traefik
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , time . Second , try . BodyContains ( "basic-auth" ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8000/auth" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusUnauthorized ) )
c . Assert ( err , checker . IsNil )
2021-11-25 13:10:06 +03:00
err = try . GetRequest ( "http://" + s . tracerIP + ":16686/api/traces?service=tracing" , 20 * time . Second , try . BodyContains ( "EntryPoint web" , "basic-auth@file" ) )
2019-10-15 17:30:06 +03:00
c . Assert ( err , checker . IsNil )
}
func ( s * TracingSuite ) TestJaegerCustomHeader ( c * check . C ) {
s . startJaeger ( c )
2021-11-25 13:10:06 +03:00
defer s . composeStop ( c , "jaeger" )
2019-10-15 17:30:06 +03:00
file := s . adaptFile ( c , "fixtures/tracing/simple-jaeger.toml" , TracingTemplate {
2021-11-25 13:10:06 +03:00
WhoamiIP : s . whoamiIP ,
WhoamiPort : s . whoamiPort ,
IP : s . tracerIP ,
2019-10-15 17:30:06 +03:00
TraceContextHeaderName : "powpow" ,
2019-06-28 01:16:04 +03:00
} )
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2019-06-28 01:16:04 +03:00
// wait for traefik
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , time . Second , try . BodyContains ( "basic-auth" ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8000/auth" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusUnauthorized ) )
c . Assert ( err , checker . IsNil )
2021-11-25 13:10:06 +03:00
err = try . GetRequest ( "http://" + s . tracerIP + ":16686/api/traces?service=tracing" , 20 * time . Second , try . BodyContains ( "EntryPoint web" , "basic-auth@file" ) )
2018-01-25 14:00:05 +03:00
c . Assert ( err , checker . IsNil )
}
2019-07-15 15:52:04 +03:00
func ( s * TracingSuite ) TestJaegerAuthCollector ( c * check . C ) {
s . startJaeger ( c )
2021-11-25 13:10:06 +03:00
defer s . composeStop ( c , "jaeger" )
2019-07-15 15:52:04 +03:00
file := s . adaptFile ( c , "fixtures/tracing/simple-jaeger-collector.toml" , TracingTemplate {
2021-11-25 13:10:06 +03:00
WhoamiIP : s . whoamiIP ,
WhoamiPort : s . whoamiPort ,
IP : s . tracerIP ,
2019-07-15 15:52:04 +03:00
} )
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2019-07-15 15:52:04 +03:00
// wait for traefik
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , time . Second , try . BodyContains ( "basic-auth" ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8000/auth" , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusUnauthorized ) )
c . Assert ( err , checker . IsNil )
2021-11-25 13:10:06 +03:00
err = try . GetRequest ( "http://" + s . tracerIP + ":16686/api/traces?service=tracing" , 20 * time . Second , try . BodyContains ( "EntryPoint web" , "basic-auth@file" ) )
2019-07-15 15:52:04 +03:00
c . Assert ( err , checker . IsNil )
}