func (s *assertMgrSuite) TestBaseSnapDeclaration(c *C) { s.state.Lock() defer s.state.Unlock() r1 := assertstest.MockBuiltinBaseDeclaration(nil) defer r1() baseDecl, err := assertstate.BaseDeclaration(s.state) c.Assert(err, Equals, asserts.ErrNotFound) c.Check(baseDecl, IsNil) r2 := assertstest.MockBuiltinBaseDeclaration([]byte(` type: base-declaration authority-id: canonical series: 16 plugs: iface: true `)) defer r2() baseDecl, err = assertstate.BaseDeclaration(s.state) c.Assert(err, IsNil) c.Check(baseDecl, NotNil) c.Check(baseDecl.PlugRule("iface"), NotNil) }
// The setup-profiles task will auto-connect plugs with viable candidates also condidering snap declarations. func (s *interfaceManagerSuite) TestDoSetupSnapSecurityAutoConnectsDeclBased(c *C) { restore := assertstest.MockBuiltinBaseDeclaration([]byte(` type: base-declaration authority-id: canonical series: 16 slots: test: allow-auto-connection: plug-publisher-id: - $SLOT_PUBLISHER_ID `)) defer restore() // Add the producer snap s.mockIface(c, &interfaces.TestInterface{InterfaceName: "test"}) s.mockSnapDecl(c, "producer", "one-publisher", nil) s.mockSnap(c, producerYaml) // Initialize the manager. This registers the producer snap. mgr := s.manager(c) // Add a sample snap with a plug with the "test" interface which should be auto-connected. s.mockSnapDecl(c, "consumer", "one-publisher", nil) snapInfo := s.mockSnap(c, consumerYaml) // Run the setup-snap-security task and let it finish. change := s.addSetupSnapSecurityChange(c, &snapstate.SnapSetup{ SideInfo: &snap.SideInfo{ RealName: snapInfo.Name(), SnapID: snapInfo.SnapID, Revision: snapInfo.Revision, }, }) mgr.Ensure() mgr.Wait() mgr.Stop() s.state.Lock() defer s.state.Unlock() // Ensure that the task succeeded. c.Assert(change.Status(), Equals, state.DoneStatus) // Ensure that "test" plug is now saved in the state as auto-connected. var conns map[string]interface{} err := s.state.Get("conns", &conns) c.Assert(err, IsNil) c.Check(conns, DeepEquals, map[string]interface{}{ "consumer:plug producer:slot": map[string]interface{}{"auto": true, "interface": "test"}, }) // Ensure that "test" is really connected. repo := mgr.Repository() plug := repo.Plug("consumer", "plug") c.Assert(plug, Not(IsNil)) c.Check(plug.Connections, HasLen, 1) }
func (s *interfaceManagerSuite) TestConnectTaskCheckAllowed(c *C) { restore := assertstest.MockBuiltinBaseDeclaration([]byte(` type: base-declaration authority-id: canonical series: 16 slots: test: allow-connection: plug-publisher-id: - $SLOT_PUBLISHER_ID `)) defer restore() s.mockIface(c, &interfaces.TestInterface{InterfaceName: "test"}) s.mockSnapDecl(c, "consumer", "one-publisher", nil) s.mockSnap(c, consumerYaml) s.mockSnapDecl(c, "producer", "one-publisher", nil) s.mockSnap(c, producerYaml) s.state.Lock() change := s.state.NewChange("kind", "summary") ts, err := ifacestate.Connect(s.state, "consumer", "plug", "producer", "slot") c.Assert(err, IsNil) ts.Tasks()[0].Set("snap-setup", &snapstate.SnapSetup{ SideInfo: &snap.SideInfo{ RealName: "consumer", }, }) change.AddAll(ts) s.state.Unlock() mgr := s.manager(c) mgr.Ensure() mgr.Wait() s.state.Lock() defer s.state.Unlock() c.Assert(change.Err(), IsNil) c.Check(change.Status(), Equals, state.DoneStatus) repo := mgr.Repository() plug := repo.Plug("consumer", "plug") slot := repo.Slot("producer", "slot") c.Assert(plug.Connections, HasLen, 1) c.Check(plug.Connections[0], DeepEquals, interfaces.SlotRef{Snap: "producer", Name: "slot"}) c.Check(slot.Connections[0], DeepEquals, interfaces.PlugRef{Snap: "consumer", Name: "plug"}) }
func (s *interfaceManagerSuite) TestCheckInterfacesDeny(c *C) { restore := assertstest.MockBuiltinBaseDeclaration([]byte(` type: base-declaration authority-id: canonical series: 16 slots: test: deny-installation: true `)) defer restore() s.mockIface(c, &interfaces.TestInterface{InterfaceName: "test"}) snapInfo := s.mockSnap(c, producerYaml) s.state.Lock() defer s.state.Unlock() c.Check(ifacestate.CheckInterfaces(s.state, snapInfo), ErrorMatches, "installation denied.*") }
func (s *interfaceManagerSuite) TestCheckInterfacesDenySkippedIfNoDecl(c *C) { restore := assertstest.MockBuiltinBaseDeclaration([]byte(` type: base-declaration authority-id: canonical series: 16 slots: test: deny-installation: true `)) defer restore() s.mockIface(c, &interfaces.TestInterface{InterfaceName: "test"}) // crucially, this test is missing this: s.mockSnapDecl(c, "producer", "producer-publisher", nil) snapInfo := s.mockSnap(c, producerYaml) s.state.Lock() defer s.state.Unlock() c.Check(ifacestate.CheckInterfaces(s.state, snapInfo), IsNil) }
func (s *interfaceManagerSuite) TestConnectTaskCheckNotAllowed(c *C) { restore := assertstest.MockBuiltinBaseDeclaration([]byte(` type: base-declaration authority-id: canonical series: 16 slots: test: allow-connection: plug-publisher-id: - $SLOT_PUBLISHER_ID `)) defer restore() s.mockIface(c, &interfaces.TestInterface{InterfaceName: "test"}) s.mockSnapDecl(c, "consumer", "consumer-publisher", nil) s.mockSnap(c, consumerYaml) s.mockSnapDecl(c, "producer", "producer-publisher", nil) s.mockSnap(c, producerYaml) s.state.Lock() change := s.state.NewChange("kind", "summary") ts, err := ifacestate.Connect(s.state, "consumer", "plug", "producer", "slot") c.Assert(err, IsNil) ts.Tasks()[0].Set("snap-setup", &snapstate.SnapSetup{ SideInfo: &snap.SideInfo{ RealName: "consumer", }, }) change.AddAll(ts) s.state.Unlock() mgr := s.manager(c) mgr.Ensure() mgr.Wait() s.state.Lock() defer s.state.Unlock() c.Check(change.Err(), ErrorMatches, `(?s).*connection not allowed by slot rule of interface "test".*`) c.Check(change.Status(), Equals, state.ErrorStatus) }
func (s *interfaceManagerSuite) testConnectTaskCheck(c *C, setup func(), check func(*state.Change)) { restore := assertstest.MockBuiltinBaseDeclaration([]byte(` type: base-declaration authority-id: canonical series: 16 slots: test: allow-connection: plug-publisher-id: - $SLOT_PUBLISHER_ID `)) defer restore() s.mockIface(c, &interfaces.TestInterface{InterfaceName: "test"}) setup() s.state.Lock() change := s.state.NewChange("kind", "summary") ts, err := ifacestate.Connect(s.state, "consumer", "plug", "producer", "slot") c.Assert(err, IsNil) ts.Tasks()[0].Set("snap-setup", &snapstate.SnapSetup{ SideInfo: &snap.SideInfo{ RealName: "consumer", }, }) change.AddAll(ts) s.state.Unlock() mgr := s.manager(c) mgr.Ensure() mgr.Wait() s.state.Lock() defer s.state.Unlock() check(change) }
func (s *interfaceManagerSuite) TestCheckInterfacesAllow(c *C) { restore := assertstest.MockBuiltinBaseDeclaration([]byte(` type: base-declaration authority-id: canonical series: 16 slots: test: deny-installation: true `)) defer restore() s.mockIface(c, &interfaces.TestInterface{InterfaceName: "test"}) s.mockSnapDecl(c, "producer", "producer-publisher", map[string]interface{}{ "slots": map[string]interface{}{ "test": "true", }, }) snapInfo := s.mockSnap(c, producerYaml) s.state.Lock() defer s.state.Unlock() c.Check(ifacestate.CheckInterfaces(s.state, snapInfo), IsNil) }