github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/integration/clientv3/examples/main_test.go (about)

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package clientv3_test
    16  
    17  import (
    18  	"log"
    19  	"os"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/lfch/etcd-io/client/pkg/v3/testutil"
    24  	integration2 "github.com/lfch/etcd-io/tests/v3/framework/integration"
    25  	"github.com/lfch/etcd-io/tests/v3/integration"
    26  )
    27  
    28  const (
    29  	dialTimeout    = 5 * time.Second
    30  	requestTimeout = 10 * time.Second
    31  )
    32  
    33  var lazyCluster = integration.NewLazyClusterWithConfig(
    34  	integration2.ClusterConfig{
    35  		Size:                        3,
    36  		WatchProgressNotifyInterval: 200 * time.Millisecond,
    37  		DisableStrictReconfigCheck:  true})
    38  
    39  func exampleEndpoints() []string { return lazyCluster.EndpointsV3() }
    40  
    41  func forUnitTestsRunInMockedContext(_ func(), example func()) {
    42  	// For integration tests runs in the provided environment
    43  	example()
    44  }
    45  
    46  // TestMain sets up an etcd cluster if running the examples.
    47  func TestMain(m *testing.M) {
    48  	testutil.ExitInShortMode("Skipping: the tests require real cluster")
    49  
    50  	tempDir, err := os.MkdirTemp(os.TempDir(), "etcd-integration")
    51  	if err != nil {
    52  		log.Printf("Failed to obtain tempDir: %v", tempDir)
    53  		os.Exit(1)
    54  	}
    55  	defer os.RemoveAll(tempDir)
    56  
    57  	err = os.Chdir(tempDir)
    58  	if err != nil {
    59  		log.Printf("Failed to change working dir to: %s: %v", tempDir, err)
    60  		os.Exit(1)
    61  	}
    62  	log.Printf("Running tests (examples) in dir(%v): ...", tempDir)
    63  	v := m.Run()
    64  	lazyCluster.Terminate()
    65  
    66  	if v == 0 {
    67  		testutil.MustCheckLeakedGoroutine()
    68  	}
    69  	os.Exit(v)
    70  }