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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 | diff --git a/corecfg/corecfg.go b/corecfg/corecfg.go
index 3c6165157..f1652c313 100644
--- a/corecfg/corecfg.go
+++ b/corecfg/corecfg.go
@@ -38,8 +38,7 @@ var (
// ensureSupportInterface checks that the system has the core-support
// interface. An error is returned if this is not the case
func ensureSupportInterface() error {
- _, err := systemd.SystemctlCmd("--version")
- return err
+ return systemd.Available()
}
func snapctlGet(key string) (string, error) {
diff --git a/corecfg/services_test.go b/corecfg/services_test.go
index 87cb93cb7..ec72748e4 100644
--- a/corecfg/services_test.go
+++ b/corecfg/services_test.go
@@ -35,16 +35,22 @@ import (
type servicesSuite struct {
systemctlArgs [][]string
+
+ restorer func()
}
var _ = Suite(&servicesSuite{})
func (s *servicesSuite) SetUpSuite(c *C) {
- systemd.SystemctlCmd = func(args ...string) ([]byte, error) {
+ s.restorer = systemd.MockSystemctl(func(args ...string) ([]byte, error) {
s.systemctlArgs = append(s.systemctlArgs, args[:])
output := []byte("ActiveState=inactive")
return output, nil
- }
+ })
+}
+
+func (s *servicesSuite) TearDownSuite(c *C) {
+ s.restorer()
}
func (s *servicesSuite) SetUpTest(c *C) {
diff --git a/daemon/api_test.go b/daemon/api_test.go
index 4923c5317..7fa18c6d7 100644
--- a/daemon/api_test.go
+++ b/daemon/api_test.go
@@ -87,6 +87,7 @@ type apiBaseSuite struct {
storeSigning *assertstest.StoreStack
restoreRelease func()
trustedRestorer func()
+ systemctlRestorer func()
origSysctlCmd func(...string) ([]byte, error)
sysctlArgses [][]string
@@ -157,16 +158,15 @@ func (s *apiBaseSuite) SetUpSuite(c *check.C) {
s.restoreRelease = release.MockForcedDevmode(false)
snapstate.CanAutoRefresh = nil
- s.origSysctlCmd = systemd.SystemctlCmd
+ s.systemctlRestorer = systemd.MockSystemctl(s.systemctl)
s.origJctlCmd = systemd.JournalctlCmd
- systemd.SystemctlCmd = s.systemctl
systemd.JournalctlCmd = s.journalctl
}
func (s *apiBaseSuite) TearDownSuite(c *check.C) {
muxVars = nil
s.restoreRelease()
- systemd.SystemctlCmd = s.origSysctlCmd
+ s.systemctlRestorer()
}
func (s *apiBaseSuite) systemctl(args ...string) (buf []byte, err error) {
diff --git a/systemd/export_test.go b/systemd/export_test.go
index 0bd70348e..2301bf0fa 100644
--- a/systemd/export_test.go
+++ b/systemd/export_test.go
@@ -25,8 +25,7 @@ import (
)
var (
- SystemdRun = run // NOTE: plain Run clashes with check.v1
- Jctl = jctl
+ Jctl = jctl
)
func MockStopDelays(checkDelay, notifyDelay time.Duration) func() {
diff --git a/systemd/systemd.go b/systemd/systemd.go
index 9bd323390..2bc78891d 100644
--- a/systemd/systemd.go
+++ b/systemd/systemd.go
@@ -45,8 +45,8 @@ var (
stopNotifyDelay = 20 * time.Second
)
-// run calls systemctl with the given args, returning its standard output (and wrapped error)
-func run(args ...string) ([]byte, error) {
+// systemctlCmd calls systemctl with the given args, returning its standard output (and wrapped error)
+var systemctlCmd = func(args ...string) ([]byte, error) {
bs, err := exec.Command("systemctl", args...).CombinedOutput()
if err != nil {
exitCode, _ := osutil.ExitCode(err)
@@ -56,9 +56,20 @@ func run(args ...string) ([]byte, error) {
return bs, nil
}
-// SystemctlCmd is called from the commands to actually call out to
+// MockSystemctl is called from the commands to actually call out to
// systemctl. It's exported so it can be overridden by testing.
-var SystemctlCmd = run
+func MockSystemctl(f func(args ...string) ([]byte, error)) func() {
+ oldSystemctlCmd := systemctlCmd
+ systemctlCmd = f
+ return func() {
+ systemctlCmd = oldSystemctlCmd
+ }
+}
+
+func Available() error {
+ _, err := systemctlCmd("--version")
+ return err
+}
var osutilStreamCommand = osutil.StreamCommand
@@ -126,25 +137,25 @@ type systemd struct {
// DaemonReload reloads systemd's configuration.
func (*systemd) DaemonReload() error {
- _, err := SystemctlCmd("daemon-reload")
+ _, err := systemctlCmd("daemon-reload")
return err
}
// Enable the given service
func (s *systemd) Enable(serviceName string) error {
- _, err := SystemctlCmd("--root", s.rootDir, "enable", serviceName)
+ _, err := systemctlCmd("--root", s.rootDir, "enable", serviceName)
return err
}
// Disable the given service
func (s *systemd) Disable(serviceName string) error {
- _, err := SystemctlCmd("--root", s.rootDir, "disable", serviceName)
+ _, err := systemctlCmd("--root", s.rootDir, "disable", serviceName)
return err
}
// Start the given service
func (*systemd) Start(serviceName string) error {
- _, err := SystemctlCmd("start", serviceName)
+ _, err := systemctlCmd("start", serviceName)
return err
}
@@ -168,7 +179,7 @@ func (s *systemd) Status(serviceNames ...string) ([]*ServiceStatus, error) {
cmd[0] = "show"
cmd[1] = "--property=" + strings.Join(expected, ",")
copy(cmd[2:], serviceNames)
- bs, err := SystemctlCmd(cmd...)
+ bs, err := systemctlCmd(cmd...)
if err != nil {
return nil, err
}
@@ -242,7 +253,7 @@ func (s *systemd) Status(serviceNames ...string) ([]*ServiceStatus, error) {
// Stop the given service, and wait until it has stopped.
func (s *systemd) Stop(serviceName string, timeout time.Duration) error {
- if _, err := SystemctlCmd("stop", serviceName); err != nil {
+ if _, err := systemctlCmd("stop", serviceName); err != nil {
return err
}
@@ -260,7 +271,7 @@ loop:
case <-giveup.C:
break loop
case <-check.C:
- bs, err := SystemctlCmd("show", "--property=ActiveState", serviceName)
+ bs, err := systemctlCmd("show", "--property=ActiveState", serviceName)
if err != nil {
return err
}
@@ -282,7 +293,7 @@ loop:
// Kill all processes of the unit with the given signal
func (s *systemd) Kill(serviceName, signal string) error {
- _, err := SystemctlCmd("kill", serviceName, "-s", signal)
+ _, err := systemctlCmd("kill", serviceName, "-s", signal)
return err
}
diff --git a/systemd/systemd_test.go b/systemd/systemd_test.go
index a23be6fce..b51efec24 100644
--- a/systemd/systemd_test.go
+++ b/systemd/systemd_test.go
@@ -64,6 +64,8 @@ type SystemdTestSuite struct {
jfollows []bool
rep *testreporter
+
+ restore func()
}
var _ = Suite(&SystemdTestSuite{})
@@ -76,7 +78,7 @@ func (s *SystemdTestSuite) SetUpTest(c *C) {
// force UTC timezone, for reproducible timestamps
os.Setenv("TZ", "")
- SystemctlCmd = s.myRun
+ s.restore = MockSystemctl(s.myRun)
s.i = 0
s.argses = nil
s.errors = nil
@@ -94,8 +96,8 @@ func (s *SystemdTestSuite) SetUpTest(c *C) {
}
func (s *SystemdTestSuite) TearDownTest(c *C) {
- SystemctlCmd = SystemdRun
JournalctlCmd = Jctl
+ s.restore()
}
func (s *SystemdTestSuite) myRun(args ...string) (out []byte, err error) {
diff --git a/wrappers/services_test.go b/wrappers/services_test.go
index d9fa62ba8..6119818ef 100644
--- a/wrappers/services_test.go
+++ b/wrappers/services_test.go
@@ -38,8 +38,9 @@ import (
)
type servicesTestSuite struct {
- tempdir string
- prevctlCmd func(...string) ([]byte, error)
+ tempdir string
+
+ restorer func()
}
var _ = Suite(&servicesTestSuite{})
@@ -48,23 +49,23 @@ func (s *servicesTestSuite) SetUpTest(c *C) {
s.tempdir = c.MkDir()
dirs.SetRootDir(s.tempdir)
- s.prevctlCmd = systemd.SystemctlCmd
- systemd.SystemctlCmd = func(cmd ...string) ([]byte, error) {
+ s.restorer = systemd.MockSystemctl(func(cmd ...string) ([]byte, error) {
return []byte("ActiveState=inactive\n"), nil
- }
+ })
}
func (s *servicesTestSuite) TearDownTest(c *C) {
dirs.SetRootDir("")
- systemd.SystemctlCmd = s.prevctlCmd
+ s.restorer()
}
func (s *servicesTestSuite) TestAddSnapServicesAndRemove(c *C) {
var sysdLog [][]string
- systemd.SystemctlCmd = func(cmd ...string) ([]byte, error) {
+ r := systemd.MockSystemctl(func(cmd ...string) ([]byte, error) {
sysdLog = append(sysdLog, cmd)
return []byte("ActiveState=inactive\n"), nil
- }
+ })
+ defer r()
info := snaptest.MockSnap(c, packageHello, contentsHello, &snap.SideInfo{Revision: snap.R(12)})
svcFile := filepath.Join(s.tempdir, "/etc/systemd/system/snap.hello-snap.svc1.service")
@@ -109,14 +110,15 @@ func (s *servicesTestSuite) TestRemoveSnapPackageFallbackToKill(c *C) {
defer restore()
var sysdLog [][]string
- systemd.SystemctlCmd = func(cmd ...string) ([]byte, error) {
+ r := systemd.MockSystemctl(func(cmd ...string) ([]byte, error) {
// filter out the "systemctl show" that
// StopServices generates
if cmd[0] != "show" {
sysdLog = append(sysdLog, cmd)
}
return []byte("ActiveState=active\n"), nil
- }
+ })
+ defer r()
info := snaptest.MockSnap(c, `name: wat
version: 42
@@ -147,10 +149,11 @@ apps:
func (s *servicesTestSuite) TestStartServices(c *C) {
var sysdLog [][]string
- systemd.SystemctlCmd = func(cmd ...string) ([]byte, error) {
+ r := systemd.MockSystemctl(func(cmd ...string) ([]byte, error) {
sysdLog = append(sysdLog, cmd)
return []byte("ActiveState=inactive\n"), nil
- }
+ })
+ defer r()
info := snaptest.MockSnap(c, packageHello, contentsHello, &snap.SideInfo{Revision: snap.R(12)})
svcFile := filepath.Join(s.tempdir, "/etc/systemd/system/snap.hello-snap.svc1.service")
@@ -168,10 +171,11 @@ func (s *servicesTestSuite) TestAddSnapMultiServicesFailCreateCleanup(c *C) {
svcFiles, _ := filepath.Glob(filepath.Join(dirs.SnapServicesDir, "snap.hello-snap.*.service"))
c.Check(svcFiles, HasLen, 0)
- systemd.SystemctlCmd = func(cmd ...string) ([]byte, error) {
+ r := systemd.MockSystemctl(func(cmd ...string) ([]byte, error) {
sysdLog = append(sysdLog, cmd)
return nil, nil
- }
+ })
+ defer r()
info := snaptest.MockSnap(c, packageHello+`
svc2:
@@ -208,7 +212,7 @@ func (s *servicesTestSuite) TestAddSnapMultiServicesFailEnableCleanup(c *C) {
svcFiles, _ := filepath.Glob(filepath.Join(dirs.SnapServicesDir, "snap.hello-snap.*.service"))
c.Check(svcFiles, HasLen, 0)
- systemd.SystemctlCmd = func(cmd ...string) ([]byte, error) {
+ r := systemd.MockSystemctl(func(cmd ...string) ([]byte, error) {
sysdLog = append(sysdLog, cmd)
sdcmd := cmd[0]
if len(cmd) >= 2 {
@@ -234,7 +238,8 @@ func (s *servicesTestSuite) TestAddSnapMultiServicesFailEnableCleanup(c *C) {
default:
panic("unexpected systemctl command " + sdcmd)
}
- }
+ })
+ defer r()
info := snaptest.MockSnap(c, packageHello+`
svc2:
@@ -267,7 +272,7 @@ func (s *servicesTestSuite) TestAddSnapMultiServicesStartFailOnSystemdReloadClea
c.Check(svcFiles, HasLen, 0)
first := true
- systemd.SystemctlCmd = func(cmd ...string) ([]byte, error) {
+ r := systemd.MockSystemctl(func(cmd ...string) ([]byte, error) {
sysdLog = append(sysdLog, cmd)
if len(cmd) < 2 {
return nil, fmt.Errorf("failed")
@@ -281,7 +286,8 @@ func (s *servicesTestSuite) TestAddSnapMultiServicesStartFailOnSystemdReloadClea
}
return nil, nil
- }
+ })
+ defer r()
info := snaptest.MockSnap(c, packageHello+`
svc2:
@@ -311,7 +317,7 @@ func (s *servicesTestSuite) TestStartSnapMultiServicesFailStartCleanup(c *C) {
svc2Name := "snap.hello-snap.svc2.service"
numStarts := 0
- systemd.SystemctlCmd = func(cmd ...string) ([]byte, error) {
+ r := systemd.MockSystemctl(func(cmd ...string) ([]byte, error) {
sysdLog = append(sysdLog, cmd)
if len(cmd) >= 2 && cmd[len(cmd)-2] == "start" {
numStarts++
@@ -325,7 +331,8 @@ func (s *servicesTestSuite) TestStartSnapMultiServicesFailStartCleanup(c *C) {
}
}
return []byte("ActiveState=inactive\n"), nil
- }
+ })
+ defer r()
info := snaptest.MockSnap(c, packageHello+`
svc2:
|