| 1 |
/* |
| 2 |
Copyright 2021 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package testserver |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"net" |
| 22 |
"net/url" |
| 23 |
"os" |
| 24 |
"strconv" |
| 25 |
"sync" |
| 26 |
"testing" |
| 27 |
"time" |
| 28 |
|
| 29 |
clientv3 "go.etcd.io/etcd/client/v3" |
| 30 |
"go.etcd.io/etcd/client/v3/kubernetes" |
| 31 |
"go.etcd.io/etcd/server/v3/embed" |
| 32 |
"go.uber.org/zap/zapcore" |
| 33 |
"go.uber.org/zap/zaptest" |
| 34 |
"google.golang.org/grpc" |
| 35 |
) |
| 36 |
|
| 37 |
// getAvailablePort returns a TCP port that is available for binding. |
| 38 |
func getAvailablePorts(count int) ([]int, error) { |
| 39 |
ports := []int{} |
| 40 |
for i := 0; i < count; i++ { |
| 41 |
l, err := net.Listen("tcp", ":0") |
| 42 |
if err != nil { |
| 43 |
return nil, fmt.Errorf("could not bind to a port: %v", err) |
| 44 |
} |
| 45 |
// It is possible but unlikely that someone else will bind this port before we get a chance to use it. |
| 46 |
defer l.Close() |
| 47 |
ports = append(ports, l.Addr().(*net.TCPAddr).Port) |
| 48 |
} |
| 49 |
return ports, nil |
| 50 |
} |
| 51 |
|
| 52 |
// NewTestConfig returns a configuration for an embedded etcd server. |
| 53 |
// The configuration is based on embed.NewConfig(), with the following adjustments: |
| 54 |
// - sets UnsafeNoFsync = true to improve test performance (only reasonable in a test-only |
| 55 |
// single-member server we never intend to restart or keep data from) |
| 56 |
// - uses free ports for client and peer listeners |
| 57 |
// - cleans up the data directory on test termination |
| 58 |
// - silences server logs other than errors |
| 59 |
func NewTestConfig(t testing.TB) *embed.Config { |
| 60 |
cfg := embed.NewConfig() |
| 61 |
|
| 62 |
cfg.UnsafeNoFsync = true |
| 63 |
|
| 64 |
ports, err := getAvailablePorts(2) |
| 65 |
if err != nil { |
| 66 |
t.Fatal(err) |
| 67 |
} |
| 68 |
clientURL := url.URL{Scheme: "http", Host: net.JoinHostPort("localhost", strconv.Itoa(ports[0]))} |
| 69 |
peerURL := url.URL{Scheme: "http", Host: net.JoinHostPort("localhost", strconv.Itoa(ports[1]))} |
| 70 |
|
| 71 |
cfg.ListenPeerUrls = []url.URL{peerURL} |
| 72 |
cfg.AdvertisePeerUrls = []url.URL{peerURL} |
| 73 |
cfg.ListenClientUrls = []url.URL{clientURL} |
| 74 |
cfg.AdvertiseClientUrls = []url.URL{clientURL} |
| 75 |
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name) |
| 76 |
|
| 77 |
cfg.ZapLoggerBuilder = embed.NewZapLoggerBuilder(zaptest.NewLogger(t, zaptest.Level(zapcore.ErrorLevel)).Named("etcd-server")) |
| 78 |
cfg.Dir = t.TempDir() |
| 79 |
os.Chmod(cfg.Dir, 0700) |
| 80 |
return cfg |
| 81 |
} |
| 82 |
|
| 83 |
var autoPortLock sync.Mutex |
| 84 |
|
| 85 |
// RunEtcd starts an embedded etcd server with the provided config |
| 86 |
// (or NewTestConfig(t) if nil), and returns a client connected to the server. |
| 87 |
// The server is terminated when the test ends. |
| 88 |
func RunEtcd(t testing.TB, cfg *embed.Config) *kubernetes.Client { |
| 89 |
t.Helper() |
| 90 |
|
| 91 |
if cfg == nil { |
| 92 |
// if we have to autopick free ports, lock until we successfully start the server on the ports we chose |
| 93 |
autoPortLock.Lock() |
| 94 |
defer autoPortLock.Unlock() |
| 95 |
cfg = NewTestConfig(t) |
| 96 |
} |
| 97 |
|
| 98 |
e, err := embed.StartEtcd(cfg) |
| 99 |
if err != nil { |
| 100 |
t.Fatal(err) |
| 101 |
} |
| 102 |
t.Cleanup(e.Close) |
| 103 |
|
| 104 |
select { |
| 105 |
case <-e.Server.ReadyNotify(): |
| 106 |
case <-time.After(60 * time.Second): |
| 107 |
e.Server.Stop() // trigger a shutdown |
| 108 |
t.Fatal("server took too long to start") |
| 109 |
} |
| 110 |
go func() { |
| 111 |
err := <-e.Err() |
| 112 |
if err != nil { |
| 113 |
t.Error(err) |
| 114 |
} |
| 115 |
}() |
| 116 |
|
| 117 |
tlsConfig, err := cfg.ClientTLSInfo.ClientConfig() |
| 118 |
if err != nil { |
| 119 |
t.Fatal(err) |
| 120 |
} |
| 121 |
|
| 122 |
client, err := kubernetes.New(clientv3.Config{ |
| 123 |
TLS: tlsConfig, |
| 124 |
Endpoints: e.Server.Cluster().ClientURLs(), |
| 125 |
DialTimeout: 10 * time.Second, |
| 126 |
DialOptions: []grpc.DialOption{grpc.WithBlock()}, |
| 127 |
Logger: zaptest.NewLogger(t, zaptest.Level(zapcore.ErrorLevel)).Named("etcd-client"), |
| 128 |
}) |
| 129 |
if err != nil { |
| 130 |
t.Fatal(err) |
| 131 |
} |
| 132 |
return client |
| 133 |
} |