|
7 | 7 |
|
8 | 8 | "github.com/brevdev/brev-cli/pkg/cmd/gpusearch" |
9 | 9 | "github.com/brevdev/brev-cli/pkg/entity" |
| 10 | + breverrors "github.com/brevdev/brev-cli/pkg/errors" |
10 | 11 | "github.com/brevdev/brev-cli/pkg/store" |
11 | 12 | "github.com/brevdev/brev-cli/pkg/terminal" |
12 | 13 | "github.com/stretchr/testify/assert" |
@@ -747,3 +748,158 @@ func TestInlineLaunchableLifeCycleScript(t *testing.T) { |
747 | 748 | assert.Equal(t, "", info.BuildRequest.VMBuild.LifeCycleScriptAttr.Script) |
748 | 749 | }) |
749 | 750 | } |
| 751 | + |
| 752 | +func TestValidateInstanceTypeAvailability(t *testing.T) { |
| 753 | + t.Run("returns nil when listing is unavailable", func(t *testing.T) { |
| 754 | + ctx := &createContext{} |
| 755 | + assert.NoError(t, ctx.validateInstanceTypeAvailability("hyperstack_H100x8_one")) |
| 756 | + }) |
| 757 | + |
| 758 | + t.Run("returns nil when type has a workspace group", func(t *testing.T) { |
| 759 | + ctx := &createContext{ |
| 760 | + allInstanceTypes: &gpusearch.AllInstanceTypesResponse{ |
| 761 | + AllInstanceTypes: []gpusearch.InstanceType{ |
| 762 | + { |
| 763 | + Type: "hyperstack_H100_sxm5x8", |
| 764 | + WorkspaceGroups: []gpusearch.WorkspaceGroup{ |
| 765 | + {ID: "wg-1", Name: "Shadeform", PlatformType: "shadeform"}, |
| 766 | + }, |
| 767 | + }, |
| 768 | + }, |
| 769 | + }, |
| 770 | + } |
| 771 | + assert.NoError(t, ctx.validateInstanceTypeAvailability("hyperstack_H100_sxm5x8")) |
| 772 | + }) |
| 773 | + |
| 774 | + t.Run("returns invalid-type error for unknown type", func(t *testing.T) { |
| 775 | + ctx := &createContext{ |
| 776 | + allInstanceTypes: &gpusearch.AllInstanceTypesResponse{ |
| 777 | + AllInstanceTypes: []gpusearch.InstanceType{ |
| 778 | + {Type: "hyperstack_H100_sxm5x8", WorkspaceGroups: []gpusearch.WorkspaceGroup{{ID: "wg-1"}}}, |
| 779 | + }, |
| 780 | + }, |
| 781 | + } |
| 782 | + err := ctx.validateInstanceTypeAvailability("hyperstack_H100x8_one") |
| 783 | + assert.Error(t, err) |
| 784 | + assert.Contains(t, err.Error(), `"hyperstack_H100x8_one"`) |
| 785 | + assert.Contains(t, err.Error(), "not a recognized type") |
| 786 | + assert.Contains(t, err.Error(), "brev search") |
| 787 | + }) |
| 788 | + |
| 789 | + t.Run("returns unavailable error for known type without workspace groups", func(t *testing.T) { |
| 790 | + ctx := &createContext{ |
| 791 | + allInstanceTypes: &gpusearch.AllInstanceTypesResponse{ |
| 792 | + AllInstanceTypes: []gpusearch.InstanceType{ |
| 793 | + {Type: "hyperstack_H100x8_NVLINK", WorkspaceGroups: nil}, |
| 794 | + }, |
| 795 | + }, |
| 796 | + } |
| 797 | + err := ctx.validateInstanceTypeAvailability("hyperstack_H100x8_NVLINK") |
| 798 | + assert.Error(t, err) |
| 799 | + assert.Contains(t, err.Error(), `"hyperstack_H100x8_NVLINK"`) |
| 800 | + assert.Contains(t, err.Error(), "currently unavailable") |
| 801 | + assert.Contains(t, err.Error(), "brev search") |
| 802 | + }) |
| 803 | + |
| 804 | + t.Run("error type is ValidationError so no stack trace is appended", func(t *testing.T) { |
| 805 | + ctx := &createContext{ |
| 806 | + allInstanceTypes: &gpusearch.AllInstanceTypesResponse{ |
| 807 | + AllInstanceTypes: []gpusearch.InstanceType{}, |
| 808 | + }, |
| 809 | + } |
| 810 | + err := ctx.validateInstanceTypeAvailability("missing") |
| 811 | + assert.Error(t, err) |
| 812 | + var ve breverrors.ValidationError |
| 813 | + assert.ErrorAs(t, err, &ve) |
| 814 | + assert.NotContains(t, err.Error(), "gpucreate.go", "validation error should not include source-file traces") |
| 815 | + }) |
| 816 | +} |
| 817 | + |
| 818 | +func TestCreateInstancesWithTypeSkipsInvalidType(t *testing.T) { |
| 819 | + mock := NewMockGPUCreateStore() |
| 820 | + ctx := &createContext{ |
| 821 | + t: terminal.New(), |
| 822 | + store: mock, |
| 823 | + opts: GPUCreateOptions{Count: 1, Parallel: 1, Name: "jt-4"}, |
| 824 | + org: mock.Org, |
| 825 | + user: mock.User, |
| 826 | + piped: true, |
| 827 | + allInstanceTypes: &gpusearch.AllInstanceTypesResponse{ |
| 828 | + AllInstanceTypes: []gpusearch.InstanceType{ |
| 829 | + { |
| 830 | + Type: "hyperstack_H100_sxm5x8", |
| 831 | + WorkspaceGroups: []gpusearch.WorkspaceGroup{ |
| 832 | + {ID: "wg-shadeform", Name: "Shadeform", PlatformType: "shadeform"}, |
| 833 | + }, |
| 834 | + }, |
| 835 | + }, |
| 836 | + }, |
| 837 | + } |
| 838 | + ctx.logf = func(_ string, _ ...interface{}) {} |
| 839 | + |
| 840 | + result := ctx.createInstancesWithType(InstanceSpec{Type: "hyperstack_H100x8_one"}, 0, 1) |
| 841 | + |
| 842 | + assert.True(t, result.hadFailure, "expected hadFailure for an invalid instance type") |
| 843 | + assert.Empty(t, result.successes, "expected no successes for invalid type") |
| 844 | + assert.NoError(t, result.fatalError, "invalid type should not be fatal — caller may try the next type") |
| 845 | + assert.Empty(t, mock.CreatedWorkspaces, "CreateWorkspace must not be called when the type is unrecognized") |
| 846 | +} |
| 847 | + |
| 848 | +func TestCreateInstancesWithTypeSkipsUnavailableType(t *testing.T) { |
| 849 | + mock := NewMockGPUCreateStore() |
| 850 | + ctx := &createContext{ |
| 851 | + t: terminal.New(), |
| 852 | + store: mock, |
| 853 | + opts: GPUCreateOptions{Count: 1, Parallel: 1, Name: "jt-4"}, |
| 854 | + org: mock.Org, |
| 855 | + user: mock.User, |
| 856 | + piped: true, |
| 857 | + allInstanceTypes: &gpusearch.AllInstanceTypesResponse{ |
| 858 | + AllInstanceTypes: []gpusearch.InstanceType{ |
| 859 | + {Type: "hyperstack_H100x8_NVLINK", WorkspaceGroups: nil}, |
| 860 | + }, |
| 861 | + }, |
| 862 | + } |
| 863 | + ctx.logf = func(_ string, _ ...interface{}) {} |
| 864 | + |
| 865 | + result := ctx.createInstancesWithType(InstanceSpec{Type: "hyperstack_H100x8_NVLINK"}, 0, 1) |
| 866 | + |
| 867 | + assert.True(t, result.hadFailure, "expected hadFailure for an unavailable instance type") |
| 868 | + assert.Empty(t, result.successes) |
| 869 | + assert.Empty(t, mock.CreatedWorkspaces, "CreateWorkspace must not be called when no workspace group is available") |
| 870 | +} |
| 871 | + |
| 872 | +func TestCreateInstancesWithTypeBypassesValidationForLaunchable(t *testing.T) { |
| 873 | + mock := NewMockGPUCreateStore() |
| 874 | + ctx := &createContext{ |
| 875 | + t: terminal.New(), |
| 876 | + store: mock, |
| 877 | + opts: GPUCreateOptions{ |
| 878 | + Count: 1, |
| 879 | + Parallel: 1, |
| 880 | + Name: "jt-4", |
| 881 | + LaunchableID: "env-abc", |
| 882 | + LaunchableInfo: &store.LaunchableResponse{ |
| 883 | + ID: "env-abc", |
| 884 | + Name: "test-launchable", |
| 885 | + CreateWorkspaceRequest: store.LaunchableWorkspaceRequest{ |
| 886 | + WorkspaceGroupID: "wg-from-launchable", |
| 887 | + InstanceType: "n2-standard-4", |
| 888 | + }, |
| 889 | + }, |
| 890 | + }, |
| 891 | + org: mock.Org, |
| 892 | + user: mock.User, |
| 893 | + piped: true, |
| 894 | + allInstanceTypes: &gpusearch.AllInstanceTypesResponse{ |
| 895 | + AllInstanceTypes: []gpusearch.InstanceType{}, // launchable's type is not in the org listing |
| 896 | + }, |
| 897 | + } |
| 898 | + ctx.logf = func(_ string, _ ...interface{}) {} |
| 899 | + |
| 900 | + result := ctx.createInstancesWithType(InstanceSpec{Type: "n2-standard-4"}, 0, 1) |
| 901 | + |
| 902 | + assert.False(t, result.hadFailure, "launchable should not be blocked by pre-flight validation") |
| 903 | + assert.Len(t, result.successes, 1, "expected the launchable instance to be created") |
| 904 | + assert.Len(t, mock.CreatedWorkspaces, 1) |
| 905 | +} |
0 commit comments