2016-08-31 20:39:36 +00:00
|
|
|
package restic_test
|
2015-07-02 20:36:31 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-05 21:56:59 +00:00
|
|
|
"context"
|
2015-07-02 20:36:31 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2015-07-02 20:36:31 +00:00
|
|
|
)
|
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
type saver struct {
|
|
|
|
fn func(restic.FileType, []byte) (restic.ID, error)
|
|
|
|
}
|
|
|
|
|
2023-05-18 17:18:09 +00:00
|
|
|
func (s saver) SaveUnpacked(_ context.Context, t restic.FileType, buf []byte) (restic.ID, error) {
|
2022-06-12 12:38:19 +00:00
|
|
|
return s.fn(t, buf)
|
|
|
|
}
|
2015-07-02 20:36:31 +00:00
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
func (s saver) Connections() uint {
|
|
|
|
return 2
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
type loader struct {
|
2023-01-27 14:01:54 +00:00
|
|
|
fn func(restic.FileType, restic.ID) ([]byte, error)
|
2022-06-12 12:38:19 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 17:18:09 +00:00
|
|
|
func (l loader) LoadUnpacked(_ context.Context, t restic.FileType, id restic.ID) (data []byte, err error) {
|
2023-01-27 14:01:54 +00:00
|
|
|
return l.fn(t, id)
|
2022-06-12 12:38:19 +00:00
|
|
|
}
|
2015-07-02 20:36:31 +00:00
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
func (l loader) Connections() uint {
|
|
|
|
return 2
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig(t *testing.T) {
|
2022-06-12 12:38:19 +00:00
|
|
|
var resultBuf []byte
|
|
|
|
save := func(tpe restic.FileType, buf []byte) (restic.ID, error) {
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Assert(t, tpe == restic.ConfigFile,
|
2015-07-02 20:36:31 +00:00
|
|
|
"wrong backend type: got %v, wanted %v",
|
2016-08-31 18:29:54 +00:00
|
|
|
tpe, restic.ConfigFile)
|
2015-07-02 20:36:31 +00:00
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
resultBuf = buf
|
2016-08-31 18:29:54 +00:00
|
|
|
return restic.ID{}, nil
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 23:52:03 +00:00
|
|
|
cfg1, err := restic.CreateConfig(restic.MaxRepoVersion)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-07-02 20:36:31 +00:00
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
err = restic.SaveConfig(context.TODO(), saver{save}, cfg1)
|
2020-02-12 21:32:45 +00:00
|
|
|
rtest.OK(t, err)
|
2016-07-31 14:27:36 +00:00
|
|
|
|
2023-01-27 14:01:54 +00:00
|
|
|
load := func(tpe restic.FileType, id restic.ID) ([]byte, error) {
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Assert(t, tpe == restic.ConfigFile,
|
2015-07-02 20:36:31 +00:00
|
|
|
"wrong backend type: got %v, wanted %v",
|
2016-08-31 18:29:54 +00:00
|
|
|
tpe, restic.ConfigFile)
|
2015-07-02 20:36:31 +00:00
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
return resultBuf, nil
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
cfg2, err := restic.LoadConfig(context.TODO(), loader{load})
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-07-02 20:36:31 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Assert(t, cfg1 == cfg2,
|
2015-07-02 20:36:31 +00:00
|
|
|
"configs aren't equal: %v != %v", cfg1, cfg2)
|
|
|
|
}
|