From 0731865de8a8f29a7c03bb72dc56f6dc5c34d6cc Mon Sep 17 00:00:00 2001 From: kirtiip20 Date: Sun, 15 Nov 2026 23:39:26 +0000 Subject: [PATCH] fix(stop): skip stop request when instance is already stopped --- pkg/cmd/stop/stop.go | 8 +++++ pkg/cmd/stop/stop_test.go | 69 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/pkg/cmd/stop/stop.go b/pkg/cmd/stop/stop.go index f8f0f6149..a267bbab8 100644 --- a/pkg/cmd/stop/stop.go +++ b/pkg/cmd/stop/stop.go @@ -150,6 +150,14 @@ func stopWorkspace(workspaceName string, t *terminal.Terminal, stopStore StopSto if err = validateWorkspaceStoppable(workspace); err != nil { return err } + // The status comes from the VM, so a stopped instance needs no stop request. + // Sending one anyway fails on providers like Crusoe after a failed start. + if workspace.Status == entity.Stopped { + if !piped { + t.Vprintf("%s", t.Yellow("Instance "+workspaceName+" is already stopped.\n")) + } + return nil + } workspaceID = workspace.ID } diff --git a/pkg/cmd/stop/stop_test.go b/pkg/cmd/stop/stop_test.go index d04c55718..508f6311a 100644 --- a/pkg/cmd/stop/stop_test.go +++ b/pkg/cmd/stop/stop_test.go @@ -2,9 +2,78 @@ package stop import ( "testing" + + "github.com/brevdev/brev-cli/pkg/entity" + "github.com/brevdev/brev-cli/pkg/store" + "github.com/brevdev/brev-cli/pkg/terminal" + "github.com/stretchr/testify/assert" ) func TestStopWorkspaceSelf(_ *testing.T) { // err := stopThisWorkspace(nil, nil) // assert.Nil(t, err) } + +type fakeStopStore struct { + workspace entity.Workspace + stoppedIDs []string +} + +func (f *fakeStopStore) GetAuthTokens() (*entity.AuthTokens, error) { return nil, nil } + +func (f *fakeStopStore) GetActiveOrganizationOrDefault() (*entity.Organization, error) { + return &entity.Organization{ID: "org-1"}, nil +} + +func (f *fakeStopStore) GetCurrentUser() (*entity.User, error) { + return &entity.User{ID: "user-1"}, nil +} + +func (f *fakeStopStore) GetWorkspaceByNameOrID(_ string, _ string) ([]entity.Workspace, error) { + return []entity.Workspace{f.workspace}, nil +} + +func (f *fakeStopStore) GetWorkspaces(_ string, _ *store.GetWorkspacesOptions) ([]entity.Workspace, error) { + return []entity.Workspace{f.workspace}, nil +} + +func (f *fakeStopStore) GetOrganizations(_ *store.GetOrganizationsOptions) ([]entity.Organization, error) { + return nil, nil +} + +func (f *fakeStopStore) StopWorkspace(workspaceID string) (*entity.Workspace, error) { + f.stoppedIDs = append(f.stoppedIDs, workspaceID) + return &f.workspace, nil +} + +func (f *fakeStopStore) IsWorkspace() (bool, error) { return false, nil } + +func (f *fakeStopStore) GetCurrentWorkspaceID() (string, error) { return "", nil } + +func newFakeStopStore(status string) *fakeStopStore { + return &fakeStopStore{workspace: entity.Workspace{ + ID: "ws-1", + Name: "my-instance", + Status: status, + CreatedByUserID: "user-1", + InstanceTypeInfo: &entity.InstanceTypeInfo{Stoppable: true}, + }} +} + +func TestStopWorkspace_SkipsStopRequestWhenAlreadyStopped(t *testing.T) { + s := newFakeStopStore(entity.Stopped) + + err := stopWorkspace("my-instance", terminal.New(), s, false) + + assert.NoError(t, err) + assert.Empty(t, s.stoppedIDs) +} + +func TestStopWorkspace_SendsStopRequestWhenRunning(t *testing.T) { + s := newFakeStopStore(entity.Running) + + err := stopWorkspace("my-instance", terminal.New(), s, false) + + assert.NoError(t, err) + assert.Equal(t, []string{"ws-1"}, s.stoppedIDs) +}