2021-07-01 23:08:35 +00:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-10-24 22:21:44 +00:00
|
|
|
"sync"
|
2021-07-01 23:08:35 +00:00
|
|
|
"testing"
|
2021-10-12 06:13:10 +00:00
|
|
|
"time"
|
2021-07-01 23:08:35 +00:00
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/clientaccess"
|
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
2024-06-11 00:29:17 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/etcd/s3"
|
2022-03-15 17:29:56 +00:00
|
|
|
testutil "github.com/k3s-io/k3s/tests"
|
2021-07-01 23:08:35 +00:00
|
|
|
"github.com/robfig/cron/v3"
|
2024-06-11 00:29:17 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2024-10-24 22:21:44 +00:00
|
|
|
"go.etcd.io/etcd/api/v3/etcdserverpb"
|
2021-07-02 19:55:47 +00:00
|
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
2021-11-29 22:46:58 +00:00
|
|
|
"go.etcd.io/etcd/server/v3/etcdserver"
|
2024-10-24 22:21:44 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"google.golang.org/grpc/health"
|
|
|
|
healthpb "google.golang.org/grpc/health/grpc_health_v1"
|
|
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
"google.golang.org/grpc/status"
|
2021-10-12 06:13:10 +00:00
|
|
|
utilnet "k8s.io/apimachinery/pkg/util/net"
|
2021-07-01 23:08:35 +00:00
|
|
|
)
|
|
|
|
|
2024-10-24 22:21:44 +00:00
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2021-10-12 06:13:10 +00:00
|
|
|
func mustGetAddress() string {
|
|
|
|
ipAddr, err := utilnet.ChooseHostInterface()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ipAddr.String()
|
|
|
|
}
|
|
|
|
|
2021-07-01 23:08:35 +00:00
|
|
|
func generateTestConfig() *config.Control {
|
2024-02-16 18:13:33 +00:00
|
|
|
hostname, _ := os.Hostname()
|
2024-02-21 18:26:13 +00:00
|
|
|
containerRuntimeReady := make(chan struct{})
|
|
|
|
close(containerRuntimeReady)
|
2021-11-23 14:46:06 +00:00
|
|
|
criticalControlArgs := config.CriticalControlArgs{
|
|
|
|
ClusterDomain: "cluster.local",
|
|
|
|
ClusterDNS: net.ParseIP("10.43.0.10"),
|
|
|
|
ClusterIPRange: testutil.ClusterIPNet(),
|
|
|
|
FlannelBackend: "vxlan",
|
|
|
|
ServiceIPRange: testutil.ServiceIPNet(),
|
|
|
|
}
|
2021-07-01 23:08:35 +00:00
|
|
|
return &config.Control{
|
2024-02-16 18:13:33 +00:00
|
|
|
ServerNodeName: hostname,
|
2024-02-21 18:26:13 +00:00
|
|
|
Runtime: config.NewRuntime(containerRuntimeReady),
|
2021-07-01 23:08:35 +00:00
|
|
|
HTTPSPort: 6443,
|
|
|
|
SupervisorPort: 6443,
|
|
|
|
AdvertisePort: 6443,
|
|
|
|
DataDir: "/tmp/k3s/", // Different than the default value
|
|
|
|
EtcdSnapshotName: "etcd-snapshot",
|
|
|
|
EtcdSnapshotCron: "0 */12 * * *",
|
|
|
|
EtcdSnapshotRetention: 5,
|
2024-06-11 00:29:17 +00:00
|
|
|
EtcdS3: &config.EtcdS3{
|
|
|
|
Endpoint: "s3.amazonaws.com",
|
|
|
|
Region: "us-east-1",
|
|
|
|
},
|
|
|
|
SANs: []string{"127.0.0.1", mustGetAddress()},
|
|
|
|
CriticalControlArgs: criticalControlArgs,
|
2021-07-01 23:08:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateTestHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
func Test_UnitETCD_IsInitialized(t *testing.T) {
|
2021-07-01 23:08:35 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
config *config.Control
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
setup func(*config.Control) error
|
|
|
|
teardown func(*config.Control) error
|
|
|
|
want bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2024-10-24 22:21:44 +00:00
|
|
|
name: "directory exists",
|
2021-07-01 23:08:35 +00:00
|
|
|
args: args{
|
|
|
|
ctx: context.TODO(),
|
|
|
|
config: generateTestConfig(),
|
|
|
|
},
|
|
|
|
setup: func(cnf *config.Control) error {
|
2021-08-10 18:13:26 +00:00
|
|
|
if err := testutil.GenerateDataDir(cnf); err != nil {
|
2021-07-01 23:08:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.MkdirAll(walDir(cnf), 0700)
|
|
|
|
},
|
|
|
|
teardown: func(cnf *config.Control) error {
|
2021-08-10 18:13:26 +00:00
|
|
|
testutil.CleanupDataDir(cnf)
|
2021-07-01 23:08:35 +00:00
|
|
|
return os.Remove(walDir(cnf))
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2024-10-24 22:21:44 +00:00
|
|
|
name: "directory does not exist",
|
2021-07-01 23:08:35 +00:00
|
|
|
args: args{
|
|
|
|
ctx: context.TODO(),
|
|
|
|
config: generateTestConfig(),
|
|
|
|
},
|
|
|
|
setup: func(cnf *config.Control) error {
|
2021-08-10 18:13:26 +00:00
|
|
|
if err := testutil.GenerateDataDir(cnf); err != nil {
|
2021-07-01 23:08:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// We don't care if removal fails to find the dir
|
|
|
|
os.Remove(walDir(cnf))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
teardown: func(cnf *config.Control) error {
|
2021-08-10 18:13:26 +00:00
|
|
|
testutil.CleanupDataDir(cnf)
|
2021-07-01 23:08:35 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
}
|
2024-06-11 00:29:17 +00:00
|
|
|
|
2021-07-01 23:08:35 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
e := NewETCD()
|
|
|
|
defer tt.teardown(tt.args.config)
|
|
|
|
if err := tt.setup(tt.args.config); err != nil {
|
|
|
|
t.Errorf("Prep for ETCD.IsInitialized() failed = %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-09-21 18:53:50 +00:00
|
|
|
if err := e.SetControlConfig(tt.args.config); err != nil {
|
|
|
|
t.Errorf("ETCD.SetControlConfig() failed= %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
got, err := e.IsInitialized()
|
2021-07-01 23:08:35 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("ETCD.IsInitialized() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("ETCD.IsInitialized() = %+v\nWant = %+v", got, tt.want)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
func Test_UnitETCD_Register(t *testing.T) {
|
2021-07-01 23:08:35 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
config *config.Control
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
setup func(cnf *config.Control) error
|
|
|
|
teardown func(cnf *config.Control) error
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2024-10-24 22:21:44 +00:00
|
|
|
name: "standard config",
|
2021-07-01 23:08:35 +00:00
|
|
|
args: args{
|
|
|
|
ctx: context.TODO(),
|
|
|
|
config: generateTestConfig(),
|
|
|
|
handler: generateTestHandler(),
|
|
|
|
},
|
|
|
|
setup: func(cnf *config.Control) error {
|
2021-08-10 18:13:26 +00:00
|
|
|
return testutil.GenerateRuntime(cnf)
|
2021-07-01 23:08:35 +00:00
|
|
|
},
|
|
|
|
teardown: func(cnf *config.Control) error {
|
2021-08-10 18:13:26 +00:00
|
|
|
testutil.CleanupDataDir(cnf)
|
2021-07-01 23:08:35 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-10-24 22:21:44 +00:00
|
|
|
name: "with a tombstone file created",
|
2021-07-01 23:08:35 +00:00
|
|
|
args: args{
|
|
|
|
ctx: context.TODO(),
|
|
|
|
config: generateTestConfig(),
|
|
|
|
handler: generateTestHandler(),
|
|
|
|
},
|
|
|
|
setup: func(cnf *config.Control) error {
|
2021-08-10 18:13:26 +00:00
|
|
|
if err := testutil.GenerateRuntime(cnf); err != nil {
|
2021-07-01 23:08:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-09-21 18:48:21 +00:00
|
|
|
if err := os.MkdirAll(dbDir(cnf), 0700); err != nil {
|
2021-07-01 23:08:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-09-21 18:48:21 +00:00
|
|
|
tombstoneFile := filepath.Join(dbDir(cnf), "tombstone")
|
2021-07-01 23:08:35 +00:00
|
|
|
if _, err := os.Create(tombstoneFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
teardown: func(cnf *config.Control) error {
|
2023-09-21 18:48:21 +00:00
|
|
|
tombstoneFile := filepath.Join(dbDir(cnf), "tombstone")
|
2021-07-01 23:08:35 +00:00
|
|
|
os.Remove(tombstoneFile)
|
2021-08-10 18:13:26 +00:00
|
|
|
testutil.CleanupDataDir(cnf)
|
2021-07-01 23:08:35 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
e := NewETCD()
|
|
|
|
|
|
|
|
defer tt.teardown(tt.args.config)
|
|
|
|
if err := tt.setup(tt.args.config); err != nil {
|
|
|
|
t.Errorf("Setup for ETCD.Register() failed = %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-09-21 18:53:50 +00:00
|
|
|
if err := e.SetControlConfig(tt.args.config); err != nil {
|
|
|
|
t.Errorf("ETCD.SetControlConfig() failed = %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err := e.Register(tt.args.handler)
|
2021-07-01 23:08:35 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("ETCD.Register() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
func Test_UnitETCD_Start(t *testing.T) {
|
2021-10-12 06:13:10 +00:00
|
|
|
type contextInfo struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
2021-07-01 23:08:35 +00:00
|
|
|
type fields struct {
|
2021-10-12 06:13:10 +00:00
|
|
|
context contextInfo
|
2021-07-02 19:55:47 +00:00
|
|
|
client *clientv3.Client
|
2021-07-01 23:08:35 +00:00
|
|
|
config *config.Control
|
|
|
|
name string
|
|
|
|
address string
|
|
|
|
cron *cron.Cron
|
2024-06-11 00:29:17 +00:00
|
|
|
s3 *s3.Controller
|
2021-07-01 23:08:35 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
clientAccessInfo *clientaccess.Info
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
2021-11-29 22:46:58 +00:00
|
|
|
setup func(e *ETCD, ctxInfo *contextInfo) error
|
|
|
|
teardown func(e *ETCD, ctxInfo *contextInfo) error
|
2021-07-01 23:08:35 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2024-10-24 22:21:44 +00:00
|
|
|
name: "nil clientAccessInfo and nil cron",
|
2021-07-01 23:08:35 +00:00
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
2021-10-12 06:13:10 +00:00
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
2021-07-01 23:08:35 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
clientAccessInfo: nil,
|
|
|
|
},
|
2021-11-29 22:46:58 +00:00
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
2021-10-12 06:13:10 +00:00
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
2021-11-29 22:46:58 +00:00
|
|
|
e.config.EtcdDisableSnapshots = true
|
|
|
|
testutil.GenerateRuntime(e.config)
|
2023-09-21 18:53:50 +00:00
|
|
|
return nil
|
2021-07-01 23:08:35 +00:00
|
|
|
},
|
2021-11-29 22:46:58 +00:00
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
// RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes
|
2024-10-24 22:21:44 +00:00
|
|
|
err := e.RemoveSelf(ctxInfo.ctx)
|
2021-10-12 06:13:10 +00:00
|
|
|
ctxInfo.cancel()
|
2024-10-24 22:21:44 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2021-11-29 22:46:58 +00:00
|
|
|
testutil.CleanupDataDir(e.config)
|
2024-10-24 22:21:44 +00:00
|
|
|
if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-01 23:08:35 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-10-24 22:21:44 +00:00
|
|
|
name: "nil clientAccessInfo",
|
2021-07-01 23:08:35 +00:00
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
2021-10-12 06:13:10 +00:00
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
2021-07-01 23:08:35 +00:00
|
|
|
cron: cron.New(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
clientAccessInfo: nil,
|
|
|
|
},
|
2021-11-29 22:46:58 +00:00
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
2021-10-12 06:13:10 +00:00
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
2021-11-29 22:46:58 +00:00
|
|
|
testutil.GenerateRuntime(e.config)
|
2023-09-21 18:53:50 +00:00
|
|
|
return nil
|
2021-07-01 23:08:35 +00:00
|
|
|
},
|
2021-11-29 22:46:58 +00:00
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
// RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes
|
2024-10-24 22:21:44 +00:00
|
|
|
err := e.RemoveSelf(ctxInfo.ctx)
|
2021-10-12 06:13:10 +00:00
|
|
|
ctxInfo.cancel()
|
|
|
|
time.Sleep(5 * time.Second)
|
2021-11-29 22:46:58 +00:00
|
|
|
testutil.CleanupDataDir(e.config)
|
2024-10-24 22:21:44 +00:00
|
|
|
if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-01 23:08:35 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-10-24 22:21:44 +00:00
|
|
|
name: "existing cluster",
|
2021-07-01 23:08:35 +00:00
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
2021-10-12 06:13:10 +00:00
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
2021-07-01 23:08:35 +00:00
|
|
|
cron: cron.New(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
clientAccessInfo: nil,
|
|
|
|
},
|
2021-11-29 22:46:58 +00:00
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
2021-10-12 06:13:10 +00:00
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
2021-11-29 22:46:58 +00:00
|
|
|
if err := testutil.GenerateRuntime(e.config); err != nil {
|
2021-07-01 23:08:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-29 22:46:58 +00:00
|
|
|
return os.MkdirAll(walDir(e.config), 0700)
|
2021-07-01 23:08:35 +00:00
|
|
|
},
|
2021-11-29 22:46:58 +00:00
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
// RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes
|
2024-10-24 22:21:44 +00:00
|
|
|
err := e.RemoveSelf(ctxInfo.ctx)
|
2021-10-12 06:13:10 +00:00
|
|
|
ctxInfo.cancel()
|
|
|
|
time.Sleep(5 * time.Second)
|
2021-11-29 22:46:58 +00:00
|
|
|
testutil.CleanupDataDir(e.config)
|
|
|
|
os.Remove(walDir(e.config))
|
2024-10-24 22:21:44 +00:00
|
|
|
if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-01 23:08:35 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
e := &ETCD{
|
|
|
|
client: tt.fields.client,
|
|
|
|
config: tt.fields.config,
|
|
|
|
name: tt.fields.name,
|
|
|
|
address: tt.fields.address,
|
|
|
|
cron: tt.fields.cron,
|
|
|
|
s3: tt.fields.s3,
|
|
|
|
}
|
2021-11-29 22:46:58 +00:00
|
|
|
|
|
|
|
if err := tt.setup(e, &tt.fields.context); err != nil {
|
2021-07-01 23:08:35 +00:00
|
|
|
t.Errorf("Setup for ETCD.Start() failed = %v", err)
|
|
|
|
return
|
|
|
|
}
|
2021-10-12 06:13:10 +00:00
|
|
|
if err := e.Start(tt.fields.context.ctx, tt.args.clientAccessInfo); (err != nil) != tt.wantErr {
|
2021-07-01 23:08:35 +00:00
|
|
|
t.Errorf("ETCD.Start() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
2021-11-29 22:46:58 +00:00
|
|
|
if err := tt.teardown(e, &tt.fields.context); err != nil {
|
|
|
|
t.Errorf("Teardown for ETCD.Start() failed = %v", err)
|
2024-10-24 22:21:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_UnitETCD_Test(t *testing.T) {
|
|
|
|
type contextInfo struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
|
|
|
type fields struct {
|
|
|
|
context contextInfo
|
|
|
|
client *clientv3.Client
|
|
|
|
config *config.Control
|
|
|
|
name string
|
|
|
|
address string
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
clientAccessInfo *clientaccess.Info
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
setup func(e *ETCD, ctxInfo *contextInfo) error
|
|
|
|
teardown func(e *ETCD, ctxInfo *contextInfo) error
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no server running",
|
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
|
|
|
},
|
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
|
|
|
testutil.GenerateRuntime(e.config)
|
|
|
|
return e.startClient(ctxInfo.ctx)
|
|
|
|
},
|
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.cancel()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
testutil.CleanupDataDir(e.config)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unreachable server",
|
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
|
|
|
},
|
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
|
|
|
testutil.GenerateRuntime(e.config)
|
|
|
|
e.config.Runtime.EtcdConfig.Endpoints = []string{"https://192.0.2.0:2379"} // RFC5737
|
|
|
|
return e.startClient(ctxInfo.ctx)
|
|
|
|
},
|
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.cancel()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
testutil.CleanupDataDir(e.config)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "learner server",
|
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
|
|
|
},
|
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
|
|
|
testutil.GenerateRuntime(e.config)
|
|
|
|
if err := startMock(ctxInfo.ctx, e, true, false, false, time.Second); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.startClient(ctxInfo.ctx)
|
|
|
|
},
|
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.cancel()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
testutil.CleanupDataDir(e.config)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "corrupt server",
|
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
|
|
|
},
|
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
|
|
|
testutil.GenerateRuntime(e.config)
|
|
|
|
if err := startMock(ctxInfo.ctx, e, false, true, false, time.Second); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.startClient(ctxInfo.ctx)
|
|
|
|
},
|
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.cancel()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
testutil.CleanupDataDir(e.config)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "leaderless server",
|
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
|
|
|
},
|
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
|
|
|
testutil.GenerateRuntime(e.config)
|
|
|
|
if err := startMock(ctxInfo.ctx, e, false, false, true, time.Second); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.startClient(ctxInfo.ctx)
|
|
|
|
},
|
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.cancel()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
testutil.CleanupDataDir(e.config)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "normal server",
|
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
|
|
|
},
|
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
|
|
|
testutil.GenerateRuntime(e.config)
|
|
|
|
if err := startMock(ctxInfo.ctx, e, false, false, false, time.Second); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.startClient(ctxInfo.ctx)
|
|
|
|
},
|
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.cancel()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
testutil.CleanupDataDir(e.config)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alarm on other server",
|
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
|
|
|
},
|
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
|
|
|
testutil.GenerateRuntime(e.config)
|
|
|
|
extraAlarm := &etcdserverpb.AlarmMember{MemberID: 2, Alarm: etcdserverpb.AlarmType_NOSPACE}
|
|
|
|
if err := startMock(ctxInfo.ctx, e, false, false, false, time.Second, extraAlarm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.startClient(ctxInfo.ctx)
|
|
|
|
},
|
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.cancel()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
testutil.CleanupDataDir(e.config)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "slow defrag",
|
|
|
|
fields: fields{
|
|
|
|
config: generateTestConfig(),
|
|
|
|
address: mustGetAddress(),
|
|
|
|
name: "default",
|
|
|
|
},
|
|
|
|
setup: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.ctx, ctxInfo.cancel = context.WithCancel(context.Background())
|
|
|
|
testutil.GenerateRuntime(e.config)
|
|
|
|
if err := startMock(ctxInfo.ctx, e, false, false, false, 40*time.Second); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.startClient(ctxInfo.ctx)
|
|
|
|
},
|
|
|
|
teardown: func(e *ETCD, ctxInfo *contextInfo) error {
|
|
|
|
ctxInfo.cancel()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
testutil.CleanupDataDir(e.config)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
e := &ETCD{
|
|
|
|
client: tt.fields.client,
|
|
|
|
config: tt.fields.config,
|
|
|
|
name: tt.fields.name,
|
|
|
|
address: tt.fields.address,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tt.setup(e, &tt.fields.context); err != nil {
|
|
|
|
t.Errorf("Setup for ETCD.Test() failed = %v", err)
|
2021-11-29 22:46:58 +00:00
|
|
|
return
|
|
|
|
}
|
2024-10-24 22:21:44 +00:00
|
|
|
start := time.Now()
|
|
|
|
err := e.Test(tt.fields.context.ctx)
|
|
|
|
duration := time.Now().Sub(start)
|
|
|
|
t.Logf("ETCD.Test() completed in %v with err=%v", duration, err)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("ETCD.Test() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
if err := tt.teardown(e, &tt.fields.context); err != nil {
|
|
|
|
t.Errorf("Teardown for ETCD.Test() failed = %v", err)
|
|
|
|
}
|
2021-07-01 23:08:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-10-24 22:21:44 +00:00
|
|
|
|
|
|
|
// startMock starts up a mock etcd grpc service with canned responses
|
|
|
|
// that can be used to test specific scenarios.
|
|
|
|
func startMock(ctx context.Context, e *ETCD, isLearner, isCorrupt, noLeader bool, defragDelay time.Duration, extraAlarms ...*etcdserverpb.AlarmMember) error {
|
|
|
|
address := authority(getEndpoints(e.config)[0])
|
|
|
|
// listen on endpoint and close listener on context cancel
|
|
|
|
listener, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up tls if enabled
|
|
|
|
gopts := []grpc.ServerOption{}
|
|
|
|
if e.config.Datastore.ServerTLSConfig.CertFile != "" && e.config.Datastore.ServerTLSConfig.KeyFile != "" {
|
|
|
|
creds, err := credentials.NewServerTLSFromFile(e.config.Datastore.ServerTLSConfig.CertFile, e.config.Datastore.ServerTLSConfig.KeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gopts = append(gopts, grpc.Creds(creds))
|
|
|
|
}
|
|
|
|
server := grpc.NewServer(gopts...)
|
|
|
|
|
|
|
|
mock := &mockEtcd{
|
|
|
|
e: e,
|
|
|
|
mu: &sync.RWMutex{},
|
|
|
|
isLearner: isLearner,
|
|
|
|
isCorrupt: isCorrupt,
|
|
|
|
noLeader: noLeader,
|
|
|
|
defragDelay: defragDelay,
|
|
|
|
extraAlarms: extraAlarms,
|
|
|
|
}
|
|
|
|
|
|
|
|
// register grpc services
|
|
|
|
etcdserverpb.RegisterKVServer(server, mock)
|
|
|
|
etcdserverpb.RegisterClusterServer(server, mock)
|
|
|
|
etcdserverpb.RegisterMaintenanceServer(server, mock)
|
|
|
|
|
|
|
|
hsrv := health.NewServer()
|
|
|
|
hsrv.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
|
|
|
|
healthpb.RegisterHealthServer(server, hsrv)
|
|
|
|
|
|
|
|
reflection.Register(server)
|
|
|
|
|
|
|
|
// shutdown on context cancel
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
server.GracefulStop()
|
|
|
|
listener.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// start serving
|
|
|
|
go func() {
|
|
|
|
logrus.Infof("Mock etcd server starting on %s", listener.Addr())
|
|
|
|
logrus.Infof("Mock etcd server exited: %v", server.Serve(listener))
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockEtcd struct {
|
|
|
|
e *ETCD
|
|
|
|
mu *sync.RWMutex
|
|
|
|
calls map[string]int
|
|
|
|
isLearner bool
|
|
|
|
isCorrupt bool
|
|
|
|
noLeader bool
|
|
|
|
defragDelay time.Duration
|
|
|
|
extraAlarms []*etcdserverpb.AlarmMember
|
|
|
|
}
|
|
|
|
|
|
|
|
// increment call counter for this function
|
|
|
|
func (m *mockEtcd) inc(call string) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if m.calls == nil {
|
|
|
|
m.calls = map[string]int{}
|
|
|
|
}
|
|
|
|
m.calls[call] = m.calls[call] + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// get call counter for this function
|
|
|
|
func (m *mockEtcd) get(call string) int {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
return m.calls[call]
|
|
|
|
}
|
|
|
|
|
|
|
|
// get alarm list
|
|
|
|
func (m *mockEtcd) alarms() []*etcdserverpb.AlarmMember {
|
|
|
|
alarms := m.extraAlarms
|
|
|
|
if m.get("alarm") < 2 {
|
|
|
|
// on the first check, return NOSPACE so that we can clear it after defragging
|
|
|
|
alarms = append(alarms, &etcdserverpb.AlarmMember{
|
|
|
|
Alarm: etcdserverpb.AlarmType_NOSPACE,
|
|
|
|
MemberID: 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if m.isCorrupt {
|
|
|
|
// return CORRUPT if so requested
|
|
|
|
alarms = append(alarms, &etcdserverpb.AlarmMember{
|
|
|
|
Alarm: etcdserverpb.AlarmType_CORRUPT,
|
|
|
|
MemberID: 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return alarms
|
|
|
|
}
|
|
|
|
|
|
|
|
// KV mocks
|
|
|
|
func (m *mockEtcd) Range(context.Context, *etcdserverpb.RangeRequest) (*etcdserverpb.RangeResponse, error) {
|
|
|
|
m.inc("range")
|
|
|
|
return nil, unsupported("range")
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) Put(context.Context, *etcdserverpb.PutRequest) (*etcdserverpb.PutResponse, error) {
|
|
|
|
m.inc("put")
|
|
|
|
return nil, unsupported("put")
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) DeleteRange(context.Context, *etcdserverpb.DeleteRangeRequest) (*etcdserverpb.DeleteRangeResponse, error) {
|
|
|
|
m.inc("deleterange")
|
|
|
|
return nil, unsupported("deleterange")
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) Txn(context.Context, *etcdserverpb.TxnRequest) (*etcdserverpb.TxnResponse, error) {
|
|
|
|
m.inc("txn")
|
|
|
|
return nil, unsupported("txn")
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) Compact(context.Context, *etcdserverpb.CompactionRequest) (*etcdserverpb.CompactionResponse, error) {
|
|
|
|
m.inc("compact")
|
|
|
|
return nil, unsupported("compact")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maintenance mocks
|
|
|
|
func (m *mockEtcd) Alarm(ctx context.Context, r *etcdserverpb.AlarmRequest) (*etcdserverpb.AlarmResponse, error) {
|
|
|
|
m.inc("alarm")
|
|
|
|
res := &etcdserverpb.AlarmResponse{
|
|
|
|
Header: &etcdserverpb.ResponseHeader{
|
|
|
|
MemberId: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if r.Action == etcdserverpb.AlarmRequest_GET {
|
|
|
|
res.Alarms = m.alarms()
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) Status(context.Context, *etcdserverpb.StatusRequest) (*etcdserverpb.StatusResponse, error) {
|
|
|
|
m.inc("status")
|
|
|
|
res := &etcdserverpb.StatusResponse{
|
|
|
|
Header: &etcdserverpb.ResponseHeader{
|
|
|
|
MemberId: 1,
|
|
|
|
},
|
|
|
|
Leader: 1,
|
|
|
|
Version: "v3.5.0-mock0",
|
|
|
|
DbSize: 1024,
|
|
|
|
DbSizeInUse: 512,
|
|
|
|
IsLearner: m.isLearner,
|
|
|
|
}
|
|
|
|
if m.noLeader {
|
|
|
|
res.Leader = 0
|
|
|
|
res.Errors = append(res.Errors, etcdserver.ErrNoLeader.Error())
|
|
|
|
}
|
|
|
|
for _, a := range m.alarms() {
|
|
|
|
res.Errors = append(res.Errors, a.String())
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) Defragment(ctx context.Context, r *etcdserverpb.DefragmentRequest) (*etcdserverpb.DefragmentResponse, error) {
|
|
|
|
m.inc("defragment")
|
|
|
|
// delay defrag response by configured time, or until the request is cancelled
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-time.After(m.defragDelay):
|
|
|
|
}
|
|
|
|
return &etcdserverpb.DefragmentResponse{
|
|
|
|
Header: &etcdserverpb.ResponseHeader{
|
|
|
|
MemberId: 1,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) Hash(context.Context, *etcdserverpb.HashRequest) (*etcdserverpb.HashResponse, error) {
|
|
|
|
m.inc("hash")
|
|
|
|
return nil, unsupported("hash")
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) HashKV(context.Context, *etcdserverpb.HashKVRequest) (*etcdserverpb.HashKVResponse, error) {
|
|
|
|
m.inc("hashkv")
|
|
|
|
return nil, unsupported("hashkv")
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) Snapshot(*etcdserverpb.SnapshotRequest, etcdserverpb.Maintenance_SnapshotServer) error {
|
|
|
|
m.inc("snapshot")
|
|
|
|
return unsupported("snapshot")
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) MoveLeader(context.Context, *etcdserverpb.MoveLeaderRequest) (*etcdserverpb.MoveLeaderResponse, error) {
|
|
|
|
m.inc("moveleader")
|
|
|
|
return nil, unsupported("moveleader")
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) Downgrade(context.Context, *etcdserverpb.DowngradeRequest) (*etcdserverpb.DowngradeResponse, error) {
|
|
|
|
m.inc("downgrade")
|
|
|
|
return nil, unsupported("downgrade")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cluster mocks
|
|
|
|
func (m *mockEtcd) MemberAdd(context.Context, *etcdserverpb.MemberAddRequest) (*etcdserverpb.MemberAddResponse, error) {
|
|
|
|
m.inc("memberadd")
|
|
|
|
return nil, unsupported("memberadd")
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) MemberRemove(context.Context, *etcdserverpb.MemberRemoveRequest) (*etcdserverpb.MemberRemoveResponse, error) {
|
|
|
|
m.inc("memberremove")
|
|
|
|
return nil, etcdserver.ErrNotEnoughStartedMembers
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) MemberUpdate(context.Context, *etcdserverpb.MemberUpdateRequest) (*etcdserverpb.MemberUpdateResponse, error) {
|
|
|
|
m.inc("memberupdate")
|
|
|
|
return nil, unsupported("memberupdate")
|
|
|
|
}
|
|
|
|
func (m *mockEtcd) MemberList(context.Context, *etcdserverpb.MemberListRequest) (*etcdserverpb.MemberListResponse, error) {
|
|
|
|
m.inc("memberlist")
|
|
|
|
scheme := "http"
|
|
|
|
if m.e.config.Datastore.ServerTLSConfig.CertFile != "" {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
return &etcdserverpb.MemberListResponse{
|
|
|
|
Header: &etcdserverpb.ResponseHeader{
|
|
|
|
MemberId: 1,
|
|
|
|
},
|
|
|
|
Members: []*etcdserverpb.Member{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
Name: m.e.name,
|
|
|
|
IsLearner: m.isLearner,
|
|
|
|
ClientURLs: []string{scheme + "://127.0.0.1:2379"},
|
|
|
|
PeerURLs: []string{scheme + "://" + m.e.address + ":2380"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockEtcd) MemberPromote(context.Context, *etcdserverpb.MemberPromoteRequest) (*etcdserverpb.MemberPromoteResponse, error) {
|
|
|
|
m.inc("memberpromote")
|
|
|
|
return nil, unsupported("memberpromote")
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsupported(field string) error {
|
|
|
|
return status.New(codes.Unimplemented, field+" is not implemented").Err()
|
|
|
|
}
|