Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -4,10 +4,12 @@ import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -193,6 +195,154 @@ func TestImportTarRejectsUnknownDiskId(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransferExportProtectsWritesAndCleansOnlyItsSource(t *testing.T) {
|
||||
agent := newReinstallRaceTestAgent(t)
|
||||
const name = "alpha"
|
||||
const transferID = "1234567890abcdef1234567890abcdef"
|
||||
serverDir := seedRaceServer(t, agent, name)
|
||||
containerPresent := true
|
||||
var removedContainer string
|
||||
dockerEngineHTTPClient = &http.Client{Transport: reinstallDockerTransportFunc(func(request *http.Request) (*http.Response, error) {
|
||||
status, body := http.StatusNotFound, `{"message":"fixture not found"}`
|
||||
switch {
|
||||
case request.Method == http.MethodGet && strings.HasSuffix(request.URL.Path, "/containers/alpha/json") && containerPresent:
|
||||
status = http.StatusOK
|
||||
body = `{"Id":"` + reinstallFixtureContainerID + `","Name":"/alpha","State":{"Status":"exited"}}`
|
||||
case request.Method == http.MethodDelete:
|
||||
removedContainer = filepath.Base(request.URL.Path)
|
||||
containerPresent = false
|
||||
status, body = http.StatusNoContent, ""
|
||||
}
|
||||
return &http.Response{StatusCode: status, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}, nil
|
||||
})}
|
||||
endpoint := "/v1/servers/alpha/export?transferId="
|
||||
export := func() {
|
||||
t.Helper()
|
||||
response := httptest.NewRecorder()
|
||||
agent.handleExportServer(response, httptest.NewRequest(http.MethodGet, endpoint+transferID, nil), name)
|
||||
if response.Code != http.StatusOK || response.Header().Get("X-ADPanel-Transfer-Id") != transferID {
|
||||
t.Fatalf("protected export: got %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
finish := func(query string) *httptest.ResponseRecorder {
|
||||
response := httptest.NewRecorder()
|
||||
agent.handleFinishTransferExport(response, httptest.NewRequest(http.MethodDelete, endpoint+query, nil), name)
|
||||
return response
|
||||
}
|
||||
write := func() *httptest.ResponseRecorder {
|
||||
return fsPost(t, agent.handleFSWrite, "/v1/fs/write", map[string]any{
|
||||
"path": filepath.Join(serverDir, "world-data.txt"), "server": name, "content": "updated",
|
||||
})
|
||||
}
|
||||
|
||||
export()
|
||||
if response := write(); response.Code != http.StatusConflict || !strings.Contains(response.Body.String(), "transfer_in_progress") {
|
||||
t.Fatalf("file write was not fenced: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
startReply := httptest.NewRecorder()
|
||||
agent.handleStart(startReply, httptest.NewRequest(http.MethodPost, "/v1/servers/alpha/start", nil), name)
|
||||
if startReply.Code != http.StatusConflict || !strings.Contains(startReply.Body.String(), "transfer_in_progress") {
|
||||
t.Fatalf("start was not fenced: %d %s", startReply.Code, startReply.Body.String())
|
||||
}
|
||||
if response := finish(strings.Repeat("a", 32) + "&cleanup=true"); response.Code != http.StatusConflict || removedContainer != "" {
|
||||
t.Fatalf("unowned cleanup changed the source: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
if response := finish(transferID); response.Code != http.StatusOK {
|
||||
t.Fatalf("rollback could not release the hold: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
if response := write(); response.Code != http.StatusOK {
|
||||
t.Fatalf("write was not restored after rollback: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
export()
|
||||
if response := finish(transferID + "&cleanup=true"); response.Code != http.StatusOK || removedContainer != reinstallFixtureContainerID {
|
||||
t.Fatalf("owned cleanup did not remove the original container: %d %s, removed %q", response.Code, response.Body.String(), removedContainer)
|
||||
}
|
||||
if response := finish(transferID + "&cleanup=true"); response.Code != http.StatusOK {
|
||||
t.Fatalf("completed cleanup must be retryable: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
if err := os.MkdirAll(serverDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response := finish(transferID + "&cleanup=true"); response.Code != http.StatusConflict {
|
||||
t.Fatalf("stale cleanup accepted replacement data: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransferImportDoesNotReplaceOrCleanUnownedDestination(t *testing.T) {
|
||||
agent, _ := newTestAgent(t)
|
||||
name := "fixture"
|
||||
disk := registerTestDisk(t, agent, t.TempDir(), 0)
|
||||
destination := filepath.Join(agent.diskLayoutVolumes(disk.MountPath), name)
|
||||
if err := os.MkdirAll(destination, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sentinel := filepath.Join(destination, "existing.txt")
|
||||
if err := os.WriteFile(sentinel, []byte("original"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
url := "/v1/servers/fixture/import-tar?transferId=1234567890abcdef1234567890abcdef"
|
||||
importReply := httptest.NewRecorder()
|
||||
agent.handleImportTar(importReply, httptest.NewRequest(http.MethodPut, url, bytes.NewReader(buildTransferTar(t, name, nil))), name)
|
||||
if importReply.Code != http.StatusConflict {
|
||||
t.Fatalf("occupied import: got %d %s", importReply.Code, importReply.Body.String())
|
||||
}
|
||||
if err := agent.saveMeta(destination, map[string]any{transferImportOwnerKey: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cleanupReply := httptest.NewRecorder()
|
||||
agent.handleDeleteTransferImport(cleanupReply, httptest.NewRequest(http.MethodDelete, url, nil), name)
|
||||
if cleanupReply.Code != http.StatusNotFound {
|
||||
t.Fatalf("unowned cleanup: got %d %s", cleanupReply.Code, cleanupReply.Body.String())
|
||||
}
|
||||
if data, err := os.ReadFile(sentinel); err != nil || string(data) != "original" {
|
||||
t.Fatalf("unowned destination changed: %q, %v", data, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransferImportRefusesBusyServerWithoutQueueing(t *testing.T) {
|
||||
agent, _ := newTestAgent(t)
|
||||
opLock := agent.getServerOpLock("fixture")
|
||||
opLock.Lock()
|
||||
defer opLock.Unlock()
|
||||
url := "/v1/servers/fixture/import-tar?transferId=1234567890abcdef1234567890abcdef"
|
||||
reply := httptest.NewRecorder()
|
||||
agent.handleImportTar(reply, httptest.NewRequest(http.MethodPut, url, nil), "fixture")
|
||||
if reply.Code != http.StatusConflict {
|
||||
t.Fatalf("busy protected import: got %d %s", reply.Code, reply.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransferImportConfirmsAndCleansOwnedCopy(t *testing.T) {
|
||||
agent := newReinstallRaceTestAgent(t)
|
||||
const name = "fixture"
|
||||
const transferID = "1234567890abcdef1234567890abcdef"
|
||||
url := "/v1/servers/fixture/import-tar?transferId=" + transferID
|
||||
archive := buildTransferTar(t, name, map[string]any{"type": "custom", transferImportOwnerKey: "old-import"})
|
||||
reply := httptest.NewRecorder()
|
||||
agent.handleImportTar(reply, httptest.NewRequest(http.MethodPut, url, bytes.NewReader(archive)), name)
|
||||
var receipt map[string]any
|
||||
if err := json.Unmarshal(reply.Body.Bytes(), &receipt); err != nil || reply.Code != http.StatusOK || receipt["transferId"] != transferID {
|
||||
t.Fatalf("protected import: got %d %s (%v)", reply.Code, reply.Body.String(), err)
|
||||
}
|
||||
serverDir := agent.serverDir(name)
|
||||
if err := agent.requireTransferImportOwner(name, transferID); err != nil {
|
||||
t.Fatalf("import ownership not recorded: %v", err)
|
||||
}
|
||||
if contents, err := os.ReadFile(filepath.Join(serverDir, "server.properties")); err != nil || string(contents) != "level-name=world\n" {
|
||||
t.Fatalf("imported payload: %q, %v", contents, err)
|
||||
}
|
||||
reply = httptest.NewRecorder()
|
||||
agent.handleDeleteTransferImport(reply, httptest.NewRequest(http.MethodDelete, url, nil), name)
|
||||
if reply.Code != http.StatusOK {
|
||||
t.Fatalf("owned cleanup: got %d %s", reply.Code, reply.Body.String())
|
||||
}
|
||||
for _, path := range []string{serverDir, agent.serverBackupsDir(name), agent.metaPath(name)} {
|
||||
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
||||
t.Fatalf("owned cleanup left %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestImportTarReimportClearsStaleSecondaryRouting guards a re-transfer: a server
|
||||
// previously routed to a secondary disk that is then re-imported onto the primary
|
||||
// disk must not retain the old secondary __diskId.
|
||||
|
||||
Reference in new issue
Block a user