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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2022 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 (
"errors"
. "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/state"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/testutil"
)
type kmodSuite struct {
testutil.BaseTest
state *state.State
mockContext *hookstate.Context
mockHandler *hooktest.MockHandler
hookTask *state.Task
// A connection state for a snap using the kmod interface with the plug
// properly configured, which we'll be reusing in different test cases
regularConnState map[string]any
}
var _ = Suite(&kmodSuite{})
func (s *kmodSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
dirs.SetRootDir(c.MkDir())
s.mockHandler = hooktest.NewMockHandler()
s.state = state.New(nil)
s.state.Lock()
defer s.state.Unlock()
task := s.state.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "snap1", Revision: snap.R(42), Hook: "kmod"}
ctx, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
c.Assert(err, IsNil)
s.mockContext = ctx
s.regularConnState = map[string]any{
"interface": "kernel-module-load",
"plug-static": map[string]any{
"modules": []any{
map[string]any{
"name": "module1",
"load": "dynamic",
},
map[string]any{
"name": "module2",
"load": "dynamic",
"options": "*",
},
},
},
}
s.hookTask = task
}
func (s *kmodSuite) injectSnapWithProperPlug(c *C) {
s.state.Lock()
mockInstalledSnap(c, s.state, `name: snap1`, "")
s.state.Set("conns", map[string]any{
"snap1:plug1 snap2:slot2": s.regularConnState,
})
s.state.Unlock()
}
func (s *kmodSuite) TestMissingContext(c *C) {
_, _, err := ctlcmd.Run(nil, []string{"kmod", "insert", "module1"}, 0)
c.Check(err, ErrorMatches, `cannot invoke snapctl operation commands \(here "kmod"\) from outside of a snap`)
_, _, err = ctlcmd.Run(nil, []string{"kmod", "remove", "module1"}, 0)
c.Check(err, ErrorMatches, `cannot invoke snapctl operation commands \(here "kmod"\) from outside of a snap`)
}
func (s *kmodSuite) TestMatchConnection(c *C) {
for _, td := range []struct {
attributes map[string]any
moduleName string
moduleOptions []string
expectedMatch bool
}{
// missing "load" attribute
{map[string]any{}, "", []string{}, false},
// empty "load" attribute
{map[string]any{"load": ""}, "", []string{}, false},
// "load" attribute must be set to "dynamic"
{map[string]any{"load": "on-boot"}, "", []string{}, false},
// different module name
{map[string]any{"load": "dynamic", "name": "mod1"}, "mod2", []string{}, false},
// options given but plug does not have "options" attribute
{map[string]any{"load": "dynamic", "name": "mod1"}, "mod1", []string{"opt1"}, false},
// options given but plug does not have "options" set to "*"
{map[string]any{"load": "dynamic", "name": "mod1", "options": "opt1"}, "mod1", []string{"opt1"}, false},
// happy with no options
{map[string]any{"load": "dynamic", "name": "mod1"}, "mod1", []string{}, true},
// happy with options and "*" on plug
{map[string]any{"load": "dynamic", "name": "mod1", "options": "*"}, "mod1", []string{"opt1"}, true},
} {
testLabel := Commentf("Attrs: %v, name: %q, opts: %q", td.attributes, td.moduleName, td.moduleOptions)
matches := ctlcmd.KmodMatchConnection(td.attributes, td.moduleName, td.moduleOptions)
c.Check(matches, Equals, td.expectedMatch, testLabel)
}
}
func (s *kmodSuite) TestFindConnectionBadConnection(c *C) {
setup := &hookstate.HookSetup{}
// Inject some invalid connection data into the state, so that
// ifacestate.ConnectionStates() will return an error.
state := state.New(nil)
state.Lock()
task := state.NewTask("test-task", "my test task")
state.Set("conns", "I wish I was JSON")
state.Unlock()
ctx, err := hookstate.NewContext(task, state, setup, s.mockHandler, "")
c.Assert(err, IsNil)
err = ctlcmd.KmodCheckConnection(ctx, "module1", []string{"one", "two"})
c.Assert(err, ErrorMatches, `.*internal error: cannot get connections: .*`)
}
func (s *kmodSuite) TestFindConnectionMissingProperPlug(c *C) {
s.state.Lock()
mockInstalledSnap(c, s.state, `name: snap1`, "")
// Inject a lot of connections in the state, but all of them defective for
// one or another reason
connections := make(map[string]any)
// wrong interface
conn := CopyMap(s.regularConnState)
conn["interface"] = "unrelated"
connections["snap1:plug1 snap2:slot1"] = conn
// undesired
conn = CopyMap(s.regularConnState)
conn["undesired"] = true
connections["snap1:plug2 snap2:slot1"] = conn
// hotplug gone
conn = CopyMap(s.regularConnState)
conn["hotplug-gone"] = true
connections["snap1:plug3 snap2:slot1"] = conn
// different snap
conn = CopyMap(s.regularConnState)
connections["othersnap:plug1 snap2:slot1"] = conn
// missing plug info
conn = CopyMap(s.regularConnState)
delete(conn, "plug-static")
connections["snap1:plug4 snap2:slot1"] = conn
// good connection, finally; but our module name won't match
conn = CopyMap(s.regularConnState)
connections["snap1:plug5 snap2:slot1"] = conn
s.state.Set("conns", connections)
s.state.Unlock()
err := ctlcmd.KmodCheckConnection(s.mockContext, "module3", []string{"opt1=v1"})
c.Check(err, ErrorMatches, "required interface not connected")
}
func (s *kmodSuite) TestFindConnectionHappy(c *C) {
s.injectSnapWithProperPlug(c)
err := ctlcmd.KmodCheckConnection(s.mockContext, "module2", []string{"opt1=v1"})
c.Check(err, IsNil)
}
func (s *kmodSuite) TestInsertFailure(c *C) {
s.injectSnapWithProperPlug(c)
var loadModuleError error
var ensureConnectionError error
r1 := ctlcmd.MockKmodCheckConnection(func(ctx *hookstate.Context, moduleName string, moduleOptions []string) error {
c.Check(moduleName, Equals, "moderr")
c.Check(moduleOptions, DeepEquals, []string{"o1=v1", "o2=v2"})
return ensureConnectionError
})
defer r1()
r2 := ctlcmd.MockKmodLoadModule(func(name string, options []string) error {
c.Check(name, Equals, "moderr")
c.Check(options, DeepEquals, []string{"o1=v1", "o2=v2"})
return loadModuleError
})
defer r2()
for _, td := range []struct {
ensureConnectionError error
loadModuleError error
expectedError string
}{
{
// error retrieving the snap connections
ensureConnectionError: errors.New("state error"),
expectedError: `cannot load module "moderr": state error`,
},
{
// error calling modprobe
loadModuleError: errors.New("modprobe failure"),
expectedError: `cannot load module "moderr": modprobe failure`,
},
} {
ensureConnectionError = td.ensureConnectionError
loadModuleError = td.loadModuleError
_, _, err := ctlcmd.Run(s.mockContext, []string{"kmod", "insert", "moderr", "o1=v1", "o2=v2"}, 0)
c.Check(err, ErrorMatches, td.expectedError)
}
}
func (s *kmodSuite) TestInsertHappy(c *C) {
s.injectSnapWithProperPlug(c)
loadModuleCalls := 0
restore := ctlcmd.MockKmodLoadModule(func(name string, options []string) error {
loadModuleCalls++
c.Check(name, Equals, "module2")
c.Check(options, DeepEquals, []string{"opt1=v1", "opt2=v2"})
return nil
})
defer restore()
_, _, err := ctlcmd.Run(s.mockContext,
[]string{"kmod", "insert", "module2", "opt1=v1", "opt2=v2"}, 0)
c.Check(err, IsNil)
c.Check(loadModuleCalls, Equals, 1)
}
func (s *kmodSuite) TestRemoveFailure(c *C) {
s.injectSnapWithProperPlug(c)
var loadModuleError error
var ensureConnectionError error
r1 := ctlcmd.MockKmodCheckConnection(func(ctx *hookstate.Context, moduleName string, moduleOptions []string) error {
c.Check(moduleName, Equals, "moderr")
c.Check(moduleOptions, HasLen, 0)
return ensureConnectionError
})
defer r1()
r2 := ctlcmd.MockKmodUnloadModule(func(name string) error {
c.Check(name, Equals, "moderr")
return loadModuleError
})
defer r2()
for _, td := range []struct {
ensureConnectionError error
loadModuleError error
expectedError string
}{
{
// error retrieving the snap connections
ensureConnectionError: errors.New("state error"),
expectedError: `cannot unload module "moderr": state error`,
},
{
// error calling modprobe
loadModuleError: errors.New("modprobe failure"),
expectedError: `cannot unload module "moderr": modprobe failure`,
},
} {
ensureConnectionError = td.ensureConnectionError
loadModuleError = td.loadModuleError
_, _, err := ctlcmd.Run(s.mockContext, []string{"kmod", "remove", "moderr"}, 0)
c.Check(err, ErrorMatches, td.expectedError)
}
}
func (s *kmodSuite) TestRemoveHappy(c *C) {
s.injectSnapWithProperPlug(c)
unloadModuleCalls := 0
restore := ctlcmd.MockKmodUnloadModule(func(name string) error {
unloadModuleCalls++
c.Check(name, Equals, "module2")
return nil
})
defer restore()
_, _, err := ctlcmd.Run(s.mockContext,
[]string{"kmod", "remove", "module2"}, 0)
c.Check(err, IsNil)
c.Check(unloadModuleCalls, Equals, 1)
}
func (s *kmodSuite) TestkmodCommandExecute(c *C) {
// This is a useless test just to make test coverage greener. The Execute()
// method exercised here is never called in real life.
cmd := &ctlcmd.KmodCommand{}
err := cmd.Execute([]string{})
c.Check(err, IsNil)
}
|