fix: use panel Python common catalog and track install jobs

This commit is contained in:
canglan committed 2026-10-30 15:20:49 +08:00
1 parent 248badd33e
commit d7c007adc4
12 files changed
+202 -22

No files matched your search

@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/go-sql-driver/mysql"
"net/http"
"net/http/httptest"
@@ -17,6 +18,7 @@ import (
"sea-vhost/internal/store"
"strconv"
"strings"
"sync"
"testing"
"time"
)
@@ -118,6 +120,49 @@ func TestResourceCRUDMySQL(t *testing.T) {
must(e)
must(db.UpdateRuntimeJob(ctx, last.ID, "completed", "verified", "test"))
})
t.Run("Python submission returns its job and uses only the source isolator", func(t *testing.T) {
p := &pythonInstallPanel{Mock: panel.NewMock()}
isolator := pythonInstallIsolation{Mock: isolation.Mock{Root: root}, panel: p}
svc := service.NewProjects(db, p, isolator, "hosts.example.invalid", root, true, secret, "control.example.invalid")
api := New(config.Config{SessionSecret: secret, ManagementDomain: "control.example.invalid", CookieSecure: true, SessionTTL: time.Hour}, db, svc)
r := httptest.NewRequest("POST", "https://control.example.invalid/api/v1/admin/runtime-environments/Python/install", strings.NewReader(`{"version":"3.13.1"}`))
r.Header.Set("Origin", "https://control.example.invalid")
r.Header.Set("Content-Type", "application/json")
r.AddCookie(&http.Cookie{Name: "sea_session", Value: token})
w := httptest.NewRecorder()
api.Handler().ServeHTTP(w, r)
var submitted struct {
OK bool `json:"ok"`
JobID uint64 `json:"jobId"`
}
if w.Code != 202 || json.Unmarshal(w.Body.Bytes(), &submitted) != nil || !submitted.OK || submitted.JobID == 0 {
t.Fatalf("missing correlated installation task: %d %s", w.Code, w.Body.String())
}
workerCtx, stop := context.WithCancel(ctx)
done := make(chan struct{})
go func() { defer close(done); svc.RunRuntimeMaintenance(workerCtx) }()
defer func() { stop(); <-done }()
deadline := time.Now().Add(7 * time.Second)
for {
job, e := db.RuntimeJob(ctx, submitted.JobID)
if e != nil {
t.Fatal(e)
}
if job.Status == "completed" {
p.mu.Lock()
calls, installed := p.sourceCalls, p.installed
p.mu.Unlock()
if calls != 1 || !installed || job.TargetVersion != "3.13.1" || !strings.Contains(job.Message, "已安装并核验") {
t.Fatalf("unexpected Python installation result: %#v", job)
}
break
}
if time.Now().After(deadline) || job.Status == "failed" || job.Status == "interrupted" {
t.Fatalf("Python installation did not complete through isolator: %#v", job)
}
time.Sleep(50 * time.Millisecond)
}
})
account, err := db.CreateTenant(ctx, store.NewTenant{Slug: "crud-customer", Name: "CRUD 用户", Email: "crud-customer@example.invalid", DisplayName: "测试联系人", Password: "crud-customer-password-123", PlanID: 1})
must(err)
user, err := db.UserByLogin(ctx, "crud-customer@example.invalid")
@@ -485,6 +530,47 @@ type pendingStatePanel struct {
states map[int64]bool
}
type pythonInstallPanel struct {
*panel.Mock
mu sync.Mutex
installed bool
sourceCalls int
}
func (p *pythonInstallPanel) RuntimeEnvironments(ctx context.Context) ([]panel.RuntimeEnvironment, error) {
items, err := p.Mock.RuntimeEnvironments(ctx)
p.mu.Lock()
installed := p.installed
p.mu.Unlock()
if installed {
for i := range items {
if items[i].Name == "Python" {
items[i].Installed = append(items[i].Installed, "3.13.1")
}
}
}
return items, err
}
func (p *pythonInstallPanel) InstallRuntimeEnvironment(context.Context, string, string) error {
return fmt.Errorf("unexpected panel installer; Python must use the source isolator")
}
type pythonInstallIsolation struct {
isolation.Mock
panel *pythonInstallPanel
}
func (i pythonInstallIsolation) InstallPythonRuntime(_ context.Context, version string) error {
if version != "3.13.1" {
return fmt.Errorf("unexpected Python version")
}
i.panel.mu.Lock()
defer i.panel.mu.Unlock()
i.panel.installed = true
i.panel.sourceCalls++
return nil
}
type busyPatchPanel struct {
*panel.Mock
entered chan struct{}