summaryrefslogtreecommitdiff
diff options
authorPaweł Stołowski <stolowski@gmail.com>2022-01-11 13:33:44 +0100
committerPaweł Stołowski <stolowski@gmail.com>2022-01-11 13:33:44 +0100
commit58537b552cf26c70a3d99408cfa603355f0feaff (patch)
treef3ff1c6731d84d26c2e9bf6272f72ca6f23a67a8
parente5fa7de437490b87d6ed3027d0a34551010f38cf (diff)
Add Active() convienience method to ConnectionState struct.ctlcmd-conns-helper
-rw-r--r--daemon/access.go2
-rw-r--r--overlord/hookstate/ctlcmd/is_connected.go2
-rw-r--r--overlord/ifacestate/ifacemgr.go6
-rw-r--r--overlord/ifacestate/ifacestate_test.go16
4 files changed, 24 insertions, 2 deletions
diff --git a/daemon/access.go b/daemon/access.go
index 2d943942fd..f3d99e08ba 100644
--- a/daemon/access.go
+++ b/daemon/access.go
@@ -191,7 +191,7 @@ func requireThemeApiAccessImpl(d *Daemon, ucred *ucrednet) *apiError {
return Forbidden("internal error: cannot get connections: %s", err)
}
for refStr, connState := range conns {
- if connState.Undesired || connState.HotplugGone || connState.Interface != "snap-themes-control" {
+ if !connState.Active() || connState.Interface != "snap-themes-control" {
continue
}
connRef, err := interfaces.ParseConnRef(refStr)
diff --git a/overlord/hookstate/ctlcmd/is_connected.go b/overlord/hookstate/ctlcmd/is_connected.go
index 7b44a44991..fb5f444567 100644
--- a/overlord/hookstate/ctlcmd/is_connected.go
+++ b/overlord/hookstate/ctlcmd/is_connected.go
@@ -152,7 +152,7 @@ func (c *isConnectedCommand) Execute(args []string) error {
// hooks). plug and slot names are unique within a snap, so there is no
// ambiguity when matching.
for refStr, connState := range conns {
- if connState.Undesired || connState.HotplugGone {
+ if !connState.Active() {
continue
}
connRef, err := interfaces.ParseConnRef(refStr)
diff --git a/overlord/ifacestate/ifacemgr.go b/overlord/ifacestate/ifacemgr.go
index 5e3a6df734..15096eedca 100644
--- a/overlord/ifacestate/ifacemgr.go
+++ b/overlord/ifacestate/ifacemgr.go
@@ -266,6 +266,12 @@ type ConnectionState struct {
HotplugGone bool
}
+// Active returns true if connection is not undesired and not removed by
+// hotplug.
+func (c ConnectionState) Active() bool {
+ return !(c.Undesired || c.HotplugGone)
+}
+
// ConnectionStates return the state of connections stored in the state.
// Note that this includes inactive connections (i.e. referring to non-
// existing plug/slots), so this map must be cross-referenced with current
diff --git a/overlord/ifacestate/ifacestate_test.go b/overlord/ifacestate/ifacestate_test.go
index 8bdf2187d2..3fb9e9a709 100644
--- a/overlord/ifacestate/ifacestate_test.go
+++ b/overlord/ifacestate/ifacestate_test.go
@@ -9024,3 +9024,19 @@ func (s *interfaceManagerSuite) TestConnectSetsUpSecurityFails(c *C) {
ifaces := repo.Interfaces()
c.Check(ifaces.Connections, HasLen, 0)
}
+
+func (s *interfaceManagerSuite) TestConnectionStateActive(c *C) {
+ for i, cs := range []struct {
+ undesired bool
+ hotplugGone bool
+ expectedActive bool
+ }{
+ {false, false, true},
+ {true, false, false},
+ {false, true, false},
+ {true, true, false},
+ } {
+ connState := ifacestate.ConnectionState{Undesired: cs.undesired, HotplugGone: cs.hotplugGone}
+ c.Assert(connState.Active(), Equals, cs.expectedActive, Commentf("#%d: %v", i, cs))
+ }
+}