From 3c15c84fbc28edc52b609b6b624d0ada7ecb3b69 Mon Sep 17 00:00:00 2001 From: Sawjan Gurung Date: Wed, 12 Jul 2023 09:33:10 +0545 Subject: [PATCH] [tests-only][full-ci] Retry ocis startup with ociswrapper (#6768) * (ociswrapper): retry starting ocis if failed * (ociswrapper): kill procces and start again * (ociswrapper): make retry configurable via cli --- tests/ociswrapper/cmd/cmd.go | 2 ++ tests/ociswrapper/ocis/config/config.go | 5 +++-- tests/ociswrapper/ocis/ocis.go | 21 +++++++++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/tests/ociswrapper/cmd/cmd.go b/tests/ociswrapper/cmd/cmd.go index 287be20456..ccd8047f4d 100644 --- a/tests/ociswrapper/cmd/cmd.go +++ b/tests/ociswrapper/cmd/cmd.go @@ -31,6 +31,7 @@ func serveCmd() *cobra.Command { // set configs ocisConfig.Set("bin", cmd.Flag("bin").Value.String()) ocisConfig.Set("url", cmd.Flag("url").Value.String()) + ocisConfig.Set("retry", cmd.Flag("retry").Value.String()) }, } @@ -38,6 +39,7 @@ func serveCmd() *cobra.Command { serveCmd.Flags().SortFlags = false serveCmd.Flags().StringP("bin", "", ocisConfig.Get("bin"), "Full oCIS binary path") serveCmd.Flags().StringP("url", "", ocisConfig.Get("url"), "oCIS server url") + serveCmd.Flags().StringP("retry", "", ocisConfig.Get("retry"), "Number of retries to start oCIS server") serveCmd.Flags().StringP("port", "p", wrapperConfig.Get("port"), "Wrapper API server port") return serveCmd diff --git a/tests/ociswrapper/ocis/config/config.go b/tests/ociswrapper/ocis/config/config.go index 18d012d933..b495db73aa 100644 --- a/tests/ociswrapper/ocis/config/config.go +++ b/tests/ociswrapper/ocis/config/config.go @@ -1,8 +1,9 @@ package config var config = map[string]string{ - "bin": "/usr/bin/ocis", - "url": "https://localhost:9200", + "bin": "/usr/bin/ocis", + "url": "https://localhost:9200", + "retry": "5", } func Set(key string, value string) { diff --git a/tests/ociswrapper/ocis/ocis.go b/tests/ociswrapper/ocis/ocis.go index b38d1b4480..55659263ab 100644 --- a/tests/ociswrapper/ocis/ocis.go +++ b/tests/ociswrapper/ocis/ocis.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "os/exec" + "strconv" "time" "ociswrapper/common" @@ -15,9 +16,12 @@ import ( ) var cmd *exec.Cmd +var retryCount = 0 func Start(envMap map[string]any) { - defer common.Wg.Done() + if retryCount == 0 { + defer common.Wg.Done() + } cmd = exec.Command(config.Get("bin"), "server") cmd.Env = os.Environ() @@ -52,6 +56,15 @@ func Start(envMap map[string]any) { for stdoutScanner.Scan() { m := stdoutScanner.Text() fmt.Println(m) + retryCount++ + + maxRetry, _ := strconv.Atoi(config.Get("retry")) + if retryCount <= maxRetry { + fmt.Println(fmt.Sprintf("Retry starting oCIS server... (retry %v)", retryCount)) + // Stop and start again + Stop() + Start(envMap) + } } } @@ -60,13 +73,16 @@ func Stop() { if err != nil { log.Panic("Cannot kill oCIS server") } + cmd.Wait() } func WaitForConnection() bool { transport := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } + // 5 seconds timeout timeoutValue := 5 * time.Second + client := http.Client{ Timeout: timeoutValue, Transport: transport, @@ -77,7 +93,7 @@ func WaitForConnection() bool { for { select { case <-timeout: - fmt.Println(fmt.Sprintf("Timeout waiting for oCIS server [%f] seconds", timeoutValue.Seconds())) + fmt.Println(fmt.Sprintf("%v seconds timeout waiting for oCIS server", int64(timeoutValue.Seconds()))) return false default: _, err := client.Get(config.Get("url")) @@ -87,6 +103,7 @@ func WaitForConnection() bool { fmt.Println(fmt.Sprintf("oCIS server is ready to accept requests")) return true } + // 500 milliseconds poll interval time.Sleep(500 * time.Millisecond) } }