integration: custom etcd gracefully termination

when running integration tests without an external etcd, the framework
spawns an etcd instance executing it in its own process and killing
it once the test stops.

Instead of killing it directly, allow etcd to exit gracefully or kill
it after 5 seconds.
This commit is contained in:
Antonio Ojea 2022-02-09 16:34:06 +01:00
parent a3207872a3
commit 1756fe60d4

View File

@ -26,6 +26,7 @@ import (
"os/exec" "os/exec"
"runtime" "runtime"
"strings" "strings"
"syscall"
"time" "time"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
@ -128,7 +129,18 @@ func RunCustomEtcd(dataDir string, customFlags []string) (url string, stopFn fun
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
stop := func() { stop := func() {
cancel() // try to exit etcd gracefully
defer cancel()
cmd.Process.Signal(syscall.SIGTERM)
go func() {
select {
case <-ctx.Done():
klog.Infof("etcd exited gracefully, context cancelled")
case <-time.After(5 * time.Second):
klog.Infof("etcd didn't exit in 5 seconds, killing it")
cancel()
}
}()
err := cmd.Wait() err := cmd.Wait()
klog.Infof("etcd exit status: %v", err) klog.Infof("etcd exit status: %v", err)
err = os.RemoveAll(etcdDataDir) err = os.RemoveAll(etcdDataDir)