mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 15:43:21 +00:00
Deduplicate handler initialization in tests
This commit is contained in:
parent
a8fdca3b9f
commit
f763db8934
1 changed files with 50 additions and 108 deletions
158
handlers_test.go
158
handlers_test.go
|
@ -165,8 +165,7 @@ func createOverwriteDeleteSeq(t testing.TB, path string, data string) []TestRequ
|
||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestResticAppendOnlyHandler runs tests on the restic handler code, especially in append-only mode.
|
func createTestHandler(t *testing.T, conf Server) (http.Handler, string, string, string, func()) {
|
||||||
func TestResticAppendOnlyHandler(t *testing.T) {
|
|
||||||
buf := make([]byte, 32)
|
buf := make([]byte, 32)
|
||||||
_, err := io.ReadFull(rand.Reader, buf)
|
_, err := io.ReadFull(rand.Reader, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -176,6 +175,38 @@ func TestResticAppendOnlyHandler(t *testing.T) {
|
||||||
dataHash := sha256.Sum256([]byte(data))
|
dataHash := sha256.Sum256([]byte(data))
|
||||||
fileID := hex.EncodeToString(dataHash[:])
|
fileID := hex.EncodeToString(dataHash[:])
|
||||||
|
|
||||||
|
// setup the server with a local backend in a temporary directory
|
||||||
|
tempdir, err := ioutil.TempDir("", "rest-server-test-")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure the tempdir is properly removed
|
||||||
|
cleanup := func() {
|
||||||
|
err := os.RemoveAll(tempdir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conf.Path = tempdir
|
||||||
|
mux, err := NewHandler(&conf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error from NewHandler: %v", err)
|
||||||
|
}
|
||||||
|
return mux, data, fileID, tempdir, cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestResticAppendOnlyHandler runs tests on the restic handler code, especially in append-only mode.
|
||||||
|
func TestResticAppendOnlyHandler(t *testing.T) {
|
||||||
|
mux, data, fileID, _, cleanup := createTestHandler(t, Server{
|
||||||
|
AppendOnly: true,
|
||||||
|
NoAuth: true,
|
||||||
|
Debug: true,
|
||||||
|
PanicOnError: true,
|
||||||
|
})
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
seq []TestRequest
|
seq []TestRequest
|
||||||
}{
|
}{
|
||||||
|
@ -227,32 +258,6 @@ func TestResticAppendOnlyHandler(t *testing.T) {
|
||||||
{createOverwriteDeleteSeq(t, "/parent2/data/"+fileID, data)},
|
{createOverwriteDeleteSeq(t, "/parent2/data/"+fileID, data)},
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup the server with a local backend in a temporary directory
|
|
||||||
tempdir, err := ioutil.TempDir("", "rest-server-test-")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure the tempdir is properly removed
|
|
||||||
defer func() {
|
|
||||||
err := os.RemoveAll(tempdir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// set append-only mode and configure path
|
|
||||||
mux, err := NewHandler(&Server{
|
|
||||||
AppendOnly: true,
|
|
||||||
Path: tempdir,
|
|
||||||
NoAuth: true,
|
|
||||||
Debug: true,
|
|
||||||
PanicOnError: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error from NewHandler: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the repos
|
// create the repos
|
||||||
for _, path := range []string{"/", "/parent1/sub1/", "/parent1/", "/parent2/"} {
|
for _, path := range []string{"/", "/parent1/sub1/", "/parent1/", "/parent2/"} {
|
||||||
checkRequest(t, mux.ServeHTTP,
|
checkRequest(t, mux.ServeHTTP,
|
||||||
|
@ -295,14 +300,12 @@ func createIdempotentDeleteSeq(t testing.TB, path string, data string) []TestReq
|
||||||
|
|
||||||
// TestResticHandler runs tests on the restic handler code, especially in append-only mode.
|
// TestResticHandler runs tests on the restic handler code, especially in append-only mode.
|
||||||
func TestResticHandler(t *testing.T) {
|
func TestResticHandler(t *testing.T) {
|
||||||
buf := make([]byte, 32)
|
mux, data, fileID, _, cleanup := createTestHandler(t, Server{
|
||||||
_, err := io.ReadFull(rand.Reader, buf)
|
NoAuth: true,
|
||||||
if err != nil {
|
Debug: true,
|
||||||
t.Fatal(err)
|
PanicOnError: true,
|
||||||
}
|
})
|
||||||
data := "random data file " + hex.EncodeToString(buf)
|
defer cleanup()
|
||||||
dataHash := sha256.Sum256([]byte(data))
|
|
||||||
fileID := hex.EncodeToString(dataHash[:])
|
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
seq []TestRequest
|
seq []TestRequest
|
||||||
|
@ -311,31 +314,6 @@ func TestResticHandler(t *testing.T) {
|
||||||
{createIdempotentDeleteSeq(t, "/data/"+fileID, data)},
|
{createIdempotentDeleteSeq(t, "/data/"+fileID, data)},
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup the server with a local backend in a temporary directory
|
|
||||||
tempdir, err := ioutil.TempDir("", "rest-server-test-")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure the tempdir is properly removed
|
|
||||||
defer func() {
|
|
||||||
err := os.RemoveAll(tempdir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// set append-only mode and configure path
|
|
||||||
mux, err := NewHandler(&Server{
|
|
||||||
Path: tempdir,
|
|
||||||
NoAuth: true,
|
|
||||||
Debug: true,
|
|
||||||
PanicOnError: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error from NewHandler: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the repo
|
// create the repo
|
||||||
checkRequest(t, mux.ServeHTTP,
|
checkRequest(t, mux.ServeHTTP,
|
||||||
newRequest(t, "POST", "/?create=true", nil),
|
newRequest(t, "POST", "/?create=true", nil),
|
||||||
|
@ -353,6 +331,13 @@ func TestResticHandler(t *testing.T) {
|
||||||
|
|
||||||
// TestResticErrorHandler runs tests on the restic handler error handling.
|
// TestResticErrorHandler runs tests on the restic handler error handling.
|
||||||
func TestResticErrorHandler(t *testing.T) {
|
func TestResticErrorHandler(t *testing.T) {
|
||||||
|
mux, _, _, tempdir, cleanup := createTestHandler(t, Server{
|
||||||
|
AppendOnly: true,
|
||||||
|
NoAuth: true,
|
||||||
|
Debug: true,
|
||||||
|
})
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
seq []TestRequest
|
seq []TestRequest
|
||||||
}{
|
}{
|
||||||
|
@ -371,31 +356,6 @@ func TestResticErrorHandler(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup the server with a local backend in a temporary directory
|
|
||||||
tempdir, err := ioutil.TempDir("", "rest-server-test-")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure the tempdir is properly removed
|
|
||||||
defer func() {
|
|
||||||
err := os.RemoveAll(tempdir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// set append-only mode and configure path
|
|
||||||
mux, err := NewHandler(&Server{
|
|
||||||
AppendOnly: true,
|
|
||||||
Path: tempdir,
|
|
||||||
NoAuth: true,
|
|
||||||
Debug: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error from NewHandler: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the repo
|
// create the repo
|
||||||
checkRequest(t, mux.ServeHTTP,
|
checkRequest(t, mux.ServeHTTP,
|
||||||
newRequest(t, "POST", "/?create=true", nil),
|
newRequest(t, "POST", "/?create=true", nil),
|
||||||
|
@ -404,7 +364,7 @@ func TestResticErrorHandler(t *testing.T) {
|
||||||
checkRequest(t, mux.ServeHTTP,
|
checkRequest(t, mux.ServeHTTP,
|
||||||
newRequest(t, "POST", "/config", strings.NewReader("example")),
|
newRequest(t, "POST", "/config", strings.NewReader("example")),
|
||||||
[]wantFunc{wantCode(http.StatusOK)})
|
[]wantFunc{wantCode(http.StatusOK)})
|
||||||
err = os.Chmod(path.Join(tempdir, "config"), 0o000)
|
err := os.Chmod(path.Join(tempdir, "config"), 0o000)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -509,31 +469,13 @@ func (d *delayErrorReader) Read(p []byte) (int, error) {
|
||||||
|
|
||||||
// TestAbortedRequest runs tests with concurrent upload requests for the same file.
|
// TestAbortedRequest runs tests with concurrent upload requests for the same file.
|
||||||
func TestAbortedRequest(t *testing.T) {
|
func TestAbortedRequest(t *testing.T) {
|
||||||
// setup the server with a local backend in a temporary directory
|
// the race condition doesn't happen for append-only repositories
|
||||||
tempdir, err := ioutil.TempDir("", "rest-server-test-")
|
mux, _, _, _, cleanup := createTestHandler(t, Server{
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure the tempdir is properly removed
|
|
||||||
defer func() {
|
|
||||||
err := os.RemoveAll(tempdir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// configure path, the race condition doesn't happen for append-only repositories
|
|
||||||
mux, err := NewHandler(&Server{
|
|
||||||
AppendOnly: false,
|
|
||||||
Path: tempdir,
|
|
||||||
NoAuth: true,
|
NoAuth: true,
|
||||||
Debug: true,
|
Debug: true,
|
||||||
PanicOnError: true,
|
PanicOnError: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
defer cleanup()
|
||||||
t.Fatalf("error from NewHandler: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the repo
|
// create the repo
|
||||||
checkRequest(t, mux.ServeHTTP,
|
checkRequest(t, mux.ServeHTTP,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue