本文整理汇总了Golang中github.com/snapcore/snapd/overlord/snapstate.Get函数的典型用法代码示例。如果您正苦于以下问题:Golang Get函数的具体用法?Golang Get怎么用?Golang Get使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Get函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestUpdateBootRevisionsKernelSimple
func (bs *bootedSuite) TestUpdateBootRevisionsKernelSimple(c *C) {
st := bs.state
st.Lock()
defer st.Unlock()
bs.makeInstalledKernelOS(c, st)
bs.bootloader.BootVars["snap_kernel"] = "canonical-pc-linux_1.snap"
err := snapstate.UpdateBootRevisions(st)
c.Assert(err, IsNil)
st.Unlock()
bs.settle()
st.Lock()
c.Assert(st.Changes(), HasLen, 1)
chg := st.Changes()[0]
c.Assert(chg.Err(), IsNil)
c.Assert(chg.Kind(), Equals, "update-revisions")
c.Assert(chg.IsReady(), Equals, true)
// canonical-pc-linux "current" got reverted but core did not
var snapst snapstate.SnapState
err = snapstate.Get(st, "canonical-pc-linux", &snapst)
c.Assert(err, IsNil)
c.Assert(snapst.Current, Equals, snap.R(1))
c.Assert(snapst.Active, Equals, true)
err = snapstate.Get(st, "core", &snapst)
c.Assert(err, IsNil)
c.Assert(snapst.Current, Equals, snap.R(2))
c.Assert(snapst.Active, Equals, true)
}
示例2: TestPatch2
func (s *patch2Suite) TestPatch2(c *C) {
restorer := patch.MockLevel(2)
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)
// go from patch level 1 -> 2
err = patch.Apply(st)
c.Assert(err, IsNil)
st.Lock()
defer st.Unlock()
// our mocks are correct
c.Assert(st.Changes(), HasLen, 2)
c.Assert(st.Tasks(), HasLen, 2)
var snapsup snapstate.SnapSetup
// transition of:
// - SnapSetup.{Name,Revision} -> SnapSetup.SideInfo.{RealName,Revision}
t := st.Task("1")
err = t.Get("snap-setup", &snapsup)
c.Assert(err, IsNil)
c.Assert(snapsup.SideInfo, DeepEquals, &snap.SideInfo{
RealName: "foo",
Revision: snap.R("x3"),
})
// transition of:
// - SnapState.Sequence is backfilled with "RealName" (if missing)
var snapst snapstate.SnapState
err = snapstate.Get(st, "foo", &snapst)
c.Assert(err, IsNil)
c.Check(snapst.Sequence[0].RealName, Equals, "foo")
c.Check(snapst.Sequence[1].RealName, Equals, "foo")
// transition of:
// - Candidate for "bar" -> tasks SnapSetup.SideInfo
t = st.Task("2")
err = t.Get("snap-setup", &snapsup)
c.Assert(err, IsNil)
c.Assert(snapsup.SideInfo, DeepEquals, &snap.SideInfo{
RealName: "bar",
Revision: snap.R("x1"),
})
// FIXME: bar is now empty and should no longer be there?
err = snapstate.Get(st, "bar", &snapst)
c.Assert(err, IsNil)
}
示例3: TestDoLinkSnapSuccess
func (s *linkSnapSuite) TestDoLinkSnapSuccess(c *C) {
s.state.Lock()
t := s.state.NewTask("link-snap", "test")
t.Set("snap-setup", &snapstate.SnapSetup{
SideInfo: &snap.SideInfo{
RealName: "foo",
Revision: snap.R(33),
},
Channel: "beta",
})
s.state.NewChange("dummy", "...").AddTask(t)
s.state.Unlock()
s.snapmgr.Ensure()
s.snapmgr.Wait()
s.state.Lock()
defer s.state.Unlock()
var snapst snapstate.SnapState
err := snapstate.Get(s.state, "foo", &snapst)
c.Assert(err, IsNil)
typ, err := snapst.Type()
c.Check(err, IsNil)
c.Check(typ, Equals, snap.TypeApp)
c.Check(snapst.Active, Equals, true)
c.Check(snapst.Sequence, HasLen, 1)
c.Check(snapst.Current, Equals, snap.R(33))
c.Check(snapst.Channel, Equals, "beta")
c.Check(t.Status(), Equals, state.DoneStatus)
c.Check(s.stateBackend.restartRequested, Equals, false)
}
示例4: undoSetupProfiles
func (m *InterfaceManager) undoSetupProfiles(task *state.Task, tomb *tomb.Tomb) error {
st := task.State()
st.Lock()
defer st.Unlock()
ss, err := snapstate.TaskSnapSetup(task)
if err != nil {
return err
}
snapName := ss.Name()
// Get the name from SnapSetup and use it to find the current SideInfo
// about the snap, if there is one.
var snapst snapstate.SnapState
err = snapstate.Get(st, snapName, &snapst)
if err != nil && err != state.ErrNoState {
return err
}
sideInfo := snapst.CurrentSideInfo()
if sideInfo == nil {
// The snap was not installed before so undo should remove security profiles.
return m.removeProfilesForSnap(task, tomb, snapName)
} else {
// The snap was installed before so undo should setup the old security profiles.
snapInfo, err := snap.ReadInfo(snapName, sideInfo)
if err != nil {
return err
}
return m.setupProfilesForSnap(task, tomb, snapInfo, snapst.DevMode)
}
}
示例5: TestDoUndoLinkSnap
func (s *linkSnapSuite) TestDoUndoLinkSnap(c *C) {
s.state.Lock()
defer s.state.Unlock()
si := &snap.SideInfo{
RealName: "foo",
Revision: snap.R(33),
}
t := s.state.NewTask("link-snap", "test")
t.Set("snap-setup", &snapstate.SnapSetup{
SideInfo: si,
Channel: "beta",
})
chg := s.state.NewChange("dummy", "...")
chg.AddTask(t)
terr := s.state.NewTask("error-trigger", "provoking total undo")
terr.WaitFor(t)
chg.AddTask(terr)
s.state.Unlock()
for i := 0; i < 3; i++ {
s.snapmgr.Ensure()
s.snapmgr.Wait()
}
s.state.Lock()
var snapst snapstate.SnapState
err := snapstate.Get(s.state, "foo", &snapst)
c.Assert(err, Equals, state.ErrNoState)
c.Check(t.Status(), Equals, state.UndoneStatus)
}
示例6: setupAffectedSnaps
func (m *InterfaceManager) setupAffectedSnaps(task *state.Task, affectingSnap string, affectedSnaps []string) error {
st := task.State()
// Setup security of the affected snaps.
for _, affectedSnapName := range affectedSnaps {
// the snap that triggered the change needs to be skipped
if affectedSnapName == affectingSnap {
continue
}
var snapst snapstate.SnapState
if err := snapstate.Get(st, affectedSnapName, &snapst); err != nil {
return err
}
affectedSnapInfo, err := snapst.CurrentInfo()
if err != nil {
return err
}
snap.AddImplicitSlots(affectedSnapInfo)
opts := confinementOptions(snapst.Flags)
if err := setupSnapSecurity(task, affectedSnapInfo, opts, m.repo); err != nil {
return err
}
}
return nil
}
示例7: doDisconnect
func (m *InterfaceManager) doDisconnect(task *state.Task, _ *tomb.Tomb) error {
st := task.State()
st.Lock()
defer st.Unlock()
plugRef, slotRef, err := getPlugAndSlotRefs(task)
if err != nil {
return err
}
conns, err := getConns(st)
if err != nil {
return err
}
var affectedConns []interfaces.ConnRef
if plugRef.Snap != "" && plugRef.Name != "" && slotRef.Snap != "" && slotRef.Name != "" {
if err := m.repo.Disconnect(plugRef.Snap, plugRef.Name, slotRef.Snap, slotRef.Name); err != nil {
return err
}
affectedConns = []interfaces.ConnRef{{plugRef, slotRef}}
} else if plugRef.Name != "" && slotRef.Snap == "" && slotRef.Name == "" {
// NOTE: plugRef.Snap can be either empty or not, Connected handles both
affectedConns, err = m.repo.Connected(plugRef.Snap, plugRef.Name)
if err != nil {
return err
}
m.repo.DisconnectAll(affectedConns)
} else if plugRef.Snap == "" && plugRef.Name == "" && slotRef.Name != "" {
// Symmetrically, slotRef.Snap can be either empty or not
affectedConns, err = m.repo.Connected(slotRef.Snap, slotRef.Name)
if err != nil {
return err
}
m.repo.DisconnectAll(affectedConns)
} else {
return fmt.Errorf("internal error, unhandled disconnect case plug: %q, slot: %q", plugRef, slotRef)
}
affectedSnaps := snapNamesFromConns(affectedConns)
for _, snapName := range affectedSnaps {
var snapst snapstate.SnapState
if err := snapstate.Get(st, snapName, &snapst); err != nil {
return err
}
snapInfo, err := snapst.CurrentInfo()
if err != nil {
return err
}
opts := confinementOptions(snapst.Flags)
if err := setupSnapSecurity(task, snapInfo, opts, m.repo); err != nil {
return &state.Retry{}
}
}
for _, conn := range affectedConns {
delete(conns, conn.ID())
}
setConns(st, conns)
return nil
}
示例8: TestDoDiscardSnapToEmpty
func (s *discardSnapSuite) TestDoDiscardSnapToEmpty(c *C) {
s.state.Lock()
snapstate.Set(s.state, "foo", &snapstate.SnapState{
Sequence: []*snap.SideInfo{
{RealName: "foo", Revision: snap.R(3)},
},
Current: snap.R(3),
SnapType: "app",
})
t := s.state.NewTask("discard-snap", "test")
t.Set("snap-setup", &snapstate.SnapSetup{
SideInfo: &snap.SideInfo{
RealName: "foo",
Revision: snap.R(33),
},
})
s.state.NewChange("dummy", "...").AddTask(t)
s.state.Unlock()
s.snapmgr.Ensure()
s.snapmgr.Wait()
s.state.Lock()
defer s.state.Unlock()
var snapst snapstate.SnapState
err := snapstate.Get(s.state, "foo", &snapst)
c.Assert(err, Equals, state.ErrNoState)
}
示例9: installLocalTestSnap
func (ms *mgrsSuite) installLocalTestSnap(c *C, snapYamlContent string) *snap.Info {
st := ms.o.State()
snapPath := makeTestSnap(c, snapYamlContent)
snapf, err := snap.Open(snapPath)
c.Assert(err, IsNil)
info, err := snap.ReadInfoFromSnapFile(snapf, nil)
c.Assert(err, IsNil)
// store current state
snapName := info.Name()
var snapst snapstate.SnapState
snapstate.Get(st, snapName, &snapst)
ts, err := snapstate.InstallPath(st, &snap.SideInfo{RealName: snapName}, snapPath, "", snapstate.Flags{DevMode: true})
c.Assert(err, IsNil)
chg := st.NewChange("install-snap", "...")
chg.AddAll(ts)
st.Unlock()
err = ms.o.Settle()
st.Lock()
c.Assert(err, IsNil)
c.Assert(chg.Err(), IsNil)
c.Assert(chg.Status(), Equals, state.DoneStatus, Commentf("install-snap change failed with: %v", chg.Err()))
return info
}
示例10: doConnect
func (m *InterfaceManager) doConnect(task *state.Task, _ *tomb.Tomb) error {
st := task.State()
st.Lock()
defer st.Unlock()
plugRef, slotRef, err := getPlugAndSlotRefs(task)
if err != nil {
return err
}
conns, err := getConns(st)
if err != nil {
return err
}
err = m.repo.Connect(plugRef.Snap, plugRef.Name, slotRef.Snap, slotRef.Name)
if err != nil {
return err
}
plug := m.repo.Plug(plugRef.Snap, plugRef.Name)
var plugSnapst snapstate.SnapState
if err := snapstate.Get(st, plugRef.Snap, &plugSnapst); err != nil {
return err
}
slot := m.repo.Slot(slotRef.Snap, slotRef.Name)
var slotSnapst snapstate.SnapState
if err := snapstate.Get(st, slotRef.Snap, &slotSnapst); err != nil {
return err
}
if err := setupSnapSecurity(task, plug.Snap, plugSnapst.DevModeAllowed(), m.repo); err != nil {
return err
}
if err := setupSnapSecurity(task, slot.Snap, slotSnapst.DevModeAllowed(), m.repo); err != nil {
return err
}
conns[connID(plugRef, slotRef)] = connState{Interface: plug.Interface}
setConns(st, conns)
return nil
}
示例11: TestUpdateRevisionsKernelSimple
func (bs *bootedSuite) TestUpdateRevisionsKernelSimple(c *C) {
st := bs.overlord.State()
bs.makeInstalledKernelOS(c, st)
bs.bootloader.BootVars["snap_kernel"] = "canonical-pc-linux_1.snap"
err := boot.UpdateRevisions(bs.overlord)
c.Assert(err, IsNil)
st.Lock()
defer st.Unlock()
// canonical-pc-linux "current" got reverted but ubuntu-core did not
var snapst snapstate.SnapState
err = snapstate.Get(st, "canonical-pc-linux", &snapst)
c.Assert(err, IsNil)
c.Assert(snapst.Current, Equals, snap.R(1))
c.Assert(snapst.Active, Equals, true)
err = snapstate.Get(st, "ubuntu-core", &snapst)
c.Assert(err, IsNil)
c.Assert(snapst.Current, Equals, snap.R(2))
c.Assert(snapst.Active, Equals, true)
}
示例12: mockUpdatedSnap
func (s *interfaceManagerSuite) mockUpdatedSnap(c *C, yamlText string, revision int) *snap.Info {
sideInfo := &snap.SideInfo{Revision: snap.R(revision)}
snapInfo := snaptest.MockSnap(c, yamlText, "", sideInfo)
sideInfo.RealName = snapInfo.Name()
s.state.Lock()
defer s.state.Unlock()
// Put the new revision (stored in SideInfo) into the state
var snapst snapstate.SnapState
err := snapstate.Get(s.state, snapInfo.Name(), &snapst)
c.Assert(err, IsNil)
snapst.Sequence = append(snapst.Sequence, sideInfo)
snapstate.Set(s.state, snapInfo.Name(), &snapst)
return snapInfo
}
示例13: TestPatch1
func (s *patch1Suite) TestPatch1(c *C) {
restore := patch.MockReadInfo(s.readInfo)
defer restore()
r, err := os.Open(dirs.SnapStateFile)
c.Assert(err, IsNil)
defer r.Close()
st, err := state.ReadState(nil, r)
c.Assert(err, IsNil)
// go from patch-level 0 to patch-level 1
restorer := patch.MockLevel(1)
defer restorer()
err = patch.Apply(st)
c.Assert(err, IsNil)
st.Lock()
defer st.Unlock()
expected := []struct {
name string
typ snap.Type
cur snap.Revision
}{
{"foo", snap.TypeApp, snap.R(22)},
{"core", snap.TypeOS, snap.R(111)},
{"borken", snap.TypeApp, snap.R(-2)},
{"wip", "", snap.R(0)},
}
for _, exp := range expected {
var snapst snapstate.SnapState
err := snapstate.Get(st, exp.name, &snapst)
c.Assert(err, IsNil)
c.Check(snap.Type(snapst.SnapType), Equals, exp.typ)
c.Check(snapst.Current, Equals, exp.cur)
}
// ensure we only moved forward to patch-level 1
var patchLevel int
err = st.Get("patch-level", &patchLevel)
c.Assert(err, IsNil)
c.Assert(patchLevel, Equals, 1)
}
示例14: hookSetup
func hookSetup(task *state.Task) (*HookSetup, *snapstate.SnapState, error) {
var hooksup HookSetup
err := task.Get("hook-setup", &hooksup)
if err != nil {
return nil, nil, fmt.Errorf("cannot extract hook setup from task: %s", err)
}
var snapst snapstate.SnapState
err = snapstate.Get(task.State(), hooksup.Snap, &snapst)
if err == state.ErrNoState {
return nil, nil, fmt.Errorf("cannot find %q snap", hooksup.Snap)
}
if err != nil {
return nil, nil, fmt.Errorf("cannot handle %q snap: %v", hooksup.Snap, err)
}
return &hooksup, &snapst, nil
}
示例15: TestDoLinkSnapTryToCleanupOnError
func (s *linkSnapSuite) TestDoLinkSnapTryToCleanupOnError(c *C) {
s.state.Lock()
defer s.state.Unlock()
si := &snap.SideInfo{
RealName: "foo",
Revision: snap.R(35),
}
t := s.state.NewTask("link-snap", "test")
t.Set("snap-setup", &snapstate.SnapSetup{
SideInfo: si,
Channel: "beta",
})
s.fakeBackend.linkSnapFailTrigger = "/snap/foo/35"
s.state.NewChange("dummy", "...").AddTask(t)
s.state.Unlock()
s.snapmgr.Ensure()
s.snapmgr.Wait()
s.state.Lock()
// state as expected
var snapst snapstate.SnapState
err := snapstate.Get(s.state, "foo", &snapst)
c.Assert(err, Equals, state.ErrNoState)
// tried to cleanup
c.Check(s.fakeBackend.ops, DeepEquals, fakeOps{
{
op: "candidate",
sinfo: *si,
},
{
op: "link-snap.failed",
name: "/snap/foo/35",
},
{
op: "unlink-snap",
name: "/snap/foo/35",
},
})
}