From 5f509bdd8cc32c1b67e7063d8c9b1eed42dfbd1b Mon Sep 17 00:00:00 2001 From: amrita-shrestha Date: Tue, 7 Jan 2025 22:13:27 +0545 Subject: [PATCH] resove conflict --- .../cliCommands/backupConsistency.feature | 2 +- tests/ociswrapper/ocis/ocis.go | 109 ++++++++---------- tests/ociswrapper/wrapper/handlers/handler.go | 2 +- 3 files changed, 47 insertions(+), 66 deletions(-) diff --git a/tests/acceptance/features/cliCommands/backupConsistency.feature b/tests/acceptance/features/cliCommands/backupConsistency.feature index b21aa996041..a83a5d147d4 100644 --- a/tests/acceptance/features/cliCommands/backupConsistency.feature +++ b/tests/acceptance/features/cliCommands/backupConsistency.feature @@ -1,4 +1,4 @@ -@env-config @backup-consistency +@env-config Feature: backup consistency As a user I want to check my data for inconsistencies diff --git a/tests/ociswrapper/ocis/ocis.go b/tests/ociswrapper/ocis/ocis.go index 25610802b89..b4fa033c788 100644 --- a/tests/ociswrapper/ocis/ocis.go +++ b/tests/ociswrapper/ocis/ocis.go @@ -7,9 +7,6 @@ import ( "fmt" "io" "net/http" - "ociswrapper/common" - "ociswrapper/log" - "ociswrapper/ocis/config" "os" "os/exec" "strconv" @@ -18,6 +15,10 @@ import ( "syscall" "time" + "ociswrapper/common" + "ociswrapper/log" + "ociswrapper/ocis/config" + "github.com/creack/pty" ) @@ -25,41 +26,45 @@ var cmd *exec.Cmd var retryCount = 0 var stopSignal = false var EnvConfigs = []string{} -var runningCommands = make(map[string]int) // Maps unique IDs to PIDs +var runningServices = make(map[string]int) func Start(envMap []string) { - StartService("", envMap) + StartService("", envMap) } func Stop() (bool, string) { - log.Println(fmt.Sprintf("Stop ocis check cmd %s\n", cmd)) log.Println("Stopping oCIS server...") stopSignal = true - for listservice, pid := range runningCommands { - log.Println(fmt.Sprintf("Services running before terminating: %s with process and id: %v\n", listservice, pid)) - process, err := os.FindProcess(pid) - err = process.Signal(syscall.SIGINT) - if err != nil { - if !strings.HasSuffix(err.Error(), "process already finished") { - log.Fatalln(err) - } else { - return true, "oCIS server is already stopped" - } - } - process.Wait() + for listservice, pid := range runningServices { + process, err := os.FindProcess(pid) + if err != nil { + log.Println(fmt.Sprintf("Error finding process for service %s: %v", listservice, err)) + continue + } + pKillError := process.Signal(syscall.SIGINT) + if pKillError != nil { + if !strings.HasSuffix(pKillError.Error(), "process already finished") { + log.Fatalln(pKillError) + } else { + return true, "oCIS server is already stopped" + } + } + _, waitErr := process.Wait() + if waitErr != nil { + log.Println(fmt.Sprintf("Error waiting for process to exit: %v\n", waitErr)) + } + delete(runningServices, "ocis") } - cmd = nil success, message := waitUntilCompleteShutdown() + + cmd = nil return success, message } func Restart(envMap []string) (bool, string) { - log.Println(fmt.Sprintf("Restarting ocis check cmd %s\n", cmd)) - log.Println(fmt.Sprintf("Restaring ocis with rollback os environ %s\n", envMap)) - log.Println(fmt.Sprintf("OS environment: %s\n", os.Environ())) - go Stop() + Stop() log.Println("Restarting oCIS server...") common.Wg.Add(1) @@ -69,8 +74,8 @@ func Restart(envMap []string) (bool, string) { } func IsOcisRunning() bool { - if runningCommands["ocis"] > 0 { - return true + if runningServices["ocis"] > 0 { + return true } return false } @@ -136,7 +141,6 @@ func WaitForConnection() (bool, string) { } func waitUntilCompleteShutdown() (bool, string) { - log.Println("Process found. Waiting... waitUntilCompleteShutdown") timeout := 30 * time.Second startTime := time.Now() @@ -207,11 +211,9 @@ func StartService(service string, envMap []string) { cmdArgs := []string{"server"} // Default command args if service != "" { - // Directly append service if provided cmdArgs = append([]string{service}, cmdArgs...) } - // wait for the log scanner to finish var wg sync.WaitGroup wg.Add(2) @@ -220,18 +222,12 @@ func StartService(service string, envMap []string) { defer common.Wg.Done() } - // Initialize the command cmd = exec.Command(config.Get("bin"), cmdArgs...) - // Use the provided envMap if not empty, otherwise use EnvConfigs if len(envMap) == 0 { cmd.Env = append(os.Environ(), EnvConfigs...) - log.Println(fmt.Sprintf("OS environment variables while running ocis service: %s\n", cmd.Env)) - log.Println(fmt.Sprintf("OS environment: %s\n", os.Environ())) } else { cmd.Env = append(os.Environ(), envMap...) - log.Println(fmt.Sprintf("OS environment variables while running ocis service: %s\n", cmd.Env)) - log.Println(fmt.Sprintf("OS environment: %s\n", os.Environ())) } logs, err := cmd.StderrPipe() @@ -243,9 +239,6 @@ func StartService(service string, envMap []string) { log.Panic(err) } -// log.Println(fmt.Sprintf("command env used to start service %s\n", cmd.Env)) -// log.Println(fmt.Sprintf("command used to start service %s\n", cmd)) - err = cmd.Start() if err != nil { @@ -256,17 +249,14 @@ func StartService(service string, envMap []string) { outputScanner := bufio.NewScanner(output) outChan := make(chan string) - // If service is an empty string, set the PID for "ocis" if service == "" { - runningCommands["ocis"] = cmd.Process.Pid + runningServices["ocis"] = cmd.Process.Pid } else { - runningCommands[service] = cmd.Process.Pid + runningServices[service] = cmd.Process.Pid } - log.Println("Started oCIS processes:") - for listservice, pid := range runningCommands { + for listservice, pid := range runningServices { log.Println(fmt.Sprintf("Service started: %s with process and id: %v\n", listservice, pid)) - } // Read the logs when the 'ocis server' command is running @@ -316,45 +306,36 @@ func StartService(service string, envMap []string) { } } } - - log.Println(fmt.Sprintf(" ---- ocis start service ending line---- %s\n", cmd)) wg.Wait() close(outChan) } // Stop oCIS service or a specific service by its unique identifier func StopService(service string) (bool, string) { - for listservice, pid := range runningCommands { - log.Println(fmt.Sprintf("Services running before terminating: %s with process and id: %v\n", listservice, pid)) - } - - pid, exists := runningCommands[service] - log.Println(fmt.Sprintf("Services process id to terminate: %s\n", pid)) + pid, exists := runningServices[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) - log.Println(fmt.Sprintf("Found process to terminate in os: %s\n", 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) } - err = process.Signal(syscall.SIGINT) - log.Println("Process terminated using signal") - if err != nil { - log.Println(fmt.Sprintf("Failed to send signal: %v", err)) + pterr := process.Signal(syscall.SIGINT) + if pterr != nil { return false, fmt.Sprintf("Failed to stop service with PID %d", pid) } + time.Sleep(30 * time.Second) - process.Wait() - log.Println("Process terminating process.wait") - delete(runningCommands, service) - for listservice, pid := range runningCommands { - log.Println(fmt.Sprintf("Service list after deleteing %s service. list contain service: %s with process and id: %v\n", service, listservice, pid)) - } + _, waitErr := process.Wait() + if waitErr != nil { + log.Println(fmt.Sprintf("Error waiting for process to exit: %v\n", waitErr)) + } + + log.Println(fmt.Sprintf("Service %s process %v terminated", service, pid)) + delete(runningServices, 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 34298dfaeb4..02bdbd921bf 100644 --- a/tests/ociswrapper/wrapper/handlers/handler.go +++ b/tests/ociswrapper/wrapper/handlers/handler.go @@ -115,7 +115,7 @@ func RollbackHandler(res http.ResponseWriter, req *http.Request) { var message string ocis.EnvConfigs = []string{} - log.Printf(fmt.Sprintf("os Environ when rollback %s", os.Environ)) + success, _ := ocis.Restart(ocis.EnvConfigs) if success { message = "oCIS configuration rolled back successfully"