Skip to content
Merged
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
4 changes: 3 additions & 1 deletion pkg/cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package start

import (
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -331,6 +332,7 @@ func createEmptyWorkspace(user *entity.User, apiKeyAuth bool, t *terminal.Termin
s := t.NewSpinner()
s.Suffix = " Creating your instance. Hang tight 🤙"
s.Start()
defer s.Stop()
w, err := startStore.CreateWorkspace(orgID, cwOptions)
if err != nil {
return breverrors.WrapAndTrace(err)
Expand Down Expand Up @@ -678,7 +680,7 @@ func runSingleStart(t *terminal.Terminal, names []string, name, setupScript, set
InstanceType: gpu,
}, startStore)
if err != nil {
if strings.Contains(err.Error(), "duplicate instance with name") {
if store.IsHTTPErrorCode(err, http.StatusBadRequest, "WORKSPACE_ALREADY_EXISTS") {
t.Vprint(t.Yellow("try running:"))
t.Vprint(t.Yellow("\tbrev start --name [different name] [repo] # or"))
t.Vprint(t.Yellow("\tbrev delete [name]"))
Expand Down
78 changes: 78 additions & 0 deletions pkg/cmd/start/start_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,92 @@
package start

import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/brevdev/brev-cli/pkg/entity"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
resty "github.com/go-resty/resty/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type failingCreateStartStore struct {
StartStore
createErr error
}

func (s failingCreateStartStore) GetAuthTokens() (*entity.AuthTokens, error) {
return nil, nil
}

func (s failingCreateStartStore) GetCurrentUser() (*entity.User, error) {
return &entity.User{ID: "user-1"}, nil
}

func (s failingCreateStartStore) GetActiveOrganizationOrDefault() (*entity.Organization, error) {
return &entity.Organization{ID: "org-1"}, nil
}

func (s failingCreateStartStore) CreateWorkspace(_ string, _ *store.CreateWorkspacesOptions) (*entity.Workspace, error) {
return nil, s.createErr
}

func startHTTPError(t *testing.T, status int, body string) error {
t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(status)
_, _ = w.Write([]byte(body))
}))
t.Cleanup(server.Close)
response, err := resty.New().R().Post(server.URL)
require.NoError(t, err)
return fmt.Errorf("create failed: %w", store.NewHTTPResponseError(response))
}

func TestRunSingleStartDuplicateGuidanceUsesWorkspaceCode(t *testing.T) {
cases := []struct {
name string
status int
body string
wantGuidance bool
}{
{"coded duplicate", http.StatusBadRequest, `{"errors":[{"type":"BadRequestError","code":"WORKSPACE_ALREADY_EXISTS","message":"Bad Request"}]}`, true},
{"generic bad request", http.StatusBadRequest, `{"errors":[{"type":"BadRequestError","message":"Bad Request"}]}`, false},
{"unrelated error with duplicate words", http.StatusBadRequest, `{"errors":[{"type":"BadRequestError","code":"OTHER","message":"duplicate instance with name"}]}`, false},
{"wrong status", http.StatusInternalServerError, `{"errors":[{"type":"InternalServerError","code":"WORKSPACE_ALREADY_EXISTS","message":"Internal Server Error"}]}`, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
createErr := startHTTPError(t, tc.status, tc.body)
read, write, err := os.Pipe()
require.NoError(t, err)
original := os.Stdout
os.Stdout = write
t.Cleanup(func() {
os.Stdout = original
_ = read.Close()
_ = write.Close()
})
term := terminal.New()
runErr := runSingleStart(term, nil, "existing-name", "", "", "", "", "", true, false, failingCreateStartStore{createErr: createErr})
require.Error(t, runErr)
require.ErrorContains(t, runErr, "create failed")
require.NoError(t, write.Close())
var output bytes.Buffer
_, err = output.ReadFrom(read)
require.NoError(t, err)
assert.Equal(t, tc.wantGuidance, strings.Contains(output.String(), "brev delete [name]"), output.String())
})
}
}

func TestMakeNewWorkspaceFromURL(t *testing.T) {
gitTruth := "github.com:brevdev/brev-cli.git"
nameTruth := "brev-cli"
Expand Down
Loading