2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 22:27:35 +00:00

backend: use generic implementation for ParseConfig tests

This commit is contained in:
Michael Eischer 2023-04-20 23:02:56 +02:00
parent 5260d38980
commit fa361dbfbd
9 changed files with 88 additions and 157 deletions

View File

@ -1,11 +1,12 @@
package azure package azure
import "testing" import (
"testing"
var configTests = []struct { "github.com/restic/restic/internal/backend/test"
s string )
cfg Config
}{ var configTests = []test.ConfigTestData[Config]{
{"azure:container-name:/", Config{ {"azure:container-name:/", Config{
Container: "container-name", Container: "container-name",
Prefix: "", Prefix: "",
@ -24,17 +25,5 @@ var configTests = []struct {
} }
func TestParseConfig(t *testing.T) { func TestParseConfig(t *testing.T) {
for i, test := range configTests { test.ParseConfigTester(t, ParseConfig, configTests)
cfg, err := ParseConfig(test.s)
if err != nil {
t.Errorf("test %d:%s failed: %v", i, test.s, err)
continue
}
if cfg != test.cfg {
t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v",
i, test.s, test.cfg, cfg)
continue
}
}
} }

View File

@ -1,11 +1,12 @@
package b2 package b2
import "testing" import (
"testing"
var configTests = []struct { "github.com/restic/restic/internal/backend/test"
s string )
cfg Config
}{ var configTests = []test.ConfigTestData[Config]{
{"b2:bucketname", Config{ {"b2:bucketname", Config{
Bucket: "bucketname", Bucket: "bucketname",
Prefix: "", Prefix: "",
@ -39,19 +40,7 @@ var configTests = []struct {
} }
func TestParseConfig(t *testing.T) { func TestParseConfig(t *testing.T) {
for _, test := range configTests { test.ParseConfigTester(t, ParseConfig, configTests)
t.Run("", func(t *testing.T) {
cfg, err := ParseConfig(test.s)
if err != nil {
t.Fatalf("%s failed: %v", test.s, err)
}
if cfg != test.cfg {
t.Fatalf("input: %s\n wrong config, want:\n %#v\ngot:\n %#v",
test.s, test.cfg, cfg)
}
})
}
} }
var invalidConfigTests = []struct { var invalidConfigTests = []struct {

View File

@ -1,11 +1,12 @@
package gs package gs
import "testing" import (
"testing"
var configTests = []struct { "github.com/restic/restic/internal/backend/test"
s string )
cfg Config
}{ var configTests = []test.ConfigTestData[Config]{
{"gs:bucketname:/", Config{ {"gs:bucketname:/", Config{
Bucket: "bucketname", Bucket: "bucketname",
Prefix: "", Prefix: "",
@ -27,17 +28,5 @@ var configTests = []struct {
} }
func TestParseConfig(t *testing.T) { func TestParseConfig(t *testing.T) {
for i, test := range configTests { test.ParseConfigTester(t, ParseConfig, configTests)
cfg, err := ParseConfig(test.s)
if err != nil {
t.Errorf("test %d:%s failed: %v", i, test.s, err)
continue
}
if cfg != test.cfg {
t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v",
i, test.s, test.cfg, cfg)
continue
}
}
} }

View File

@ -1,37 +1,24 @@
package rclone package rclone
import ( import (
"reflect"
"testing" "testing"
"github.com/restic/restic/internal/backend/test"
) )
func TestParseConfig(t *testing.T) { var configTests = []test.ConfigTestData[Config]{
var tests = []struct { {
s string "rclone:local:foo:/bar",
cfg Config Config{
}{ Remote: "local:foo:/bar",
{ Program: defaultConfig.Program,
"rclone:local:foo:/bar", Args: defaultConfig.Args,
Config{ Connections: defaultConfig.Connections,
Remote: "local:foo:/bar", Timeout: defaultConfig.Timeout,
Program: defaultConfig.Program,
Args: defaultConfig.Args,
Connections: defaultConfig.Connections,
Timeout: defaultConfig.Timeout,
},
}, },
} },
}
for _, test := range tests {
t.Run("", func(t *testing.T) { func TestParseConfig(t *testing.T) {
cfg, err := ParseConfig(test.s) test.ParseConfigTester(t, ParseConfig, configTests)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(cfg, test.cfg) {
t.Fatalf("wrong config, want:\n %v\ngot:\n %v", test.cfg, cfg)
}
})
}
} }

View File

@ -2,8 +2,9 @@ package rest
import ( import (
"net/url" "net/url"
"reflect"
"testing" "testing"
"github.com/restic/restic/internal/backend/test"
) )
func parseURL(s string) *url.URL { func parseURL(s string) *url.URL {
@ -15,20 +16,17 @@ func parseURL(s string) *url.URL {
return u return u
} }
var configTests = []struct { var configTests = []test.ConfigTestData[Config]{
s string
cfg Config
}{
{ {
s: "rest:http://localhost:1234", S: "rest:http://localhost:1234",
cfg: Config{ Cfg: Config{
URL: parseURL("http://localhost:1234/"), URL: parseURL("http://localhost:1234/"),
Connections: 5, Connections: 5,
}, },
}, },
{ {
s: "rest:http://localhost:1234/", S: "rest:http://localhost:1234/",
cfg: Config{ Cfg: Config{
URL: parseURL("http://localhost:1234/"), URL: parseURL("http://localhost:1234/"),
Connections: 5, Connections: 5,
}, },
@ -36,17 +34,5 @@ var configTests = []struct {
} }
func TestParseConfig(t *testing.T) { func TestParseConfig(t *testing.T) {
for _, test := range configTests { test.ParseConfigTester(t, ParseConfig, configTests)
t.Run("", func(t *testing.T) {
cfg, err := ParseConfig(test.s)
if err != nil {
t.Fatalf("%s failed: %v", test.s, err)
}
if !reflect.DeepEqual(cfg, test.cfg) {
t.Fatalf("\ninput: %s\n wrong config, want:\n %v\ngot:\n %v",
test.s, test.cfg, cfg)
}
})
}
} }

View File

@ -3,12 +3,11 @@ package s3
import ( import (
"strings" "strings"
"testing" "testing"
"github.com/restic/restic/internal/backend/test"
) )
var configTests = []struct { var configTests = []test.ConfigTestData[Config]{
s string
cfg Config
}{
{"s3://eu-central-1/bucketname", Config{ {"s3://eu-central-1/bucketname", Config{
Endpoint: "eu-central-1", Endpoint: "eu-central-1",
Bucket: "bucketname", Bucket: "bucketname",
@ -100,19 +99,7 @@ var configTests = []struct {
} }
func TestParseConfig(t *testing.T) { func TestParseConfig(t *testing.T) {
for i, test := range configTests { test.ParseConfigTester(t, ParseConfig, configTests)
cfg, err := ParseConfig(test.s)
if err != nil {
t.Errorf("test %d:%s failed: %v", i, test.s, err)
continue
}
if cfg != test.cfg {
t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v",
i, test.s, test.cfg, cfg)
continue
}
}
} }
func TestParseError(t *testing.T) { func TestParseError(t *testing.T) {

View File

@ -2,12 +2,11 @@ package sftp
import ( import (
"testing" "testing"
"github.com/restic/restic/internal/backend/test"
) )
var configTests = []struct { var configTests = []test.ConfigTestData[Config]{
in string
cfg Config
}{
// first form, user specified sftp://user@host/dir // first form, user specified sftp://user@host/dir
{ {
"sftp://user@host/dir/subdir", "sftp://user@host/dir/subdir",
@ -77,19 +76,7 @@ var configTests = []struct {
} }
func TestParseConfig(t *testing.T) { func TestParseConfig(t *testing.T) {
for i, test := range configTests { test.ParseConfigTester(t, ParseConfig, configTests)
cfg, err := ParseConfig(test.in)
if err != nil {
t.Errorf("test %d:%s failed: %v", i, test.in, err)
continue
}
if cfg != test.cfg {
t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v",
i, test.in, test.cfg, cfg)
continue
}
}
} }
var configTestsInvalid = []string{ var configTestsInvalid = []string{

View File

@ -1,11 +1,12 @@
package swift package swift
import "testing" import (
"testing"
var configTests = []struct { "github.com/restic/restic/internal/backend/test"
s string )
cfg Config
}{ var configTests = []test.ConfigTestData[Config]{
{ {
"swift:cnt1:/", "swift:cnt1:/",
Config{ Config{
@ -31,19 +32,7 @@ var configTests = []struct {
} }
func TestParseConfig(t *testing.T) { func TestParseConfig(t *testing.T) {
for _, test := range configTests { test.ParseConfigTester(t, ParseConfig, configTests)
t.Run("", func(t *testing.T) {
cfg, err := ParseConfig(test.s)
if err != nil {
t.Fatalf("parsing %q failed: %v", test.s, err)
}
if cfg != test.cfg {
t.Fatalf("wrong output for %q, want:\n %#v\ngot:\n %#v",
test.s, test.cfg, cfg)
}
})
}
} }
var configTestsInvalid = []string{ var configTestsInvalid = []string{

View File

@ -0,0 +1,28 @@
package test
import (
"fmt"
"reflect"
"testing"
)
type ConfigTestData[C comparable] struct {
S string
Cfg C
}
func ParseConfigTester[C comparable](t *testing.T, parser func(s string) (C, error), tests []ConfigTestData[C]) {
for i, test := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
cfg, err := parser(test.S)
if err != nil {
t.Fatalf("%s failed: %v", test.S, err)
}
if !reflect.DeepEqual(cfg, test.Cfg) {
t.Fatalf("input: %s\n wrong config, want:\n %#v\ngot:\n %#v",
test.S, test.Cfg, cfg)
}
})
}
}