Change repository Init() function to allow better testing

This commit is contained in:
Alexander Neumann 2016-07-31 16:27:36 +02:00
parent fe79177b40
commit d5323223f4
3 changed files with 21 additions and 8 deletions

View File

@ -7,9 +7,10 @@ import (
"errors"
"io"
"github.com/restic/chunker"
"restic/backend"
"restic/debug"
"github.com/restic/chunker"
)
// Config contains the configuration for a repository.
@ -37,8 +38,8 @@ type JSONUnpackedLoader interface {
}
// CreateConfig creates a config file with a randomly selected polynomial and
// ID and saves the config in the repository.
func CreateConfig(r JSONUnpackedSaver) (Config, error) {
// ID.
func CreateConfig() (Config, error) {
var (
err error
cfg Config
@ -59,9 +60,7 @@ func CreateConfig(r JSONUnpackedSaver) (Config, error) {
cfg.Version = RepoVersion
debug.Log("Repo.CreateConfig", "New config: %#v", cfg)
_, err = r.SaveJSONUnpacked(backend.Config, cfg)
return cfg, err
return cfg, nil
}
// LoadConfig returns loads, checks and returns the config for a repository.

View File

@ -32,9 +32,11 @@ func TestConfig(t *testing.T) {
return backend.ID{}, nil
}
cfg1, err := repository.CreateConfig(saver(save))
cfg1, err := repository.CreateConfig()
OK(t, err)
_, err = saver(save).SaveJSONUnpacked(backend.Config, cfg1)
load := func(tpe backend.Type, id backend.ID, arg interface{}) error {
Assert(t, tpe == backend.Config,
"wrong backend type: got %v, wanted %v",

View File

@ -416,6 +416,17 @@ func (r *Repository) Init(password string) error {
return errors.New("repository master key and config already initialized")
}
cfg, err := CreateConfig()
if err != nil {
return err
}
return r.init(password, cfg)
}
// init creates a new master key with the supplied password and uses it to save
// the config into the repo.
func (r *Repository) init(password string, cfg Config) error {
key, err := createMasterKey(r, password)
if err != nil {
return err
@ -424,7 +435,8 @@ func (r *Repository) Init(password string) error {
r.key = key.master
r.packerManager.key = key.master
r.keyName = key.Name()
r.Config, err = CreateConfig(r)
r.Config = cfg
_, err = r.SaveJSONUnpacked(backend.Config, cfg)
return err
}