2023-06-08 11:04:34 +00:00
|
|
|
package location_test
|
2015-12-28 14:51:24 +00:00
|
|
|
|
|
|
|
import (
|
2016-02-21 14:24:37 +00:00
|
|
|
"net/url"
|
2015-12-28 14:51:24 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend/b2"
|
|
|
|
"github.com/restic/restic/internal/backend/local"
|
2023-06-08 11:04:34 +00:00
|
|
|
"github.com/restic/restic/internal/backend/location"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend/rest"
|
|
|
|
"github.com/restic/restic/internal/backend/s3"
|
|
|
|
"github.com/restic/restic/internal/backend/sftp"
|
|
|
|
"github.com/restic/restic/internal/backend/swift"
|
2015-12-28 14:51:24 +00:00
|
|
|
)
|
|
|
|
|
2016-02-21 14:24:37 +00:00
|
|
|
func parseURL(s string) *url.URL {
|
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2015-12-28 14:51:24 +00:00
|
|
|
var parseTests = []struct {
|
|
|
|
s string
|
2023-06-08 11:04:34 +00:00
|
|
|
u location.Location
|
2015-12-28 14:51:24 +00:00
|
|
|
}{
|
2017-03-25 12:27:14 +00:00
|
|
|
{
|
|
|
|
"local:/srv/repo",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: "/srv/repo",
|
|
|
|
Connections: 2,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"local:dir1/dir2",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: "dir1/dir2",
|
|
|
|
Connections: 2,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"local:dir1/dir2",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: "dir1/dir2",
|
|
|
|
Connections: 2,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"dir1/dir2",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: "dir1/dir2",
|
|
|
|
Connections: 2,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-30 18:40:27 +00:00
|
|
|
{
|
|
|
|
"/dir1/dir2",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: "/dir1/dir2",
|
|
|
|
Connections: 2,
|
2017-06-30 18:40:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-03-25 12:27:14 +00:00
|
|
|
{
|
|
|
|
"local:../dir1/dir2",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: "../dir1/dir2",
|
|
|
|
Connections: 2,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/dir1/dir2",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: "/dir1/dir2",
|
|
|
|
Connections: 2,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-30 18:40:27 +00:00
|
|
|
{
|
|
|
|
"/dir1:foobar/dir2",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: "/dir1:foobar/dir2",
|
|
|
|
Connections: 2,
|
2017-06-30 18:40:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`\dir1\foobar\dir2`,
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: `\dir1\foobar\dir2`,
|
|
|
|
Connections: 2,
|
2017-06-30 18:40:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`c:\dir1\foobar\dir2`,
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: `c:\dir1\foobar\dir2`,
|
|
|
|
Connections: 2,
|
2017-06-30 18:40:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: `C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
|
|
|
|
Connections: 2,
|
2017-06-30 18:40:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`c:/dir1/foobar/dir2`,
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "local",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &local.Config{
|
2021-08-07 17:50:00 +00:00
|
|
|
Path: `c:/dir1/foobar/dir2`,
|
|
|
|
Connections: 2,
|
2017-06-30 18:40:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-03-25 12:27:14 +00:00
|
|
|
{
|
|
|
|
"sftp:user@host:/srv/repo",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "sftp",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &sftp.Config{
|
2021-08-07 17:56:59 +00:00
|
|
|
User: "user",
|
|
|
|
Host: "host",
|
|
|
|
Path: "/srv/repo",
|
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"sftp:host:/srv/repo",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "sftp",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &sftp.Config{
|
2021-08-07 17:56:59 +00:00
|
|
|
User: "",
|
|
|
|
Host: "host",
|
|
|
|
Path: "/srv/repo",
|
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"sftp://user@host/srv/repo",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "sftp",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &sftp.Config{
|
2021-08-07 17:56:59 +00:00
|
|
|
User: "user",
|
|
|
|
Host: "host",
|
|
|
|
Path: "srv/repo",
|
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"sftp://user@host//srv/repo",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "sftp",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &sftp.Config{
|
2021-08-07 17:56:59 +00:00
|
|
|
User: "user",
|
|
|
|
Host: "host",
|
|
|
|
Path: "/srv/repo",
|
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-12-28 14:51:24 +00:00
|
|
|
|
2017-03-25 12:27:14 +00:00
|
|
|
{
|
|
|
|
"s3://eu-central-1/bucketname",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "s3",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &s3.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
Endpoint: "eu-central-1",
|
|
|
|
Bucket: "bucketname",
|
2017-11-20 21:29:15 +00:00
|
|
|
Prefix: "",
|
2017-06-11 11:45:06 +00:00
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"s3://hostname.foo/bucketname",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "s3",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &s3.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
Endpoint: "hostname.foo",
|
|
|
|
Bucket: "bucketname",
|
2017-11-20 21:29:15 +00:00
|
|
|
Prefix: "",
|
2017-06-11 11:45:06 +00:00
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"s3://hostname.foo/bucketname/prefix/directory",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "s3",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &s3.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
Endpoint: "hostname.foo",
|
|
|
|
Bucket: "bucketname",
|
|
|
|
Prefix: "prefix/directory",
|
2017-06-11 11:45:06 +00:00
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"s3:eu-central-1/repo",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "s3",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &s3.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
Endpoint: "eu-central-1",
|
|
|
|
Bucket: "repo",
|
2017-11-20 21:29:15 +00:00
|
|
|
Prefix: "",
|
2017-06-11 11:45:06 +00:00
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"s3:eu-central-1/repo/prefix/directory",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "s3",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &s3.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
Endpoint: "eu-central-1",
|
|
|
|
Bucket: "repo",
|
|
|
|
Prefix: "prefix/directory",
|
2017-06-11 11:45:06 +00:00
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"s3:https://hostname.foo/repo",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "s3",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &s3.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
Endpoint: "hostname.foo",
|
|
|
|
Bucket: "repo",
|
2017-11-20 21:29:15 +00:00
|
|
|
Prefix: "",
|
2017-06-11 11:45:06 +00:00
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"s3:https://hostname.foo/repo/prefix/directory",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "s3",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &s3.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
Endpoint: "hostname.foo",
|
|
|
|
Bucket: "repo",
|
|
|
|
Prefix: "prefix/directory",
|
2017-06-11 11:45:06 +00:00
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"s3:http://hostname.foo/repo",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "s3",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &s3.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
Endpoint: "hostname.foo",
|
|
|
|
Bucket: "repo",
|
2017-11-20 21:29:15 +00:00
|
|
|
Prefix: "",
|
2017-06-06 19:12:38 +00:00
|
|
|
UseHTTP: true,
|
2017-06-11 11:45:06 +00:00
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-03-29 21:58:25 +00:00
|
|
|
{
|
|
|
|
"swift:container17:/",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "swift",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &swift.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
Container: "container17",
|
|
|
|
Prefix: "",
|
2017-06-11 11:46:54 +00:00
|
|
|
Connections: 5,
|
2017-03-29 21:58:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"swift:container17:/prefix97",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "swift",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &swift.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
Container: "container17",
|
|
|
|
Prefix: "prefix97",
|
2017-06-11 11:46:54 +00:00
|
|
|
Connections: 5,
|
2017-03-29 21:58:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-03-25 12:27:14 +00:00
|
|
|
{
|
|
|
|
"rest:http://hostname.foo:1234/",
|
2023-06-08 11:04:34 +00:00
|
|
|
location.Location{Scheme: "rest",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &rest.Config{
|
2017-06-06 19:12:38 +00:00
|
|
|
URL: parseURL("http://hostname.foo:1234/"),
|
2017-06-11 11:46:54 +00:00
|
|
|
Connections: 5,
|
2017-03-25 12:27:14 +00:00
|
|
|
},
|
|
|
|
},
|
2016-02-21 14:24:37 +00:00
|
|
|
},
|
2017-05-28 08:19:01 +00:00
|
|
|
{
|
2023-06-08 11:04:34 +00:00
|
|
|
"b2:bucketname:/prefix", location.Location{Scheme: "b2",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &b2.Config{
|
2017-05-28 08:19:01 +00:00
|
|
|
Bucket: "bucketname",
|
|
|
|
Prefix: "prefix",
|
|
|
|
Connections: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-06-08 11:04:34 +00:00
|
|
|
"b2:bucketname", location.Location{Scheme: "b2",
|
2023-04-21 19:35:34 +00:00
|
|
|
Config: &b2.Config{
|
2017-05-28 08:19:01 +00:00
|
|
|
Bucket: "bucketname",
|
|
|
|
Prefix: "",
|
|
|
|
Connections: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-12-28 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 15:42:44 +00:00
|
|
|
func TestParse(t *testing.T) {
|
2015-12-28 14:51:24 +00:00
|
|
|
for i, test := range parseTests {
|
2017-03-25 12:27:14 +00:00
|
|
|
t.Run(test.s, func(t *testing.T) {
|
2023-06-08 11:04:34 +00:00
|
|
|
u, err := location.Parse(test.s)
|
2017-03-25 12:27:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-12-28 14:51:24 +00:00
|
|
|
|
2017-03-25 12:27:14 +00:00
|
|
|
if test.u.Scheme != u.Scheme {
|
|
|
|
t.Errorf("test %d: scheme does not match, want %q, got %q",
|
|
|
|
i, test.u.Scheme, u.Scheme)
|
|
|
|
}
|
2015-12-28 14:51:24 +00:00
|
|
|
|
2017-03-25 12:27:14 +00:00
|
|
|
if !reflect.DeepEqual(test.u.Config, u.Config) {
|
|
|
|
t.Errorf("test %d: cfg map does not match, want:\n %#v\ngot: \n %#v",
|
|
|
|
i, test.u.Config, u.Config)
|
|
|
|
}
|
|
|
|
})
|
2015-12-28 14:51:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 18:40:27 +00:00
|
|
|
|
|
|
|
func TestInvalidScheme(t *testing.T) {
|
|
|
|
var invalidSchemes = []string{
|
|
|
|
"foobar:xxx",
|
|
|
|
"foobar:/dir/dir2",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range invalidSchemes {
|
|
|
|
t.Run(s, func(t *testing.T) {
|
2023-06-08 11:04:34 +00:00
|
|
|
_, err := location.Parse(s)
|
2017-06-30 18:40:27 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("error for invalid location %q not found", s)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|