Skip to content

Commit

Permalink
review addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
amrita-shrestha committed Jan 13, 2025
1 parent ca283c8 commit e78d3cc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 40 deletions.
2 changes: 1 addition & 1 deletion tests/acceptance/bootstrap/OcisConfigContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function theConfigHasBeenSetToValue(TableNode $table): void {
* @return void
* @throws GuzzleException
*/
public function theOcisServerHasExcludedService(string $service) {
public function theOcisServerHasServedServiceSeparately(string $service) {
$response = OcisConfigHelper::startService($service);

Assert::assertEquals(
Expand Down
4 changes: 2 additions & 2 deletions tests/ociswrapper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Also, see `./bin/ociswrapper help` for more information.
- `200 OK` - oCIS server is stopped
- `500 Internal Server Error` - Unable to stop oCIS server

6. `POST /services/<service-name>`
6. `POST /services/{service-name}`

Restart oCIS with service excluded and start excluded oCIS service individually, not covered by the oCIS supervisor.

Expand All @@ -143,7 +143,7 @@ Also, see `./bin/ociswrapper help` for more information.
- `200 OK` - oCIS server is stopped
- `500 Internal Server Error` - Unable to stop oCIS server

7. `DELETE /services/<service-name>`
7. `DELETE /services/{service-name}`

Stop individually running oCIS service

Expand Down
16 changes: 5 additions & 11 deletions tests/ociswrapper/ocis/ocis.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var EnvConfigs = []string{}
var runningServices = make(map[string]int)

func Start(envMap []string) {
log.Println("Starting oCIS service........\n")
StartService("", envMap)
}

Expand All @@ -37,7 +38,7 @@ func Stop() (bool, string) {
stopSignal = true

for service := range runningServices {
StopService(service)
go StopService(service)
}

success, message := waitUntilCompleteShutdown()
Expand Down Expand Up @@ -188,14 +189,7 @@ func RunCommand(command string, inputs []string) (int, string) {
return c.ProcessState.ExitCode(), cmdOutput
}

func RunOcisService(service string, envMap []string) {
log.Println(fmt.Sprintf("Environment variable envMap: %s\n", envMap))
StartService(service, envMap)
}

// startService is a common function for starting a service (ocis or other)
func StartService(service string, envMap []string) {
log.Println(fmt.Sprintf("Start service: %s with Environment variable envMap: %s\n", service, envMap))
// Initialize command args based on service presence
cmdArgs := []string{"server"} // Default command args

Expand Down Expand Up @@ -244,8 +238,8 @@ func StartService(service string, envMap []string) {
runningServices[service] = cmd.Process.Pid
}

for listservice, pid := range runningServices {
log.Println(fmt.Sprintf("Service started: %s with process and id: %v\n", listservice, pid))
for listService, pid := range runningServices {
log.Println(fmt.Sprintf("%s service started with process id %v\n", listService, pid))
}

// Read the logs when the 'ocis server' command is running
Expand Down Expand Up @@ -303,7 +297,7 @@ func StartService(service string, envMap []string) {
func StopService(service string) (bool, string) {
pid, exists := runningServices[service]
if !exists {
return false, fmt.Sprintf("Service %s is not running", service)
return false, fmt.Sprintf("Running service doesn't not include %s service", service)
}

process, err := os.FindProcess(pid)
Expand Down
33 changes: 8 additions & 25 deletions tests/ociswrapper/wrapper/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"ociswrapper/common"
"ociswrapper/ocis"
"strings"
)

type BasicResponse struct {
Expand Down Expand Up @@ -206,29 +205,15 @@ func CommandHandler(res http.ResponseWriter, req *http.Request) {
}

func OcisServiceHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost && req.Method != http.MethodDelete {
sendResponse(res, http.StatusMethodNotAllowed, "Method not allowed")
return
}

serviceName := strings.TrimPrefix(req.URL.Path, "/services/")

if serviceName == "" {
sendResponse(res, http.StatusUnprocessableEntity, "Service name not specified")
return
}

serviceName := req.PathValue("service")
envMap := []string{fmt.Sprintf("OCIS_EXCLUDE_RUN_SERVICES=%s", serviceName)}

if req.Method == http.MethodPost {
// restart oCIS without service that need to start separately
success, _ := ocis.Restart(envMap)
if success {
// Clear `EnvConfigs` to prevent persistence of temporary changes
log.Println(fmt.Sprintf("Environment Config when service Post request has been hit: %s\n", ocis.EnvConfigs))

var envBody map[string]interface{}
var envMap []string
var serviceEnvMap []string

if req.Body != nil && req.ContentLength > 0 {
var err error
Expand All @@ -240,29 +225,27 @@ func OcisServiceHandler(res http.ResponseWriter, req *http.Request) {
}

for key, value := range envBody {
envMap = append(envMap, fmt.Sprintf("%s=%v", key, value))
serviceEnvMap = append(serviceEnvMap, fmt.Sprintf("%s=%v", key, value))
}

log.Println(fmt.Sprintf("serviceName to start: %s\n", serviceName))
log.Println(fmt.Sprintf("Starting oCIS service %s......", serviceName))

go ocis.RunOcisService(serviceName, envMap)
go ocis.StartService(serviceName, serviceEnvMap)
success, _ := ocis.WaitForConnection()
if success {
sendResponse(res, http.StatusOK, fmt.Sprintf("oCIS service %s started successfully", serviceName))
return
}
}

sendResponse(res, http.StatusInternalServerError, fmt.Sprintf("Failed to restart oCIS without service %s", serviceName))
}

if req.Method == http.MethodDelete {
} else if req.Method == http.MethodDelete {
success, message := ocis.StopService(serviceName)
if success {
sendResponse(res, http.StatusOK, fmt.Sprintf("oCIS service %s stopped successfully", serviceName))
} else {
sendResponse(res, http.StatusInternalServerError, message)
}
} else {
sendResponse(res, http.StatusMethodNotAllowed, "Invalid method requested")
}

}
2 changes: 1 addition & 1 deletion tests/ociswrapper/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/{service}", handlers.OcisServiceHandler)

httpServer.Handler = mux

Expand Down

0 comments on commit e78d3cc

Please sign in to comment.