2024-01-29 03:23:56 +00:00
|
|
|
//go:build go1.20
|
|
|
|
// +build go1.20
|
|
|
|
|
2016-02-20 21:05:48 +00:00
|
|
|
package rest_test
|
|
|
|
|
|
|
|
import (
|
2024-01-29 03:23:56 +00:00
|
|
|
"bufio"
|
2017-05-11 20:40:50 +00:00
|
|
|
"context"
|
2024-01-29 03:23:56 +00:00
|
|
|
"fmt"
|
2016-02-20 21:05:48 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2017-05-01 21:08:16 +00:00
|
|
|
"os/exec"
|
2024-01-29 03:23:56 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2017-05-01 21:08:16 +00:00
|
|
|
"testing"
|
2017-05-12 19:33:34 +00:00
|
|
|
"time"
|
2016-02-20 21:05:48 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend/rest"
|
|
|
|
"github.com/restic/restic/internal/backend/test"
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2016-02-20 21:05:48 +00:00
|
|
|
)
|
|
|
|
|
2024-01-29 03:23:56 +00:00
|
|
|
var (
|
|
|
|
serverStartedRE = regexp.MustCompile("^start server on (.*)$")
|
|
|
|
)
|
|
|
|
|
|
|
|
func runRESTServer(ctx context.Context, t testing.TB, dir, reqListenAddr string) (*url.URL, func()) {
|
2017-05-01 21:08:16 +00:00
|
|
|
srv, err := exec.LookPath("rest-server")
|
|
|
|
if err != nil {
|
|
|
|
t.Skip(err)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 03:23:56 +00:00
|
|
|
// create our own context, so that our cleanup can cancel and wait for completion
|
|
|
|
// this will ensure any open ports, open unix sockets etc are properly closed
|
|
|
|
processCtx, cancel := context.WithCancel(ctx)
|
|
|
|
cmd := exec.CommandContext(processCtx, srv, "--no-auth", "--path", dir, "--listen", reqListenAddr)
|
|
|
|
|
|
|
|
// this cancel func is called by when the process context is done
|
|
|
|
cmd.Cancel = func() error {
|
|
|
|
// we execute in a Go-routine as we know the caller will
|
|
|
|
// be waiting on a .Wait() regardless
|
|
|
|
go func() {
|
|
|
|
// try to send a graceful termination signal
|
|
|
|
if cmd.Process.Signal(syscall.SIGTERM) == nil {
|
|
|
|
// if we succeed, then wait a few seconds
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
// and then make sure it's killed either way, ignoring any error code
|
|
|
|
_ = cmd.Process.Kill()
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is the cleanup function that we return the caller,
|
|
|
|
// which will cancel our process context, and then wait for it to finish
|
|
|
|
cleanup := func() {
|
|
|
|
cancel()
|
|
|
|
_ = cmd.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// but in-case we don't finish this method, e.g. by calling t.Fatal()
|
|
|
|
// we also defer a call to clean it up ourselves, guarded by a flag to
|
|
|
|
// indicate that we returned the function to the caller to deal with.
|
|
|
|
callerWillCleanUp := false
|
|
|
|
defer func() {
|
|
|
|
if !callerWillCleanUp {
|
|
|
|
cleanup()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// send stdout to our std out
|
2017-05-01 21:08:16 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
2024-01-29 03:23:56 +00:00
|
|
|
|
|
|
|
// capture stderr with a pipe, as we want to examine this output
|
|
|
|
// to determine when the server is started and listening.
|
|
|
|
cmdErr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
2017-05-01 21:08:16 +00:00
|
|
|
t.Fatal(err)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 03:23:56 +00:00
|
|
|
// start the rest-server
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-12 19:45:18 +00:00
|
|
|
|
2024-01-29 03:23:56 +00:00
|
|
|
// create a channel to receive the actual listen address on
|
|
|
|
listenAddrCh := make(chan string)
|
|
|
|
go func() {
|
|
|
|
defer close(listenAddrCh)
|
|
|
|
matched := false
|
|
|
|
br := bufio.NewReader(cmdErr)
|
|
|
|
for {
|
|
|
|
line, err := br.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
// we ignore errors, as code that relies on this
|
|
|
|
// will happily fail via timeout and empty closed
|
|
|
|
// channel.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
line = strings.Trim(line, "\r\n")
|
|
|
|
if !matched {
|
|
|
|
// look for the server started message, and return the address
|
|
|
|
// that it's listening on
|
|
|
|
matchedServerListen := serverStartedRE.FindSubmatch([]byte(line))
|
|
|
|
if len(matchedServerListen) == 2 {
|
|
|
|
listenAddrCh <- string(matchedServerListen[1])
|
|
|
|
matched = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintln(os.Stdout, line) // print all output to console
|
2017-05-01 21:08:16 +00:00
|
|
|
}
|
2024-01-29 03:23:56 +00:00
|
|
|
}()
|
2017-05-12 19:33:34 +00:00
|
|
|
|
2024-01-29 03:23:56 +00:00
|
|
|
// wait for us to get an address,
|
|
|
|
// or the parent context to cancel,
|
|
|
|
// or for us to timeout
|
|
|
|
var actualListenAddr string
|
|
|
|
select {
|
|
|
|
case <-processCtx.Done():
|
|
|
|
t.Fatal(context.Canceled)
|
|
|
|
case <-time.NewTimer(2 * time.Second).C:
|
|
|
|
t.Fatal(context.DeadlineExceeded)
|
|
|
|
case a, ok := <-listenAddrCh:
|
|
|
|
if !ok {
|
|
|
|
t.Fatal(context.Canceled)
|
2017-05-12 19:33:34 +00:00
|
|
|
}
|
2024-01-29 03:23:56 +00:00
|
|
|
actualListenAddr = a
|
2017-05-12 19:33:34 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 03:23:56 +00:00
|
|
|
// this translate the address that the server is listening on
|
|
|
|
// to a URL suitable for us to connect to
|
|
|
|
var addrToConnectTo string
|
|
|
|
if strings.HasPrefix(reqListenAddr, "unix:") {
|
|
|
|
addrToConnectTo = fmt.Sprintf("http+unix://%s:/restic-test/", actualListenAddr)
|
|
|
|
} else {
|
|
|
|
// while we may listen on 0.0.0.0, we connect to localhost
|
|
|
|
addrToConnectTo = fmt.Sprintf("http://%s/restic-test/", strings.Replace(actualListenAddr, "0.0.0.0", "localhost", 1))
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 03:23:56 +00:00
|
|
|
// parse to a URL
|
|
|
|
url, err := url.Parse(addrToConnectTo)
|
2018-01-20 09:16:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-01-29 03:23:56 +00:00
|
|
|
// indicate that we've completed successfully, and that the caller
|
|
|
|
// is responsible for calling cleanup
|
|
|
|
callerWillCleanUp = true
|
2018-01-20 09:16:50 +00:00
|
|
|
return url, cleanup
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
2017-05-01 21:08:16 +00:00
|
|
|
|
2023-06-08 17:35:20 +00:00
|
|
|
func newTestSuite(url *url.URL, minimalData bool) *test.Suite[rest.Config] {
|
2023-04-21 19:06:56 +00:00
|
|
|
return &test.Suite[rest.Config]{
|
2018-01-20 09:16:50 +00:00
|
|
|
MinimalData: minimalData,
|
|
|
|
|
2017-05-01 21:08:16 +00:00
|
|
|
// NewConfig returns a config for a new temporary backend that will be used in tests.
|
2023-04-21 19:35:34 +00:00
|
|
|
NewConfig: func() (*rest.Config, error) {
|
2017-06-05 22:25:22 +00:00
|
|
|
cfg := rest.NewConfig()
|
|
|
|
cfg.URL = url
|
2023-04-21 19:35:34 +00:00
|
|
|
return &cfg, nil
|
2017-05-01 21:08:16 +00:00
|
|
|
},
|
|
|
|
|
2023-06-08 14:53:55 +00:00
|
|
|
Factory: rest.NewFactory(),
|
2017-05-01 21:08:16 +00:00
|
|
|
}
|
2017-05-13 19:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendREST(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if t.Skipped() {
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.SkipDisallowed(t, "restic/backend/rest.TestBackendREST")
|
2017-05-13 19:56:18 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-12-09 12:42:33 +00:00
|
|
|
dir := rtest.TempDir(t)
|
2024-01-29 03:23:56 +00:00
|
|
|
serverURL, cleanup := runRESTServer(ctx, t, dir, ":0")
|
2017-05-13 19:56:18 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2023-06-08 17:35:20 +00:00
|
|
|
newTestSuite(serverURL, false).RunTests(t)
|
2017-05-13 19:56:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 09:25:47 +00:00
|
|
|
func TestBackendRESTExternalServer(t *testing.T) {
|
|
|
|
repostr := os.Getenv("RESTIC_TEST_REST_REPOSITORY")
|
|
|
|
if repostr == "" {
|
|
|
|
t.Skipf("environment variable %v not set", "RESTIC_TEST_REST_REPOSITORY")
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := rest.ParseConfig(repostr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-06-08 17:35:20 +00:00
|
|
|
newTestSuite(cfg.URL, true).RunTests(t)
|
2018-01-20 09:25:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 19:56:18 +00:00
|
|
|
func BenchmarkBackendREST(t *testing.B) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-12-09 12:42:33 +00:00
|
|
|
dir := rtest.TempDir(t)
|
2024-01-29 03:23:56 +00:00
|
|
|
serverURL, cleanup := runRESTServer(ctx, t, dir, ":0")
|
2017-05-13 19:56:18 +00:00
|
|
|
defer cleanup()
|
2017-05-01 21:08:16 +00:00
|
|
|
|
2023-06-08 17:35:20 +00:00
|
|
|
newTestSuite(serverURL, false).RunBenchmarks(t)
|
2017-05-01 21:08:16 +00:00
|
|
|
}
|