| | package launcher_test |
| |
|
| | import ( |
| | "os" |
| | "path/filepath" |
| | "runtime" |
| | "time" |
| |
|
| | . "github.com/onsi/ginkgo/v2" |
| | . "github.com/onsi/gomega" |
| |
|
| | launcher "github.com/mudler/LocalAI/cmd/launcher/internal" |
| | ) |
| |
|
| | var _ = Describe("ReleaseManager", func() { |
| | var ( |
| | rm *launcher.ReleaseManager |
| | tempDir string |
| | ) |
| |
|
| | BeforeEach(func() { |
| | var err error |
| | tempDir, err = os.MkdirTemp("", "launcher-test-*") |
| | Expect(err).ToNot(HaveOccurred()) |
| |
|
| | rm = launcher.NewReleaseManager() |
| | |
| | rm.BinaryPath = tempDir |
| | }) |
| |
|
| | AfterEach(func() { |
| | os.RemoveAll(tempDir) |
| | }) |
| |
|
| | Describe("NewReleaseManager", func() { |
| | It("should create a release manager with correct defaults", func() { |
| | newRM := launcher.NewReleaseManager() |
| | Expect(newRM.GitHubOwner).To(Equal("mudler")) |
| | Expect(newRM.GitHubRepo).To(Equal("LocalAI")) |
| | Expect(newRM.BinaryPath).To(ContainSubstring(".localai")) |
| | Expect(newRM.HTTPClient).ToNot(BeNil()) |
| | Expect(newRM.HTTPClient.Timeout).To(Equal(30 * time.Second)) |
| | }) |
| | }) |
| |
|
| | Describe("GetBinaryName", func() { |
| | It("should return correct binary name for current platform", func() { |
| | binaryName := rm.GetBinaryName("v3.4.0") |
| | expectedOS := runtime.GOOS |
| | expectedArch := runtime.GOARCH |
| |
|
| | expected := "local-ai-v3.4.0-" + expectedOS + "-" + expectedArch |
| | Expect(binaryName).To(Equal(expected)) |
| | }) |
| |
|
| | It("should handle version with and without 'v' prefix", func() { |
| | withV := rm.GetBinaryName("v3.4.0") |
| | withoutV := rm.GetBinaryName("3.4.0") |
| |
|
| | |
| | Expect(withV).To(Equal(withoutV)) |
| | }) |
| | }) |
| |
|
| | Describe("GetBinaryPath", func() { |
| | It("should return the correct binary path", func() { |
| | path := rm.GetBinaryPath() |
| | expected := filepath.Join(tempDir, "local-ai") |
| | Expect(path).To(Equal(expected)) |
| | }) |
| | }) |
| |
|
| | Describe("GetInstalledVersion", func() { |
| | It("should return empty when no binary exists", func() { |
| | version := rm.GetInstalledVersion() |
| | Expect(version).To(BeEmpty()) |
| | }) |
| |
|
| | It("should return empty version when binary exists but no metadata", func() { |
| | |
| | err := os.MkdirAll(rm.BinaryPath, 0755) |
| | Expect(err).ToNot(HaveOccurred()) |
| |
|
| | binaryPath := rm.GetBinaryPath() |
| | err = os.WriteFile(binaryPath, []byte("fake binary"), 0755) |
| | Expect(err).ToNot(HaveOccurred()) |
| |
|
| | version := rm.GetInstalledVersion() |
| | Expect(version).To(BeEmpty()) |
| | }) |
| | }) |
| |
|
| | Context("with mocked responses", func() { |
| | |
| | |
| |
|
| | Describe("GetLatestRelease", func() { |
| | It("should handle network errors gracefully", func() { |
| | |
| | |
| | _, err := rm.GetLatestRelease() |
| | |
| | |
| | if err != nil { |
| | Expect(err.Error()).To(ContainSubstring("failed to fetch")) |
| | } |
| | }) |
| | }) |
| |
|
| | Describe("DownloadRelease", func() { |
| | It("should create binary directory if it doesn't exist", func() { |
| | |
| | os.RemoveAll(tempDir) |
| |
|
| | |
| | rm.DownloadRelease("v3.4.0", nil) |
| |
|
| | |
| | _, err := os.Stat(tempDir) |
| | Expect(err).ToNot(HaveOccurred()) |
| | }) |
| | }) |
| | }) |
| |
|
| | Describe("VerifyChecksum functionality", func() { |
| | var ( |
| | testFile string |
| | checksumFile string |
| | ) |
| |
|
| | BeforeEach(func() { |
| | testFile = filepath.Join(tempDir, "test-binary") |
| | checksumFile = filepath.Join(tempDir, "checksums.txt") |
| | }) |
| |
|
| | It("should verify checksums correctly", func() { |
| | |
| | testContent := []byte("test content for checksum") |
| | err := os.WriteFile(testFile, testContent, 0644) |
| | Expect(err).ToNot(HaveOccurred()) |
| |
|
| | |
| | |
| | checksumContent := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 test-binary\n" |
| | err = os.WriteFile(checksumFile, []byte(checksumContent), 0644) |
| | Expect(err).ToNot(HaveOccurred()) |
| |
|
| | |
| | |
| | |
| | err = rm.VerifyChecksum(testFile, checksumFile, "test-binary") |
| | |
| | Expect(err).To(HaveOccurred()) |
| | Expect(err.Error()).To(ContainSubstring("checksum mismatch")) |
| | }) |
| |
|
| | It("should handle missing checksum file", func() { |
| | |
| | err := os.WriteFile(testFile, []byte("test"), 0644) |
| | Expect(err).ToNot(HaveOccurred()) |
| |
|
| | err = rm.VerifyChecksum(testFile, checksumFile, "test-binary") |
| | Expect(err).To(HaveOccurred()) |
| | Expect(err.Error()).To(ContainSubstring("failed to open checksums file")) |
| | }) |
| |
|
| | It("should handle missing binary in checksums", func() { |
| | |
| | err := os.WriteFile(testFile, []byte("test"), 0644) |
| | Expect(err).ToNot(HaveOccurred()) |
| |
|
| | checksumContent := "hash other-binary\n" |
| | err = os.WriteFile(checksumFile, []byte(checksumContent), 0644) |
| | Expect(err).ToNot(HaveOccurred()) |
| |
|
| | err = rm.VerifyChecksum(testFile, checksumFile, "test-binary") |
| | Expect(err).To(HaveOccurred()) |
| | Expect(err.Error()).To(ContainSubstring("checksum not found")) |
| | }) |
| | }) |
| | }) |
| |
|