tests/integration: Simplify the testMain for examples.

We introduce a LazyCluster abstraction (instead of copy-pasted logic)
that makes clusters to be created only if there are runnable tests
in need for the infrastructure.
This commit is contained in:
Piotr Tabor
2020-10-06 13:40:32 +02:00
parent 73b92fe688
commit c5ccebf792
5 changed files with 95 additions and 26 deletions

View File

@@ -34,8 +34,8 @@ func ExampleKeysAPI_directory() {
mockKeysAPI_directory,
func() {
c, err := client.New(client.Config{
Endpoints: exampleEndpoints,
Transport: exampleTransport,
Endpoints: exampleEndpoints(),
Transport: exampleTransport(),
})
if err != nil {
log.Fatal(err)
@@ -85,8 +85,8 @@ func ExampleKeysAPI_setget() {
mockKeysAPI_setget,
func() {
c, err := client.New(client.Config{
Endpoints: exampleEndpoints,
Transport: exampleTransport,
Endpoints: exampleEndpoints(),
Transport: exampleTransport(),
})
if err != nil {
log.Fatal(err)

View File

@@ -15,19 +15,18 @@
package client_test
import (
"fmt"
"net/http"
"os"
"testing"
"time"
"go.etcd.io/etcd/tests/v3/integration"
"go.etcd.io/etcd/v3/pkg/testutil"
"go.etcd.io/etcd/v3/pkg/transport"
)
var exampleEndpoints []string
var exampleTransport *http.Transport
var lazyCluster = integration.NewLazyCluster()
func exampleEndpoints() []string { return lazyCluster.EndpointsV2() }
func exampleTransport() *http.Transport { return lazyCluster.Transport() }
func forUnitTestsRunInMockedContext(mocking func(), example func()) {
// For integration tests runs in the provided environment
@@ -36,22 +35,8 @@ func forUnitTestsRunInMockedContext(mocking func(), example func()) {
// TestMain sets up an etcd cluster if running the examples.
func TestMain(m *testing.M) {
tr, trerr := transport.NewTransport(transport.TLSInfo{}, time.Second)
if trerr != nil {
fmt.Fprintf(os.Stderr, "%v", trerr)
os.Exit(1)
}
cfg := integration.ClusterConfig{Size: 1}
clus := integration.NewClusterV3(nil, &cfg)
exampleEndpoints = []string{clus.Members[0].URL()}
exampleTransport = tr
v := m.Run()
clus.Terminate(nil)
if err := testutil.CheckAfterTest(time.Second); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
lazyCluster.Terminate()
if v == 0 {
testutil.MustCheckLeakedGoroutine()
}