func (s *patch4Suite) TestPatch4OnRefreshes(c *C) { restorer := patch.MockLevel(4) defer restorer() r, err := os.Open(dirs.SnapStateFile) c.Assert(err, IsNil) defer r.Close() st, err := state.ReadState(nil, r) c.Assert(err, IsNil) func() { st.Lock() defer st.Unlock() task := st.Task("16") c.Assert(task, NotNil) // simulate that the task was running (but the change // is not fully done yet) task.SetStatus(state.DoneStatus) snapsup, err := patch.Patch4TaskSnapSetup(task) c.Assert(err, IsNil) c.Check(snapsup.Flags.Revert(), Equals, false) var had bool var idx int c.Check(task.Get("had-candidate", &had), IsNil) c.Check(had, Equals, false) c.Check(task.Get("old-candidate-index", &idx), Equals, state.ErrNoState) c.Check(len(task.Change().Tasks()), Equals, 7) }() // go from patch level 3 -> 4 err = patch.Apply(st) c.Assert(err, IsNil) st.Lock() defer st.Unlock() task := st.Task("16") c.Assert(task, NotNil) snapsup, err := patch.Patch4TaskSnapSetup(task) c.Assert(err, IsNil) c.Check(snapsup.Flags.Revert(), Equals, false) var had bool var idx int c.Check(task.Get("had-candidate", &had), Equals, state.ErrNoState) c.Check(task.Get("old-candidate-index", &idx), IsNil) c.Check(idx, Equals, 1) // we added cleanup c.Check(len(task.Change().Tasks()), Equals, 7+1) }
// This test simulates a link-snap task that is scheduled but has not // run yet. It has no "had-candidate" data set yet. func (s *patch4Suite) TestPatch4OnRefreshesNoHadCandidateYet(c *C) { restorer := patch.MockLevel(4) defer restorer() r, err := os.Open(dirs.SnapStateFile) c.Assert(err, IsNil) defer r.Close() st, err := state.ReadState(nil, r) c.Assert(err, IsNil) func() { st.Lock() defer st.Unlock() task := st.Task("16") c.Assert(task, NotNil) // its ready to run but has not run yet task.Clear("had-candidate") task.SetStatus(state.DoStatus) ss, err := patch.Patch4TaskSnapSetup(task) c.Assert(err, IsNil) c.Check(ss.Flags.Revert(), Equals, false) var had bool var idx int c.Check(task.Get("had-candidate", &had), Equals, state.ErrNoState) c.Check(task.Get("old-candidate-index", &idx), Equals, state.ErrNoState) c.Check(len(task.Change().Tasks()), Equals, 7) }() // go from patch level 3 -> 4 err = patch.Apply(st) c.Assert(err, IsNil) st.Lock() defer st.Unlock() task := st.Task("16") c.Assert(task, NotNil) ss, err := patch.Patch4TaskSnapSetup(task) c.Assert(err, IsNil) c.Check(ss.Flags.Revert(), Equals, false) var had bool var idx int c.Check(task.Get("had-candidate", &had), Equals, state.ErrNoState) c.Check(task.Get("old-candidate-index", &idx), IsNil) c.Check(idx, Equals, 1) // we added cleanup c.Check(len(task.Change().Tasks()), Equals, 7+1) }