File: kernel_module_load_test.go

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (213 lines) | stat: -rw-r--r-- 6,293 bytes parent folder | download | duplicates (3)
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2021 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 builtin_test

import (
	"fmt"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/interfaces"
	"github.com/snapcore/snapd/interfaces/builtin"
	"github.com/snapcore/snapd/interfaces/kmod"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/testutil"
)

type KernelModuleLoadInterfaceSuite struct {
	testutil.BaseTest

	iface    interfaces.Interface
	slotInfo *snap.SlotInfo
	slot     *interfaces.ConnectedSlot
	plugInfo *snap.PlugInfo
	plug     *interfaces.ConnectedPlug
}

var _ = Suite(&KernelModuleLoadInterfaceSuite{
	iface: builtin.MustInterface("kernel-module-load"),
})

const kernelModuleLoadConsumerYaml = `name: consumer
version: 0
plugs:
 kmod:
  interface: kernel-module-load
  modules:
  - name: forbidden
    load: denied
  - name: mymodule1
    load: on-boot
    options: p1=3 p2=true p3
  - name: mymodule2
    options: param_1=ok param_2=false
  - name: expandvar
    options: opt=$FOO path=$SNAP_COMMON/bar
  - name: dyn-module1
    load: dynamic
    options: opt1=v1 opt2=v2
  - name: dyn-module2
    load: dynamic
    options: "*"
apps:
 app:
  plugs: [kmod]
`

const kernelModuleLoadCoreYaml = `name: core
version: 0
type: os
slots:
  kernel-module-load:
`

func (s *KernelModuleLoadInterfaceSuite) SetUpTest(c *C) {
	s.BaseTest.SetUpTest(c)

	s.plug, s.plugInfo = MockConnectedPlug(c, kernelModuleLoadConsumerYaml, nil, "kmod")
	s.slot, s.slotInfo = MockConnectedSlot(c, kernelModuleLoadCoreYaml, nil, "kernel-module-load")
}

func (s *KernelModuleLoadInterfaceSuite) TestName(c *C) {
	c.Assert(s.iface.Name(), Equals, "kernel-module-load")
}

func (s *KernelModuleLoadInterfaceSuite) TestSanitizeSlot(c *C) {
	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
}

func (s *KernelModuleLoadInterfaceSuite) TestSanitizePlug(c *C) {
	c.Check(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
	c.Check(interfaces.BeforeConnectPlug(s.iface, s.plug), IsNil)
}

func (s *KernelModuleLoadInterfaceSuite) TestSanitizePlugUnhappy(c *C) {
	var kernelModuleLoadYaml = `name: consumer
version: 0
plugs:
 kmod:
  interface: kernel-module-load
  %s
apps:
 app:
  plugs: [kmod]
`
	data := []struct {
		plugYaml      string
		expectedError string
	}{
		{
			"", // missing "modules" attribute
			`kernel-module-load "modules" attribute must be a list of dictionaries`,
		},
		{
			"modules: a string",
			`kernel-module-load "modules" attribute must be a list of dictionaries`,
		},
		{
			"modules: [this, is, a, list]",
			`kernel-module-load "modules" attribute must be a list of dictionaries`,
		},
		{
			"modules:\n  - name: [this, is, a, list]",
			`kernel-module-load "name" must be a string`,
		},
		{
			"modules:\n  - name: w3/rd*",
			`kernel-module-load "name" attribute is not a valid module name`,
		},
		{
			"modules:\n  - name: pcspkr",
			`kernel-module-load: must specify at least "load" or "options"`,
		},
		{
			"modules:\n  - name: pcspkr\n    load: [yes, no]",
			`kernel-module-load "load" must be a string`,
		},
		{
			"modules:\n  - name: pcspkr\n    load: maybe",
			`kernel-module-load "load" value is unrecognized: "maybe"`,
		},
		{
			"modules:\n  - name: pcspkr\n    options: [one, two]",
			`kernel-module-load "options" must be a string`,
		},
		{
			"modules:\n  - name: pcspkr\n    options: \"a\\nnewline\"",
			`kernel-module-load "options" attribute contains invalid characters: "a\\nnewline"`,
		},
		{
			"modules:\n  - name: pcspkr\n    options: \"5tartWithNumber=1\"",
			`kernel-module-load "options" attribute contains invalid characters: "5tartWithNumber=1"`,
		},
		{
			"modules:\n  - name: pcspkr\n    options: \"no-dashes\"",
			`kernel-module-load "options" attribute contains invalid characters: "no-dashes"`,
		},
		{
			// "*" is only allowed for `load: dynamic`
			"modules:\n  - name: pcspkr\n    options: \"*\"",
			`kernel-module-load "options" attribute contains invalid characters: "\*"`,
		},
		{
			"modules:\n  - name: pcspkr\n    load: denied\n    options: p1=true",
			`kernel-module-load "options" attribute incompatible with "load: denied"`,
		},
	}

	for _, testData := range data {
		snapYaml := fmt.Sprintf(kernelModuleLoadYaml, testData.plugYaml)
		plug, _ := MockConnectedPlug(c, snapYaml, nil, "kmod")
		err := interfaces.BeforeConnectPlug(s.iface, plug)
		c.Check(err, ErrorMatches, testData.expectedError, Commentf("yaml: %s", testData.plugYaml))
	}
}

func (s *KernelModuleLoadInterfaceSuite) TestKModSpec(c *C) {
	spec := &kmod.Specification{}
	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
	c.Check(spec.Modules(), DeepEquals, map[string]bool{
		"mymodule1": true,
	})
	c.Check(spec.ModuleOptions(), DeepEquals, map[string]string{
		"mymodule1":   "p1=3 p2=true p3",
		"mymodule2":   "param_1=ok param_2=false",
		"expandvar":   "opt=$FOO path=/var/snap/consumer/common/bar",
		"dyn-module1": "opt1=v1 opt2=v2",
		// No entry for dyn-module2, which has options set to "*"
	})
	c.Check(spec.DisallowedModules(), DeepEquals, []string{"forbidden"})
}

func (s *KernelModuleLoadInterfaceSuite) TestStaticInfo(c *C) {
	si := interfaces.StaticInfoOf(s.iface)
	c.Assert(si.ImplicitOnCore, Equals, true)
	c.Assert(si.ImplicitOnClassic, Equals, true)
	c.Assert(si.Summary, Equals, `allows constrained control over kernel module loading`)
	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "kernel-module-load")
}

func (s *KernelModuleLoadInterfaceSuite) TestAutoConnect(c *C) {
	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
}

func (s *KernelModuleLoadInterfaceSuite) TestInterfaces(c *C) {
	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
}