Skip to content

Commit

Permalink
Use t.Run on transport helper tests (#3973)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Steffen Siering authored and tsg committed Apr 10, 2017
1 parent 3054154 commit 7604f8f
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions libbeat/outputs/transport/transptest/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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()

Expand All @@ -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))
}

0 comments on commit 7604f8f

Please sign in to comment.