Skip to content

Commit

Permalink
Add unmarshalSnapConfig() helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
stolowski committed Mar 9, 2021
1 parent cf07e16 commit a8db7e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
32 changes: 17 additions & 15 deletions overlord/snapshotstate/snapshotmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,10 @@ func prepareSave(task *state.Task) (snapshot *snapshotSetup, cur *snap.Info, cfg
snapshot.Filename = filename(snapshot.SetID, cur)
task.Set("snapshot-setup", &snapshot)

rawCfg, err := configGetSnapConfig(st, snapshot.Snap)
cfg, err = unmarshalSnapConfig(st, snapshot.Snap)
if err != nil {
return nil, nil, nil, err
}
if rawCfg != nil {
if err := json.Unmarshal(*rawCfg, &cfg); err != nil {
return nil, nil, nil, err
}
}

// this should be done last because of it modifies the state and the caller needs to undo this if other operation fails.
if snapshot.Auto {
Expand Down Expand Up @@ -247,17 +242,10 @@ func prepareRestore(task *state.Task) (snapshot *snapshotSetup, oldCfg map[strin
return nil, nil, nil, taskGetErrMsg(task, err, "snapshot")
}

rawCfg, err := configGetSnapConfig(st, snapshot.Snap)
oldCfg, err = unmarshalSnapConfig(st, snapshot.Snap)
if err != nil {
return nil, nil, nil, fmt.Errorf("internal error: cannot obtain current snap config for snapshot restore: %v", err)
}

if rawCfg != nil {
if err := json.Unmarshal(*rawCfg, &oldCfg); err != nil {
return nil, nil, nil, fmt.Errorf("internal error: cannot decode current snap config: %v", err)
}
return nil, nil, nil, err
}

reader, err = backendOpen(snapshot.Filename, backend.ExtractFnameSetID)
if err != nil {
return nil, nil, nil, fmt.Errorf("cannot open snapshot: %v", err)
Expand All @@ -283,6 +271,20 @@ func marshalSnapConfig(cfg map[string]interface{}) (*json.RawMessage, error) {
return raw, err
}

func unmarshalSnapConfig(st *state.State, snapName string) (map[string]interface{}, error) {
rawCfg, err := configGetSnapConfig(st, snapName)
if err != nil {
return nil, fmt.Errorf("internal error: cannot obtain current snap config: %v", err)
}
var cfg map[string]interface{}
if rawCfg != nil {
if err := json.Unmarshal(*rawCfg, &cfg); err != nil {
return nil, fmt.Errorf("internal error: cannot decode current snap config: %v", err)
}
}
return cfg, nil
}

func doRestore(task *state.Task, tomb *tomb.Tomb) error {
snapshot, oldCfg, reader, err := prepareRestore(task)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions overlord/snapshotstate/snapshotmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func (snapshotSuite) TestDoSaveFailsConfigError(c *check.C) {
})
st.Unlock()
err := snapshotstate.DoSave(task, &tomb.Tomb{})
c.Assert(err, check.ErrorMatches, "bzzt")
c.Assert(err, check.ErrorMatches, "internal error: cannot obtain current snap config: bzzt")
}

func (snapshotSuite) TestDoSaveFailsBadConfig(c *check.C) {
Expand Down Expand Up @@ -641,7 +641,7 @@ func (rs *readerSuite) TestDoRestoreFailsOnGetConfigError(c *check.C) {
})()

err := snapshotstate.DoRestore(rs.task, &tomb.Tomb{})
c.Assert(err, check.ErrorMatches, "internal error: cannot obtain current snap config for snapshot restore: bzzt")
c.Assert(err, check.ErrorMatches, "internal error: cannot obtain current snap config: bzzt")
c.Check(rs.calls, check.DeepEquals, []string{"get config"})
}

Expand Down

0 comments on commit a8db7e5

Please sign in to comment.