mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 16:34:52 +03:00
test(ssl): preserve installer migration during upstream merge
Keep both installer regressions and verify a failed certificate still allows one migration before service startup. Retain the current PostgreSQL gates.
This commit is contained in:
+125
-5
@@ -31,7 +31,11 @@ func scriptFunction(t *testing.T, script, name string) string {
|
||||
const settingsCLIMock = `#!/bin/bash
|
||||
set -eu
|
||||
state=$TEST_SETTINGS_DIR
|
||||
if [[ "$1" == migrate ]]; then exit 0; fi
|
||||
if [[ "$1" == migrate ]]; then
|
||||
printf 'migrate\n' >> "$state/lifecycle"
|
||||
if [[ "$TEST_READ_MODE" == migration_failure ]]; then exit 1; fi
|
||||
exit 0
|
||||
fi
|
||||
shift
|
||||
case "$1" in
|
||||
-show)
|
||||
@@ -129,7 +133,10 @@ systemctl() { printf '%s\n' "$*" >> "$TEST_SETTINGS_DIR/restarts"; }
|
||||
rc-service() { printf '%s\n' "$*" >> "$TEST_SETTINGS_DIR/restarts"; }
|
||||
prompt_and_setup_ssl() {
|
||||
printf '%s\n' "$*" > "$TEST_SETTINGS_DIR/ssl"
|
||||
[[ "$TEST_READ_MODE" != ssl_failure ]]
|
||||
if [[ "$TEST_READ_MODE" == ssl_failure ]]; then
|
||||
printf 'ssl\n' >> "$TEST_SETTINGS_DIR/lifecycle"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
write_install_result() { printf '%s\n' "$@" > "$TEST_SETTINGS_DIR/result"; }
|
||||
install_postgres_local() { echo 'unexpected PostgreSQL installation' >&2; exit 97; }
|
||||
@@ -154,7 +161,8 @@ fail2ban_mode=install
|
||||
body := scriptFunction(t, script, parent)
|
||||
startCall := function
|
||||
if script == "update.sh" {
|
||||
startCall = "setup_fail2ban"
|
||||
startCall = `echo -e "${green}Migrating database...${plain}"`
|
||||
driver += scriptFunction(t, script, "_fail")
|
||||
}
|
||||
start := strings.Index(body, "\n "+startCall)
|
||||
if start < 0 {
|
||||
@@ -283,7 +291,7 @@ func TestScriptConfigReadFailureKeepsServiceSetup(t *testing.T) {
|
||||
if err != nil || !strings.Contains(out, "Unable to read webBasePath") {
|
||||
t.Errorf("service setup should continue after the settings warning: %v\n%s", err, out)
|
||||
}
|
||||
wantLifecycle, wantServices := "fail2ban\nunit\n", "daemon-reload\nenable x-ui\nstart x-ui\n"
|
||||
wantLifecycle, wantServices := "migrate\nfail2ban\nunit\n", "daemon-reload\nenable x-ui\nstart x-ui\n"
|
||||
if script == "install.sh" {
|
||||
wantServices = "daemon-reload\nenable x-ui\nrestart x-ui\n"
|
||||
}
|
||||
@@ -308,10 +316,122 @@ func TestInstallerSSLFailureKeepsServiceSetup(t *testing.T) {
|
||||
t.Fatalf("SSL setup was not attempted: %v\n%s", err, out)
|
||||
}
|
||||
for name, want := range map[string]string{
|
||||
"lifecycle": "fail2ban\nunit\n", "restarts": "daemon-reload\nenable x-ui\nrestart x-ui\n",
|
||||
"lifecycle": "ssl\nmigrate\nfail2ban\nunit\n", "restarts": "daemon-reload\nenable x-ui\nrestart x-ui\n",
|
||||
} {
|
||||
if actual := readScriptState(t, dir, name); actual != want {
|
||||
t.Errorf("%s after a failed certificate = %q, want %q: the panel must still be installed and started", name, actual, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallMigrationFailurePreventsServiceStart(t *testing.T) {
|
||||
dir, out, err := runScriptConfig(t, "install.sh", "/a/", false, "migration_failure", 1, true)
|
||||
if err == nil || !strings.Contains(out, "Database migration failed") {
|
||||
t.Fatalf("migration failure should abort the update: %v\n%s", err, out)
|
||||
}
|
||||
if got := readScriptState(t, dir, "restarts"); got != "" {
|
||||
t.Fatalf("service started after a failed migration: %q", got)
|
||||
}
|
||||
if got := readScriptState(t, dir, "lifecycle"); got != "migrate\n" {
|
||||
t.Fatalf("lifecycle after failed migration: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateMigrationFailureKeepsServiceUnitForRetry(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("updater regression requires a POSIX environment")
|
||||
}
|
||||
bash, err := exec.LookPath("bash")
|
||||
if err != nil {
|
||||
t.Skip("bash is required for the isolated updater regression")
|
||||
}
|
||||
for _, distro := range []struct {
|
||||
name, unit, stopped, started string
|
||||
}{
|
||||
{"ubuntu", "systemd/x-ui.service", "systemctl stop x-ui\nsystemctl disable x-ui\n", "systemctl daemon-reload\nsystemctl enable x-ui\nsystemctl start x-ui\n"},
|
||||
{"alpine", "init.d/x-ui", "rc-service x-ui stop\nrc-update del x-ui\n", "rc-update add x-ui\nrc-service x-ui start\n"},
|
||||
} {
|
||||
t.Run(distro.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for _, folder := range []string{"x-ui", "systemd", "init.d", "release/x-ui", "bin"} {
|
||||
if err := os.MkdirAll(filepath.Join(dir, folder), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
const binary = `#!/bin/bash
|
||||
if [[ $1 == migrate ]]; then
|
||||
printf 'migrate\n' >> "$TEST_ROOT/events"
|
||||
[[ $TEST_FAIL_MIGRATION == 0 ]]
|
||||
else
|
||||
printf 'test version\n'
|
||||
fi
|
||||
`
|
||||
for name, value := range map[string]string{
|
||||
distro.unit: "old unit\n", "x-ui/x-ui": binary, "release/x-ui/x-ui": binary,
|
||||
"release/x-ui/x-ui.sh": "#!/bin/bash\n", "release/x-ui/x-ui.service": "new unit\n",
|
||||
} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(value), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
driver := `red= green= yellow= blue= plain=
|
||||
xui_folder="$TEST_ROOT/x-ui"
|
||||
xui_service="$TEST_ROOT/systemd"
|
||||
release="$TEST_DISTRO"
|
||||
XUI_UPDATE_TAG=test
|
||||
curl_bin=fake_curl
|
||||
load_xui_env() { :; }
|
||||
setup_fail2ban() { :; }
|
||||
config_after_update() { :; }
|
||||
_xui_service_write_paths_dropin() { :; }
|
||||
_xui_warn_unsupported_hardening() { :; }
|
||||
chown() { :; }
|
||||
pkill() { :; }
|
||||
systemctl() { printf 'systemctl %s\n' "$*" >> "$TEST_ROOT/events"; }
|
||||
rc-service() { printf 'rc-service %s\n' "$*" >> "$TEST_ROOT/events"; }
|
||||
rc-update() { printf 'rc-update %s\n' "$*" >> "$TEST_ROOT/events"; }
|
||||
fake_curl() {
|
||||
case "$*" in
|
||||
*.sha256) printf 404 ;;
|
||||
*/x-ui.rc) printf 'new unit\n' > "$2" ;;
|
||||
*) tar -czf "$2" -C "$TEST_ROOT/release" x-ui ;;
|
||||
esac
|
||||
}
|
||||
`
|
||||
driver += scriptFunction(t, "update.sh", "_fail") + scriptFunction(t, "update.sh", "_install_xui_service_unit")
|
||||
driver += strings.NewReplacer(
|
||||
"/etc/init.d/x-ui", "$TEST_ROOT/init.d/x-ui",
|
||||
"/usr/bin/x-ui", "$TEST_ROOT/bin/x-ui",
|
||||
"/var/log/x-ui", "$TEST_ROOT/log",
|
||||
).Replace(scriptFunction(t, "update.sh", "update_x-ui"))
|
||||
driver += "update_x-ui\n"
|
||||
|
||||
run := func(failMigration string) (string, error) {
|
||||
t.Helper()
|
||||
cmd := exec.Command(bash, "-c", driver)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "TEST_ROOT="+dir, "TEST_DISTRO="+distro.name, "TEST_FAIL_MIGRATION="+failMigration)
|
||||
out, err := cmd.CombinedOutput()
|
||||
return string(out), err
|
||||
}
|
||||
if out, err := run("1"); err == nil || !strings.Contains(out, "Database migration failed") {
|
||||
t.Fatalf("first update should fail in migration: %v\n%s", err, out)
|
||||
}
|
||||
if unit := readScriptState(t, dir, distro.unit); unit != "old unit\n" {
|
||||
t.Fatalf("failed update changed the service unit: %q", unit)
|
||||
}
|
||||
if got := readScriptState(t, dir, "events"); got != distro.stopped+"migrate\n" {
|
||||
t.Fatalf("failed update service actions: %q", got)
|
||||
}
|
||||
if out, err := run("0"); err != nil {
|
||||
t.Fatalf("ordinary update retry failed: %v\n%s", err, out)
|
||||
}
|
||||
if unit := readScriptState(t, dir, distro.unit); unit != "new unit\n" {
|
||||
t.Fatalf("retry did not install the replacement unit: %q", unit)
|
||||
}
|
||||
if got := readScriptState(t, dir, "events"); got != strings.Repeat(distro.stopped+"migrate\n", 2)+distro.started {
|
||||
t.Fatalf("retry service actions: %q", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user