1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ctlcmd_test
import (
"fmt"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
"github.com/snapcore/snapd/overlord/hookstate/hooktest"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/snapstate/snapstatetest"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
)
type isConnectedSuite struct {
testutil.BaseTest
st *state.State
mockHandler *hooktest.MockHandler
}
var _ = Suite(&isConnectedSuite{})
func (s *isConnectedSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
dirs.SetRootDir(c.MkDir())
s.AddCleanup(func() { dirs.SetRootDir("/") })
s.st = state.New(nil)
s.mockHandler = hooktest.NewMockHandler()
}
var isConnectedTests = []struct {
args []string
stdout, stderr, err string
exitCode int
}{{
args: []string{"is-connected"},
err: "must specify either a plug/slot name or --list",
}, {
args: []string{"is-connected", "plug1"},
}, {
args: []string{"is-connected", "slot1"},
}, {
// reported as not connected because of undesired flag
args: []string{"is-connected", "plug2"},
exitCode: 1,
}, {
// reported as not connected because of hotplug-gone flag
args: []string{"is-connected", "plug3"},
exitCode: 1,
}, {
args: []string{"is-connected", "slot2"},
err: `snap "snap1" has no plug or slot named "slot2"`,
}, {
args: []string{"is-connected", "foo"},
err: `snap "snap1" has no plug or slot named "foo"`,
}, {
// snap1:plug1 does not use an allowed interface
args: []string{"is-connected", "--pid", "1002", "plug1"},
err: `cannot use --pid check with snap1:plug1`,
}, {
// snap1:slot1 does not use an allowed interface
args: []string{"is-connected", "--pid", "1002", "slot1"},
err: `cannot use --pid check with snap1:slot1`,
}, {
// snap1:cc slot is not connected to snap2
args: []string{"is-connected", "--pid", "1002", "cc"},
exitCode: 1,
}, {
// snap1:cc slot is connected to snap3
args: []string{"is-connected", "--pid", "1003", "cc"},
exitCode: 0,
}, {
// snap1:cc slot is not connected to a non-snap pid
args: []string{"is-connected", "--pid", "42", "cc"},
exitCode: ctlcmd.NotASnapCode,
}, {
// snap1:cc slot is connected to a classic snap5
args: []string{"is-connected", "--pid", "1005", "cc"},
exitCode: 0,
}, {
// snap1:audio-record slot is not connected to classic snap5
args: []string{"is-connected", "--pid", "1005", "audio-record"},
exitCode: ctlcmd.ClassicSnapCode,
}, {
// snap1:plug1 does not use an allowed interface
args: []string{"is-connected", "--apparmor-label", "snap.snap2.app", "plug1"},
err: `cannot use --apparmor-label check with snap1:plug1`,
}, {
// snap1:slot1 does not use an allowed interface
args: []string{"is-connected", "--apparmor-label", "snap.snap2.app", "slot1"},
err: `cannot use --apparmor-label check with snap1:slot1`,
}, {
// snap1:cc slot is not connected to snap2
args: []string{"is-connected", "--apparmor-label", "snap.snap2.app", "cc"},
exitCode: 1,
}, {
// snap1:cc slot is connected to snap3
args: []string{"is-connected", "--apparmor-label", "snap.snap3.app", "cc"},
exitCode: 0,
}, {
// snap1:cc slot is not connected to a non-snap pid
args: []string{"is-connected", "--apparmor-label", "/usr/bin/evince", "cc"},
exitCode: ctlcmd.NotASnapCode,
}, {
// snap1:cc slot is connected to a classic snap5
args: []string{"is-connected", "--apparmor-label", "snap.snap5.app", "cc"},
exitCode: 0,
}, {
// snap1:audio-record slot is not connected to classic snap5
args: []string{"is-connected", "--apparmor-label", "snap.snap5.app", "audio-record"},
exitCode: ctlcmd.ClassicSnapCode,
}}
func mockInstalledSnap(c *C, st *state.State, snapYaml, cohortKey string) *snap.Info {
info := snaptest.MockSnapCurrent(c, snapYaml, &snap.SideInfo{Revision: snap.R(1)})
snapstate.Set(st, info.InstanceName(), &snapstate.SnapState{
Active: true,
Sequence: snapstatetest.NewSequenceFromSnapSideInfos([]*snap.SideInfo{
{
RealName: info.SnapName(),
Revision: info.Revision,
SnapID: info.InstanceName() + "-id",
},
}),
Current: info.Revision,
TrackingChannel: "stable",
CohortKey: cohortKey,
})
return info
}
func (s *isConnectedSuite) testIsConnected(c *C, context *hookstate.Context) {
mockInstalledSnap(c, s.st, `name: snap1
plugs:
plug1:
interface: x11
plug2:
interface: x11
plug3:
interface: x11
slots:
slot1:
interface: x11
cc:
interface: cups-control
audio-record:
interface: audio-record`, "")
mockInstalledSnap(c, s.st, `name: snap2
slots:
slot2:
interface: x11`, "")
mockInstalledSnap(c, s.st, `name: snap3
plugs:
plug4:
interface: x11
cc:
interface: cups-control
slots:
slot3:
interface: x11`, "")
mockInstalledSnap(c, s.st, `name: snap4
slots:
slot4:
interface: x11`, "")
mockInstalledSnap(c, s.st, `name: snap5
confinement: classic
plugs:
cc:
interface: cups-control`, "")
restore := ctlcmd.MockCgroupSnapNameFromPid(func(pid int) (string, error) {
switch {
case 1000 < pid && pid < 1100:
return fmt.Sprintf("snap%d", pid-1000), nil
default:
return "", fmt.Errorf("Not a snap")
}
})
defer restore()
s.st.Set("conns", map[string]any{
"snap1:plug1 snap2:slot2": map[string]any{},
"snap1:plug2 snap3:slot3": map[string]any{"undesired": true},
"snap1:plug3 snap4:slot4": map[string]any{"hotplug-gone": true},
"snap3:plug4 snap1:slot1": map[string]any{},
"snap3:cc snap1:cc": map[string]any{},
"snap5:cc snap1:cc": map[string]any{},
})
s.st.Unlock()
defer s.st.Lock()
for _, test := range isConnectedTests {
stdout, stderr, err := ctlcmd.Run(context, test.args, 0)
comment := Commentf("%s", test.args)
if test.exitCode > 0 {
c.Check(err, DeepEquals, &ctlcmd.UnsuccessfulError{ExitCode: test.exitCode}, comment)
} else {
if test.err == "" {
c.Check(err, IsNil, comment)
} else {
c.Check(err, ErrorMatches, test.err, comment)
}
}
c.Check(string(stdout), Equals, test.stdout, comment)
c.Check(string(stderr), Equals, "", comment)
}
}
func (s *isConnectedSuite) TestIsConnectedFromHook(c *C) {
s.st.Lock()
defer s.st.Unlock()
task := s.st.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "snap1", Revision: snap.R(1), Hook: "test-hook"}
mockContext, err := hookstate.NewContext(task, s.st, setup, s.mockHandler, "")
c.Check(err, IsNil)
s.testIsConnected(c, mockContext)
}
func (s *isConnectedSuite) TestIsConnectedFromApp(c *C) {
s.st.Lock()
defer s.st.Unlock()
// ephemeral context
setup := &hookstate.HookSetup{Snap: "snap1", Revision: snap.R(1)}
mockContext, err := hookstate.NewContext(nil, s.st, setup, s.mockHandler, "")
c.Check(err, IsNil)
// validity
c.Assert(mockContext.IsEphemeral(), Equals, true)
s.testIsConnected(c, mockContext)
}
func (s *isConnectedSuite) TestNoContextError(c *C) {
stdout, stderr, err := ctlcmd.Run(nil, []string{"is-connected", "foo"}, 0)
c.Check(err, ErrorMatches, `cannot invoke snapctl operation commands \(here "is-connected"\) from outside of a snap`)
c.Check(string(stdout), Equals, "")
c.Check(string(stderr), Equals, "")
}
func (s *isConnectedSuite) TestGetRegularUser(c *C) {
s.st.Lock()
mockInstalledSnap(c, s.st, `name: snap1
plugs:
plug1:
interface: x11`, "")
s.st.Set("conns", map[string]any{
"snap1:plug1 snap2:slot2": map[string]any{},
})
setup := &hookstate.HookSetup{Snap: "snap1", Revision: snap.R(1)}
s.st.Unlock()
mockContext, err := hookstate.NewContext(nil, s.st, setup, s.mockHandler, "")
c.Assert(err, IsNil)
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"is-connected", "plug1"}, 1000)
c.Check(err, IsNil)
c.Check(string(stdout), Equals, "")
c.Check(string(stderr), Equals, "")
}
func (s *isConnectedSuite) TestIsConnectedList(c *C) {
s.st.Lock()
defer s.st.Unlock()
task := s.st.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "snap1", Revision: snap.R(1), Hook: "test-hook"}
mockContext, err := hookstate.NewContext(task, s.st, setup, s.mockHandler, "")
c.Check(err, IsNil)
mockInstalledSnap(c, s.st, `name: snap1
plugs:
plug1a:
interface: x11
plug1b:
interface: x11
plug1c:
interface: x11
plug1d:
interface: x11
slots:
slot1a:
interface: x11
slot1b:
interface: x11`, "")
mockInstalledSnap(c, s.st, `name: snap2
plugs:
plug2a:
interface: x11
plug2b:
interface: x11
slots:
slot2a:
interface: x11
slot2b:
interface: x11`, "")
mockInstalledSnap(c, s.st, `name: snap3
plugs:
plug3a:
interface: x11
slots:
slot3a:
interface: x11
slot3b:
interface: x11`, "")
s.st.Set("conns", map[string]any{
"snap1:plug1a snap2:slot2a": map[string]any{},
"snap2:plug2a snap1:slot1a": map[string]any{},
"snap3:plug3a snap1:slot1a": map[string]any{},
"snap1:plug1c snap3:slot3a": map[string]any{"undesired": true},
"snap1:plug1d snap3:slot3b": map[string]any{"hotplug-gone": true},
})
s.st.Unlock()
defer s.st.Lock()
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"is-connected", "--list"}, 0)
c.Check(err, IsNil)
c.Check(string(stdout), Equals, "plug1a\nslot1a\n")
c.Check(string(stderr), Equals, "")
}
|