mirror of
https://github.com/restic/restic.git
synced 2025-01-03 05:35:43 +00:00
location: Make ParseConfig-test backend agnostic
The backend specific parts of the test are now directly handled by the respective backend. Duplicate tests were removed.
This commit is contained in:
parent
9aa9e0d1ec
commit
555be49a79
3 changed files with 87 additions and 322 deletions
|
@ -11,6 +11,34 @@ var configTests = []test.ConfigTestData[Config]{
|
||||||
Path: "/some/path",
|
Path: "/some/path",
|
||||||
Connections: 2,
|
Connections: 2,
|
||||||
}},
|
}},
|
||||||
|
{S: "local:dir1/dir2", Cfg: Config{
|
||||||
|
Path: "dir1/dir2",
|
||||||
|
Connections: 2,
|
||||||
|
}},
|
||||||
|
{S: "local:../dir1/dir2", Cfg: Config{
|
||||||
|
Path: "../dir1/dir2",
|
||||||
|
Connections: 2,
|
||||||
|
}},
|
||||||
|
{S: "local:/dir1:foobar/dir2", Cfg: Config{
|
||||||
|
Path: "/dir1:foobar/dir2",
|
||||||
|
Connections: 2,
|
||||||
|
}},
|
||||||
|
{S: `local:\dir1\foobar\dir2`, Cfg: Config{
|
||||||
|
Path: `\dir1\foobar\dir2`,
|
||||||
|
Connections: 2,
|
||||||
|
}},
|
||||||
|
{S: `local:c:\dir1\foobar\dir2`, Cfg: Config{
|
||||||
|
Path: `c:\dir1\foobar\dir2`,
|
||||||
|
Connections: 2,
|
||||||
|
}},
|
||||||
|
{S: `local:C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`, Cfg: Config{
|
||||||
|
Path: `C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
|
||||||
|
Connections: 2,
|
||||||
|
}},
|
||||||
|
{S: `local:c:/dir1/foobar/dir2`, Cfg: Config{
|
||||||
|
Path: `c:/dir1/foobar/dir2`,
|
||||||
|
Connections: 2,
|
||||||
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseConfig(t *testing.T) {
|
func TestParseConfig(t *testing.T) {
|
||||||
|
|
|
@ -1,345 +1,64 @@
|
||||||
package location_test
|
package location_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/backend/b2"
|
|
||||||
"github.com/restic/restic/internal/backend/local"
|
|
||||||
"github.com/restic/restic/internal/backend/location"
|
"github.com/restic/restic/internal/backend/location"
|
||||||
"github.com/restic/restic/internal/backend/rest"
|
"github.com/restic/restic/internal/restic"
|
||||||
"github.com/restic/restic/internal/backend/s3"
|
"github.com/restic/restic/internal/test"
|
||||||
"github.com/restic/restic/internal/backend/sftp"
|
|
||||||
"github.com/restic/restic/internal/backend/swift"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseURL(s string) *url.URL {
|
type testConfig struct {
|
||||||
u, err := url.Parse(s)
|
loc string
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return u
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var parseTests = []struct {
|
func testFactory() location.Factory {
|
||||||
s string
|
return location.NewHTTPBackendFactory[testConfig, restic.Backend](
|
||||||
u location.Location
|
func(s string) (*testConfig, error) {
|
||||||
}{
|
return &testConfig{loc: s}, nil
|
||||||
{
|
}, nil, nil, nil,
|
||||||
"local:/srv/repo",
|
)
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: "/srv/repo",
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"local:dir1/dir2",
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: "dir1/dir2",
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"local:dir1/dir2",
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: "dir1/dir2",
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"dir1/dir2",
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: "dir1/dir2",
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/dir1/dir2",
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: "/dir1/dir2",
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"local:../dir1/dir2",
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: "../dir1/dir2",
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/dir1/dir2",
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: "/dir1/dir2",
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/dir1:foobar/dir2",
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: "/dir1:foobar/dir2",
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
`\dir1\foobar\dir2`,
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: `\dir1\foobar\dir2`,
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
`c:\dir1\foobar\dir2`,
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: `c:\dir1\foobar\dir2`,
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
`C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: `C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
`c:/dir1/foobar/dir2`,
|
|
||||||
location.Location{Scheme: "local",
|
|
||||||
Config: &local.Config{
|
|
||||||
Path: `c:/dir1/foobar/dir2`,
|
|
||||||
Connections: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"sftp:user@host:/srv/repo",
|
|
||||||
location.Location{Scheme: "sftp",
|
|
||||||
Config: &sftp.Config{
|
|
||||||
User: "user",
|
|
||||||
Host: "host",
|
|
||||||
Path: "/srv/repo",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"sftp:host:/srv/repo",
|
|
||||||
location.Location{Scheme: "sftp",
|
|
||||||
Config: &sftp.Config{
|
|
||||||
User: "",
|
|
||||||
Host: "host",
|
|
||||||
Path: "/srv/repo",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"sftp://user@host/srv/repo",
|
|
||||||
location.Location{Scheme: "sftp",
|
|
||||||
Config: &sftp.Config{
|
|
||||||
User: "user",
|
|
||||||
Host: "host",
|
|
||||||
Path: "srv/repo",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"sftp://user@host//srv/repo",
|
|
||||||
location.Location{Scheme: "sftp",
|
|
||||||
Config: &sftp.Config{
|
|
||||||
User: "user",
|
|
||||||
Host: "host",
|
|
||||||
Path: "/srv/repo",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
"s3://eu-central-1/bucketname",
|
|
||||||
location.Location{Scheme: "s3",
|
|
||||||
Config: &s3.Config{
|
|
||||||
Endpoint: "eu-central-1",
|
|
||||||
Bucket: "bucketname",
|
|
||||||
Prefix: "",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"s3://hostname.foo/bucketname",
|
|
||||||
location.Location{Scheme: "s3",
|
|
||||||
Config: &s3.Config{
|
|
||||||
Endpoint: "hostname.foo",
|
|
||||||
Bucket: "bucketname",
|
|
||||||
Prefix: "",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"s3://hostname.foo/bucketname/prefix/directory",
|
|
||||||
location.Location{Scheme: "s3",
|
|
||||||
Config: &s3.Config{
|
|
||||||
Endpoint: "hostname.foo",
|
|
||||||
Bucket: "bucketname",
|
|
||||||
Prefix: "prefix/directory",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"s3:eu-central-1/repo",
|
|
||||||
location.Location{Scheme: "s3",
|
|
||||||
Config: &s3.Config{
|
|
||||||
Endpoint: "eu-central-1",
|
|
||||||
Bucket: "repo",
|
|
||||||
Prefix: "",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"s3:eu-central-1/repo/prefix/directory",
|
|
||||||
location.Location{Scheme: "s3",
|
|
||||||
Config: &s3.Config{
|
|
||||||
Endpoint: "eu-central-1",
|
|
||||||
Bucket: "repo",
|
|
||||||
Prefix: "prefix/directory",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"s3:https://hostname.foo/repo",
|
|
||||||
location.Location{Scheme: "s3",
|
|
||||||
Config: &s3.Config{
|
|
||||||
Endpoint: "hostname.foo",
|
|
||||||
Bucket: "repo",
|
|
||||||
Prefix: "",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"s3:https://hostname.foo/repo/prefix/directory",
|
|
||||||
location.Location{Scheme: "s3",
|
|
||||||
Config: &s3.Config{
|
|
||||||
Endpoint: "hostname.foo",
|
|
||||||
Bucket: "repo",
|
|
||||||
Prefix: "prefix/directory",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"s3:http://hostname.foo/repo",
|
|
||||||
location.Location{Scheme: "s3",
|
|
||||||
Config: &s3.Config{
|
|
||||||
Endpoint: "hostname.foo",
|
|
||||||
Bucket: "repo",
|
|
||||||
Prefix: "",
|
|
||||||
UseHTTP: true,
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"swift:container17:/",
|
|
||||||
location.Location{Scheme: "swift",
|
|
||||||
Config: &swift.Config{
|
|
||||||
Container: "container17",
|
|
||||||
Prefix: "",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"swift:container17:/prefix97",
|
|
||||||
location.Location{Scheme: "swift",
|
|
||||||
Config: &swift.Config{
|
|
||||||
Container: "container17",
|
|
||||||
Prefix: "prefix97",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rest:http://hostname.foo:1234/",
|
|
||||||
location.Location{Scheme: "rest",
|
|
||||||
Config: &rest.Config{
|
|
||||||
URL: parseURL("http://hostname.foo:1234/"),
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"b2:bucketname:/prefix", location.Location{Scheme: "b2",
|
|
||||||
Config: &b2.Config{
|
|
||||||
Bucket: "bucketname",
|
|
||||||
Prefix: "prefix",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"b2:bucketname", location.Location{Scheme: "b2",
|
|
||||||
Config: &b2.Config{
|
|
||||||
Bucket: "bucketname",
|
|
||||||
Prefix: "",
|
|
||||||
Connections: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
for i, test := range parseTests {
|
registry := location.NewRegistry()
|
||||||
t.Run(test.s, func(t *testing.T) {
|
registry.Register("test", testFactory())
|
||||||
u, err := location.Parse(test.s)
|
|
||||||
|
path := "test:example"
|
||||||
|
u, err := location.Parse(registry, path)
|
||||||
|
test.OK(t, err)
|
||||||
|
test.Equals(t, "test", u.Scheme)
|
||||||
|
test.Equals(t, &testConfig{loc: path}, u.Config)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseFallback(t *testing.T) {
|
||||||
|
fallbackTests := []string{
|
||||||
|
"dir1/dir2",
|
||||||
|
"/dir1/dir2",
|
||||||
|
"/dir1:foobar/dir2",
|
||||||
|
`\dir1\foobar\dir2`,
|
||||||
|
`c:\dir1\foobar\dir2`,
|
||||||
|
`C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
|
||||||
|
`c:/dir1/foobar/dir2`,
|
||||||
|
}
|
||||||
|
|
||||||
|
registry := location.NewRegistry()
|
||||||
|
registry.Register("local", testFactory())
|
||||||
|
|
||||||
|
for _, path := range fallbackTests {
|
||||||
|
t.Run(path, func(t *testing.T) {
|
||||||
|
u, err := location.Parse(registry, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
test.Equals(t, "local", u.Scheme)
|
||||||
if test.u.Scheme != u.Scheme {
|
test.Equals(t, "local:"+path, u.Config.(*testConfig).loc)
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidScheme(t *testing.T) {
|
func TestInvalidScheme(t *testing.T) {
|
||||||
|
registry := location.NewRegistry()
|
||||||
var invalidSchemes = []string{
|
var invalidSchemes = []string{
|
||||||
"foobar:xxx",
|
"foobar:xxx",
|
||||||
"foobar:/dir/dir2",
|
"foobar:/dir/dir2",
|
||||||
|
@ -347,7 +66,7 @@ func TestInvalidScheme(t *testing.T) {
|
||||||
|
|
||||||
for _, s := range invalidSchemes {
|
for _, s := range invalidSchemes {
|
||||||
t.Run(s, func(t *testing.T) {
|
t.Run(s, func(t *testing.T) {
|
||||||
_, err := location.Parse(s)
|
_, err := location.Parse(registry, s)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("error for invalid location %q not found", s)
|
t.Fatalf("error for invalid location %q not found", s)
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,24 @@ var configTests = []test.ConfigTestData[Config]{
|
||||||
Prefix: "prefix/directory",
|
Prefix: "prefix/directory",
|
||||||
Connections: 5,
|
Connections: 5,
|
||||||
}},
|
}},
|
||||||
|
{S: "s3:hostname.foo/foobar", Cfg: Config{
|
||||||
|
Endpoint: "hostname.foo",
|
||||||
|
Bucket: "foobar",
|
||||||
|
Prefix: "",
|
||||||
|
Connections: 5,
|
||||||
|
}},
|
||||||
|
{S: "s3:hostname.foo/foobar/prefix/directory", Cfg: Config{
|
||||||
|
Endpoint: "hostname.foo",
|
||||||
|
Bucket: "foobar",
|
||||||
|
Prefix: "prefix/directory",
|
||||||
|
Connections: 5,
|
||||||
|
}},
|
||||||
|
{S: "s3:https://hostname/foobar", Cfg: Config{
|
||||||
|
Endpoint: "hostname",
|
||||||
|
Bucket: "foobar",
|
||||||
|
Prefix: "",
|
||||||
|
Connections: 5,
|
||||||
|
}},
|
||||||
{S: "s3:https://hostname:9999/foobar", Cfg: Config{
|
{S: "s3:https://hostname:9999/foobar", Cfg: Config{
|
||||||
Endpoint: "hostname:9999",
|
Endpoint: "hostname:9999",
|
||||||
Bucket: "foobar",
|
Bucket: "foobar",
|
||||||
|
|
Loading…
Reference in a new issue