Skip to content

Commit

Permalink
fix: ignore the current status of the service during a start/stop (#1965
Browse files Browse the repository at this point in the history
)
  • Loading branch information
h4ck3rk3y committed Dec 15, 2023
1 parent b0e27e6 commit 1c4863f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
Expand Up @@ -463,9 +463,6 @@ func (network *DefaultServiceNetwork) StartServices(
if err != nil {
return nil, nil, stacktrace.Propagate(err, "An error occurred while getting service registration for identifier '%v'", serviceIdentifier)
}
if serviceRegistration.GetStatus() == service.ServiceStatus_Started {
return nil, nil, stacktrace.NewError("Service '%v' is already started", serviceRegistration.GetName())
}
serviceRegistrations[serviceRegistration.GetUUID()] = serviceRegistration
}

Expand Down Expand Up @@ -531,9 +528,6 @@ func (network *DefaultServiceNetwork) StopServices(
if err != nil {
return nil, nil, stacktrace.Propagate(err, "An error occurred while getting service registration for identifier '%v'", serviceIdentifier)
}
if serviceRegistration.GetStatus() == service.ServiceStatus_Stopped {
return nil, nil, stacktrace.NewError("Service '%v' is already stopped", serviceRegistration.GetName())
}
serviceUuids[serviceRegistration.GetUUID()] = true
serviceNamesByUuid[serviceRegistration.GetUUID()] = serviceRegistration.GetName()
}
Expand Down
Expand Up @@ -685,13 +685,19 @@ func TestStopService_ServiceAlreadyStopped(t *testing.T) {
},
Statuses: nil,
},
).Maybe().Times(0)
).Times(1).Return(
map[service.ServiceUUID]bool{
serviceUuid: true,
},
map[service.ServiceUUID]error{},
nil,
)

err = network.StopService(ctx, string(serviceName))
require.NotNil(t, err)
expectedErrorMsg := fmt.Sprintf("Service '%s' is already stopped", string(serviceName))
require.Contains(t, err.Error(), expectedErrorMsg)
require.Equal(t, service.ServiceStatus_Stopped, serviceRegistration.GetStatus())
require.Nil(t, err)
serviceRegistrationAfterBeingStopped, err := network.serviceRegistrationRepository.Get(serviceName)
require.NoError(t, err)
require.Equal(t, serviceRegistrationAfterBeingStopped.GetStatus(), service.ServiceStatus_Stopped)
}

func TestStartService_Successful(t *testing.T) {
Expand Down Expand Up @@ -814,6 +820,7 @@ func TestStartService_ServiceAlreadyStarted(t *testing.T) {
serviceRegistration.SetStatus(service.ServiceStatus_Started)
serviceConfig := testServiceConfig(t, testContainerImageName)
serviceRegistration.SetConfig(serviceConfig)
serviceObj := service.NewService(serviceRegistration, map[string]*port_spec.PortSpec{}, successfulServiceIp, map[string]*port_spec.PortSpec{}, container.NewContainer(container.ContainerStatus_Running, testContainerImageName, nil, nil, nil))

file, err := os.CreateTemp("/tmp", "*.db")
defer os.Remove(file.Name())
Expand Down Expand Up @@ -841,13 +848,19 @@ func TestStartService_ServiceAlreadyStarted(t *testing.T) {
map[service.ServiceUUID]*service.ServiceConfig{
serviceUuid: serviceConfig,
},
).Maybe().Times(0)
).Times(1).Return(
map[service.ServiceUUID]*service.Service{
serviceUuid: serviceObj,
},
map[service.ServiceUUID]error{},
nil,
)

err = network.StartService(ctx, string(serviceName))
require.NotNil(t, err)
expectedErrorMsg := fmt.Sprintf("Service '%s' is already started", string(serviceName))
require.Contains(t, err.Error(), expectedErrorMsg)
require.Equal(t, serviceRegistration.GetStatus(), service.ServiceStatus_Started)
require.Nil(t, err)
serviceRegistrationAfterBeingStarted, err := network.serviceRegistrationRepository.Get(serviceName)
require.NoError(t, err)
require.Equal(t, serviceRegistrationAfterBeingStarted.GetStatus(), service.ServiceStatus_Started)
}

func TestUpdateService(t *testing.T) {
Expand Down
Expand Up @@ -2,7 +2,6 @@ package startosis_stop_service_test

import (
"context"
"fmt"
"testing"

"github.com/kurtosis-tech/kurtosis-cli/golang_internal_testsuite/test_helpers"
Expand Down Expand Up @@ -108,8 +107,6 @@ Service ` + serviceName + ` deployed successfully.
require.Nil(t, runResult.InterpretationError, "Unexpected interpretation error")
require.Empty(t, runResult.ValidationErrors, "Unexpected validation error")
require.NotEmpty(t, runResult.ExecutionError, "Expected execution error coming from already started service")
expectedErrorStr := fmt.Sprintf("Service '%v' is already started", serviceName)
require.Contains(t, runResult.ExecutionError.ErrorMessage, expectedErrorStr)

// we run the stop and restart script and validate that the service is unreachable.
runResult, err = test_helpers.RunScriptWithDefaultConfig(ctx, enclaveCtx, stopAndStartScript)
Expand All @@ -136,6 +133,4 @@ Service ` + serviceName + ` deployed successfully.
require.Nil(t, runResult.InterpretationError, "Unexpected interpretation error")
require.Empty(t, runResult.ValidationErrors, "Unexpected validation error")
require.NotEmpty(t, runResult.ExecutionError, "Expected execution error coming from already started service")
expectedErrorStr = fmt.Sprintf("Service '%v' is already started", serviceName)
require.Contains(t, runResult.ExecutionError.ErrorMessage, expectedErrorStr)
}
Expand Up @@ -2,7 +2,6 @@ package startosis_stop_service_test

import (
"context"
"fmt"
"testing"

"github.com/kurtosis-tech/kurtosis-cli/golang_internal_testsuite/test_helpers"
Expand Down Expand Up @@ -116,11 +115,9 @@ Service ` + serviceName + ` deployed successfully.

logrus.Infof("Validated that the service is stopped")

// we run the stop script one more time and validate that an error is returned since the service is already stopped.
// already stopped service should be able to stop just fine
runResult, _ = test_helpers.RunScriptWithDefaultConfig(ctx, enclaveCtx, stopScript2)
require.Nil(t, runResult.InterpretationError, "Unexpected interpretation error")
require.Empty(t, runResult.ValidationErrors, "Unexpected validation error")
require.NotEmpty(t, runResult.ExecutionError, "Expected execution error coming from already stopped service")
expectedErrorStr := fmt.Sprintf("Service '%v' is already stopped", serviceName)
require.Contains(t, runResult.ExecutionError.ErrorMessage, expectedErrorStr)
require.Nil(t, runResult.ExecutionError)
}

0 comments on commit 1c4863f

Please sign in to comment.