From 733f1852eab1ecb383398d359b377e074d08fbcb Mon Sep 17 00:00:00 2001 From: Saw-jan Date: Fri, 5 Jul 2024 15:08:45 +0545 Subject: [PATCH] feat: add start endpoint --- tests/ociswrapper/ocis/ocis.go | 7 +++++++ tests/ociswrapper/wrapper/handlers/handler.go | 21 ++++++++++++++++++- tests/ociswrapper/wrapper/wrapper.go | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/ociswrapper/ocis/ocis.go b/tests/ociswrapper/ocis/ocis.go index 32d51602ed..2957294b7b 100644 --- a/tests/ociswrapper/ocis/ocis.go +++ b/tests/ociswrapper/ocis/ocis.go @@ -138,6 +138,13 @@ func Restart(envMap map[string]any) (bool, string) { return WaitForConnection() } +func IsOcisRunning() bool { + if cmd != nil { + return cmd.Process.Pid > 0 + } + return false +} + func waitAllServices(startTime time.Time, timeout time.Duration) { timeoutS := timeout * time.Second diff --git a/tests/ociswrapper/wrapper/handlers/handler.go b/tests/ociswrapper/wrapper/handlers/handler.go index 308bc23437..92db245a95 100644 --- a/tests/ociswrapper/wrapper/handlers/handler.go +++ b/tests/ociswrapper/wrapper/handlers/handler.go @@ -5,6 +5,7 @@ import ( "errors" "io" "net/http" + "ociswrapper/common" "ociswrapper/ocis" ) @@ -115,7 +116,7 @@ func RollbackHandler(res http.ResponseWriter, req *http.Request) { } func StopOcisHandler(res http.ResponseWriter, req *http.Request) { - if req.Method != http.MethodDelete { + if req.Method != http.MethodPost { http.Error(res, "Method not allowed", http.StatusMethodNotAllowed) return } @@ -124,6 +125,24 @@ func StopOcisHandler(res http.ResponseWriter, req *http.Request) { sendResponse(res, success, message) } +func StartOcisHandler(res http.ResponseWriter, req *http.Request) { + if req.Method != http.MethodPost { + http.Error(res, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + if ocis.IsOcisRunning() { + sendResponse(res, false, "oCIS server is already running") + return + } + + common.Wg.Add(1) + go ocis.Start(nil) + + success, message := ocis.WaitForConnection() + sendResponse(res, success, message) +} + func CommandHandler(res http.ResponseWriter, req *http.Request) { if req.Method != http.MethodPost { http.Error(res, "Method not allowed", http.StatusMethodNotAllowed) diff --git a/tests/ociswrapper/wrapper/wrapper.go b/tests/ociswrapper/wrapper/wrapper.go index 3ecf94285f..35b7873134 100644 --- a/tests/ociswrapper/wrapper/wrapper.go +++ b/tests/ociswrapper/wrapper/wrapper.go @@ -26,6 +26,7 @@ func Start(port string) { mux.HandleFunc("/rollback", handlers.RollbackHandler) mux.HandleFunc("/command", handlers.CommandHandler) mux.HandleFunc("/stop", handlers.StopOcisHandler) + mux.HandleFunc("/start", handlers.StartOcisHandler) httpServer.Handler = mux