1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// Copyright 2021 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package integration
import (
"os"
"path/filepath"
"testing"
"go.etcd.io/etcd/client/pkg/v3/testutil"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/server/v3/embed"
"go.etcd.io/etcd/server/v3/verify"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)
var insideTestContext bool
func init() {
}
type testOptions struct {
goLeakDetection bool
skipInShort bool
}
func newTestOptions(opts ...TestOption) *testOptions {
o := &testOptions{goLeakDetection: true, skipInShort: true}
for _, opt := range opts {
opt(o)
}
return o
}
type TestOption func(opt *testOptions)
// WithoutGoLeakDetection disables checking whether a testcase leaked a goroutine.
func WithoutGoLeakDetection() TestOption {
return func(opt *testOptions) { opt.goLeakDetection = false }
}
func WithoutSkipInShort() TestOption {
return func(opt *testOptions) { opt.skipInShort = false }
}
// BeforeTestExternal initializes test context and is targeted for external APIs.
// In general the `integration` package is not targeted to be used outside of
// etcd project, but till the dedicated package is developed, this is
// the best entry point so far (without backward compatibility promise).
func BeforeTestExternal(t testutil.TB) {
BeforeTest(t, WithoutSkipInShort(), WithoutGoLeakDetection())
}
func BeforeTest(t testutil.TB, opts ...TestOption) {
t.Helper()
options := newTestOptions(opts...)
if options.skipInShort {
testutil.SkipTestIfShortMode(t, "Cannot create clusters in --short tests")
}
if options.goLeakDetection {
testutil.RegisterLeakDetection(t)
}
previousWD, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
previousInsideTestContext := insideTestContext
// Registering cleanup early, such it will get executed even if the helper fails.
t.Cleanup(func() {
insideTestContext = previousInsideTestContext
os.Chdir(previousWD)
})
if insideTestContext {
t.Fatal("already in test context. BeforeTest was likely already called")
}
insideTestContext = true
// Integration tests should verify written state as much as possible.
os.Setenv(verify.ENV_VERIFY, verify.ENV_VERIFY_ALL_VALUE)
os.Chdir(t.TempDir())
}
func assertInTestContext(t testutil.TB) {
if !insideTestContext {
t.Errorf("the function can be called only in the test context. Was integration.BeforeTest() called ?")
}
}
func MustAbsPath(path string) string {
abs, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return abs
}
func NewEmbedConfig(t testing.TB, name string) *embed.Config {
cfg := embed.NewConfig()
cfg.Name = name
lg := zaptest.NewLogger(t, zaptest.Level(zapcore.InfoLevel)).Named(cfg.Name)
cfg.ZapLoggerBuilder = embed.NewZapLoggerBuilder(lg)
cfg.Dir = t.TempDir()
return cfg
}
func NewClient(t testing.TB, cfg clientv3.Config) (*clientv3.Client, error) {
if cfg.Logger != nil {
cfg.Logger = zaptest.NewLogger(t)
}
return clientv3.New(cfg)
}
|