2021-07-01 23:08:35 +00:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"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"
|
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"
|
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"
|
2021-10-12 06:13:10 +00:00
|
|
|
utilnet "k8s.io/apimachinery/pkg/util/net"
|
2021-07-01 23:08:35 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2021-10-12 06:13:10 +00:00
|
|
|
agentReady := make(chan struct{})
|
|
|
|
close(agentReady)
|
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{
|
2023-02-08 00:37:10 +00:00
|
|
|
Runtime: config.NewRuntime(agentReady),
|
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,
|
|
|
|
EtcdS3Endpoint: "s3.amazonaws.com",
|
|
|
|
EtcdS3Region: "us-east-1",
|
2024-01-16 22:43:08 +00:00
|
|
|
SANs: []string{"127.0.0.1", mustGetAddress()},
|
2021-11-23 14:46:06 +00:00
|
|
|
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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Directory exists",
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Directory does not exist",
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Call Register with standard config",
|
|
|
|
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
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Call Register with a tombstone file created",
|
|
|
|
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
|
|
|
|
s3 *S3
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Start etcd without clientAccessInfo and without snapshots",
|
|
|
|
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
|
|
|
|
if err := e.RemoveSelf(ctxInfo.ctx); err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-12 06:13:10 +00:00
|
|
|
ctxInfo.cancel()
|
2021-11-29 22:46:58 +00:00
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
testutil.CleanupDataDir(e.config)
|
2021-07-01 23:08:35 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Start etcd without clientAccessInfo on",
|
|
|
|
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
|
|
|
|
if err := e.RemoveSelf(ctxInfo.ctx); err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() {
|
|
|
|
return err
|
|
|
|
}
|
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)
|
2021-07-01 23:08:35 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Start etcd with an existing cluster",
|
|
|
|
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
|
|
|
|
if err := e.RemoveSelf(ctxInfo.ctx); err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() {
|
|
|
|
return err
|
|
|
|
}
|
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))
|
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)
|
|
|
|
return
|
|
|
|
}
|
2021-07-01 23:08:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|