Initial commit: DSH Web Manager
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.misaka.ren/m1saka/dsh-web/internal/store"
|
||||
)
|
||||
|
||||
type pausedDownload struct {
|
||||
*httptest.ResponseRecorder
|
||||
started chan struct{}
|
||||
release chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (w *pausedDownload) Write(p []byte) (int, error) {
|
||||
w.once.Do(func() { close(w.started) })
|
||||
<-w.release
|
||||
return w.ResponseRecorder.Write(p)
|
||||
}
|
||||
|
||||
func TestSlowDownloadDoesNotHoldLifecycleLock(t *testing.T) {
|
||||
a, sid := runtimeTestAPI(t, func(w http.ResponseWriter, r *http.Request) { t.Error("unexpected Docker call") })
|
||||
for _, id := range []string{"one", "two"} {
|
||||
if err := a.s.AddInstance(store.Instance{ID: id, UID: "owner"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dir := filepath.Join(a.root, "workspaces/owner/instances", id, "home")
|
||||
if err := os.MkdirAll(dir, 0750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "file.txt"), []byte("content"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
w := &pausedDownload{ResponseRecorder: httptest.NewRecorder(), started: make(chan struct{}), release: make(chan struct{})}
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
a.workspace(w, runtimeRequest("GET", "/api/ws/download?instance=one&path=file.txt", "", sid))
|
||||
}()
|
||||
defer func() { close(w.release); <-done }()
|
||||
select {
|
||||
case <-w.started:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("download not started")
|
||||
}
|
||||
if !a.mu.TryLock() {
|
||||
t.Fatal("slow network holds global lifecycle lock")
|
||||
}
|
||||
active := a.workspaceReaders["one"]
|
||||
a.mu.Unlock()
|
||||
if active != 1 {
|
||||
t.Fatalf("lease count %d", active)
|
||||
}
|
||||
other := httptest.NewRecorder()
|
||||
otherDone := make(chan struct{})
|
||||
go func() {
|
||||
defer close(otherDone)
|
||||
a.workspace(other, runtimeRequest("GET", "/api/ws/tree?instance=two", "", sid))
|
||||
}()
|
||||
select {
|
||||
case <-otherDone:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("other instance blocked")
|
||||
}
|
||||
if other.Code != 200 {
|
||||
t.Fatalf("other instance: %d", other.Code)
|
||||
}
|
||||
proxyDone := make(chan struct{})
|
||||
proxyResponse := httptest.NewRecorder()
|
||||
go func() {
|
||||
defer close(proxyDone)
|
||||
a.proxy(proxyResponse, runtimeRequest("GET", "/dsh/missing/", "", sid))
|
||||
}()
|
||||
select {
|
||||
case <-proxyDone:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("proxy entry blocked by download")
|
||||
}
|
||||
if proxyResponse.Code != 404 {
|
||||
t.Fatalf("proxy status %d", proxyResponse.Code)
|
||||
}
|
||||
for _, action := range []string{"restore", "upgrade", "stop", ""} {
|
||||
method := "POST"
|
||||
if action == "" {
|
||||
method = "DELETE"
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
url := "/api/instances/one"
|
||||
if action != "" {
|
||||
url += "/" + action
|
||||
}
|
||||
body := `{}`
|
||||
if action == "upgrade" {
|
||||
body = `{"image_id":"sha256:` + strings.Repeat("a", 64) + `"}`
|
||||
}
|
||||
a.instance(response, runtimeRequest(method, url, body, sid))
|
||||
if response.Code != 409 {
|
||||
t.Errorf("%s during download: %d %s", action, response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectoryEntryLimit(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for i := 0; i <= maxDirectoryEntries; i++ {
|
||||
f, err := os.Create(filepath.Join(dir, fmt.Sprint(i)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
root, err := os.OpenRoot(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer root.Close()
|
||||
if _, err = readRootDir(root, "."); !errors.Is(err, errDirectoryLimit) {
|
||||
t.Fatalf("limit: %v", err)
|
||||
}
|
||||
if err = os.Remove(filepath.Join(dir, fmt.Sprint(maxDirectoryEntries))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, err := readRootDir(root, ".")
|
||||
if err != nil || len(entries) != maxDirectoryEntries {
|
||||
t.Fatalf("boundary: %d %v", len(entries), err)
|
||||
}
|
||||
}
|
||||
|
||||
type pausedUpload struct {
|
||||
started chan struct{}
|
||||
release chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (r *pausedUpload) Read(p []byte) (int, error) {
|
||||
r.once.Do(func() { close(r.started) })
|
||||
<-r.release
|
||||
return 0, io.EOF
|
||||
}
|
||||
func (r *pausedUpload) Close() error { return nil }
|
||||
|
||||
func TestSlowUploadDoesNotHoldLifecycleLock(t *testing.T) {
|
||||
a, sid := runtimeTestAPI(t, func(w http.ResponseWriter, r *http.Request) { t.Error("unexpected Docker call") })
|
||||
if err := a.s.AddInstance(store.Instance{ID: "one", UID: "owner"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := a.s.DB.Exec("UPDATE users SET quota=? WHERE id='owner'", 1<<30); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(a.root, "workspaces/owner/instances/one/home"), 0750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := &pausedUpload{started: make(chan struct{}), release: make(chan struct{})}
|
||||
r := runtimeRequest("POST", "/api/ws/upload?instance=one", "", sid)
|
||||
r.Body = body
|
||||
r.Header.Set("Content-Type", "multipart/form-data; boundary=test")
|
||||
done := make(chan struct{})
|
||||
go func() { defer close(done); a.workspace(httptest.NewRecorder(), r) }()
|
||||
defer func() { close(body.release); <-done }()
|
||||
select {
|
||||
case <-body.started:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("upload not reading body")
|
||||
}
|
||||
if !a.mu.TryLock() {
|
||||
t.Fatal("slow upload holds global lock")
|
||||
}
|
||||
a.mu.Unlock()
|
||||
if !workspaceMu.TryLock() {
|
||||
t.Fatal("slow upload holds workspace write lock")
|
||||
}
|
||||
workspaceMu.Unlock()
|
||||
}
|
||||
|
||||
func TestWorkspaceErrorResponseReleasesLocks(t *testing.T) {
|
||||
a, sid := runtimeTestAPI(t, func(w http.ResponseWriter, r *http.Request) { t.Error("unexpected Docker call") })
|
||||
if err := a.s.AddInstance(store.Instance{ID: "one", UID: "owner"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(a.root, "workspaces/owner/instances/one/home"), 0750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := &pausedDownload{ResponseRecorder: httptest.NewRecorder(), started: make(chan struct{}), release: make(chan struct{})}
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
a.workspace(w, runtimeRequest("GET", "/api/ws/tree?instance=one&path=missing", "", sid))
|
||||
}()
|
||||
defer func() { close(w.release); <-done }()
|
||||
select {
|
||||
case <-w.started:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("response not started")
|
||||
}
|
||||
if !workspaceMu.TryLock() {
|
||||
t.Fatal("error response holds workspace lock")
|
||||
}
|
||||
workspaceMu.Unlock()
|
||||
if !a.mu.TryLock() {
|
||||
t.Fatal("error response holds lifecycle lock")
|
||||
}
|
||||
leases := len(a.workspaceReaders)
|
||||
a.mu.Unlock()
|
||||
if leases != 0 {
|
||||
t.Fatal("response retains lease")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user