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" "errors"
"io" "io"
"github.com/restic/chunker"
"restic/backend" "restic/backend"
"restic/debug" "restic/debug"
"github.com/restic/chunker"
) )
// Config contains the configuration for a repository. // 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 // CreateConfig creates a config file with a randomly selected polynomial and
// ID and saves the config in the repository. // ID.
func CreateConfig(r JSONUnpackedSaver) (Config, error) { func CreateConfig() (Config, error) {
var ( var (
err error err error
cfg Config cfg Config
@ -59,9 +60,7 @@ func CreateConfig(r JSONUnpackedSaver) (Config, error) {
cfg.Version = RepoVersion cfg.Version = RepoVersion
debug.Log("Repo.CreateConfig", "New config: %#v", cfg) debug.Log("Repo.CreateConfig", "New config: %#v", cfg)
return cfg, nil
_, err = r.SaveJSONUnpacked(backend.Config, cfg)
return cfg, err
} }
// LoadConfig returns loads, checks and returns the config for a repository. // 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 return backend.ID{}, nil
} }
cfg1, err := repository.CreateConfig(saver(save)) cfg1, err := repository.CreateConfig()
OK(t, err) OK(t, err)
_, err = saver(save).SaveJSONUnpacked(backend.Config, cfg1)
load := func(tpe backend.Type, id backend.ID, arg interface{}) error { load := func(tpe backend.Type, id backend.ID, arg interface{}) error {
Assert(t, tpe == backend.Config, Assert(t, tpe == backend.Config,
"wrong backend type: got %v, wanted %v", "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") 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) key, err := createMasterKey(r, password)
if err != nil { if err != nil {
return err return err
@ -424,7 +435,8 @@ func (r *Repository) Init(password string) error {
r.key = key.master r.key = key.master
r.packerManager.key = key.master r.packerManager.key = key.master
r.keyName = key.Name() r.keyName = key.Name()
r.Config, err = CreateConfig(r) r.Config = cfg
_, err = r.SaveJSONUnpacked(backend.Config, cfg)
return err return err
} }