From 9cd47dd2aa8baeab14c517483aa5382bf108fcbb Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 6 Aug 2018 20:00:03 +0200 Subject: [PATCH] Small code enhancements --- .golangci.toml | 30 ++++++++++++++++++++ cluster/datastore.go | 2 +- cmd/context.go | 2 +- cmd/traefik/traefik.go | 8 ++++-- configuration/router/internal_router.go | 4 ++- configuration/router/internal_router_test.go | 26 +++++++++++++---- docs/configuration/acme.md | 2 +- docs/configuration/backends/ecs.md | 4 +-- healthcheck/healthcheck.go | 30 +++++++++++--------- healthcheck/healthcheck_test.go | 2 +- hostresolver/hostresolver.go | 4 ++- integration/access_log_test.go | 9 ++++-- integration/consul_catalog_test.go | 6 ++-- integration/etcd3_test.go | 3 +- integration/etcd_test.go | 5 ++-- integration/fake_dns_server.go | 4 ++- integration/grpc_test.go | 7 ++++- integration/https_test.go | 5 +++- middlewares/accesslog/logger.go | 4 +-- middlewares/accesslog/logger_test.go | 6 +++- middlewares/auth/forward.go | 5 +++- middlewares/cbreaker.go | 6 +++- middlewares/compress_test.go | 3 +- middlewares/errorpages/error_pages.go | 5 +++- middlewares/errorpages/error_pages_test.go | 3 +- middlewares/headers_test.go | 9 ++++-- middlewares/retry_test.go | 7 +++-- provider/acme/local_store.go | 2 +- provider/file/file.go | 2 +- provider/file/file_test.go | 8 ++++-- provider/kubernetes/client.go | 2 +- provider/kv/kv_mock_test.go | 6 ++-- provider/kv/kv_test.go | 5 +++- provider/marathon/builder_test.go | 6 ---- provider/rancher/metadata.go | 2 +- server/server.go | 4 +-- server/server_configuration.go | 6 +++- server/server_loadbalancer.go | 2 +- server/server_middlewares.go | 7 ++--- server/server_test.go | 14 ++++----- version/version.go | 5 +++- 41 files changed, 187 insertions(+), 85 deletions(-) create mode 100644 .golangci.toml diff --git a/.golangci.toml b/.golangci.toml new file mode 100644 index 000000000..f3f1e8f9e --- /dev/null +++ b/.golangci.toml @@ -0,0 +1,30 @@ +[linters-settings] + + [linters-settings.govet] + check-shadowing = true + + [linters-settings.golint] + min-confidence = 0.0 + + [linters-settings.gocyclo] + min-complexity = 22.0 + + [linters-settings.maligned] + suggest-new = true + + [linters-settings.goconst] + min-len = 2.0 + min-occurrences = 2.0 + + [linters-settings.misspell] + locale = "US" + +[linters] + enable-all = true + disable = [ + "maligned", + "lll", + "gas", + "dupl", + "prealloc" + ] \ No newline at end of file diff --git a/cluster/datastore.go b/cluster/datastore.go index 081c1f5f2..7bcf0a4c2 100644 --- a/cluster/datastore.go +++ b/cluster/datastore.go @@ -233,7 +233,7 @@ func (s *datastoreTransaction) Commit(object Object) error { } err = s.kv.StoreConfig(s.Datastore.meta) if err != nil { - return fmt.Errorf("StoreConfig error: %s", err) + return fmt.Errorf("storeConfig error: %s", err) } err = s.remoteLock.Unlock() diff --git a/cmd/context.go b/cmd/context.go index 002db47ec..cc25e4aae 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -7,7 +7,7 @@ import ( "syscall" ) -// ContextWithSignal create a context cancelled when SIGINT or SIGTERM are notified +// ContextWithSignal creates a context canceled when SIGINT or SIGTERM are notified func ContextWithSignal(ctx context.Context) context.Context { newCtx, cancel := context.WithCancel(ctx) signals := make(chan os.Signal) diff --git a/cmd/traefik/traefik.go b/cmd/traefik/traefik.go index 4c69f8385..070d02759 100644 --- a/cmd/traefik/traefik.go +++ b/cmd/traefik/traefik.go @@ -158,7 +158,9 @@ func runCmd(globalConfiguration *configuration.GlobalConfiguration, configFile s http.DefaultTransport.(*http.Transport).Proxy = http.ProxyFromEnvironment - roundrobin.SetDefaultWeight(0) + if err := roundrobin.SetDefaultWeight(0); err != nil { + log.Error(err) + } globalConfiguration.SetEffectiveConfiguration(configFile) globalConfiguration.ValidateConfiguration() @@ -182,8 +184,8 @@ func runCmd(globalConfiguration *configuration.GlobalConfiguration, configFile s acmeprovider := globalConfiguration.InitACMEProvider() if acmeprovider != nil { - err := providerAggregator.AddProvider(acmeprovider) - if err != nil { + + if err := providerAggregator.AddProvider(acmeprovider); err != nil { log.Errorf("Error initializing provider ACME: %v", err) acmeprovider = nil } diff --git a/configuration/router/internal_router.go b/configuration/router/internal_router.go index 3abc0c380..1233d533f 100644 --- a/configuration/router/internal_router.go +++ b/configuration/router/internal_router.go @@ -79,7 +79,9 @@ func (wm *WithMiddleware) AddRoutes(systemRouter *mux.Router) { wm.router.AddRoutes(realRouter) if len(wm.routerMiddlewares) > 0 { - realRouter.Walk(wrapRoute(wm.routerMiddlewares)) + if err := realRouter.Walk(wrapRoute(wm.routerMiddlewares)); err != nil { + log.Error(err) + } } } diff --git a/configuration/router/internal_router_test.go b/configuration/router/internal_router_test.go index c1d7e8721..4c726a6e2 100644 --- a/configuration/router/internal_router_test.go +++ b/configuration/router/internal_router_test.go @@ -9,6 +9,7 @@ import ( "github.com/containous/traefik/acme" "github.com/containous/traefik/api" "github.com/containous/traefik/configuration" + "github.com/containous/traefik/log" "github.com/containous/traefik/ping" acmeprovider "github.com/containous/traefik/provider/acme" "github.com/containous/traefik/safe" @@ -104,20 +105,35 @@ func TestWithMiddleware(t *testing.T) { router := WithMiddleware{ router: MockInternalRouterFunc(func(systemRouter *mux.Router) { systemRouter.Handle("/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("router")) + + if _, err := w.Write([]byte("router")); err != nil { + log.Error(err) + } })) }), routerMiddlewares: []negroni.Handler{ negroni.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { - rw.Write([]byte("before middleware1|")) + if _, err := rw.Write([]byte("before middleware1|")); err != nil { + log.Error(err) + } + next.ServeHTTP(rw, r) - rw.Write([]byte("|after middleware1")) + + if _, err := rw.Write([]byte("|after middleware1")); err != nil { + log.Error(err) + } }), negroni.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { - rw.Write([]byte("before middleware2|")) + if _, err := rw.Write([]byte("before middleware2|")); err != nil { + log.Error(err) + } + next.ServeHTTP(rw, r) - rw.Write([]byte("|after middleware2")) + + if _, err := rw.Write([]byte("|after middleware2")); err != nil { + log.Error(err) + } }), }, } diff --git a/docs/configuration/acme.md b/docs/configuration/acme.md index 07c10678f..2f282fa73 100644 --- a/docs/configuration/acme.md +++ b/docs/configuration/acme.md @@ -328,7 +328,7 @@ It is not possible to request a double wildcard certificate for a domain (for ex Due to ACME limitation it is not possible to define wildcards in SANs (alternative domains). Thus, the wildcard domain has to be defined as a main domain. Most likely the root domain should receive a certificate too, so it needs to be specified as SAN and 2 `DNS-01` challenges are executed. In this case the generated DNS TXT record for both domains is the same. -Eventhough this behaviour is [DNS RFC](https://community.letsencrypt.org/t/wildcard-issuance-two-txt-records-for-the-same-name/54528/2) compliant, it can lead to problems as all DNS providers keep DNS records cached for a certain time (TTL) and this TTL can be superior to the challenge timeout making the `DNS-01` challenge fail. +Eventhough this behavior is [DNS RFC](https://community.letsencrypt.org/t/wildcard-issuance-two-txt-records-for-the-same-name/54528/2) compliant, it can lead to problems as all DNS providers keep DNS records cached for a certain time (TTL) and this TTL can be superior to the challenge timeout making the `DNS-01` challenge fail. The Træfik ACME client library [LEGO](https://github.com/xenolf/lego) supports some but not all DNS providers to work around this issue. The [`provider` table](/configuration/acme/#provider) indicates if they allow generating certificates for a wildcard domain and its root domain. diff --git a/docs/configuration/backends/ecs.md b/docs/configuration/backends/ecs.md index 4e3dcb84c..a7194b41f 100644 --- a/docs/configuration/backends/ecs.md +++ b/docs/configuration/backends/ecs.md @@ -126,9 +126,9 @@ Træfik needs the following policy to read ECS information: } ``` -## Labels: overriding default behaviour +## Labels: overriding default behavior -Labels can be used on task containers to override default behaviour: +Labels can be used on task containers to override default behavior: | Label | Description | |------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| diff --git a/healthcheck/healthcheck.go b/healthcheck/healthcheck.go index f24ba8766..ba14bdcf5 100644 --- a/healthcheck/healthcheck.go +++ b/healthcheck/healthcheck.go @@ -130,30 +130,34 @@ func (hc *HealthCheck) execute(ctx context.Context, backend *BackendConfig) { func (hc *HealthCheck) checkBackend(backend *BackendConfig) { enabledURLs := backend.LB.Servers() var newDisabledURLs []*url.URL - for _, url := range backend.disabledURLs { + for _, disableURL := range backend.disabledURLs { serverUpMetricValue := float64(0) - if err := checkHealth(url, backend); err == nil { - log.Warnf("Health check up: Returning to server list. Backend: %q URL: %q", backend.name, url.String()) - backend.LB.UpsertServer(url, roundrobin.Weight(1)) + if err := checkHealth(disableURL, backend); err == nil { + log.Warnf("Health check up: Returning to server list. Backend: %q URL: %q", backend.name, disableURL.String()) + if err := backend.LB.UpsertServer(disableURL, roundrobin.Weight(1)); err != nil { + log.Error(err) + } serverUpMetricValue = 1 } else { - log.Warnf("Health check still failing. Backend: %q URL: %q Reason: %s", backend.name, url.String(), err) - newDisabledURLs = append(newDisabledURLs, url) + log.Warnf("Health check still failing. Backend: %q URL: %q Reason: %s", backend.name, disableURL.String(), err) + newDisabledURLs = append(newDisabledURLs, disableURL) } - labelValues := []string{"backend", backend.name, "url", url.String()} + labelValues := []string{"backend", backend.name, "url", disableURL.String()} hc.metrics.BackendServerUpGauge().With(labelValues...).Set(serverUpMetricValue) } backend.disabledURLs = newDisabledURLs - for _, url := range enabledURLs { + for _, enableURL := range enabledURLs { serverUpMetricValue := float64(1) - if err := checkHealth(url, backend); err != nil { - log.Warnf("Health check failed: Remove from server list. Backend: %q URL: %q Reason: %s", backend.name, url.String(), err) - backend.LB.RemoveServer(url) - backend.disabledURLs = append(backend.disabledURLs, url) + if err := checkHealth(enableURL, backend); err != nil { + log.Warnf("Health check failed: Remove from server list. Backend: %q URL: %q Reason: %s", backend.name, enableURL.String(), err) + if err := backend.LB.RemoveServer(enableURL); err != nil { + log.Error(err) + } + backend.disabledURLs = append(backend.disabledURLs, enableURL) serverUpMetricValue = 0 } - labelValues := []string{"backend", backend.name, "url", url.String()} + labelValues := []string{"backend", backend.name, "url", enableURL.String()} hc.metrics.BackendServerUpGauge().With(labelValues...).Set(serverUpMetricValue) } } diff --git a/healthcheck/healthcheck_test.go b/healthcheck/healthcheck_test.go index 131070e6d..5b7528bf8 100644 --- a/healthcheck/healthcheck_test.go +++ b/healthcheck/healthcheck_test.go @@ -94,7 +94,7 @@ func TestSetBackendsConfiguration(t *testing.T) { t.Run(test.desc, func(t *testing.T) { t.Parallel() - // The context is passed to the health check and canonically cancelled by + // The context is passed to the health check and canonically canceled by // the test server once all expected requests have been received. ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/hostresolver/hostresolver.go b/hostresolver/hostresolver.go index 5f3ec5777..30dab4080 100644 --- a/hostresolver/hostresolver.go +++ b/hostresolver/hostresolver.go @@ -63,7 +63,9 @@ func (hr *Resolver) CNAMEFlatten(host string) (string, string) { request = resolv.Record } - hr.cache.Add(host, strings.Join(result, ","), cacheDuration) + if err := hr.cache.Add(host, strings.Join(result, ","), cacheDuration); err != nil { + log.Error(err) + } } return result[0], result[len(result)-1] diff --git a/integration/access_log_test.go b/integration/access_log_test.go index 27da9adb8..2e73b0b34 100644 --- a/integration/access_log_test.go +++ b/integration/access_log_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/containous/traefik/integration/try" + "github.com/containous/traefik/log" "github.com/containous/traefik/middlewares/accesslog" "github.com/go-check/check" checker "github.com/vdemeester/shakers" @@ -324,13 +325,17 @@ func digestParts(resp *http.Response) map[string]string { func getMD5(data string) string { digest := md5.New() - digest.Write([]byte(data)) + if _, err := digest.Write([]byte(data)); err != nil { + log.Error(err) + } return fmt.Sprintf("%x", digest.Sum(nil)) } func getCnonce() string { b := make([]byte, 8) - io.ReadFull(rand.Reader, b) + if _, err := io.ReadFull(rand.Reader, b); err != nil { + log.Error(err) + } return fmt.Sprintf("%x", b)[:16] } diff --git a/integration/consul_catalog_test.go b/integration/consul_catalog_test.go index 7607b41d8..4e776efe7 100644 --- a/integration/consul_catalog_test.go +++ b/integration/consul_catalog_test.go @@ -530,7 +530,8 @@ func (s *ConsulCatalogSuite) TestRetryWithConsulServer(c *check.C) { // Scale consul to 1 s.composeProject.Scale(c, "consul", 1) - s.waitToElectConsulLeader() + err = s.waitToElectConsulLeader() + c.Assert(err, checker.IsNil) whoami := s.composeProject.Container(c, "whoami1") // Register service @@ -576,7 +577,8 @@ func (s *ConsulCatalogSuite) TestServiceWithMultipleHealthCheck(c *check.C) { // Scale consul to 1 s.composeProject.Scale(c, "consul", 1) - s.waitToElectConsulLeader() + err = s.waitToElectConsulLeader() + c.Assert(err, checker.IsNil) whoami := s.composeProject.Container(c, "whoami1") // Register service diff --git a/integration/etcd3_test.go b/integration/etcd3_test.go index ad877469b..1ccb12cfd 100644 --- a/integration/etcd3_test.go +++ b/integration/etcd3_test.go @@ -404,7 +404,8 @@ func (s *Etcd3Suite) TestCommandStoreConfig(c *check.C) { c.Assert(err, checker.IsNil) // wait for traefik finish without error - cmd.Wait() + err = cmd.Wait() + c.Assert(err, checker.IsNil) // CHECK checkmap := map[string]string{ diff --git a/integration/etcd_test.go b/integration/etcd_test.go index 80dcf0b40..c7abe57b4 100644 --- a/integration/etcd_test.go +++ b/integration/etcd_test.go @@ -411,8 +411,9 @@ func (s *EtcdSuite) TestCommandStoreConfig(c *check.C) { err := cmd.Start() c.Assert(err, checker.IsNil) - // wait for Træfik finish without error - cmd.Wait() + // wait for traefik finish without error + err = cmd.Wait() + c.Assert(err, checker.IsNil) // CHECK checkmap := map[string]string{ diff --git a/integration/fake_dns_server.go b/integration/fake_dns_server.go index 3319b703b..fa7cba2a4 100644 --- a/integration/fake_dns_server.go +++ b/integration/fake_dns_server.go @@ -93,7 +93,9 @@ func (s *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { auth.Minttl = 1 m.Ns = append(m.Ns, auth) - w.WriteMsg(m) + if err := w.WriteMsg(m); err != nil { + log.Fatalf("Failed to write message %v", err) + } } func startFakeDNSServer() *dns.Server { diff --git a/integration/grpc_test.go b/integration/grpc_test.go index 8ec9813a0..ee043fccd 100644 --- a/integration/grpc_test.go +++ b/integration/grpc_test.go @@ -13,6 +13,7 @@ import ( "github.com/containous/traefik/integration/helloworld" "github.com/containous/traefik/integration/try" + "github.com/containous/traefik/log" "github.com/go-check/check" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -47,7 +48,11 @@ func (s *myserver) StreamExample(in *helloworld.StreamExampleRequest, server hel for i := range data { data[i] = randCharset[rand.Intn(len(randCharset))] } - server.Send(&helloworld.StreamExampleReply{Data: string(data)}) + + if err := server.Send(&helloworld.StreamExampleReply{Data: string(data)}); err != nil { + log.Error(err) + } + <-s.stopStreamExample return nil } diff --git a/integration/https_test.go b/integration/https_test.go index 5db8a54ab..a85fab003 100644 --- a/integration/https_test.go +++ b/integration/https_test.go @@ -708,7 +708,10 @@ func modifyCertificateConfFileContent(c *check.C, certFileName, confFileName, en defer func() { f.Close() }() - f.Truncate(0) + + err = f.Truncate(0) + c.Assert(err, checker.IsNil) + // If certificate file is not provided, just truncate the configuration file if len(certFileName) > 0 { tlsConf := types.Configuration{ diff --git a/middlewares/accesslog/logger.go b/middlewares/accesslog/logger.go index 78fd94934..a5fedd0ca 100644 --- a/middlewares/accesslog/logger.go +++ b/middlewares/accesslog/logger.go @@ -26,10 +26,10 @@ const ( DataTableKey key = "LogDataTable" // CommonFormat is the common logging format (CLF) - CommonFormat = "common" + CommonFormat string = "common" // JSONFormat is the JSON logging format - JSONFormat = "json" + JSONFormat string = "json" ) type logHandlerParams struct { diff --git a/middlewares/accesslog/logger_test.go b/middlewares/accesslog/logger_test.go index 107e5ac2f..c2ade2df3 100644 --- a/middlewares/accesslog/logger_test.go +++ b/middlewares/accesslog/logger_test.go @@ -15,6 +15,7 @@ import ( "time" "github.com/containous/flaeg/parse" + "github.com/containous/traefik/log" "github.com/containous/traefik/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -626,7 +627,10 @@ func doLogging(t *testing.T, config *types.AccessLog) { } func logWriterTestHandlerFunc(rw http.ResponseWriter, r *http.Request) { - rw.Write([]byte(testContent)) + if _, err := rw.Write([]byte(testContent)); err != nil { + log.Error(err) + } + rw.WriteHeader(testStatus) logDataTable := GetLogDataTable(r) diff --git a/middlewares/auth/forward.go b/middlewares/auth/forward.go index 12e885fda..158347804 100644 --- a/middlewares/auth/forward.go +++ b/middlewares/auth/forward.go @@ -90,7 +90,10 @@ func Forward(config *types.Forward, w http.ResponseWriter, r *http.Request, next tracing.LogResponseCode(tracing.GetSpan(r), forwardResponse.StatusCode) w.WriteHeader(forwardResponse.StatusCode) - w.Write(body) + + if _, err = w.Write(body); err != nil { + log.Error(err) + } return } diff --git a/middlewares/cbreaker.go b/middlewares/cbreaker.go index e06783456..8afdcd6f0 100644 --- a/middlewares/cbreaker.go +++ b/middlewares/cbreaker.go @@ -3,6 +3,7 @@ package middlewares import ( "net/http" + "github.com/containous/traefik/log" "github.com/containous/traefik/middlewares/tracing" "github.com/vulcand/oxy/cbreaker" ) @@ -27,7 +28,10 @@ func NewCircuitBreakerOptions(expression string) cbreaker.CircuitBreakerOption { tracing.LogEventf(r, "blocked by circuit-breaker (%q)", expression) w.WriteHeader(http.StatusServiceUnavailable) - w.Write([]byte(http.StatusText(http.StatusServiceUnavailable))) + + if _, err := w.Write([]byte(http.StatusText(http.StatusServiceUnavailable))); err != nil { + log.Error(err) + } })) } diff --git a/middlewares/compress_test.go b/middlewares/compress_test.go index 743a02dba..742d4b626 100644 --- a/middlewares/compress_test.go +++ b/middlewares/compress_test.go @@ -29,7 +29,8 @@ func TestShouldCompressWhenNoContentEncodingHeader(t *testing.T) { baseBody := generateBytes(gziphandler.DefaultMinSize) next := func(rw http.ResponseWriter, r *http.Request) { - rw.Write(baseBody) + _, err := rw.Write(baseBody) + assert.NoError(t, err) } rw := httptest.NewRecorder() diff --git a/middlewares/errorpages/error_pages.go b/middlewares/errorpages/error_pages.go index 9fbe84706..6be332514 100644 --- a/middlewares/errorpages/error_pages.go +++ b/middlewares/errorpages/error_pages.go @@ -103,7 +103,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request, next http. utils.CopyHeaders(w.Header(), recorderErrorPage.Header()) w.WriteHeader(recorder.GetCode()) - w.Write(recorderErrorPage.GetBody().Bytes()) + + if _, err = w.Write(recorderErrorPage.GetBody().Bytes()); err != nil { + log.Error(err) + } return } } diff --git a/middlewares/errorpages/error_pages_test.go b/middlewares/errorpages/error_pages_test.go index 9cf19d87d..db11c7076 100644 --- a/middlewares/errorpages/error_pages_test.go +++ b/middlewares/errorpages/error_pages_test.go @@ -218,7 +218,8 @@ func TestHandlerOldWay(t *testing.T) { require.NoError(t, err) errorPageHandler.FallbackURL = "http://localhost" - errorPageHandler.PostLoad(test.errorPageForwarder) + err = errorPageHandler.PostLoad(test.errorPageForwarder) + require.NoError(t, err) handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(test.backendCode) diff --git a/middlewares/headers_test.go b/middlewares/headers_test.go index 7520353f7..846d8fc38 100644 --- a/middlewares/headers_test.go +++ b/middlewares/headers_test.go @@ -49,7 +49,8 @@ func TestModifyResponseHeaders(t *testing.T) { res := httptest.NewRecorder() res.HeaderMap.Add("X-Custom-Response-Header", "test_response") - header.ModifyResponseHeaders(res.Result()) + err := header.ModifyResponseHeaders(res.Result()) + assert.NoError(t, err) assert.Equal(t, http.StatusOK, res.Code, "Status not OK") assert.Equal(t, "test_response", res.Header().Get("X-Custom-Response-Header"), "Did not get expected header") @@ -57,7 +58,8 @@ func TestModifyResponseHeaders(t *testing.T) { res = httptest.NewRecorder() res.HeaderMap.Add("X-Custom-Response-Header", "") - header.ModifyResponseHeaders(res.Result()) + err = header.ModifyResponseHeaders(res.Result()) + assert.NoError(t, err) assert.Equal(t, http.StatusOK, res.Code, "Status not OK") assert.Equal(t, "", res.Header().Get("X-Custom-Response-Header"), "Did not get expected header") @@ -65,7 +67,8 @@ func TestModifyResponseHeaders(t *testing.T) { res = httptest.NewRecorder() res.HeaderMap.Add("X-Custom-Response-Header", "test_override") - header.ModifyResponseHeaders(res.Result()) + err = header.ModifyResponseHeaders(res.Result()) + assert.NoError(t, err) assert.Equal(t, http.StatusOK, res.Code, "Status not OK") assert.Equal(t, "test_override", res.Header().Get("X-Custom-Response-Header"), "Did not get expected header") diff --git a/middlewares/retry_test.go b/middlewares/retry_test.go index b73044b29..9ca32a68e 100644 --- a/middlewares/retry_test.go +++ b/middlewares/retry_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/containous/traefik/testhelpers" + "github.com/stretchr/testify/assert" "github.com/vulcand/oxy/forward" "github.com/vulcand/oxy/roundrobin" ) @@ -91,11 +92,13 @@ func TestRetry(t *testing.T) { // See: https://stackoverflow.com/questions/528538/non-routable-ip-address/18436928#18436928 // We only use the port specification here because the URL is used as identifier // in the load balancer and using the exact same URL would not add a new server. - loadBalancer.UpsertServer(testhelpers.MustParseURL("http://192.0.2.0:" + string(basePort+i))) + err = loadBalancer.UpsertServer(testhelpers.MustParseURL("http://192.0.2.0:" + string(basePort+i))) + assert.NoError(t, err) } // add the functioning server to the end of the load balancer list - loadBalancer.UpsertServer(testhelpers.MustParseURL(backendServer.URL)) + err = loadBalancer.UpsertServer(testhelpers.MustParseURL(backendServer.URL)) + assert.NoError(t, err) retryListener := &countingRetryListener{} retry := NewRetry(tc.maxRequestAttempts, loadBalancer, retryListener) diff --git a/provider/acme/local_store.go b/provider/acme/local_store.go index 2407fbc83..97714e7eb 100644 --- a/provider/acme/local_store.go +++ b/provider/acme/local_store.go @@ -188,7 +188,7 @@ func (s *LocalStore) SetHTTPChallengeToken(token, domain string, keyAuth []byte) s.storedData.HTTPChallenges[token] = map[string][]byte{} } - s.storedData.HTTPChallenges[token][domain] = []byte(keyAuth) + s.storedData.HTTPChallenges[token][domain] = keyAuth return nil } diff --git a/provider/file/file.go b/provider/file/file.go index b15236768..b54f07fcf 100644 --- a/provider/file/file.go +++ b/provider/file/file.go @@ -76,7 +76,7 @@ func (p *Provider) BuildConfiguration() (*types.Configuration, error) { return p.loadFileConfig(p.TraefikFile, false) } - return nil, errors.New("Error using file configuration backend, no filename defined") + return nil, errors.New("error using file configuration backend, no filename defined") } func (p *Provider) addWatcher(pool *safe.Pool, directory string, configurationChan chan<- types.ConfigMessage, callback func(chan<- types.ConfigMessage, fsnotify.Event)) error { diff --git a/provider/file/file_test.go b/provider/file/file_test.go index 1e62175ae..4f0758ffa 100644 --- a/provider/file/file_test.go +++ b/provider/file/file_test.go @@ -241,11 +241,15 @@ func TestProvideWithWatch(t *testing.T) { } if len(test.fileContent) > 0 { - ioutil.WriteFile(provider.Filename, []byte(test.fileContent), 0755) + if err := ioutil.WriteFile(provider.Filename, []byte(test.fileContent), 0755); err != nil { + t.Error(err) + } } if len(test.traefikFileContent) > 0 { - ioutil.WriteFile(provider.TraefikFile, []byte(test.traefikFileContent), 0755) + if err := ioutil.WriteFile(provider.TraefikFile, []byte(test.traefikFileContent), 0755); err != nil { + t.Error(err) + } } if len(test.directoryContent) > 0 { diff --git a/provider/kubernetes/client.go b/provider/kubernetes/client.go index d09bf5a40..78f71c5d8 100644 --- a/provider/kubernetes/client.go +++ b/provider/kubernetes/client.go @@ -241,7 +241,7 @@ func (c *clientImpl) newResourceEventHandler(events chan<- interface{}) cache.Re // eventHandlerFunc will pass the obj on to the events channel or drop it. // This is so passing the events along won't block in the case of high volume. -// The events are only used for signalling anyway so dropping a few is ok. +// The events are only used for signaling anyway so dropping a few is ok. func eventHandlerFunc(events chan<- interface{}, obj interface{}) { select { case events <- obj: diff --git a/provider/kv/kv_mock_test.go b/provider/kv/kv_mock_test.go index ef7280207..a1916b7d0 100644 --- a/provider/kv/kv_mock_test.go +++ b/provider/kv/kv_mock_test.go @@ -44,7 +44,7 @@ func newKvClientMock(kvPairs []*store.KVPair, err error) *Mock { } func (s *Mock) Put(key string, value []byte, opts *store.WriteOptions) error { - return errors.New("Put not supported") + return errors.New("put not supported") } func (s *Mock) Get(key string, options *store.ReadOptions) (*store.KVPair, error) { @@ -60,7 +60,7 @@ func (s *Mock) Get(key string, options *store.ReadOptions) (*store.KVPair, error } func (s *Mock) Delete(key string) error { - return errors.New("Delete not supported") + return errors.New("delete not supported") } // Exists mock @@ -78,7 +78,7 @@ func (s *Mock) Exists(key string, options *store.ReadOptions) (bool, error) { // Watch mock func (s *Mock) Watch(key string, stopCh <-chan struct{}, options *store.ReadOptions) (<-chan *store.KVPair, error) { - return nil, errors.New("Watch not supported") + return nil, errors.New("watch not supported") } // WatchTree mock diff --git a/provider/kv/kv_test.go b/provider/kv/kv_test.go index e82da84eb..7e14af4f8 100644 --- a/provider/kv/kv_test.go +++ b/provider/kv/kv_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/abronan/valkeyrie/store" + "github.com/containous/traefik/log" "github.com/containous/traefik/types" ) @@ -22,7 +23,9 @@ func TestKvWatchTree(t *testing.T) { configChan := make(chan types.ConfigMessage) go func() { - provider.watchKv(configChan, "prefix", make(chan bool, 1)) + if err := provider.watchKv(configChan, "prefix", make(chan bool, 1)); err != nil { + log.Error(err) + } }() select { diff --git a/provider/marathon/builder_test.go b/provider/marathon/builder_test.go index 36a428dd3..86a190e2a 100644 --- a/provider/marathon/builder_test.go +++ b/provider/marathon/builder_test.go @@ -95,12 +95,6 @@ func containerNetwork() func(*marathon.Application) { } } -func hostNetwork() func(*marathon.Application) { - return func(app *marathon.Application) { - app.SetNetwork("host", marathon.HostNetworkMode) - } -} - func ipAddrPerTask(port int) func(*marathon.Application) { return func(app *marathon.Application) { p := marathon.Port{ diff --git a/provider/rancher/metadata.go b/provider/rancher/metadata.go index 76e91e550..92d02edc6 100644 --- a/provider/rancher/metadata.go +++ b/provider/rancher/metadata.go @@ -109,7 +109,7 @@ func (p *Provider) longPoll(client rancher.Client, updateConfiguration func(stri // Holds the connection until there is either a change in the metadata // repository or `p.RefreshSeconds` has elapsed. Long polling should be - // favoured for the most accurate configuration updates. + // favored for the most accurate configuration updates. safe.Go(func() { client.OnChange(p.RefreshSeconds, updateConfiguration) }) diff --git a/server/server.go b/server/server.go index 14a9882c1..b7800f1f9 100644 --- a/server/server.go +++ b/server/server.go @@ -343,7 +343,7 @@ func (s *Server) AddListener(listener func(types.Configuration)) { s.configurationListeners = append(s.configurationListeners, listener) } -// getCertificate allows to customize tlsConfig.GetCertificate behaviour to get the certificates inserted dynamically +// getCertificate allows to customize tlsConfig.GetCertificate behavior to get the certificates inserted dynamically func (s *serverEntryPoint) getCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) { domainToCheck := types.CanonicalDomain(clientHello.ServerName) @@ -498,7 +498,7 @@ func (s *Server) startServer(serverEntryPoint *serverEntryPoint) { } func (s *Server) setupServerEntryPoint(newServerEntryPointName string, newServerEntryPoint *serverEntryPoint) *serverEntryPoint { - serverMiddlewares, err := s.buildServerEntryPointMiddlewares(newServerEntryPointName, newServerEntryPoint) + serverMiddlewares, err := s.buildServerEntryPointMiddlewares(newServerEntryPointName) if err != nil { log.Fatal("Error preparing server: ", err) } diff --git a/server/server_configuration.go b/server/server_configuration.go index 08f8b74ca..0e9135e1a 100644 --- a/server/server_configuration.go +++ b/server/server_configuration.go @@ -165,7 +165,7 @@ func (s *Server) loadFrontendConfig( if backendsHandlers[entryPointName+providerName+frontendHash] == nil { log.Debugf("Creating backend %s", frontend.Backend) - handlers, responseModifier, postConfig, err := s.buildMiddlewares(frontendName, frontend, config.Backends, entryPointName, entryPoint, providerName) + handlers, responseModifier, postConfig, err := s.buildMiddlewares(frontendName, frontend, config.Backends, entryPointName, providerName) if err != nil { return nil, err } @@ -566,11 +566,15 @@ func (s *Server) buildServerEntryPoints() map[string]*serverEntryPoint { if entryPoint.Configuration.TLS.DefaultCertificate != nil { cert, err := tls.LoadX509KeyPair(entryPoint.Configuration.TLS.DefaultCertificate.CertFile.String(), entryPoint.Configuration.TLS.DefaultCertificate.KeyFile.String()) if err != nil { + log.Error(err) + continue } serverEntryPoints[entryPointName].certs.DefaultCertificate = &cert } else { cert, err := generate.DefaultCertificate() if err != nil { + log.Error(err) + continue } serverEntryPoints[entryPointName].certs.DefaultCertificate = cert } diff --git a/server/server_loadbalancer.go b/server/server_loadbalancer.go index 6dc0ac180..721f93c26 100644 --- a/server/server_loadbalancer.go +++ b/server/server_loadbalancer.go @@ -228,7 +228,7 @@ func (s *Server) getRoundTripper(entryPointName string, passTLSCert bool, tls *t // For the settings that can't be configured in Traefik it uses the default http.Transport settings. // An exception to this is the MaxIdleConns setting as we only provide the option MaxIdleConnsPerHost // in Traefik at this point in time. Setting this value to the default of 100 could lead to confusing -// behaviour and backwards compatibility issues. +// behavior and backwards compatibility issues. func createHTTPTransport(globalConfiguration configuration.GlobalConfiguration) (*http.Transport, error) { dialer := &net.Dialer{ Timeout: configuration.DefaultDialTimeout, diff --git a/server/server_middlewares.go b/server/server_middlewares.go index af99a5ec9..95befea62 100644 --- a/server/server_middlewares.go +++ b/server/server_middlewares.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" - "github.com/containous/traefik/configuration" "github.com/containous/traefik/log" "github.com/containous/traefik/middlewares" "github.com/containous/traefik/middlewares/accesslog" @@ -22,9 +21,7 @@ type handlerPostConfig func(backendsHandlers map[string]http.Handler) error type modifyResponse func(*http.Response) error func (s *Server) buildMiddlewares(frontendName string, frontend *types.Frontend, - backends map[string]*types.Backend, - entryPointName string, entryPoint *configuration.EntryPoint, - providerName string) ([]negroni.Handler, modifyResponse, handlerPostConfig, error) { + backends map[string]*types.Backend, entryPointName string, providerName string) ([]negroni.Handler, modifyResponse, handlerPostConfig, error) { var middle []negroni.Handler var postConfig handlerPostConfig @@ -109,7 +106,7 @@ func (s *Server) buildMiddlewares(frontendName string, frontend *types.Frontend, return middle, buildModifyResponse(secureMiddleware, headerMiddleware), postConfig, nil } -func (s *Server) buildServerEntryPointMiddlewares(serverEntryPointName string, serverEntryPoint *serverEntryPoint) ([]negroni.Handler, error) { +func (s *Server) buildServerEntryPointMiddlewares(serverEntryPointName string) ([]negroni.Handler, error) { serverMiddlewares := []negroni.Handler{middlewares.NegroniRecoverHandler()} if s.tracingMiddleware.IsEnabled() { diff --git a/server/server_test.go b/server/server_test.go index 2e990aa2d..62f9a914e 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -35,16 +35,16 @@ func TestPrepareServerTimeouts(t *testing.T) { WriteTimeout: parse.Duration(14 * time.Second), }, }, - expectedIdleTimeout: time.Duration(10 * time.Second), - expectedReadTimeout: time.Duration(12 * time.Second), - expectedWriteTimeout: time.Duration(14 * time.Second), + expectedIdleTimeout: 10 * time.Second, + expectedReadTimeout: 12 * time.Second, + expectedWriteTimeout: 14 * time.Second, }, { desc: "using defaults", globalConfig: configuration.GlobalConfiguration{}, - expectedIdleTimeout: time.Duration(180 * time.Second), - expectedReadTimeout: time.Duration(0 * time.Second), - expectedWriteTimeout: time.Duration(0 * time.Second), + expectedIdleTimeout: 180 * time.Second, + expectedReadTimeout: 0 * time.Second, + expectedWriteTimeout: 0 * time.Second, }, } @@ -106,7 +106,7 @@ func TestListenProvidersSkipsSameConfigurationForProvider(t *testing.T) { case config := <-server.configurationValidatedChan: // set the current configuration // this is usually done in the processing part of the published configuration - // so we have to emulate the behaviour here + // so we have to emulate the behavior here currentConfigurations := server.currentConfigurations.Get().(types.Configurations) currentConfigurations[config.ProviderName] = config.Configuration server.currentConfigurations.Set(currentConfigurations) diff --git a/version/version.go b/version/version.go index 61df98b88..77b35b612 100644 --- a/version/version.go +++ b/version/version.go @@ -41,7 +41,10 @@ func (v Handler) AddRoutes(router *mux.Router) { Version: Version, Codename: Codename, } - templatesRenderer.JSON(response, http.StatusOK, v) + + if err := templatesRenderer.JSON(response, http.StatusOK, v); err != nil { + log.Error(err) + } }) }