Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions core/services/galleryop/backend_failed_op_cleanup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package galleryop_test

import (
"context"
"errors"
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/mudler/LocalAI/core/config"
"github.com/mudler/LocalAI/core/gallery"
"github.com/mudler/LocalAI/core/services/galleryop"
"github.com/mudler/LocalAI/pkg/system"
)

// failingBackendManager fails every install and upgrade, the way a download
// error does. It never touches the backends directory itself, so what is left
// on disk afterwards is what the handler did.
type failingBackendManager struct{ blockingBackendManager }

var errDownload = errors.New("download failed")

func (failingBackendManager) InstallBackend(context.Context, *galleryop.ManagementOp[gallery.GalleryBackend, any], galleryop.ProgressCallback) error {
return errDownload
}

func (failingBackendManager) UpgradeBackend(context.Context, *galleryop.ManagementOp[gallery.GalleryBackend, any], galleryop.ProgressCallback) error {
return errDownload
}

// A failed operation must not take an installed backend with it. The handler
// cleans up after a failed first install (a half-written directory), but the
// name of an upgrade is the working build it was meant to replace.
var _ = Describe("backend op failure cleanup", func() {
var (
backendsDir string
svc *galleryop.GalleryService
)

runShPath := func() string { return filepath.Join(backendsDir, "test-backend", "run.sh") }

runOp := func(id string, upgrade bool) *galleryop.OpStatus {
svc.EnqueueBackendOp(galleryop.ManagementOp[gallery.GalleryBackend, any]{
ID: id,
GalleryElementName: "test-backend",
Upgrade: upgrade,
})
Eventually(func() bool {
st := svc.GetStatus(id)
return st != nil && st.Processed
}, "5s", "20ms").Should(BeTrue())
return svc.GetStatus(id)
}

BeforeEach(func() {
var err error
backendsDir, err = os.MkdirTemp("", "failed-op-backends-*")
Expect(err).NotTo(HaveOccurred())
DeferCleanup(func() { Expect(os.RemoveAll(backendsDir)).To(Succeed()) })

Expect(os.MkdirAll(filepath.Dir(runShPath()), 0o755)).To(Succeed())
Expect(os.WriteFile(runShPath(), []byte("#!/bin/sh\necho installed\n"), 0o755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(backendsDir, "test-backend", "metadata.json"), []byte("{}"), 0o644)).To(Succeed())

systemState, err := system.GetSystemState(system.WithBackendPath(backendsDir))
Expect(err).NotTo(HaveOccurred())

svc = galleryop.NewGalleryService(&config.ApplicationConfig{SystemState: systemState}, nil)
svc.SetBackendManager(failingBackendManager{})
ctx, cancel := context.WithCancel(context.Background())
DeferCleanup(cancel)
Expect(svc.Start(ctx, nil, systemState)).To(Succeed())
})

It("keeps the installed backend when its upgrade fails", func() {
st := runOp("upgrade-fail", true)

Expect(st.Error).To(MatchError(errDownload))
Expect(runShPath()).To(BeARegularFile(), "a failed upgrade must leave the installed build runnable")
})

It("removes the leftover directory when a plain install fails", func() {
st := runOp("install-fail", false)

Expect(st.Error).To(MatchError(errDownload))
Expect(filepath.Dir(runShPath())).NotTo(BeADirectory())
})
})
8 changes: 6 additions & 2 deletions core/services/galleryop/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,12 @@ func (g *GalleryService) backendHandler(op *ManagementOp[gallery.GalleryBackend,
xlog.Error("error installing backend", "error", err, "backend", op.GalleryElementName)
// A refused op wrote nothing, so there is nothing to clean up — and
// the name may belong to an installed backend a forced reinstall
// targeted.
if !op.Delete && !errors.Is(err, gallery.ErrBackendInstallDisabled) {
// targeted. An upgrade must never run this cleanup either: its name
// is the installed, working build, gallery.UpgradeBackend restores it
// when its own swap fails, and a failure before the swap leaves it
// untouched. Removing it here would turn a failed upgrade into a
// lost backend.
if !op.Delete && !op.Upgrade && !errors.Is(err, gallery.ErrBackendInstallDisabled) {
// If we didn't install the backend, we need to make sure we don't have a leftover directory
gallery.DeleteBackendFromSystem(systemState, op.GalleryElementName)
}
Expand Down
Loading