From f54136b6025ea951562a90a3ef178ce425a13234 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Tue, 29 Dec 2020 10:54:03 +0100 Subject: [PATCH] chore: update linter. --- .golangci.toml | 3 +++ build.Dockerfile | 2 +- pkg/metrics/pilot_test.go | 10 ++++++++++ pkg/metrics/prometheus_test.go | 10 ++++++++++ pkg/middlewares/accesslog/logger_test.go | 12 ++++++++++++ pkg/provider/file/file_test.go | 16 +++++++++------- pkg/server/server_entrypoint_tcp_test.go | 4 ++++ pkg/server/server_entrypoint_udp_test.go | 2 ++ pkg/server/service/proxy_websocket_test.go | 4 ++++ pkg/tcp/proxy_test.go | 2 ++ pkg/udp/conn_test.go | 4 ++++ pkg/udp/proxy_test.go | 2 ++ 12 files changed, 63 insertions(+), 8 deletions(-) diff --git a/.golangci.toml b/.golangci.toml index 64e06b670..c028010c2 100644 --- a/.golangci.toml +++ b/.golangci.toml @@ -57,7 +57,10 @@ "nlreturn", # Not relevant "wrapcheck", # Too strict "tparallel", # Not relevant + "paralleltest", # Not relevant "exhaustivestruct", # Not relevant + "makezero", # not relevant + "forbidigo", # not relevant ] [issues] diff --git a/build.Dockerfile b/build.Dockerfile index 6df51c544..29b4c1b24 100644 --- a/build.Dockerfile +++ b/build.Dockerfile @@ -19,7 +19,7 @@ RUN mkdir -p /usr/local/bin \ && chmod +x /usr/local/bin/go-bindata # Download golangci-lint binary to bin folder in $GOPATH -RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.32.2 +RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.34.0 # Download misspell binary to bin folder in $GOPATH RUN curl -sfL https://raw.githubusercontent.com/client9/misspell/master/install-misspell.sh | bash -s -- -b $GOPATH/bin v0.3.4 diff --git a/pkg/metrics/pilot_test.go b/pkg/metrics/pilot_test.go index 73ffb0335..451c1eb93 100644 --- a/pkg/metrics/pilot_test.go +++ b/pkg/metrics/pilot_test.go @@ -300,6 +300,8 @@ func findPilotMetric(name string, metrics []PilotMetric) *PilotMetric { } func buildPilotCounterAssert(t *testing.T, metricName string, expectedValue float64) func(metric *PilotMetric) { + t.Helper() + return func(metric *PilotMetric) { for _, value := range metric.Observations { if cv := value.(float64); cv != expectedValue { @@ -311,6 +313,8 @@ func buildPilotCounterAssert(t *testing.T, metricName string, expectedValue floa } func buildPilotGreaterThanCounterAssert(t *testing.T, metricName string, expectedMinValue float64) func(metric *PilotMetric) { + t.Helper() + return func(metric *PilotMetric) { for _, value := range metric.Observations { if cv := value.(float64); cv < expectedMinValue { @@ -322,6 +326,8 @@ func buildPilotGreaterThanCounterAssert(t *testing.T, metricName string, expecte } func buildPilotHistogramAssert(t *testing.T, metricName string, expectedSampleCount float64) func(metric *PilotMetric) { + t.Helper() + return func(metric *PilotMetric) { for _, value := range metric.Observations { if pho := value.(*pilotHistogramObservation); pho.Count != expectedSampleCount { @@ -333,6 +339,8 @@ func buildPilotHistogramAssert(t *testing.T, metricName string, expectedSampleCo } func buildPilotGaugeAssert(t *testing.T, metricName string, expectedValue float64) func(metric *PilotMetric) { + t.Helper() + return func(metric *PilotMetric) { for _, value := range metric.Observations { if gv := value.(float64); gv != expectedValue { @@ -344,6 +352,8 @@ func buildPilotGaugeAssert(t *testing.T, metricName string, expectedValue float6 } func buildPilotTimestampAssert(t *testing.T, metricName string) func(metric *PilotMetric) { + t.Helper() + return func(metric *PilotMetric) { for _, value := range metric.Observations { if ts := time.Unix(int64(value.(float64)), 0); time.Since(ts) > time.Minute { diff --git a/pkg/metrics/prometheus_test.go b/pkg/metrics/prometheus_test.go index 87fc1b650..af6ed5d2a 100644 --- a/pkg/metrics/prometheus_test.go +++ b/pkg/metrics/prometheus_test.go @@ -472,6 +472,8 @@ func assertCounterValue(t *testing.T, want float64, family *dto.MetricFamily, la } func buildCounterAssert(t *testing.T, metricName string, expectedValue int) func(family *dto.MetricFamily) { + t.Helper() + return func(family *dto.MetricFamily) { if cv := int(family.Metric[0].Counter.GetValue()); cv != expectedValue { t.Errorf("metric %s has value %d, want %d", metricName, cv, expectedValue) @@ -480,6 +482,8 @@ func buildCounterAssert(t *testing.T, metricName string, expectedValue int) func } func buildGreaterThanCounterAssert(t *testing.T, metricName string, expectedMinValue int) func(family *dto.MetricFamily) { + t.Helper() + return func(family *dto.MetricFamily) { if cv := int(family.Metric[0].Counter.GetValue()); cv < expectedMinValue { t.Errorf("metric %s has value %d, want at least %d", metricName, cv, expectedMinValue) @@ -488,6 +492,8 @@ func buildGreaterThanCounterAssert(t *testing.T, metricName string, expectedMinV } func buildHistogramAssert(t *testing.T, metricName string, expectedSampleCount int) func(family *dto.MetricFamily) { + t.Helper() + return func(family *dto.MetricFamily) { if sc := int(family.Metric[0].Histogram.GetSampleCount()); sc != expectedSampleCount { t.Errorf("metric %s has sample count value %d, want %d", metricName, sc, expectedSampleCount) @@ -496,6 +502,8 @@ func buildHistogramAssert(t *testing.T, metricName string, expectedSampleCount i } func buildGaugeAssert(t *testing.T, metricName string, expectedValue int) func(family *dto.MetricFamily) { + t.Helper() + return func(family *dto.MetricFamily) { if gv := int(family.Metric[0].Gauge.GetValue()); gv != expectedValue { t.Errorf("metric %s has value %d, want %d", metricName, gv, expectedValue) @@ -504,6 +512,8 @@ func buildGaugeAssert(t *testing.T, metricName string, expectedValue int) func(f } func buildTimestampAssert(t *testing.T, metricName string) func(family *dto.MetricFamily) { + t.Helper() + return func(family *dto.MetricFamily) { if ts := time.Unix(int64(family.Metric[0].Gauge.GetValue()), 0); time.Since(ts) > time.Minute { t.Errorf("metric %s has wrong timestamp %v", metricName, ts) diff --git a/pkg/middlewares/accesslog/logger_test.go b/pkg/middlewares/accesslog/logger_test.go index 5bc749eab..aaa145e5b 100644 --- a/pkg/middlewares/accesslog/logger_test.go +++ b/pkg/middlewares/accesslog/logger_test.go @@ -683,6 +683,8 @@ func TestNewLogHandlerOutputStdout(t *testing.T) { } func assertValidLogData(t *testing.T, expected string, logData []byte) { + t.Helper() + if len(expected) == 0 { assert.Zero(t, len(logData)) t.Log(string(logData)) @@ -716,6 +718,8 @@ func assertValidLogData(t *testing.T, expected string, logData []byte) { } func captureStdout(t *testing.T) (out *os.File, restoreStdout func()) { + t.Helper() + file, err := ioutil.TempFile("", "testlogger") require.NoError(t, err, "failed to create temp file") @@ -731,6 +735,8 @@ func captureStdout(t *testing.T) (out *os.File, restoreStdout func()) { } func createTempDir(t *testing.T, prefix string) string { + t.Helper() + tmpDir, err := ioutil.TempDir("", prefix) require.NoError(t, err, "failed to create temp dir") @@ -740,6 +746,8 @@ func createTempDir(t *testing.T, prefix string) string { } func doLoggingTLSOpt(t *testing.T, config *types.AccessLog, enableTLS bool) { + t.Helper() + logger, err := NewHandler(config) require.NoError(t, err) defer logger.Close() @@ -771,10 +779,14 @@ func doLoggingTLSOpt(t *testing.T, config *types.AccessLog, enableTLS bool) { } func doLoggingTLS(t *testing.T, config *types.AccessLog) { + t.Helper() + doLoggingTLSOpt(t, config, true) } func doLogging(t *testing.T, config *types.AccessLog) { + t.Helper() + doLoggingTLSOpt(t, config, false) } diff --git a/pkg/provider/file/file_test.go b/pkg/provider/file/file_test.go index be4755766..ca6f36728 100644 --- a/pkg/provider/file/file_test.go +++ b/pkg/provider/file/file_test.go @@ -76,8 +76,7 @@ func TestErrorWhenEmptyConfig(t *testing.T) { func TestProvideWithoutWatch(t *testing.T) { for _, test := range getTestCases() { t.Run(test.desc+" without watch", func(t *testing.T) { - provider, clean := createProvider(t, test, false) - defer clean() + provider := createProvider(t, test, false) configChan := make(chan dynamic.Message) provider.DebugLogGeneratedTemplate = true @@ -109,8 +108,7 @@ func TestProvideWithoutWatch(t *testing.T) { func TestProvideWithWatch(t *testing.T) { for _, test := range getTestCases() { t.Run(test.desc+" with watch", func(t *testing.T) { - provider, clean := createProvider(t, test, true) - defer clean() + provider := createProvider(t, test, true) configChan := make(chan dynamic.Message) go func() { @@ -244,7 +242,9 @@ func getTestCases() []ProvideTestCase { } } -func createProvider(t *testing.T, test ProvideTestCase, watch bool) (*Provider, func()) { +func createProvider(t *testing.T, test ProvideTestCase, watch bool) *Provider { + t.Helper() + tempDir := createTempDir(t, "testdir") provider := &Provider{} @@ -276,9 +276,11 @@ func createProvider(t *testing.T, test ProvideTestCase, watch bool) (*Provider, provider.Filename = file.Name() } - return provider, func() { + t.Cleanup(func() { os.RemoveAll(tempDir) - } + }) + + return provider } // createTempDir Helper. diff --git a/pkg/server/server_entrypoint_tcp_test.go b/pkg/server/server_entrypoint_tcp_test.go index 6c76c768d..a8845d16b 100644 --- a/pkg/server/server_entrypoint_tcp_test.go +++ b/pkg/server/server_entrypoint_tcp_test.go @@ -28,6 +28,7 @@ func TestShutdownHijacked(t *testing.T) { err = resp.Write(conn) require.NoError(t, err) })) + testShutdown(t, router) } @@ -37,6 +38,7 @@ func TestShutdownHTTP(t *testing.T) { rw.WriteHeader(http.StatusOK) time.Sleep(time.Second) })) + testShutdown(t, router) } @@ -61,6 +63,8 @@ func TestShutdownTCP(t *testing.T) { } func testShutdown(t *testing.T, router *tcp.Router) { + t.Helper() + epConfig := &static.EntryPointsTransport{} epConfig.SetDefaults() diff --git a/pkg/server/server_entrypoint_udp_test.go b/pkg/server/server_entrypoint_udp_test.go index 00a6ba8cd..2aa5e70d8 100644 --- a/pkg/server/server_entrypoint_udp_test.go +++ b/pkg/server/server_entrypoint_udp_test.go @@ -103,6 +103,8 @@ func TestShutdownUDPConn(t *testing.T) { // It fatals if the read blocks longer than timeout, which is useful to detect // regressions that would make a test wait forever. func requireEcho(t *testing.T, data string, conn io.ReadWriter, timeout time.Duration) { + t.Helper() + _, err := conn.Write([]byte(data)) require.NoError(t, err) diff --git a/pkg/server/service/proxy_websocket_test.go b/pkg/server/service/proxy_websocket_test.go index a759cf95a..a03a06666 100644 --- a/pkg/server/service/proxy_websocket_test.go +++ b/pkg/server/service/proxy_websocket_test.go @@ -696,12 +696,16 @@ func (w *websocketRequest) open() (*websocket.Conn, net.Conn, error) { } func parseURI(t *testing.T, uri string) *url.URL { + t.Helper() + out, err := url.ParseRequestURI(uri) require.NoError(t, err) return out } func createProxyWithForwarder(t *testing.T, proxy http.Handler, url string) *httptest.Server { + t.Helper() + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { path := req.URL.Path // keep the original path // Set new backend URL diff --git a/pkg/tcp/proxy_test.go b/pkg/tcp/proxy_test.go index e83017fba..11fdb47c4 100644 --- a/pkg/tcp/proxy_test.go +++ b/pkg/tcp/proxy_test.go @@ -17,6 +17,8 @@ import ( ) func fakeRedis(t *testing.T, listener net.Listener) { + t.Helper() + for { conn, err := listener.Accept() fmt.Println("Accept on server") diff --git a/pkg/udp/conn_test.go b/pkg/udp/conn_test.go index f97eb3958..3f0116e07 100644 --- a/pkg/udp/conn_test.go +++ b/pkg/udp/conn_test.go @@ -171,6 +171,8 @@ func TestTimeoutWithoutRead(t *testing.T) { } func testTimeout(t *testing.T, withRead bool) { + t.Helper() + addr, err := net.ResolveUDPAddr("udp", ":0") require.NoError(t, err) @@ -312,6 +314,8 @@ func TestShutdown(t *testing.T) { // It fatals if the read blocks longer than timeout, // which is useful to detect regressions that would make a test wait forever. func requireEcho(t *testing.T, data string, conn io.ReadWriter, timeout time.Duration) { + t.Helper() + _, err := conn.Write([]byte(data)) require.NoError(t, err) diff --git a/pkg/udp/proxy_test.go b/pkg/udp/proxy_test.go index 509f9aabd..e2995846d 100644 --- a/pkg/udp/proxy_test.go +++ b/pkg/udp/proxy_test.go @@ -41,6 +41,8 @@ func TestUDPProxy(t *testing.T) { } func newServer(t *testing.T, addr string, handler Handler) { + t.Helper() + addrL, err := net.ResolveUDPAddr("udp", addr) require.NoError(t, err)