|
| 1 | +package localai_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + |
| 12 | + "github.com/labstack/echo/v4" |
| 13 | + "github.com/mudler/LocalAI/core/config" |
| 14 | + "github.com/mudler/LocalAI/core/gallery" |
| 15 | + . "github.com/mudler/LocalAI/core/http/endpoints/localai" |
| 16 | + "github.com/mudler/LocalAI/pkg/system" |
| 17 | + . "github.com/onsi/ginkgo/v2" |
| 18 | + . "github.com/onsi/gomega" |
| 19 | + "gopkg.in/yaml.v3" |
| 20 | +) |
| 21 | + |
| 22 | +// Installed-state on a distributed controller is derived from the controller's |
| 23 | +// own filesystem, where a backend installed on a GPU worker does not exist. |
| 24 | +// The capability fix (#10947) made those backends *listable*; every surface |
| 25 | +// that filters on installed-state still dropped them, so an admin with a |
| 26 | +// fine-tuning-capable GPU worker got an empty dropdown. |
| 27 | +var _ = Describe("Backend discovery with cluster install state", func() { |
| 28 | + var ( |
| 29 | + appCfg *config.ApplicationConfig |
| 30 | + systemState *system.SystemState |
| 31 | + galleries []config.Gallery |
| 32 | + tmpDir string |
| 33 | + ) |
| 34 | + |
| 35 | + BeforeEach(func() { |
| 36 | + var err error |
| 37 | + tmpDir, err = os.MkdirTemp("", "discovery-cluster-installed-*") |
| 38 | + Expect(err).NotTo(HaveOccurred()) |
| 39 | + DeferCleanup(func() { |
| 40 | + Expect(os.RemoveAll(tmpDir)).To(Succeed()) |
| 41 | + }) |
| 42 | + |
| 43 | + // cpu-trainer sits on the controller's disk; gpu-trainer exists only on |
| 44 | + // a worker, so nothing on this filesystem can prove it is installed. |
| 45 | + writeFakeSystemBackend(tmpDir, "cpu-trainer") |
| 46 | + |
| 47 | + galleryPath := filepath.Join(tmpDir, "gallery.yaml") |
| 48 | + data, err := yaml.Marshal([]gallery.GalleryBackend{ |
| 49 | + { |
| 50 | + Metadata: gallery.Metadata{ |
| 51 | + Name: "gpu-trainer", |
| 52 | + Tags: []string{"fine-tuning", "quantization"}, |
| 53 | + }, |
| 54 | + CapabilitiesMap: map[string]string{"nvidia-cuda-13": "gpu-trainer-cuda-13"}, |
| 55 | + }, |
| 56 | + { |
| 57 | + Metadata: gallery.Metadata{ |
| 58 | + Name: "cpu-trainer", |
| 59 | + Tags: []string{"fine-tuning", "quantization"}, |
| 60 | + }, |
| 61 | + CapabilitiesMap: map[string]string{"default": "cpu-trainer-cpu"}, |
| 62 | + }, |
| 63 | + }) |
| 64 | + Expect(err).NotTo(HaveOccurred()) |
| 65 | + Expect(os.WriteFile(galleryPath, data, 0o644)).To(Succeed()) |
| 66 | + |
| 67 | + galleries = []config.Gallery{{Name: "test-gallery", URL: "file://" + galleryPath}} |
| 68 | + // A GPU-less controller pod: capability "default". |
| 69 | + systemState = system.NewCapabilityState("default", |
| 70 | + system.WithBackendPath(tmpDir), system.WithBackendSystemPath(tmpDir)) |
| 71 | + appCfg = &config.ApplicationConfig{ |
| 72 | + BackendGalleries: galleries, |
| 73 | + SystemState: systemState, |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + nvidiaWorker := func(ctx context.Context) ([]string, error) { |
| 78 | + return []string{"nvidia-cuda-13"}, nil |
| 79 | + } |
| 80 | + installedOnWorker := func(ctx context.Context) ([]string, error) { |
| 81 | + return []string{"gpu-trainer"}, nil |
| 82 | + } |
| 83 | + registryDown := func(ctx context.Context) ([]string, error) { |
| 84 | + return nil, errors.New("registry unavailable") |
| 85 | + } |
| 86 | + |
| 87 | + // listNames drives handler over its route and returns the backend names. |
| 88 | + listNames := func(path string, handler echo.HandlerFunc) []string { |
| 89 | + e := echo.New() |
| 90 | + e.GET(path, handler) |
| 91 | + |
| 92 | + req := httptest.NewRequest(http.MethodGet, path, nil) |
| 93 | + rec := httptest.NewRecorder() |
| 94 | + e.ServeHTTP(rec, req) |
| 95 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 96 | + |
| 97 | + var backends []struct { |
| 98 | + Name string `json:"name"` |
| 99 | + } |
| 100 | + Expect(json.Unmarshal(rec.Body.Bytes(), &backends)).To(Succeed()) |
| 101 | + |
| 102 | + names := []string{} |
| 103 | + for _, b := range backends { |
| 104 | + names = append(names, b.Name) |
| 105 | + } |
| 106 | + return names |
| 107 | + } |
| 108 | + |
| 109 | + Describe("fine-tune backends", func() { |
| 110 | + It("lists a backend installed only on a worker node", func() { |
| 111 | + names := listNames("/backends", ListFineTuneBackendsEndpoint(appCfg, nvidiaWorker, installedOnWorker)) |
| 112 | + Expect(names).To(ContainElements("cpu-trainer", "gpu-trainer")) |
| 113 | + }) |
| 114 | + |
| 115 | + It("keeps single-node listings unchanged with no provider", func() { |
| 116 | + names := listNames("/backends", ListFineTuneBackendsEndpoint(appCfg, nil, nil)) |
| 117 | + Expect(names).To(ContainElement("cpu-trainer")) |
| 118 | + Expect(names).NotTo(ContainElement("gpu-trainer")) |
| 119 | + }) |
| 120 | + |
| 121 | + It("degrades to the local listing when the registry errors", func() { |
| 122 | + names := listNames("/backends", ListFineTuneBackendsEndpoint(appCfg, nvidiaWorker, registryDown)) |
| 123 | + Expect(names).To(ContainElement("cpu-trainer")) |
| 124 | + Expect(names).NotTo(ContainElement("gpu-trainer")) |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + Describe("quantization backends", func() { |
| 129 | + It("lists a backend installed only on a worker node", func() { |
| 130 | + names := listNames("/backends", ListQuantizationBackendsEndpoint(appCfg, nvidiaWorker, installedOnWorker)) |
| 131 | + Expect(names).To(ContainElements("cpu-trainer", "gpu-trainer")) |
| 132 | + }) |
| 133 | + |
| 134 | + It("keeps single-node listings unchanged with no provider", func() { |
| 135 | + names := listNames("/backends", ListQuantizationBackendsEndpoint(appCfg, nil, nil)) |
| 136 | + Expect(names).To(ContainElement("cpu-trainer")) |
| 137 | + Expect(names).NotTo(ContainElement("gpu-trainer")) |
| 138 | + }) |
| 139 | + }) |
| 140 | + |
| 141 | + Describe("GET /backends/available", func() { |
| 142 | + installedFlags := func(capabilities ClusterCapabilityProvider, installed ClusterInstalledProvider) map[string]bool { |
| 143 | + e := echo.New() |
| 144 | + svc := CreateBackendEndpointService(galleries, systemState, nil, nil) |
| 145 | + e.GET("/backends/available", svc.ListAvailableBackendsEndpoint(systemState, capabilities, installed)) |
| 146 | + |
| 147 | + req := httptest.NewRequest(http.MethodGet, "/backends/available", nil) |
| 148 | + rec := httptest.NewRecorder() |
| 149 | + e.ServeHTTP(rec, req) |
| 150 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 151 | + |
| 152 | + var backends []gallery.GalleryBackend |
| 153 | + Expect(json.Unmarshal(rec.Body.Bytes(), &backends)).To(Succeed()) |
| 154 | + |
| 155 | + flags := map[string]bool{} |
| 156 | + for _, b := range backends { |
| 157 | + flags[b.Name] = b.Installed |
| 158 | + } |
| 159 | + return flags |
| 160 | + } |
| 161 | + |
| 162 | + It("reports a worker-installed backend as installed", func() { |
| 163 | + flags := installedFlags(nvidiaWorker, installedOnWorker) |
| 164 | + Expect(flags).To(HaveKeyWithValue("gpu-trainer", true)) |
| 165 | + Expect(flags).To(HaveKeyWithValue("cpu-trainer", true)) |
| 166 | + }) |
| 167 | + |
| 168 | + It("keeps single-node install state unchanged with no provider", func() { |
| 169 | + flags := installedFlags(nvidiaWorker, nil) |
| 170 | + Expect(flags).To(HaveKeyWithValue("gpu-trainer", false)) |
| 171 | + Expect(flags).To(HaveKeyWithValue("cpu-trainer", true)) |
| 172 | + }) |
| 173 | + }) |
| 174 | +}) |
0 commit comments