1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2025-03-16 00:00:05 +01:00

integrated the rest backend in the tests

This commit is contained in:
Chapuis Bertil 2015-08-05 14:53:09 +02:00
parent 4fbfca0c31
commit 4e12e6080b
2 changed files with 165 additions and 7 deletions

View file

@ -9,6 +9,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/restic/restic/backend"
)
@ -17,11 +18,14 @@ const connLimit = 10
// Returns the url of the resource
func restPath(url *url.URL, t backend.Type, name string) string {
if t == backend.Config {
return url.String() + "/" + string(t)
location := url.String()
if !strings.HasSuffix(location, "/") {
location += "/"
}
return url.String() + "/" + string(t) + "/" + name
if t == backend.Config {
return location + string(t)
}
return location + string(t) + "/" + name
}
type RestBlob struct {

View file

@ -1,9 +1,19 @@
package backend_test
import (
"github.com/restic/restic/backend/rest"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"testing"
"github.com/gorilla/mux"
"github.com/restic/restic/backend"
"github.com/restic/restic/backend/rest"
)
func setupRestBackend(t *testing.T) *rest.Rest {
@ -13,6 +23,150 @@ func setupRestBackend(t *testing.T) *rest.Rest {
}
func TestRestBackend(t *testing.T) {
s := setupRestBackend(t)
testBackend(s, t)
// Initializing a temporary repository for the backend
path, _ := ioutil.TempDir("", "restic-repository-")
defer os.RemoveAll(path)
dirs := []string{
path,
filepath.Join(path, "data"),
filepath.Join(path, "snapshot"),
filepath.Join(path, "index"),
filepath.Join(path, "lock"),
filepath.Join(path, "key"),
filepath.Join(path, "temp"),
}
for _, d := range dirs {
os.MkdirAll(d, backend.Modes.Dir)
}
// Routing the repository requests
r := mux.NewRouter()
// Exists
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
file := filepath.Join(path, "config")
if _, err := os.Stat(file); err != nil {
http.Error(w, "No repository here", 404)
}
}).Methods("HEAD")
// List blobs
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
file := filepath.Join(path, "config")
if _, err := os.Stat(file); err != nil {
http.Error(w, "No repository here", 404)
}
}).Methods("GET")
// Head file
r.HandleFunc("/{file}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
file := filepath.Join(path, vars["file"])
if _, err := os.Stat(file); err != nil {
http.Error(w, "File not found", 404)
}
}).Methods("HEAD")
// Get file
r.HandleFunc("/{file}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
file := filepath.Join(path, vars["file"])
if _, err := os.Stat(file); err == nil {
bytes, _ := ioutil.ReadFile(file)
w.Write(bytes)
} else {
http.Error(w, "File not found", 404)
}
}).Methods("GET")
// Put file
r.HandleFunc("/{file}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
file := filepath.Join(path, vars["file"])
if _, err := os.Stat(file); err == nil {
fmt.Fprintf(w, "Blob already uploaded", 404)
} else {
bytes, _ := ioutil.ReadAll(r.Body)
ioutil.WriteFile(file, bytes, 0600)
}
}).Methods("POST")
// Delete file
r.HandleFunc("/{file}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
file := filepath.Join(path, vars["file"])
if _, err := os.Stat(file); err == nil {
os.Remove(file)
} else {
fmt.Fprintf(w, "File not found", 404)
}
}).Methods("DELETE")
// List blobs
r.HandleFunc("/{type}/", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
path := filepath.Join(path, vars["type"])
files, _ := ioutil.ReadDir(path)
names := make([]string, len(files))
for i, f := range files {
names[i] = f.Name()
}
data, _ := json.Marshal(names)
w.Write(data)
}).Methods("GET")
// Head blob
r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
blob := filepath.Join(path, vars["type"], vars["blob"])
if _, err := os.Stat(blob); err != nil {
http.Error(w, "File not found", 404)
}
}).Methods("HEAD")
// Get blob
r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
blob := filepath.Join(path, vars["type"], vars["blob"])
if _, err := os.Stat(blob); err == nil {
bytes, _ := ioutil.ReadFile(blob)
w.Write(bytes)
} else {
http.Error(w, "Blob not found", 404)
}
}).Methods("GET")
// Put blob
r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
blob := filepath.Join(path, vars["type"], vars["blob"])
if _, err := os.Stat(blob); err == nil {
fmt.Fprintf(w, "Blob already uploaded", 404)
} else {
bytes, _ := ioutil.ReadAll(r.Body)
ioutil.WriteFile(blob, bytes, 0600)
}
}).Methods("POST")
// Delete blob
r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
blob := filepath.Join(path, vars["type"], vars["blob"])
if _, err := os.Stat(blob); err == nil {
os.Remove(blob)
} else {
fmt.Fprintf(w, "Blob not found", 404)
}
}).Methods("DELETE")
s := httptest.NewServer(r)
defer s.Close()
u, _ := url.Parse(s.URL)
backend, _ := rest.Open(u)
testBackend(backend, t)
}