restic/internal/backend/location/location_test.go

340 lines
5.8 KiB
Go
Raw Normal View History

2015-12-28 14:57:20 +00:00
package location
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"
"restic/backend/b2"
2017-03-25 12:20:03 +00:00
"restic/backend/local"
2016-02-21 14:24:37 +00:00
"restic/backend/rest"
"restic/backend/s3"
"restic/backend/sftp"
"restic/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
2015-12-28 14:57:20 +00:00
u Location
2015-12-28 14:51:24 +00:00
}{
{
"local:/srv/repo",
Location{Scheme: "local",
Config: local.Config{
Path: "/srv/repo",
},
},
},
{
"local:dir1/dir2",
Location{Scheme: "local",
Config: local.Config{
Path: "dir1/dir2",
},
},
},
{
"local:dir1/dir2",
Location{Scheme: "local",
Config: local.Config{
Path: "dir1/dir2",
},
},
},
{
"dir1/dir2",
Location{Scheme: "local",
Config: local.Config{
Path: "dir1/dir2",
},
},
},
{
"/dir1/dir2",
Location{Scheme: "local",
Config: local.Config{
Path: "/dir1/dir2",
},
},
},
{
"local:../dir1/dir2",
Location{Scheme: "local",
Config: local.Config{
Path: "../dir1/dir2",
},
},
},
{
"/dir1/dir2",
Location{Scheme: "local",
Config: local.Config{
Path: "/dir1/dir2",
},
},
},
{
"/dir1:foobar/dir2",
Location{Scheme: "local",
Config: local.Config{
Path: "/dir1:foobar/dir2",
},
},
},
{
`\dir1\foobar\dir2`,
Location{Scheme: "local",
Config: local.Config{
Path: `\dir1\foobar\dir2`,
},
},
},
{
`c:\dir1\foobar\dir2`,
Location{Scheme: "local",
Config: local.Config{
Path: `c:\dir1\foobar\dir2`,
},
},
},
{
`C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
Location{Scheme: "local",
Config: local.Config{
Path: `C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
},
},
},
{
`c:/dir1/foobar/dir2`,
Location{Scheme: "local",
Config: local.Config{
Path: `c:/dir1/foobar/dir2`,
},
},
},
{
"sftp:user@host:/srv/repo",
Location{Scheme: "sftp",
Config: sftp.Config{
User: "user",
Host: "host",
2017-04-10 20:41:06 +00:00
Path: "/srv/repo",
},
},
},
{
"sftp:host:/srv/repo",
Location{Scheme: "sftp",
Config: sftp.Config{
User: "",
Host: "host",
2017-04-10 20:41:06 +00:00
Path: "/srv/repo",
},
},
},
{
"sftp://user@host/srv/repo",
Location{Scheme: "sftp",
Config: sftp.Config{
User: "user",
Host: "host",
2017-04-10 20:41:06 +00:00
Path: "srv/repo",
},
},
},
{
"sftp://user@host//srv/repo",
Location{Scheme: "sftp",
Config: sftp.Config{
User: "user",
Host: "host",
2017-04-10 20:41:06 +00:00
Path: "/srv/repo",
},
},
},
2015-12-28 14:51:24 +00:00
{
"s3://eu-central-1/bucketname",
Location{Scheme: "s3",
Config: s3.Config{
2017-06-06 19:12:38 +00:00
Endpoint: "eu-central-1",
Bucket: "bucketname",
Prefix: "restic",
2017-06-11 11:45:06 +00:00
Connections: 5,
},
},
},
{
"s3://hostname.foo/bucketname",
Location{Scheme: "s3",
Config: s3.Config{
2017-06-06 19:12:38 +00:00
Endpoint: "hostname.foo",
Bucket: "bucketname",
Prefix: "restic",
2017-06-11 11:45:06 +00:00
Connections: 5,
},
},
},
{
"s3://hostname.foo/bucketname/prefix/directory",
Location{Scheme: "s3",
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,
},
},
},
{
"s3:eu-central-1/repo",
Location{Scheme: "s3",
Config: s3.Config{
2017-06-06 19:12:38 +00:00
Endpoint: "eu-central-1",
Bucket: "repo",
Prefix: "restic",
2017-06-11 11:45:06 +00:00
Connections: 5,
},
},
},
{
"s3:eu-central-1/repo/prefix/directory",
Location{Scheme: "s3",
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,
},
},
},
{
"s3:https://hostname.foo/repo",
Location{Scheme: "s3",
Config: s3.Config{
2017-06-06 19:12:38 +00:00
Endpoint: "hostname.foo",
Bucket: "repo",
Prefix: "restic",
2017-06-11 11:45:06 +00:00
Connections: 5,
},
},
},
{
"s3:https://hostname.foo/repo/prefix/directory",
Location{Scheme: "s3",
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,
},
},
},
{
"s3:http://hostname.foo/repo",
Location{Scheme: "s3",
Config: s3.Config{
2017-06-06 19:12:38 +00:00
Endpoint: "hostname.foo",
Bucket: "repo",
Prefix: "restic",
UseHTTP: true,
2017-06-11 11:45:06 +00:00
Connections: 5,
},
},
},
{
"swift:container17:/",
Location{Scheme: "swift",
Config: swift.Config{
2017-06-06 19:12:38 +00:00
Container: "container17",
Prefix: "",
Connections: 5,
},
},
},
{
"swift:container17:/prefix97",
Location{Scheme: "swift",
Config: swift.Config{
2017-06-06 19:12:38 +00:00
Container: "container17",
Prefix: "prefix97",
Connections: 5,
},
},
},
{
"rest:http://hostname.foo:1234/",
Location{Scheme: "rest",
Config: rest.Config{
2017-06-06 19:12:38 +00:00
URL: parseURL("http://hostname.foo:1234/"),
Connections: 5,
},
},
2016-02-21 14:24:37 +00:00
},
{
"b2:bucketname:/prefix", Location{Scheme: "b2",
Config: b2.Config{
Bucket: "bucketname",
Prefix: "prefix",
Connections: 5,
},
},
},
{
"b2:bucketname", Location{Scheme: "b2",
Config: b2.Config{
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 {
t.Run(test.s, func(t *testing.T) {
u, err := Parse(test.s)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
2015-12-28 14:51:24 +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
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
}
}
func TestInvalidScheme(t *testing.T) {
var invalidSchemes = []string{
"foobar:xxx",
"foobar:/dir/dir2",
}
for _, s := range invalidSchemes {
t.Run(s, func(t *testing.T) {
_, err := Parse(s)
if err == nil {
t.Fatalf("error for invalid location %q not found", s)
}
})
}
}