Move code to pkg

This commit is contained in:
Ludovic Fernandez 2019-03-15 09:42:03 +01:00 committed by Traefiker Bot
parent bd4c822670
commit f1b085fa36
465 changed files with 656 additions and 680 deletions

154
pkg/testhelpers/config.go Normal file
View file

@ -0,0 +1,154 @@
package testhelpers
import (
"github.com/containous/traefik/pkg/config"
)
// BuildConfiguration is a helper to create a configuration.
func BuildConfiguration(dynamicConfigBuilders ...func(*config.HTTPConfiguration)) *config.HTTPConfiguration {
conf := &config.HTTPConfiguration{}
for _, build := range dynamicConfigBuilders {
build(conf)
}
return conf
}
// WithRouters is a helper to create a configuration.
func WithRouters(opts ...func(*config.Router) string) func(*config.HTTPConfiguration) {
return func(c *config.HTTPConfiguration) {
c.Routers = make(map[string]*config.Router)
for _, opt := range opts {
b := &config.Router{}
name := opt(b)
c.Routers[name] = b
}
}
}
// WithRouter is a helper to create a configuration.
func WithRouter(routerName string, opts ...func(*config.Router)) func(*config.Router) string {
return func(r *config.Router) string {
for _, opt := range opts {
opt(r)
}
return routerName
}
}
// WithRouterMiddlewares is a helper to create a configuration.
func WithRouterMiddlewares(middlewaresName ...string) func(*config.Router) {
return func(r *config.Router) {
r.Middlewares = middlewaresName
}
}
// WithServiceName is a helper to create a configuration.
func WithServiceName(serviceName string) func(*config.Router) {
return func(r *config.Router) {
r.Service = serviceName
}
}
// WithLoadBalancerServices is a helper to create a configuration.
func WithLoadBalancerServices(opts ...func(service *config.LoadBalancerService) string) func(*config.HTTPConfiguration) {
return func(c *config.HTTPConfiguration) {
c.Services = make(map[string]*config.Service)
for _, opt := range opts {
b := &config.LoadBalancerService{}
name := opt(b)
c.Services[name] = &config.Service{
LoadBalancer: b,
}
}
}
}
// WithService is a helper to create a configuration.
func WithService(name string, opts ...func(*config.LoadBalancerService)) func(*config.LoadBalancerService) string {
return func(r *config.LoadBalancerService) string {
for _, opt := range opts {
opt(r)
}
return name
}
}
// WithMiddlewares is a helper to create a configuration.
func WithMiddlewares(opts ...func(*config.Middleware) string) func(*config.HTTPConfiguration) {
return func(c *config.HTTPConfiguration) {
c.Middlewares = make(map[string]*config.Middleware)
for _, opt := range opts {
b := &config.Middleware{}
name := opt(b)
c.Middlewares[name] = b
}
}
}
// WithMiddleware is a helper to create a configuration.
func WithMiddleware(name string, opts ...func(*config.Middleware)) func(*config.Middleware) string {
return func(r *config.Middleware) string {
for _, opt := range opts {
opt(r)
}
return name
}
}
// WithBasicAuth is a helper to create a configuration.
func WithBasicAuth(auth *config.BasicAuth) func(*config.Middleware) {
return func(r *config.Middleware) {
r.BasicAuth = auth
}
}
// WithEntryPoints is a helper to create a configuration.
func WithEntryPoints(eps ...string) func(*config.Router) {
return func(f *config.Router) {
f.EntryPoints = eps
}
}
// WithRule is a helper to create a configuration.
func WithRule(rule string) func(*config.Router) {
return func(f *config.Router) {
f.Rule = rule
}
}
// WithServers is a helper to create a configuration.
func WithServers(opts ...func(*config.Server)) func(*config.LoadBalancerService) {
return func(b *config.LoadBalancerService) {
for _, opt := range opts {
server := config.Server{Weight: 1}
opt(&server)
b.Servers = append(b.Servers, server)
}
}
}
// WithServer is a helper to create a configuration.
func WithServer(url string, opts ...func(*config.Server)) func(*config.Server) {
return func(s *config.Server) {
for _, opt := range opts {
opt(s)
}
s.URL = url
}
}
// WithLBMethod is a helper to create a configuration.
func WithLBMethod(method string) func(*config.LoadBalancerService) {
return func(b *config.LoadBalancerService) {
b.Method = method
}
}
// WithStickiness is a helper to create a configuration.
func WithStickiness(cookieName string) func(*config.LoadBalancerService) {
return func(b *config.LoadBalancerService) {
b.Stickiness = &config.Stickiness{
CookieName: cookieName,
}
}
}

View file

@ -0,0 +1,26 @@
package testhelpers
import (
"fmt"
"io"
"net/http"
"net/url"
)
// MustNewRequest creates a new http get request or panics if it can't
func MustNewRequest(method, urlStr string, body io.Reader) *http.Request {
request, err := http.NewRequest(method, urlStr, body)
if err != nil {
panic(fmt.Sprintf("failed to create HTTP %s Request for '%s': %s", method, urlStr, err))
}
return request
}
// MustParseURL parses a URL or panics if it can't
func MustParseURL(rawURL string) *url.URL {
u, err := url.Parse(rawURL)
if err != nil {
panic(fmt.Sprintf("failed to parse URL '%s': %s", rawURL, err))
}
return u
}

View file

@ -0,0 +1,57 @@
package testhelpers
import "github.com/go-kit/kit/metrics"
// CollectingCounter is a metrics.Counter implementation that enables access to the CounterValue and LastLabelValues.
type CollectingCounter struct {
CounterValue float64
LastLabelValues []string
}
// With is there to satisfy the metrics.Counter interface.
func (c *CollectingCounter) With(labelValues ...string) metrics.Counter {
c.LastLabelValues = labelValues
return c
}
// Add is there to satisfy the metrics.Counter interface.
func (c *CollectingCounter) Add(delta float64) {
c.CounterValue += delta
}
// CollectingGauge is a metrics.Gauge implementation that enables access to the GaugeValue and LastLabelValues.
type CollectingGauge struct {
GaugeValue float64
LastLabelValues []string
}
// With is there to satisfy the metrics.Gauge interface.
func (g *CollectingGauge) With(labelValues ...string) metrics.Gauge {
g.LastLabelValues = labelValues
return g
}
// Set is there to satisfy the metrics.Gauge interface.
func (g *CollectingGauge) Set(value float64) {
g.GaugeValue = value
}
// Add is there to satisfy the metrics.Gauge interface.
func (g *CollectingGauge) Add(delta float64) {
g.GaugeValue = delta
}
// CollectingHealthCheckMetrics can be used for testing the Metrics instrumentation of the HealthCheck package.
type CollectingHealthCheckMetrics struct {
Gauge *CollectingGauge
}
// BackendServerUpGauge is there to satisfy the healthcheck.metricsRegistry interface.
func (m *CollectingHealthCheckMetrics) BackendServerUpGauge() metrics.Gauge {
return m.Gauge
}
// NewCollectingHealthCheckMetrics creates a new CollectingHealthCheckMetrics instance.
func NewCollectingHealthCheckMetrics() *CollectingHealthCheckMetrics {
return &CollectingHealthCheckMetrics{&CollectingGauge{}}
}