295 lines
11 KiB
Go
295 lines
11 KiB
Go
package panel
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPHPPatchUpdateUsesOnlyAdvertisedFreeBranch(t *testing.T) {
|
|
submitted := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.FormValue("action") {
|
|
case "get_soft_find":
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"name": "php-8.4", "price": 0, "panel_free": 1, "setup": true, "version": "8.4.25", "versions": []map[string]any{{"m_version": "8.4", "version": "27", "beta": 0}}})
|
|
case "install_plugin":
|
|
submitted++
|
|
if r.FormValue("sName") != "php-8.4" || r.FormValue("version") != "8.4" || r.FormValue("min_version") != "27" || r.FormValue("upgrade") != "1" {
|
|
t.Error("PHP update parameters escaped the selected branch")
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"status": true})
|
|
default:
|
|
t.Errorf("unexpected request %s", r.FormValue("action"))
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
client := NewClient(server.URL, "test", false, 5*time.Second)
|
|
if err := client.UpdatePHPRuntime(context.Background(), "8.4", "8.5.0"); err == nil {
|
|
t.Fatal("cross-branch patch target accepted")
|
|
}
|
|
if submitted != 0 {
|
|
t.Fatal("invalid target mutated panel")
|
|
}
|
|
if err := client.UpdatePHPRuntime(context.Background(), "8.4", "8.4.27"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if submitted != 1 {
|
|
t.Fatalf("submitted %d updates", submitted)
|
|
}
|
|
for _, values := range []url.Values{
|
|
{"action": {"install_plugin"}, "sName": {"php-8.4"}, "version": {"8.5"}, "min_version": {"27"}, "type": {"0"}, "upgrade": {"1"}},
|
|
{"action": {"install_plugin"}, "sName": {"php-8.4"}, "version": {"8.4"}, "min_version": {"27;id"}, "type": {"0"}, "upgrade": {"1"}},
|
|
{"action": {"getData"}, "table": {"tasks"}, "search": {""}},
|
|
} {
|
|
route := "/plugin"
|
|
if values.Get("table") != "" {
|
|
route = "/data"
|
|
}
|
|
if validateFreePanelRequest("POST", route, values) == nil {
|
|
t.Fatal("invalid maintenance operation passed allowlist")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPHPQueueUsesTaskTypeAndExactBranch(t *testing.T) {
|
|
for _, test := range []struct {
|
|
tasks string
|
|
pending bool
|
|
}{
|
|
{`[{"name":"upgrade[php-8.4]","status":"0"}]`, true},
|
|
{`[{"name":"upgrade[php-8.4]","status":-1}]`, true},
|
|
{`[{"name":"upgrade[php-8.5]","status":"0"},{"name":"upgrade[php-8.4]","status":"1"}]`, false},
|
|
{`[]`, false},
|
|
} {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.FormValue("search") != "execshell" || r.FormValue("table") != "tasks" {
|
|
t.Error("aaPanel task search must use type rather than name")
|
|
}
|
|
_, _ = w.Write([]byte(`{"data":` + test.tasks + `}`))
|
|
}))
|
|
client := NewClient(server.URL, "test", false, time.Second)
|
|
pending, err := client.PHPRuntimeUpdatePending(context.Background(), "8.4")
|
|
server.Close()
|
|
if err != nil || pending != test.pending {
|
|
t.Fatalf("queue result=%v err=%v", pending, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
type runtimeReplacementPanel struct {
|
|
Interface
|
|
calls []RuntimeSpec
|
|
failVersion string
|
|
}
|
|
|
|
func (p *runtimeReplacementPanel) RuntimeEnvironments(context.Context) ([]RuntimeEnvironment, error) {
|
|
return []RuntimeEnvironment{{Name: "Python", Installed: []string{"3.12.0", "3.12.15"}}}, nil
|
|
}
|
|
func (p *runtimeReplacementPanel) ConfigureRuntime(_ context.Context, s RuntimeSpec) error {
|
|
p.calls = append(p.calls, s)
|
|
if s.Version == p.failVersion {
|
|
return errors.New("injected launch failure")
|
|
}
|
|
return nil
|
|
}
|
|
func TestBrokerRuntimeReplacementRollsBackAndRejectsPathChanges(t *testing.T) {
|
|
direct := &runtimeReplacementPanel{Interface: NewMock(), failVersion: "3.12.15"}
|
|
server, err := newBrokerServer(t.TempDir(), t.TempDir(), "01234567890123456789012345678901", func(BrokerCredentials) Interface { return direct })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server.direct = direct
|
|
old := RuntimeSpec{ProjectName: "sv0123456789", Runtime: "Python", Version: "3.12.0", Path: filepath.Join(server.tenantRoot, "sv0123456789"), RunPath: "/", RunUser: "sv0123456789", Port: 20501, ProcessLimit: 10}
|
|
server.registry.Sites[1] = &managedSite{Runtime: &old}
|
|
next := old
|
|
next.Version = "3.12.15"
|
|
next.Path = "/etc"
|
|
if _, err = server.execute(context.Background(), brokerRequest{Operation: "replace_runtime", SiteID: 1, Runtime: &next}); err == nil {
|
|
t.Fatal("unmanaged path accepted")
|
|
}
|
|
if len(direct.calls) != 0 {
|
|
t.Fatal("invalid runtime triggered mutation")
|
|
}
|
|
next.Path = old.Path
|
|
if _, err = server.execute(context.Background(), brokerRequest{Operation: "replace_runtime", SiteID: 1, Runtime: &next}); err == nil {
|
|
t.Fatal("injected migration failure hidden")
|
|
}
|
|
if !reflect.DeepEqual(direct.calls, []RuntimeSpec{next, old}) {
|
|
t.Fatalf("rollback calls=%#v", direct.calls)
|
|
}
|
|
if *server.registry.Sites[1].Runtime != old {
|
|
t.Fatal("registry lost original runtime")
|
|
}
|
|
direct.failVersion = ""
|
|
if _, err = server.execute(context.Background(), brokerRequest{Operation: "replace_runtime", SiteID: 1, Runtime: &next}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if *server.registry.Sites[1].Runtime != next {
|
|
t.Fatal("registry did not commit target runtime")
|
|
}
|
|
// The old patch is still installed and must remain available for rollback.
|
|
if _, err = server.execute(context.Background(), brokerRequest{Operation: "replace_runtime", SiteID: 1, Runtime: &old}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before := len(direct.calls)
|
|
if _, err = server.execute(context.Background(), brokerRequest{Operation: "replace_runtime", SiteID: 1, Runtime: &old}); err != nil || len(direct.calls) != before+1 {
|
|
t.Fatal("same registered version must still repair a missing actual project")
|
|
}
|
|
}
|
|
|
|
func TestRuntimeRemovalStopsListenerBeforeDeletingMetadata(t *testing.T) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
port := listener.Addr().(*net.TCPAddr).Port
|
|
var calls []string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch r.URL.Path {
|
|
case "/project/python/StopProject/stype":
|
|
calls = append(calls, "stop")
|
|
_ = listener.Close()
|
|
case "/project/python/RemoveProject/stype":
|
|
calls = append(calls, "remove")
|
|
default:
|
|
t.Errorf("unexpected route: %s", r.URL.Path)
|
|
}
|
|
_, _ = w.Write([]byte(`{"status":true}`))
|
|
}))
|
|
defer server.Close()
|
|
client := NewClient(server.URL, "test", false, time.Second)
|
|
if err = client.RemoveRuntime(context.Background(), RuntimeSpec{Runtime: "Python", ProjectName: "sv0123456789", Port: port}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Join(calls, ",") != "stop,remove" {
|
|
t.Fatalf("runtime deletion order = %v", calls)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeRemovalPreservesMetadataWhenStopDoesNotCloseListener(t *testing.T) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
port := listener.Addr().(*net.TCPAddr).Port
|
|
removed := false
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if r.URL.Path == "/project/python/RemoveProject/stype" {
|
|
removed = true
|
|
}
|
|
_, _ = w.Write([]byte(`{"status":true}`))
|
|
}))
|
|
defer server.Close()
|
|
client := NewClient(server.URL, "test", false, time.Second)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond)
|
|
defer cancel()
|
|
if err = client.RemoveRuntime(ctx, RuntimeSpec{Runtime: "Python", ProjectName: "sv0123456789", Port: port}); err == nil {
|
|
t.Fatal("runtime with a residual listener was removed")
|
|
}
|
|
if removed {
|
|
t.Fatal("project metadata was deleted before its listener stopped")
|
|
}
|
|
}
|
|
|
|
func TestRuntimeRemovalRetryRequiresAbsentRecordAndClosedPort(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
present, listening, invalid bool
|
|
wantErr bool
|
|
}{
|
|
{"absent", false, false, false, false}, {"residual listener", false, true, false, true}, {"record remains", true, false, false, true}, {"unverifiable", false, false, true, true},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
port := listener.Addr().(*net.TCPAddr).Port
|
|
if !test.listening {
|
|
_ = listener.Close()
|
|
} else {
|
|
defer listener.Close()
|
|
}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/project/python/RemoveProject/stype" {
|
|
_, _ = w.Write([]byte(`{"status":false,"msg":"missing project"}`))
|
|
return
|
|
}
|
|
if test.invalid {
|
|
_, _ = w.Write([]byte(`{"status":false}`))
|
|
return
|
|
}
|
|
items := []map[string]any{}
|
|
if test.present {
|
|
items = append(items, map[string]any{"name": "sv0123456789"})
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"data": items})
|
|
}))
|
|
defer server.Close()
|
|
client := NewClient(server.URL, "test", false, time.Second)
|
|
err = client.RemoveRuntime(context.Background(), RuntimeSpec{Runtime: "Python", ProjectName: "sv0123456789", Port: port})
|
|
if (err != nil) != test.wantErr {
|
|
t.Fatalf("removal error=%v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type runtimeResetTestPanel struct {
|
|
Interface
|
|
proxies []string
|
|
removed int
|
|
}
|
|
|
|
func (p *runtimeResetTestPanel) RemoveManagedProxy(_ context.Context, name string) error {
|
|
p.proxies = append(p.proxies, name)
|
|
return nil
|
|
}
|
|
func (p *runtimeResetTestPanel) RemoveRuntime(context.Context, RuntimeSpec) error {
|
|
p.removed++
|
|
return nil
|
|
}
|
|
func TestRuntimeResetOnlyTouchesManagedSiteAndFixedProxy(t *testing.T) {
|
|
direct := &runtimeResetTestPanel{Interface: NewMock()}
|
|
server, e := newBrokerServer(t.TempDir(), t.TempDir(), "01234567890123456789012345678901", func(BrokerCredentials) Interface { return direct })
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
server.direct = direct
|
|
old := RuntimeSpec{Runtime: "Node.js", ProjectName: "sv0123456789", Port: 20050}
|
|
server.registry.Sites[3] = &managedSite{Domain: "host.example.com", Runtime: &old, Port: 20050, ProxyReady: true}
|
|
for _, req := range []brokerRequest{{Operation: "reset_managed_runtime", SiteID: 9, Name: "host.example.com"}, {Operation: "reset_managed_runtime", SiteID: 3, Name: "outside.example.com"}} {
|
|
if _, e = server.execute(context.Background(), req); e == nil {
|
|
t.Fatal("unmanaged runtime reset accepted")
|
|
}
|
|
}
|
|
if direct.removed != 0 || len(direct.proxies) != 0 {
|
|
t.Fatal("unmanaged request mutated panel")
|
|
}
|
|
if _, e = server.execute(context.Background(), brokerRequest{Operation: "reset_managed_runtime", SiteID: 3, Name: "host.example.com"}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if direct.removed != 1 || !reflect.DeepEqual(direct.proxies, []string{"host.example.com"}) || server.registry.Sites[3].Runtime != nil || server.registry.Sites[3].ProxyReady {
|
|
t.Fatal("managed reset incomplete")
|
|
}
|
|
if _, e = server.execute(context.Background(), brokerRequest{Operation: "reset_managed_runtime", SiteID: 3, Name: "host.example.com"}); e != nil {
|
|
t.Fatal("reset not idempotent", e)
|
|
}
|
|
if direct.removed != 1 {
|
|
t.Fatal("repeated reset removed another project")
|
|
}
|
|
}
|