func (ss *stateSuite) TestCheckpointPreserveLastIds(c *C) { b := new(fakeStateBackend) st := state.New(b) st.Lock() st.NewChange("install", "...") st.NewTask("download", "...") st.NewTask("download", "...") // implicit checkpoint st.Unlock() c.Assert(b.checkpoints, HasLen, 1) buf := bytes.NewBuffer(b.checkpoints[0]) st2, err := state.ReadState(nil, buf) c.Assert(err, IsNil) st2.Lock() defer st2.Unlock() c.Assert(st2.NewTask("download", "...").ID(), Equals, "3") c.Assert(st2.NewChange("install", "...").ID(), Equals, "2") }
func loadState(backend state.Backend) (*state.State, error) { if !osutil.FileExists(dirs.SnapStateFile) { return state.New(backend), nil } r, err := os.Open(dirs.SnapStateFile) if err != nil { return nil, fmt.Errorf("failed to read the state file: %s", err) } defer r.Close() return state.ReadState(backend, r) }
func (ss *stateSuite) TestNewChangeAndCheckpoint(c *C) { b := new(fakeStateBackend) st := state.New(b) st.Lock() chg := st.NewChange("install", "summary") c.Assert(chg, NotNil) chgID := chg.ID() chg.Set("a", 1) chg.SetStatus(state.ErrorStatus) spawnTime := chg.SpawnTime() readyTime := chg.ReadyTime() // implicit checkpoint st.Unlock() c.Assert(b.checkpoints, HasLen, 1) buf := bytes.NewBuffer(b.checkpoints[0]) st2, err := state.ReadState(nil, buf) c.Assert(err, IsNil) c.Assert(st2, NotNil) st2.Lock() defer st2.Unlock() chgs := st2.Changes() c.Assert(chgs, HasLen, 1) chg0 := chgs[0] c.Check(chg0.ID(), Equals, chgID) c.Check(chg0.Kind(), Equals, "install") c.Check(chg0.Summary(), Equals, "summary") c.Check(chg0.SpawnTime().Equal(spawnTime), Equals, true) c.Check(chg0.ReadyTime().Equal(readyTime), Equals, true) var v int err = chg0.Get("a", &v) c.Check(v, Equals, 1) c.Check(chg0.Status(), Equals, state.ErrorStatus) select { case <-chg0.Ready(): default: c.Errorf("Change didn't preserve Ready channel closed after deserialization") } }
func (ss *stateSuite) TestImplicitCheckpointAndRead(c *C) { b := new(fakeStateBackend) st := state.New(b) st.Lock() st.Set("v", 1) mSt1 := &mgrState1{A: "foo"} st.Set("mgr1", mSt1) mSt2 := &mgrState2{C: &Count2{B: 42}} st.Set("mgr2", mSt2) // implicit checkpoint st.Unlock() c.Assert(b.checkpoints, HasLen, 1) buf := bytes.NewBuffer(b.checkpoints[0]) st2, err := state.ReadState(nil, buf) c.Assert(err, IsNil) c.Assert(st2.Modified(), Equals, false) st2.Lock() defer st2.Unlock() var v int err = st2.Get("v", &v) c.Assert(err, IsNil) c.Check(v, Equals, 1) var mSt1B mgrState1 err = st2.Get("mgr1", &mSt1B) c.Assert(err, IsNil) c.Check(&mSt1B, DeepEquals, mSt1) var mSt2B mgrState2 err = st2.Get("mgr2", &mSt2B) c.Assert(err, IsNil) c.Check(&mSt2B, DeepEquals, mSt2) }
func (ss *stateSuite) TestNewChangeAndCheckpointTaskDerivedStatus(c *C) { b := new(fakeStateBackend) st := state.New(b) st.Lock() chg := st.NewChange("install", "summary") c.Assert(chg, NotNil) chgID := chg.ID() t1 := st.NewTask("download", "1...") t1.SetStatus(state.DoneStatus) chg.AddTask(t1) // implicit checkpoint st.Unlock() c.Assert(b.checkpoints, HasLen, 1) buf := bytes.NewBuffer(b.checkpoints[0]) st2, err := state.ReadState(nil, buf) c.Assert(err, IsNil) st2.Lock() defer st2.Unlock() chgs := st2.Changes() c.Assert(chgs, HasLen, 1) chg0 := chgs[0] c.Check(chg0.ID(), Equals, chgID) c.Check(chg0.Status(), Equals, state.DoneStatus) select { case <-chg0.Ready(): default: c.Errorf("Change didn't preserve Ready channel closed after deserialization") } }
func (ss *stateSuite) TestNewTaskAndCheckpoint(c *C) { b := new(fakeStateBackend) st := state.New(b) st.Lock() chg := st.NewChange("install", "summary") c.Assert(chg, NotNil) t1 := st.NewTask("download", "1...") chg.AddTask(t1) t1ID := t1.ID() t1.Set("a", 1) t1.SetStatus(state.DoneStatus) t1.SetProgress(5, 10) t2 := st.NewTask("inst", "2...") chg.AddTask(t2) t2ID := t2.ID() t2.WaitFor(t1) t3 := st.NewTask("three", "3...") t3ID := t3.ID() // implicit checkpoint st.Unlock() c.Assert(b.checkpoints, HasLen, 1) buf := bytes.NewBuffer(b.checkpoints[0]) st2, err := state.ReadState(nil, buf) c.Assert(err, IsNil) c.Assert(st2, NotNil) st2.Lock() defer st2.Unlock() chgs := st2.Changes() c.Assert(chgs, HasLen, 1) chg0 := chgs[0] tasks0 := make(map[string]*state.Task) for _, t := range chg0.Tasks() { tasks0[t.ID()] = t } c.Assert(tasks0, HasLen, 2) task0_1 := tasks0[t1ID] c.Check(task0_1.ID(), Equals, t1ID) c.Check(task0_1.Kind(), Equals, "download") c.Check(task0_1.Summary(), Equals, "1...") c.Check(task0_1.Change(), Equals, chg0) var v int err = task0_1.Get("a", &v) c.Check(v, Equals, 1) c.Check(task0_1.Status(), Equals, state.DoneStatus) cur, tot := task0_1.Progress() c.Check(cur, Equals, 5) c.Check(tot, Equals, 10) task0_2 := tasks0[t2ID] c.Check(task0_2.WaitTasks(), DeepEquals, []*state.Task{task0_1}) c.Check(task0_1.HaltTasks(), DeepEquals, []*state.Task{task0_2}) tasks2 := make(map[string]*state.Task) for _, t := range st2.Tasks() { tasks2[t.ID()] = t } c.Assert(tasks2, HasLen, 3) c.Check(tasks2[t3ID].Kind(), Equals, "three") }