2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-04 12:34:13 +00:00
restic/src/restic/location/location_test.go

141 lines
3.2 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"
2016-02-21 14:24:37 +00:00
"restic/backend/rest"
"restic/backend/s3"
"restic/backend/sftp"
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
}{
2015-12-28 14:57:20 +00:00
{"local:/srv/repo", Location{Scheme: "local", Config: "/srv/repo"}},
{"local:dir1/dir2", Location{Scheme: "local", Config: "dir1/dir2"}},
{"local:dir1/dir2", Location{Scheme: "local", Config: "dir1/dir2"}},
{"dir1/dir2", Location{Scheme: "local", Config: "dir1/dir2"}},
{"local:../dir1/dir2", Location{Scheme: "local", Config: "../dir1/dir2"}},
{"/dir1/dir2", Location{Scheme: "local", Config: "/dir1/dir2"}},
2015-12-28 14:51:24 +00:00
2015-12-28 14:57:20 +00:00
{"sftp:user@host:/srv/repo", Location{Scheme: "sftp",
2015-12-28 14:51:24 +00:00
Config: sftp.Config{
User: "user",
Host: "host",
Dir: "/srv/repo",
}}},
2015-12-28 14:57:20 +00:00
{"sftp:host:/srv/repo", Location{Scheme: "sftp",
2015-12-28 14:51:24 +00:00
Config: sftp.Config{
User: "",
Host: "host",
Dir: "/srv/repo",
}}},
2015-12-28 14:57:20 +00:00
{"sftp://user@host/srv/repo", Location{Scheme: "sftp",
2015-12-28 14:51:24 +00:00
Config: sftp.Config{
User: "user",
Host: "host",
Dir: "srv/repo",
}}},
2015-12-28 14:57:20 +00:00
{"sftp://user@host//srv/repo", Location{Scheme: "sftp",
2015-12-28 14:51:24 +00:00
Config: sftp.Config{
User: "user",
Host: "host",
Dir: "/srv/repo",
}}},
2015-12-28 14:57:20 +00:00
{"s3://eu-central-1/bucketname", Location{Scheme: "s3",
2015-12-28 14:51:24 +00:00
Config: s3.Config{
2016-01-02 14:33:17 +00:00
Endpoint: "eu-central-1",
Bucket: "bucketname",
Prefix: "restic",
2015-12-28 14:51:24 +00:00
}},
},
2015-12-28 14:57:20 +00:00
{"s3://hostname.foo/bucketname", Location{Scheme: "s3",
2015-12-28 14:51:24 +00:00
Config: s3.Config{
2016-01-02 14:33:17 +00:00
Endpoint: "hostname.foo",
Bucket: "bucketname",
Prefix: "restic",
}},
},
{"s3://hostname.foo/bucketname/prefix/directory", Location{Scheme: "s3",
Config: s3.Config{
Endpoint: "hostname.foo",
Bucket: "bucketname",
Prefix: "prefix/directory",
2015-12-28 14:51:24 +00:00
}},
},
2015-12-28 23:27:29 +00:00
{"s3:eu-central-1/repo", Location{Scheme: "s3",
Config: s3.Config{
2016-01-02 14:33:17 +00:00
Endpoint: "eu-central-1",
Bucket: "repo",
Prefix: "restic",
}},
},
{"s3:eu-central-1/repo/prefix/directory", Location{Scheme: "s3",
Config: s3.Config{
Endpoint: "eu-central-1",
Bucket: "repo",
Prefix: "prefix/directory",
2015-12-28 23:27:29 +00:00
}},
},
2015-12-28 17:30:42 +00:00
{"s3:https://hostname.foo/repo", Location{Scheme: "s3",
2015-12-28 14:51:24 +00:00
Config: s3.Config{
2016-01-02 14:33:17 +00:00
Endpoint: "hostname.foo",
Bucket: "repo",
Prefix: "restic",
}},
},
{"s3:https://hostname.foo/repo/prefix/directory", Location{Scheme: "s3",
Config: s3.Config{
Endpoint: "hostname.foo",
Bucket: "repo",
Prefix: "prefix/directory",
2016-01-02 14:33:17 +00:00
}},
},
{"s3:http://hostname.foo/repo", Location{Scheme: "s3",
Config: s3.Config{
Endpoint: "hostname.foo",
Bucket: "repo",
Prefix: "restic",
2016-01-02 14:33:17 +00:00
UseHTTP: true,
2015-12-28 14:51:24 +00:00
}},
},
2016-02-21 14:24:37 +00:00
{"rest:http://hostname.foo:1234/", Location{Scheme: "rest",
Config: rest.Config{
URL: parseURL("http://hostname.foo:1234/"),
}},
},
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 {
2015-12-28 15:42:44 +00:00
u, err := Parse(test.s)
2015-12-28 14:51:24 +00:00
if err != nil {
t.Errorf("unexpected error: %v", err)
continue
}
if test.u.Scheme != u.Scheme {
t.Errorf("test %d: scheme does not match, want %q, got %q",
i, test.u.Scheme, u.Scheme)
}
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)
}
}
}