Commit 1179fad7 authored by Brad Davidson's avatar Brad Davidson Committed by Brad Davidson

Fix flakey etcd startup tests

Increase etcd shutdown delay to avoid "bind: address already in use" errors seen in CI. Also uses test TmpDir to ensure dir is cleaned up between tests. Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com>
parent becff1d6
...@@ -49,7 +49,7 @@ func mustGetAddress() string { ...@@ -49,7 +49,7 @@ func mustGetAddress() string {
return ipAddr.String() return ipAddr.String()
} }
func generateTestConfig() *config.Control { func generateTestConfig(t *testing.T) *config.Control {
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
containerRuntimeReady := make(chan struct{}) containerRuntimeReady := make(chan struct{})
close(containerRuntimeReady) close(containerRuntimeReady)
...@@ -66,7 +66,7 @@ func generateTestConfig() *config.Control { ...@@ -66,7 +66,7 @@ func generateTestConfig() *config.Control {
HTTPSPort: 6443, HTTPSPort: 6443,
SupervisorPort: 6443, SupervisorPort: 6443,
AdvertisePort: 6443, AdvertisePort: 6443,
DataDir: "/tmp/k3s/", // Different than the default value DataDir: t.TempDir(),
EtcdSnapshotName: "etcd-snapshot", EtcdSnapshotName: "etcd-snapshot",
EtcdSnapshotCron: "0 */12 * * *", EtcdSnapshotCron: "0 */12 * * *",
EtcdSnapshotReconcile: metav1.Duration{Duration: 10 * time.Minute}, EtcdSnapshotReconcile: metav1.Duration{Duration: 10 * time.Minute},
...@@ -99,7 +99,7 @@ func Test_UnitETCD_IsInitialized(t *testing.T) { ...@@ -99,7 +99,7 @@ func Test_UnitETCD_IsInitialized(t *testing.T) {
{ {
name: "directory exists", name: "directory exists",
args: args{ args: args{
config: generateTestConfig(), config: generateTestConfig(t),
}, },
setup: func(cnf *config.Control) error { setup: func(cnf *config.Control) error {
if err := testutil.GenerateDataDir(cnf); err != nil { if err := testutil.GenerateDataDir(cnf); err != nil {
...@@ -117,7 +117,7 @@ func Test_UnitETCD_IsInitialized(t *testing.T) { ...@@ -117,7 +117,7 @@ func Test_UnitETCD_IsInitialized(t *testing.T) {
{ {
name: "directory does not exist", name: "directory does not exist",
args: args{ args: args{
config: generateTestConfig(), config: generateTestConfig(t),
}, },
setup: func(cnf *config.Control) error { setup: func(cnf *config.Control) error {
if err := testutil.GenerateDataDir(cnf); err != nil { if err := testutil.GenerateDataDir(cnf); err != nil {
...@@ -178,7 +178,7 @@ func Test_UnitETCD_Register(t *testing.T) { ...@@ -178,7 +178,7 @@ func Test_UnitETCD_Register(t *testing.T) {
{ {
name: "standard config", name: "standard config",
args: args{ args: args{
config: generateTestConfig(), config: generateTestConfig(t),
handler: generateTestHandler(), handler: generateTestHandler(),
}, },
setup: func(cnf *config.Control) error { setup: func(cnf *config.Control) error {
...@@ -192,7 +192,7 @@ func Test_UnitETCD_Register(t *testing.T) { ...@@ -192,7 +192,7 @@ func Test_UnitETCD_Register(t *testing.T) {
{ {
name: "with a tombstone file created", name: "with a tombstone file created",
args: args{ args: args{
config: generateTestConfig(), config: generateTestConfig(t),
handler: generateTestHandler(), handler: generateTestHandler(),
}, },
setup: func(cnf *config.Control) error { setup: func(cnf *config.Control) error {
...@@ -283,7 +283,7 @@ func Test_UnitETCD_Start(t *testing.T) { ...@@ -283,7 +283,7 @@ func Test_UnitETCD_Start(t *testing.T) {
{ {
name: "nil clientAccessInfo and nil cron", name: "nil clientAccessInfo and nil cron",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
}, },
...@@ -297,7 +297,7 @@ func Test_UnitETCD_Start(t *testing.T) { ...@@ -297,7 +297,7 @@ func Test_UnitETCD_Start(t *testing.T) {
// RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes // RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes
err := e.RemoveSelf(ctxInfo.ctx) err := e.RemoveSelf(ctxInfo.ctx)
ctxInfo.cancel() ctxInfo.cancel()
time.Sleep(5 * time.Second) time.Sleep(10 * time.Second)
testutil.CleanupDataDir(e.config) testutil.CleanupDataDir(e.config)
if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() { if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() {
return err return err
...@@ -308,7 +308,7 @@ func Test_UnitETCD_Start(t *testing.T) { ...@@ -308,7 +308,7 @@ func Test_UnitETCD_Start(t *testing.T) {
{ {
name: "nil clientAccessInfo", name: "nil clientAccessInfo",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
cron: cron.New(), cron: cron.New(),
...@@ -322,7 +322,7 @@ func Test_UnitETCD_Start(t *testing.T) { ...@@ -322,7 +322,7 @@ func Test_UnitETCD_Start(t *testing.T) {
// RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes // RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes
err := e.RemoveSelf(ctxInfo.ctx) err := e.RemoveSelf(ctxInfo.ctx)
ctxInfo.cancel() ctxInfo.cancel()
time.Sleep(5 * time.Second) time.Sleep(10 * time.Second)
testutil.CleanupDataDir(e.config) testutil.CleanupDataDir(e.config)
if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() { if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() {
return err return err
...@@ -333,7 +333,7 @@ func Test_UnitETCD_Start(t *testing.T) { ...@@ -333,7 +333,7 @@ func Test_UnitETCD_Start(t *testing.T) {
{ {
name: "valid clientAccessInfo", name: "valid clientAccessInfo",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
cron: cron.New(), cron: cron.New(),
...@@ -354,7 +354,7 @@ func Test_UnitETCD_Start(t *testing.T) { ...@@ -354,7 +354,7 @@ func Test_UnitETCD_Start(t *testing.T) {
// RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes // RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes
err := e.RemoveSelf(ctxInfo.ctx) err := e.RemoveSelf(ctxInfo.ctx)
ctxInfo.cancel() ctxInfo.cancel()
time.Sleep(5 * time.Second) time.Sleep(10 * time.Second)
testutil.CleanupDataDir(e.config) testutil.CleanupDataDir(e.config)
if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() { if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() {
return err return err
...@@ -365,7 +365,7 @@ func Test_UnitETCD_Start(t *testing.T) { ...@@ -365,7 +365,7 @@ func Test_UnitETCD_Start(t *testing.T) {
{ {
name: "existing cluster", name: "existing cluster",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
cron: cron.New(), cron: cron.New(),
...@@ -381,7 +381,7 @@ func Test_UnitETCD_Start(t *testing.T) { ...@@ -381,7 +381,7 @@ func Test_UnitETCD_Start(t *testing.T) {
// RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes // RemoveSelf will fail with a specific error, but it still does cleanup for testing purposes
err := e.RemoveSelf(ctxInfo.ctx) err := e.RemoveSelf(ctxInfo.ctx)
ctxInfo.cancel() ctxInfo.cancel()
time.Sleep(5 * time.Second) time.Sleep(10 * time.Second)
testutil.CleanupDataDir(e.config) testutil.CleanupDataDir(e.config)
os.Remove(walDir(e.config)) os.Remove(walDir(e.config))
if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() { if err != nil && err.Error() != etcdserver.ErrNotEnoughStartedMembers.Error() {
...@@ -453,7 +453,7 @@ func Test_UnitETCD_Test(t *testing.T) { ...@@ -453,7 +453,7 @@ func Test_UnitETCD_Test(t *testing.T) {
{ {
name: "no server running", name: "no server running",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
}, },
...@@ -473,7 +473,7 @@ func Test_UnitETCD_Test(t *testing.T) { ...@@ -473,7 +473,7 @@ func Test_UnitETCD_Test(t *testing.T) {
{ {
name: "unreachable server", name: "unreachable server",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
}, },
...@@ -494,7 +494,7 @@ func Test_UnitETCD_Test(t *testing.T) { ...@@ -494,7 +494,7 @@ func Test_UnitETCD_Test(t *testing.T) {
{ {
name: "learner server", name: "learner server",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
}, },
...@@ -517,7 +517,7 @@ func Test_UnitETCD_Test(t *testing.T) { ...@@ -517,7 +517,7 @@ func Test_UnitETCD_Test(t *testing.T) {
{ {
name: "corrupt server", name: "corrupt server",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
}, },
...@@ -540,7 +540,7 @@ func Test_UnitETCD_Test(t *testing.T) { ...@@ -540,7 +540,7 @@ func Test_UnitETCD_Test(t *testing.T) {
{ {
name: "leaderless server", name: "leaderless server",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
}, },
...@@ -563,7 +563,7 @@ func Test_UnitETCD_Test(t *testing.T) { ...@@ -563,7 +563,7 @@ func Test_UnitETCD_Test(t *testing.T) {
{ {
name: "normal server", name: "normal server",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
}, },
...@@ -586,7 +586,7 @@ func Test_UnitETCD_Test(t *testing.T) { ...@@ -586,7 +586,7 @@ func Test_UnitETCD_Test(t *testing.T) {
{ {
name: "alarm on other server", name: "alarm on other server",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
}, },
...@@ -610,7 +610,7 @@ func Test_UnitETCD_Test(t *testing.T) { ...@@ -610,7 +610,7 @@ func Test_UnitETCD_Test(t *testing.T) {
{ {
name: "slow defrag", name: "slow defrag",
fields: fields{ fields: fields{
config: generateTestConfig(), config: generateTestConfig(t),
address: mustGetAddress(), address: mustGetAddress(),
name: "default", name: "default",
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment