|
| 1 | +package routes_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/labstack/echo/v4" |
| 15 | + "github.com/mudler/LocalAI/core/config" |
| 16 | + "github.com/mudler/LocalAI/core/gallery" |
| 17 | + "github.com/mudler/LocalAI/core/http/routes" |
| 18 | + "github.com/mudler/LocalAI/core/services" |
| 19 | + "github.com/mudler/LocalAI/pkg/model" |
| 20 | + "github.com/mudler/LocalAI/pkg/system" |
| 21 | + . "github.com/onsi/ginkgo/v2" |
| 22 | + . "github.com/onsi/gomega" |
| 23 | +) |
| 24 | + |
| 25 | +func TestRoutes(t *testing.T) { |
| 26 | + RegisterFailHandler(Fail) |
| 27 | + RunSpecs(t, "Routes Suite") |
| 28 | +} |
| 29 | + |
| 30 | +var _ = Describe("Backend API Routes", func() { |
| 31 | + var ( |
| 32 | + app *echo.Echo |
| 33 | + tempDir string |
| 34 | + appConfig *config.ApplicationConfig |
| 35 | + galleryService *services.GalleryService |
| 36 | + modelLoader *model.ModelLoader |
| 37 | + systemState *system.SystemState |
| 38 | + configLoader *config.ModelConfigLoader |
| 39 | + ) |
| 40 | + |
| 41 | + BeforeEach(func() { |
| 42 | + var err error |
| 43 | + tempDir, err = os.MkdirTemp("", "backend-routes-test-*") |
| 44 | + Expect(err).NotTo(HaveOccurred()) |
| 45 | + |
| 46 | + systemState, err = system.GetSystemState( |
| 47 | + system.WithBackendPath(filepath.Join(tempDir, "backends")), |
| 48 | + ) |
| 49 | + Expect(err).NotTo(HaveOccurred()) |
| 50 | + systemState.Model.ModelsPath = filepath.Join(tempDir, "models") |
| 51 | + |
| 52 | + // Create directories |
| 53 | + err = os.MkdirAll(systemState.Backend.BackendsPath, 0750) |
| 54 | + Expect(err).NotTo(HaveOccurred()) |
| 55 | + err = os.MkdirAll(systemState.Model.ModelsPath, 0750) |
| 56 | + Expect(err).NotTo(HaveOccurred()) |
| 57 | + |
| 58 | + modelLoader = model.NewModelLoader(systemState) |
| 59 | + configLoader = config.NewModelConfigLoader(tempDir) |
| 60 | + |
| 61 | + appConfig = config.NewApplicationConfig( |
| 62 | + config.WithContext(context.Background()), |
| 63 | + ) |
| 64 | + appConfig.SystemState = systemState |
| 65 | + appConfig.BackendGalleries = []config.Gallery{} |
| 66 | + |
| 67 | + galleryService = services.NewGalleryService(appConfig, modelLoader) |
| 68 | + // Start the gallery service |
| 69 | + err = galleryService.Start(context.Background(), configLoader, systemState) |
| 70 | + Expect(err).NotTo(HaveOccurred()) |
| 71 | + |
| 72 | + app = echo.New() |
| 73 | + |
| 74 | + // Register the API routes for backends |
| 75 | + opcache := services.NewOpCache(galleryService) |
| 76 | + routes.RegisterUIAPIRoutes(app, configLoader, modelLoader, appConfig, galleryService, opcache, nil) |
| 77 | + }) |
| 78 | + |
| 79 | + AfterEach(func() { |
| 80 | + os.RemoveAll(tempDir) |
| 81 | + }) |
| 82 | + |
| 83 | + Describe("POST /api/backends/install-external", func() { |
| 84 | + It("should return error when URI is missing", func() { |
| 85 | + reqBody := map[string]string{ |
| 86 | + "name": "test-backend", |
| 87 | + } |
| 88 | + jsonBody, err := json.Marshal(reqBody) |
| 89 | + Expect(err).NotTo(HaveOccurred()) |
| 90 | + |
| 91 | + req := httptest.NewRequest(http.MethodPost, "/api/backends/install-external", bytes.NewBuffer(jsonBody)) |
| 92 | + req.Header.Set("Content-Type", "application/json") |
| 93 | + rec := httptest.NewRecorder() |
| 94 | + |
| 95 | + app.ServeHTTP(rec, req) |
| 96 | + |
| 97 | + Expect(rec.Code).To(Equal(http.StatusBadRequest)) |
| 98 | + |
| 99 | + var response map[string]interface{} |
| 100 | + err = json.Unmarshal(rec.Body.Bytes(), &response) |
| 101 | + Expect(err).NotTo(HaveOccurred()) |
| 102 | + Expect(response["error"]).To(Equal("uri is required")) |
| 103 | + }) |
| 104 | + |
| 105 | + It("should accept valid request and return job ID", func() { |
| 106 | + reqBody := map[string]string{ |
| 107 | + "uri": "oci://quay.io/example/backend:latest", |
| 108 | + "name": "test-backend", |
| 109 | + "alias": "test-alias", |
| 110 | + } |
| 111 | + jsonBody, err := json.Marshal(reqBody) |
| 112 | + Expect(err).NotTo(HaveOccurred()) |
| 113 | + |
| 114 | + req := httptest.NewRequest(http.MethodPost, "/api/backends/install-external", bytes.NewBuffer(jsonBody)) |
| 115 | + req.Header.Set("Content-Type", "application/json") |
| 116 | + rec := httptest.NewRecorder() |
| 117 | + |
| 118 | + app.ServeHTTP(rec, req) |
| 119 | + |
| 120 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 121 | + |
| 122 | + var response map[string]interface{} |
| 123 | + err = json.Unmarshal(rec.Body.Bytes(), &response) |
| 124 | + Expect(err).NotTo(HaveOccurred()) |
| 125 | + Expect(response["jobID"]).NotTo(BeEmpty()) |
| 126 | + Expect(response["message"]).To(Equal("External backend installation started")) |
| 127 | + }) |
| 128 | + |
| 129 | + It("should accept request with only URI", func() { |
| 130 | + reqBody := map[string]string{ |
| 131 | + "uri": "/path/to/local/backend", |
| 132 | + } |
| 133 | + jsonBody, err := json.Marshal(reqBody) |
| 134 | + Expect(err).NotTo(HaveOccurred()) |
| 135 | + |
| 136 | + req := httptest.NewRequest(http.MethodPost, "/api/backends/install-external", bytes.NewBuffer(jsonBody)) |
| 137 | + req.Header.Set("Content-Type", "application/json") |
| 138 | + rec := httptest.NewRecorder() |
| 139 | + |
| 140 | + app.ServeHTTP(rec, req) |
| 141 | + |
| 142 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 143 | + |
| 144 | + var response map[string]interface{} |
| 145 | + err = json.Unmarshal(rec.Body.Bytes(), &response) |
| 146 | + Expect(err).NotTo(HaveOccurred()) |
| 147 | + Expect(response["jobID"]).NotTo(BeEmpty()) |
| 148 | + }) |
| 149 | + |
| 150 | + It("should return error for invalid JSON body", func() { |
| 151 | + req := httptest.NewRequest(http.MethodPost, "/api/backends/install-external", bytes.NewBufferString("invalid json")) |
| 152 | + req.Header.Set("Content-Type", "application/json") |
| 153 | + rec := httptest.NewRecorder() |
| 154 | + |
| 155 | + app.ServeHTTP(rec, req) |
| 156 | + |
| 157 | + Expect(rec.Code).To(Equal(http.StatusBadRequest)) |
| 158 | + }) |
| 159 | + }) |
| 160 | + |
| 161 | + Describe("GET /api/backends/job/:uid", func() { |
| 162 | + It("should return queued status for unknown job", func() { |
| 163 | + req := httptest.NewRequest(http.MethodGet, "/api/backends/job/unknown-job-id", nil) |
| 164 | + rec := httptest.NewRecorder() |
| 165 | + |
| 166 | + app.ServeHTTP(rec, req) |
| 167 | + |
| 168 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 169 | + |
| 170 | + var response map[string]interface{} |
| 171 | + err := json.Unmarshal(rec.Body.Bytes(), &response) |
| 172 | + Expect(err).NotTo(HaveOccurred()) |
| 173 | + Expect(response["queued"]).To(Equal(true)) |
| 174 | + Expect(response["processed"]).To(Equal(false)) |
| 175 | + }) |
| 176 | + }) |
| 177 | +}) |
| 178 | + |
| 179 | +// Helper function to make POST request |
| 180 | +func postRequest(url string, body interface{}) (*http.Response, error) { |
| 181 | + jsonBody, err := json.Marshal(body) |
| 182 | + if err != nil { |
| 183 | + return nil, err |
| 184 | + } |
| 185 | + |
| 186 | + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody)) |
| 187 | + if err != nil { |
| 188 | + return nil, err |
| 189 | + } |
| 190 | + req.Header.Set("Content-Type", "application/json") |
| 191 | + |
| 192 | + client := &http.Client{} |
| 193 | + return client.Do(req) |
| 194 | +} |
| 195 | + |
| 196 | +// Helper function to read response body |
| 197 | +func readResponseBody(resp *http.Response) (map[string]interface{}, error) { |
| 198 | + defer resp.Body.Close() |
| 199 | + body, err := io.ReadAll(resp.Body) |
| 200 | + if err != nil { |
| 201 | + return nil, err |
| 202 | + } |
| 203 | + |
| 204 | + var result map[string]interface{} |
| 205 | + err = json.Unmarshal(body, &result) |
| 206 | + return result, err |
| 207 | +} |
| 208 | + |
| 209 | +// Avoid unused import errors |
| 210 | +var _ = gallery.GalleryModel{} |
0 commit comments