diff --git a/tests/ociswrapper/ocis/ocis.go b/tests/ociswrapper/ocis/ocis.go index 44e85ebd3bd..a867b6b51b9 100644 --- a/tests/ociswrapper/ocis/ocis.go +++ b/tests/ociswrapper/ocis/ocis.go @@ -26,6 +26,7 @@ var cmd *exec.Cmd var retryCount = 0 var stopSignal = false var EnvConfigs = []string{} +var runningCommands = make(map[string]int) // Maps unique IDs to PIDs func Start(envMap []string) { // wait for the log scanner to finish @@ -272,7 +273,7 @@ func RunCommand(command string, inputs []string) (int, string) { return c.ProcessState.ExitCode(), cmdOutput } -func runOcisService(service string) { +func RunOcisService(service string) { // wait for the log scanner to finish var wg sync.WaitGroup wg.Add(2) @@ -282,13 +283,7 @@ func runOcisService(service string) { defer common.Wg.Done() } - cmd = exec.Command(config.Get("bin"), service , "server") -// output, err := cmd.CombinedOutput() -// if err != nil { -// fmt.Printf("Error running service %s: %v\nOutput: %s\n", service, err, output) -// return -// } -// fmt.Printf("Service %s started successfully: %s\n", service, output) + cmd = exec.Command(config.Get("bin"), service, "server") logs, err := cmd.StderrPipe() if err != nil { @@ -307,7 +302,7 @@ func runOcisService(service string) { logScanner := bufio.NewScanner(logs) outputScanner := bufio.NewScanner(output) outChan := make(chan string) - + runningCommands[service] = cmd.Process.Pid // Read the logs when the 'ocis server' command is running go func() { defer wg.Done() @@ -349,8 +344,7 @@ func runOcisService(service string) { log.Println(fmt.Sprintf("Retry starting oCIS server... (retry %v)", retryCount)) // wait 500 milliseconds before retrying time.Sleep(500 * time.Millisecond) - runOcisService(service) - return + RunOcisService(service) } } } @@ -358,3 +352,30 @@ func runOcisService(service string) { wg.Wait() close(outChan) } + +// Stop oCIS service or a specific service by its unique identifier +func StopService(service string) (bool, string) { + pid, exists := runningCommands[service] + if !exists { + return false, fmt.Sprintf("Service %s is not running", service) + } + + // Find the process by PID and send SIGINT to stop it + process, err := os.FindProcess(pid) + if err != nil { + log.Println(fmt.Sprintf("Failed to find process: %v", err)) + return false, fmt.Sprintf("Failed to find process with ID %d", pid) + } + + // Send SIGINT to stop the command + err = process.Signal(syscall.SIGINT) + if err != nil { + log.Println(fmt.Sprintf("Failed to send signal: %v", err)) + return false, fmt.Sprintf("Failed to stop service with ID %s", service) + } + + // Wait for the service to stop + process.Wait() + delete(runningCommands, service) + return true, fmt.Sprintf("Service %s stopped successfully", service) +} diff --git a/tests/ociswrapper/wrapper/handlers/handler.go b/tests/ociswrapper/wrapper/handlers/handler.go index 3d53db839c4..9154fd02ccf 100644 --- a/tests/ociswrapper/wrapper/handlers/handler.go +++ b/tests/ociswrapper/wrapper/handlers/handler.go @@ -9,6 +9,7 @@ import ( "ociswrapper/common" "ociswrapper/ocis" "os" + "strings" ) type BasicResponse struct { @@ -209,25 +210,33 @@ func OcisServiceHandler(res http.ResponseWriter, req *http.Request) { return } - envBody, err := parseJsonBody(req.Body) - if err != nil { - sendResponse(res, http.StatusMethodNotAllowed, "Invalid json body") - return - } + serviceName := strings.TrimPrefix(req.URL.Path, "/services/") - var envMap []string - for key, value := range envBody { - envMap = append(envMap, fmt.Sprintf("%s=%v", key, value)) + if serviceName == "" { + sendResponse(res, http.StatusUnprocessableEntity, "Service name is required") + return } - ocis.EnvConfigs = append(ocis.EnvConfigs, envMap...) + envVar := fmt.Sprintf("OCIS_EXCLUDE_RUN_SERVICES=%s", serviceName) + ocis.EnvConfigs = append(ocis.EnvConfigs, envVar) if req.Method == http.MethodPost { success, _ := ocis.Restart(ocis.EnvConfigs) if success { sendResponse(res, http.StatusOK, "oCIS configured successfully") + ocis.RunOcisService(serviceName) return } sendResponse(res, http.StatusInternalServerError, "Failed to restart oCIS with new configuration") } + if req.Method == http.MethodDelete { + // Attempt to stop the service + success, message := ocis.StopService(serviceName) + if success { + sendResponse(res, http.StatusOK, fmt.Sprintf("Service %s stopped successfully", serviceName)) + } else { + sendResponse(res, http.StatusInternalServerError, message) + } + } + } diff --git a/tests/ociswrapper/wrapper/wrapper.go b/tests/ociswrapper/wrapper/wrapper.go index fda2604d3c0..2c28077cd35 100644 --- a/tests/ociswrapper/wrapper/wrapper.go +++ b/tests/ociswrapper/wrapper/wrapper.go @@ -27,7 +27,7 @@ func Start(port string) { mux.HandleFunc("/command", handlers.CommandHandler) mux.HandleFunc("/stop", handlers.StopOcisHandler) mux.HandleFunc("/start", handlers.StartOcisHandler) - mux.HandleFunc("/services", handlers.OcisServiceHandler) + mux.HandleFunc("/services/", handlers.OcisServiceHandler) httpServer.Handler = mux