Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/cmd/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
69 changes: 69 additions & 0 deletions pkg/cmd/stop/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading