func (ms *mgrsSuite) TestHappyRevert(c *C) { st := ms.o.State() st.Lock() defer st.Unlock() x1Yaml := `name: foo version: 1.0 apps: x1: command: bin/bar ` x1binary := filepath.Join(dirs.SnapBinariesDir, "foo.x1") x2Yaml := `name: foo version: 2.0 apps: x2: command: bin/bar ` x2binary := filepath.Join(dirs.SnapBinariesDir, "foo.x2") ms.installLocalTestSnap(c, x1Yaml) ms.installLocalTestSnap(c, x2Yaml) // ensure we are on x2 _, err := os.Lstat(x2binary) c.Assert(err, IsNil) _, err = os.Lstat(x1binary) c.Assert(err, ErrorMatches, ".*no such file.*") // now do the revert ts, err := snapstate.Revert(st, "foo", snapstate.Flags{}) c.Assert(err, IsNil) chg := st.NewChange("revert-snap", "...") chg.AddAll(ts) st.Unlock() err = ms.o.Settle() st.Lock() c.Assert(err, IsNil) c.Assert(chg.Status(), Equals, state.DoneStatus, Commentf("revert-snap change failed with: %v", chg.Err())) // ensure that we use x1 now _, err = os.Lstat(x1binary) c.Assert(err, IsNil) _, err = os.Lstat(x2binary) c.Assert(err, ErrorMatches, ".*no such file.*") // ensure that x1,x2 is still there, revert just moves the "current" // pointer for _, fn := range []string{"foo_x2.snap", "foo_x1.snap"} { p := filepath.Join(dirs.SnapBlobDir, fn) c.Assert(osutil.FileExists(p), Equals, true) } }
func snapRevert(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) { var ts *state.TaskSet flags, err := modeFlags(inst.DevMode, inst.JailMode) if err != nil { return "", nil, err } if inst.Revision.Unset() { ts, err = snapstate.Revert(st, inst.Snaps[0], flags) } else { ts, err = snapstate.RevertToRevision(st, inst.Snaps[0], inst.Revision, flags) } if err != nil { return "", nil, err } msg := fmt.Sprintf(i18n.G("Revert %q snap"), inst.Snaps[0]) return msg, []*state.TaskSet{ts}, nil }