From 9e555bb1879c1bec8167338b3760abf4f8b50092 Mon Sep 17 00:00:00 2001 From: Saarthak Verma Date: Sat, 4 Jan 2025 17:39:22 +0530 Subject: [PATCH] test(resume downloads): implement file download tc Signed-off-by: Saarthak Verma --- pkg/downloader/uri_test.go | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/pkg/downloader/uri_test.go b/pkg/downloader/uri_test.go index 861ee90e..c2ac43b8 100644 --- a/pkg/downloader/uri_test.go +++ b/pkg/downloader/uri_test.go @@ -1,6 +1,13 @@ package downloader_test import ( + "crypto/rand" + "crypto/sha256" + "fmt" + "net/http" + "net/http/httptest" + "os" + . "github.com/mudler/LocalAI/pkg/downloader" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -40,8 +47,32 @@ var _ = Describe("Gallery API tests", func() { }) var _ = Describe("Download Test", func() { - Context("URI", func() { - It("Resumes partially downloaded files") - It("It accurately updates progress") + Context("URI DownloadFile", func() { + It("fetches files from mock server", func() { + mockData := make([]byte, 20000) + _, err := rand.Read(mockData) + Expect(err).ToNot(HaveOccurred()) + + mockDataSha := sha256.New() + mockDataSha.Write(mockData) + + mockServer := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write(mockData) + })) + mockServer.EnableHTTP2 = true + mockServer.Start() + dir, err := os.Getwd() + filePath := dir + "/my_supercool_model" + Expect(err).NotTo(HaveOccurred()) + uri := URI(mockServer.URL) + err = uri.DownloadFile(filePath, fmt.Sprintf("%x", mockDataSha.Sum(nil)), 1, 1, func(s1, s2, s3 string, f float64) {}) + Expect(err).ToNot(HaveOccurred()) + err = os.Remove(filePath) // cleanup, also checks existance of filePath` + Expect(err).ToNot(HaveOccurred()) + }) + // It("resumes partially downloaded files") + // It("it accurately updates progress") + // It("deletes partial file if after completion hash of downloaded file doesn't match hash of the file in the server") }) })