87
91
func (s *UpstartSuite) TestInstalled(c *gc.C) {
88
c.Assert(s.service.Installed(), jc.IsFalse)
92
installed, err := s.service.Installed()
93
c.Assert(err, jc.ErrorIsNil)
94
c.Check(installed, jc.IsFalse)
90
c.Assert(s.service.Installed(), jc.IsTrue)
97
installed, err = s.service.Installed()
98
c.Assert(err, jc.ErrorIsNil)
99
c.Check(installed, jc.IsTrue)
93
102
func (s *UpstartSuite) TestExists(c *gc.C) {
94
103
// Setup creates the file, but it is empty.
95
c.Assert(s.service.Exists(), jc.IsFalse)
104
exists, err := s.service.Exists()
105
c.Assert(err, jc.ErrorIsNil)
106
c.Check(exists, jc.IsFalse)
97
c.Assert(s.service.Exists(), jc.IsTrue)
109
exists, err = s.service.Exists()
110
c.Assert(err, jc.ErrorIsNil)
111
c.Check(exists, jc.IsTrue)
100
114
func (s *UpstartSuite) TestExistsNonEmpty(c *gc.C) {
102
s.service.Conf.Cmd = "something else"
103
c.Assert(s.service.Exists(), jc.IsFalse)
116
s.service.Service.Conf.ExecStart = "/path/to/other-command"
118
exists, err := s.service.Exists()
119
c.Assert(err, jc.ErrorIsNil)
120
c.Check(exists, jc.IsFalse)
106
123
func (s *UpstartSuite) TestRunning(c *gc.C) {
107
124
s.MakeTool(c, "status", "exit 1")
108
c.Assert(s.service.Running(), jc.IsFalse)
125
running, err := s.service.Running()
126
c.Assert(err, jc.ErrorIsNil)
127
c.Check(running, jc.IsFalse)
109
129
s.MakeTool(c, "status", `echo "GIBBERISH NONSENSE"`)
110
c.Assert(s.service.Running(), jc.IsFalse)
112
c.Assert(s.service.Running(), jc.IsTrue)
130
running, err = s.service.Running()
131
c.Assert(err, jc.ErrorIsNil)
132
c.Check(running, jc.IsFalse)
134
s.RunningStatusNoProcessID(c)
135
running, err = s.service.Running()
136
c.Assert(err, jc.ErrorIsNil)
137
c.Check(running, jc.IsTrue)
139
s.RunningStatusWithProcessID(c)
140
running, err = s.service.Running()
141
c.Assert(err, jc.ErrorIsNil)
142
c.Check(running, jc.IsTrue)
115
145
func (s *UpstartSuite) TestStart(c *gc.C) {
146
s.RunningStatusWithProcessID(c)
117
147
s.MakeTool(c, "start", "exit 99")
118
c.Assert(s.service.Start(), gc.IsNil)
148
c.Assert(s.service.Start(), jc.ErrorIsNil)
119
149
s.StoppedStatus(c)
120
150
c.Assert(s.service.Start(), gc.ErrorMatches, ".*exit status 99.*")
121
151
s.MakeTool(c, "start", "exit 0")
122
c.Assert(s.service.Start(), gc.IsNil)
152
c.Assert(s.service.Start(), jc.ErrorIsNil)
125
155
func (s *UpstartSuite) TestStop(c *gc.C) {
126
156
s.StoppedStatus(c)
127
157
s.MakeTool(c, "stop", "exit 99")
128
c.Assert(s.service.Stop(), gc.IsNil)
158
c.Assert(s.service.Stop(), jc.ErrorIsNil)
159
s.RunningStatusWithProcessID(c)
130
160
c.Assert(s.service.Stop(), gc.ErrorMatches, ".*exit status 99.*")
131
161
s.MakeTool(c, "stop", "exit 0")
132
c.Assert(s.service.Stop(), gc.IsNil)
162
c.Assert(s.service.Stop(), jc.ErrorIsNil)
135
165
func (s *UpstartSuite) TestRemoveMissing(c *gc.C) {
136
c.Assert(s.service.StopAndRemove(), gc.IsNil)
166
err := s.service.Remove()
168
c.Check(err, jc.ErrorIsNil)
139
171
func (s *UpstartSuite) TestRemoveStopped(c *gc.C) {
141
173
s.StoppedStatus(c)
142
c.Assert(s.service.StopAndRemove(), gc.IsNil)
143
_, err := os.Stat(filepath.Join(s.service.Conf.InitDir, "some-service.conf"))
144
c.Assert(err, jc.Satisfies, os.IsNotExist)
175
err := s.service.Remove()
176
c.Assert(err, jc.ErrorIsNil)
178
filename := filepath.Join(upstart.InitDir, "some-service.conf")
179
_, err = os.Stat(filename)
180
c.Check(err, jc.Satisfies, os.IsNotExist)
147
func (s *UpstartSuite) TestRemoveRunning(c *gc.C) {
183
func (s *UpstartSuite) TestStopRunning(c *gc.C) {
185
s.RunningStatusWithProcessID(c)
150
186
s.MakeTool(c, "stop", "exit 99")
151
c.Assert(s.service.StopAndRemove(), gc.ErrorMatches, ".*exit status 99.*")
152
_, err := os.Stat(filepath.Join(s.service.Conf.InitDir, "some-service.conf"))
187
filename := filepath.Join(upstart.InitDir, "some-service.conf")
188
err := s.service.Stop()
189
c.Assert(err, gc.ErrorMatches, ".*exit status 99.*")
191
_, err = os.Stat(filename)
153
192
c.Assert(err, jc.ErrorIsNil)
154
194
s.MakeTool(c, "stop", "exit 0")
155
c.Assert(s.service.StopAndRemove(), gc.IsNil)
156
_, err = os.Stat(filepath.Join(s.service.Conf.InitDir, "some-service.conf"))
157
c.Assert(err, jc.Satisfies, os.IsNotExist)
160
func (s *UpstartSuite) TestStopAndRemove(c *gc.C) {
163
s.MakeTool(c, "stop", "exit 99")
165
// StopAndRemove will fail, as it calls stop.
166
c.Assert(s.service.StopAndRemove(), gc.ErrorMatches, ".*exit status 99.*")
167
_, err := os.Stat(filepath.Join(s.service.Conf.InitDir, "some-service.conf"))
195
err = s.service.Stop()
168
196
c.Assert(err, jc.ErrorIsNil)
170
// Plain old Remove will succeed.
171
c.Assert(s.service.Remove(), gc.IsNil)
172
_, err = os.Stat(filepath.Join(s.service.Conf.InitDir, "some-service.conf"))
173
c.Assert(err, jc.Satisfies, os.IsNotExist)
176
199
func (s *UpstartSuite) TestInstallErrors(c *gc.C) {
202
223
func (s *UpstartSuite) dummyConf(c *gc.C) common.Conf {
203
224
return common.Conf{
204
Desc: "this is an upstart service",
225
Desc: "this is an upstart service",
226
ExecStart: "/path/to/some-command x y z",
210
230
func (s *UpstartSuite) assertInstall(c *gc.C, conf common.Conf, expectEnd string) {
211
231
expectContent := expectStart + expectEnd
212
expectPath := filepath.Join(conf.InitDir, "some-service.conf")
232
expectPath := filepath.Join(upstart.InitDir, "some-service.conf")
214
s.service.Conf = conf
234
s.service.Service.Conf = conf
216
236
cmds, err := s.service.InstallCommands()
217
237
c.Assert(err, jc.ErrorIsNil)
218
238
c.Assert(cmds, gc.DeepEquals, []string{
219
"cat >> " + expectPath + " << 'EOF'\n" + expectContent + "EOF\n",
239
"cat > " + expectPath + " << 'EOF'\n" + expectContent + "EOF\n",
241
cmds, err = s.service.StartCommands()
242
c.Assert(err, jc.ErrorIsNil)
243
c.Assert(cmds, gc.DeepEquals, []string{
220
244
"start some-service",
247
s.MakeTool(c, "status", `echo "some-service stop/waiting"`)
223
248
s.MakeTool(c, "start", "exit 99")
224
249
err = svc.Install()
250
c.Assert(err, jc.ErrorIsNil)
225
252
c.Assert(err, gc.ErrorMatches, ".*exit status 99.*")
226
254
s.MakeTool(c, "start", "exit 0")
227
255
err = svc.Install()
228
256
c.Assert(err, jc.ErrorIsNil)
258
c.Assert(err, jc.ErrorIsNil)
229
260
content, err := ioutil.ReadFile(expectPath)
230
261
c.Assert(err, jc.ErrorIsNil)
231
262
c.Assert(string(content), gc.Equals, expectContent)
234
265
func (s *UpstartSuite) TestInstallSimple(c *gc.C) {
235
266
conf := s.dummyConf(c)
236
s.assertInstall(c, conf, "\n\nscript\n\n\n exec do something\nend script\n")
267
s.assertInstall(c, conf, `
272
exec /path/to/some-command x y z
239
277
func (s *UpstartSuite) TestInstallExtraScript(c *gc.C) {
240
278
conf := s.dummyConf(c)
241
279
conf.ExtraScript = "extra lines of script"
242
s.assertInstall(c, conf, "\n\nscript\nextra lines of script\n\n exec do something\nend script\n")
280
s.assertInstall(c, conf, `
283
extra lines of script
285
exec /path/to/some-command x y z
245
func (s *UpstartSuite) TestInstallOutput(c *gc.C) {
290
func (s *UpstartSuite) TestInstallLogfile(c *gc.C) {
246
291
conf := s.dummyConf(c)
247
conf.Out = "/some/output/path"
248
s.assertInstall(c, conf, "\n\nscript\n\n\n # Ensure log files are properly protected\n touch /some/output/path\n chown syslog:syslog /some/output/path\n chmod 0600 /some/output/path\n\n exec do something >> /some/output/path 2>&1\nend script\n")
292
conf.Logfile = "/some/output/path"
293
s.assertInstall(c, conf, `
298
# Ensure log files are properly protected
299
touch /some/output/path
300
chown syslog:syslog /some/output/path
301
chmod 0600 /some/output/path
303
exec /path/to/some-command x y z >> /some/output/path 2>&1
251
308
func (s *UpstartSuite) TestInstallEnv(c *gc.C) {
295
355
err := symlink.New(pathTo("status-started"), pathTo("status"))
296
356
c.Assert(err, jc.ErrorIsNil)
298
conf := s.dummyConf(c)
299
s.service.UpdateConfig(conf)
300
err = s.service.Install()
301
c.Assert(err, jc.ErrorIsNil)
302
c.Assert(s.service, jc.Satisfies, (*upstart.Service).Running)
358
svc := upstart.NewService("some-service", s.dummyConf(c))
360
c.Assert(err, jc.ErrorIsNil)
361
installed, err := svc.Running()
362
c.Assert(err, jc.ErrorIsNil)
363
c.Check(installed, jc.IsTrue)
366
type IsRunningSuite struct {
367
coretesting.BaseSuite
370
var _ = gc.Suite(&IsRunningSuite{})
372
const modeExecutable = 0500
373
const modeNotExecutable = 0400
375
// createInitctl creates a dummy initctl which returns the given
376
// exitcode and patches the upstart package to use it.
377
func (s *IsRunningSuite) createInitctl(c *gc.C, stderr string, exitcode int, mode os.FileMode) {
378
path := filepath.Join(c.MkDir(), "initctl")
382
body = ">&2 echo " + utils.ShQuote(stderr)
384
script := fmt.Sprintf(`
388
`[1:], body, exitcode)
390
err := ioutil.WriteFile(path, []byte(script), mode)
391
c.Assert(err, jc.ErrorIsNil)
392
s.PatchValue(upstart.InitctlPath, path)
395
func (s *IsRunningSuite) TestUpstartInstalled(c *gc.C) {
396
s.createInitctl(c, "", 0, modeExecutable)
398
isUpstart, err := upstart.IsRunning()
399
c.Assert(isUpstart, jc.IsTrue)
400
c.Assert(err, jc.ErrorIsNil)
403
func (s *IsRunningSuite) TestUpstartNotInstalled(c *gc.C) {
404
s.PatchValue(upstart.InitctlPath, "/foo/bar/not-exist")
406
isUpstart, err := upstart.IsRunning()
407
c.Assert(isUpstart, jc.IsFalse)
408
c.Assert(err, jc.ErrorIsNil)
411
func (s *IsRunningSuite) TestUpstartInstalledButBroken(c *gc.C) {
412
const stderr = "<something broke>"
414
s.createInitctl(c, stderr, errorCode, modeExecutable)
416
isUpstart, err := upstart.IsRunning()
417
c.Assert(isUpstart, jc.IsFalse)
418
c.Assert(err, gc.ErrorMatches, fmt.Sprintf(".*exit status %d", errorCode))
421
func (s *IsRunningSuite) TestUpstartInstalledButNotRunning(c *gc.C) {
422
const stderr = `Name "com.ubuntu.Upstart" does not exist`
424
s.createInitctl(c, stderr, errorCode, modeExecutable)
426
isUpstart, err := upstart.IsRunning()
427
c.Assert(isUpstart, jc.IsFalse)
428
c.Assert(err, gc.ErrorMatches, fmt.Sprintf(".*exit status %d", errorCode))
431
func (s *IsRunningSuite) TestInitctlCantBeRun(c *gc.C) {
432
s.createInitctl(c, "", 0, modeNotExecutable)
434
isUpstart, err := upstart.IsRunning()
435
c.Assert(isUpstart, jc.IsFalse)
436
c.Assert(err, gc.ErrorMatches, ".+: permission denied")