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
101 changes: 95 additions & 6 deletions pkg/cmd/gpucreate/gpucreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func NewCmdGPUCreate(t *terminal.Terminal, gpuCreateStore GPUCreateStore) *cobra
}

if dryRun {
return runDryRun(t, gpuCreateStore, opts.InstanceTypes, &filters)
return runDryRun(t, gpuCreateStore, opts.Name, opts.InstanceTypes, &filters)
}

return RunGPUCreate(cmd.Context(), t, gpuCreateStore, opts)
Expand Down Expand Up @@ -730,13 +730,17 @@ func getFilteredInstanceTypes(s GPUCreateStore, filters *searchFilterFlags) ([]I
return specs, nil
}

// runDryRun shows the instance types that would be used without creating anything
func runDryRun(t *terminal.Terminal, s GPUCreateStore, specs []InstanceSpec, filters *searchFilterFlags) error {
if len(specs) > 0 {
t.Print(formatInstanceSpecs(specs))
return nil
// runDryRun shows what would be created without provisioning.
// Explicit types are resolved against the catalog so GPU, disk, and price are
// visible. A type the catalog does not list keeps the previous type-name line.
func runDryRun(t *terminal.Terminal, s GPUCreateStore, name string, specs []InstanceSpec, filters *searchFilterFlags) error {
if len(specs) == 0 {
return runDryRunSearch(t, s, filters)
}
return runDryRunExplicit(t, s, name, specs)
}

func runDryRunSearch(t *terminal.Terminal, s GPUCreateStore, filters *searchFilterFlags) error {
filtered, _, err := searchInstances(s, filters)
if err != nil {
return breverrors.WrapAndTrace(err)
Expand All @@ -749,6 +753,91 @@ func runDryRun(t *terminal.Terminal, s GPUCreateStore, specs []InstanceSpec, fil
return nil
}

func runDryRunExplicit(t *terminal.Terminal, s GPUCreateStore, name string, specs []InstanceSpec) error {
matched, missing, err := lookupExplicitInstances(s, specs)
if err != nil {
return breverrors.WrapAndTrace(err)
}
if len(matched) == 0 {
t.Print(formatInstanceSpecs(specs))
return nil
}

printDryRunPlanHeader(t, s, name)
piped := gpusearch.IsStdoutPiped()
if err := gpusearch.DisplayGPUResults(t, matched, false, piped, false); err != nil {
return breverrors.WrapAndTrace(err)
}
if len(missing) > 0 {
t.Print("Unknown instance types: " + strings.Join(missing, ", "))
}
return nil
}

func printDryRunPlanHeader(t *terminal.Terminal, s GPUCreateStore, name string) {
if name != "" {
t.Print("Name: " + name)
}
org, err := s.GetActiveOrganizationOrDefault()
if err != nil || org == nil || org.Name == "" {
return
}
t.Print("Org: " + org.Name)
}

// lookupExplicitInstances returns catalog rows for the requested types, in the
// order given. A type may yield more than one row when it lists several GPUs.
// GetInstanceTypes(false) is the GPU catalog; CPU-only types are reported missing.
func lookupExplicitInstances(s GPUCreateStore, specs []InstanceSpec) ([]gpusearch.GPUInstanceInfo, []string, error) {
response, err := s.GetInstanceTypes(false)
if err != nil {
return nil, nil, breverrors.WrapAndTrace(err)
}

var items []gpusearch.InstanceType
if response != nil {
items = response.Items
}
byType := make(map[string][]gpusearch.GPUInstanceInfo, len(items))
for _, inst := range gpusearch.ProcessInstances(items) {
byType[inst.Type] = append(byType[inst.Type], inst)
}

var matched []gpusearch.GPUInstanceInfo
var missing []string
seen := make(map[string]bool, len(specs))
for _, spec := range specs {
if seen[spec.Type] {
continue
}
seen[spec.Type] = true
rows := byType[spec.Type]
if len(rows) == 0 {
missing = append(missing, spec.Type)
continue
}
for _, inst := range rows {
applyRequestedDisk(&inst, spec.DiskGB)
matched = append(matched, inst)
}
}
return matched, missing, nil
}

// applyRequestedDisk shows the disk create would request. DiskGB 0 keeps the
// catalog size and fills TargetDisk so a piped table is not 0.
func applyRequestedDisk(inst *gpusearch.GPUInstanceInfo, diskGB float64) {
if diskGB > 0 {
inst.TargetDisk = diskGB
inst.DiskMin = diskGB
inst.DiskMax = diskGB
return
}
if inst.TargetDisk == 0 {
inst.TargetDisk = inst.DiskMin
}
}

// orDefault returns val if it's non-zero, otherwise returns def
func orDefault(val, def float64) float64 {
if val > 0 {
Expand Down
93 changes: 92 additions & 1 deletion pkg/cmd/gpucreate/gpucreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package gpucreate
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -150,7 +152,8 @@ func (m *MockGPUCreateStore) GetInstanceTypes(_ bool) (*gpusearch.InstanceTypesR
return &gpusearch.InstanceTypesResponse{
Items: []gpusearch.InstanceType{
{
Type: "g5.xlarge",
Type: "g5.xlarge",
Provider: "aws",
SupportedGPUs: []gpusearch.GPU{
{Count: 1, Name: "A10G", Manufacturer: "NVIDIA", Memory: "24GiB"},
},
Expand Down Expand Up @@ -861,6 +864,94 @@ func TestCreateDryRunWithExplicitTypesDoesNotProvision(t *testing.T) {
assert.Empty(t, mock.CreatedWorkspaces)
}

// captureStdout runs fn while os.Stdout is a pipe and returns what was written.
// terminal.New must be called inside fn so it binds the pipe.
func captureStdout(t *testing.T, fn func()) string {
t.Helper()
r, w, err := os.Pipe()
require.NoError(t, err)
orig := os.Stdout
os.Stdout = w
t.Cleanup(func() { os.Stdout = orig })

fn()

require.NoError(t, w.Close())
os.Stdout = orig
out, err := io.ReadAll(r)
require.NoError(t, err)
return string(out)
}

// TestCreateDryRunWithExplicitTypePrintsCatalogDetails covers issue #431:
// `brev create <name> --type X --dry-run` must show the catalog row (GPU, disk,
// price, org), not only the type string the user already typed.
func TestCreateDryRunWithExplicitTypePrintsCatalogDetails(t *testing.T) {
mock := NewMockGPUCreateStore()
var err error
out := captureStdout(t, func() {
term := terminal.New()
cmd := NewCmdGPUCreate(term, mock)
cmd.SetArgs([]string{"dry-run-test", "--type", "g5.xlarge", "--dry-run"})
err = cmd.Execute()
})

require.NoError(t, err)
assert.Empty(t, mock.CreatedWorkspaces)
assert.Contains(t, out, "Name: dry-run-test")
assert.Contains(t, out, "Org: test-org")
assert.Contains(t, out, "g5.xlarge")
assert.Contains(t, out, "aws")
assert.Contains(t, out, "A10G")
assert.Contains(t, out, "500GB")
assert.Contains(t, out, "$1.01")
assert.NotEqual(t, "g5.xlarge\n", out)
}

func TestCreateDryRunUnknownTypePrintsNameWithoutProvisioning(t *testing.T) {
mock := NewMockGPUCreateStore()
var err error
out := captureStdout(t, func() {
term := terminal.New()
cmd := NewCmdGPUCreate(term, mock)
cmd.SetArgs([]string{"dry-run-test", "--type", "missing-type9", "--dry-run"})
err = cmd.Execute()
})

require.NoError(t, err)
assert.Empty(t, mock.CreatedWorkspaces)
assert.Contains(t, out, "missing-type9")
assert.NotContains(t, out, "A10G")
assert.NotContains(t, out, "Name:")
}

func TestLookupExplicitInstances(t *testing.T) {
mock := NewMockGPUCreateStore()
matched, missing, err := lookupExplicitInstances(mock, []InstanceSpec{
{Type: "g5.xlarge", DiskGB: 200},
{Type: "g5.xlarge"},
{Type: "missing-type9"},
})
require.NoError(t, err)
assert.Equal(t, []string{"missing-type9"}, missing)
require.Len(t, matched, 1)
assert.Equal(t, "g5.xlarge", matched[0].Type)
assert.Equal(t, "aws", matched[0].Provider)
assert.Equal(t, "A10G", matched[0].GPUName)
assert.Equal(t, 1, matched[0].GPUCount)
assert.Equal(t, 24.0, matched[0].VRAMPerGPU)
assert.Equal(t, 200.0, matched[0].DiskMin)
assert.Equal(t, 200.0, matched[0].TargetDisk)
assert.InDelta(t, 1.006, matched[0].PricePerHour, 0.0001)

catalog, missing, err := lookupExplicitInstances(mock, []InstanceSpec{{Type: "g5.xlarge"}})
require.NoError(t, err)
assert.Empty(t, missing)
require.Len(t, catalog, 1)
assert.Equal(t, 500.0, catalog[0].DiskMin)
assert.Equal(t, 500.0, catalog[0].TargetDisk)
}

func TestGetFilteredInstanceTypesDefaults(t *testing.T) {
mock := NewMockGPUCreateStore()

Expand Down