From 7604f8fd0c847bcd21914d4f9c9e88e7e793665c Mon Sep 17 00:00:00 2001 From: Steffen Siering Date: Mon, 10 Apr 2017 22:06:27 +0200 Subject: [PATCH] Use t.Run on transport helper tests (#3973) Currently up to 4 tests (tls/tcp with and without socks5 proxy) are run within one unit test. If one fails, no hint which of these 4 tests did fail is given. Plus, `-test.run` option can not be used to select particular tests. Using t.Run ensures each sub-test has a name for reporting and filtering purposes. --- .../transport/transptest/testing_test.go | 60 ++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/libbeat/outputs/transport/transptest/testing_test.go b/libbeat/outputs/transport/transptest/testing_test.go index 83e51162166b..5ddef996c585 100644 --- a/libbeat/outputs/transport/transptest/testing_test.go +++ b/libbeat/outputs/transport/transptest/testing_test.go @@ -54,7 +54,7 @@ func TestTransportReconnectsOnConnect(t *testing.T) { timeout := 2 * time.Second GenCertsForIPIfMIssing(t, net.IP{127, 0, 0, 1}, certName) - run := func(makeServer MockServerFactory, proxy *transport.ProxyConfig) { + testServer(t, &config, func(t *testing.T, makeServer MockServerFactory, proxy *transport.ProxyConfig) { server := makeServer(t, timeout, certName, proxy) defer server.Close() @@ -81,12 +81,7 @@ func TestTransportReconnectsOnConnect(t *testing.T) { } transp.Close() - } - - run(NewMockServerTCP, nil) - run(NewMockServerTLS, nil) - run(NewMockServerTCP, &config) - run(NewMockServerTLS, &config) + }) } func TestTransportFailConnectUnknownAddress(t *testing.T) { @@ -98,21 +93,28 @@ func TestTransportFailConnectUnknownAddress(t *testing.T) { invalidAddr := "invalid.dns.fqdn-unknown.invalid:100" - run := func(makeTransp TransportFactory, proxy *transport.ProxyConfig) { - transp, err := makeTransp(invalidAddr, proxy) - if err != nil { - t.Fatalf("failed to generate transport client: %v", err) + run := func(makeTransp TransportFactory, proxy *transport.ProxyConfig) func(*testing.T) { + return func(t *testing.T) { + transp, err := makeTransp(invalidAddr, proxy) + if err != nil { + t.Fatalf("failed to generate transport client: %v", err) + } + + err = transp.Connect() + assert.NotNil(t, err) } + } - err = transp.Connect() - assert.NotNil(t, err) + factoryTests := func(f TransportFactory) func(*testing.T) { + return func(t *testing.T) { + t.Run("connect", run(f, nil)) + t.Run("socks5", run(f, &config)) + } } timeout := 100 * time.Millisecond - run(connectTCP(timeout), nil) - run(connectTLS(timeout, certName), nil) - run(connectTCP(timeout), &config) - run(connectTLS(timeout, certName), &config) + t.Run("tcp", factoryTests(connectTCP(timeout))) + t.Run("tls", factoryTests(connectTLS(timeout, certName))) } func TestTransportClosedOnWriteReadError(t *testing.T) { @@ -123,7 +125,7 @@ func TestTransportClosedOnWriteReadError(t *testing.T) { timeout := 2 * time.Second GenCertsForIPIfMIssing(t, net.IP{127, 0, 0, 1}, certName) - run := func(makeServer MockServerFactory, proxy *transport.ProxyConfig) { + testServer(t, &config, func(t *testing.T, makeServer MockServerFactory, proxy *transport.ProxyConfig) { server := makeServer(t, timeout, certName, proxy) defer server.Close() @@ -137,10 +139,24 @@ func TestTransportClosedOnWriteReadError(t *testing.T) { transp.Write([]byte("test3")) _, err = transp.Read(buf[:]) assert.NotNil(t, err) + }) +} + +func testServer(t *testing.T, config *transport.ProxyConfig, run func(*testing.T, MockServerFactory, *transport.ProxyConfig)) { + + runner := func(f MockServerFactory, c *transport.ProxyConfig) func(t *testing.T) { + return func(t *testing.T) { + run(t, f, config) + } + } + + factoryTests := func(f MockServerFactory) func(t *testing.T) { + return func(t *testing.T) { + t.Run("connect", runner(f, nil)) + t.Run("socks5", runner(f, config)) + } } - run(NewMockServerTCP, nil) - run(NewMockServerTLS, nil) - run(NewMockServerTCP, &config) - run(NewMockServerTLS, &config) + t.Run("tcp", factoryTests(NewMockServerTCP)) + t.Run("tls", factoryTests(NewMockServerTLS)) }