~fwereade/pyjuju/go-charm-resolve

« back to all changes in this revision

Viewing changes to upstart/upstart_test.go

  • Committer: William Reade
  • Author(s): William Reade
  • Date: 2012-05-31 08:20:45 UTC
  • mfrom: (152.5.15 go)
  • Revision ID: fwereade@gmail.com-20120531082045-wgpg7l4f1j4yueyn
Add upstart package with Service and Conf types

Service enables control of, and visibility into, the state of an upstart
service; Conf embeds a Service and additionally allows for installation
(both directly, as used by a machine agent creating a unit agent service,
and as a set of commands to run later, as used when constructing a
cloud-init file to run a machine and/or provisioning agent on a new
instance).

R=niemeyer, rog, dfc
CC=
https://codereview.appspot.com/6190061

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package upstart_test
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        "io/ioutil"
 
6
        . "launchpad.net/gocheck"
 
7
        "launchpad.net/juju/go/upstart"
 
8
        "os"
 
9
        "path/filepath"
 
10
        "testing"
 
11
)
 
12
 
 
13
func Test(t *testing.T) { TestingT(t) }
 
14
 
 
15
type UpstartSuite struct {
 
16
        origPath string
 
17
        testPath string
 
18
        service  *upstart.Service
 
19
}
 
20
 
 
21
var _ = Suite(&UpstartSuite{})
 
22
 
 
23
func (s *UpstartSuite) SetUpTest(c *C) {
 
24
        s.origPath = os.Getenv("PATH")
 
25
        s.testPath = c.MkDir()
 
26
        os.Setenv("PATH", s.testPath+":"+s.origPath)
 
27
        s.service = &upstart.Service{Name: "some-service", InitDir: c.MkDir()}
 
28
        _, err := os.Create(filepath.Join(s.service.InitDir, "some-service.conf"))
 
29
        c.Assert(err, IsNil)
 
30
}
 
31
 
 
32
func (s *UpstartSuite) TearDownTest(c *C) {
 
33
        os.Setenv("PATH", s.origPath)
 
34
}
 
35
 
 
36
func (s *UpstartSuite) MakeTool(c *C, name, script string) {
 
37
        path := filepath.Join(s.testPath, name)
 
38
        err := ioutil.WriteFile(path, []byte("#!/bin/bash\n"+script), 0755)
 
39
        c.Assert(err, IsNil)
 
40
}
 
41
 
 
42
func (s *UpstartSuite) StoppedStatus(c *C) {
 
43
        s.MakeTool(c, "status", `echo "some-service stop/waiting"`)
 
44
}
 
45
 
 
46
func (s *UpstartSuite) RunningStatus(c *C) {
 
47
        s.MakeTool(c, "status", `echo "some-service start/running, process 123"`)
 
48
}
 
49
 
 
50
func (s *UpstartSuite) TestInitDir(c *C) {
 
51
        svc := upstart.NewService("blah")
 
52
        c.Assert(svc.InitDir, Equals, "/etc/init")
 
53
}
 
54
 
 
55
func (s *UpstartSuite) TestInstalled(c *C) {
 
56
        c.Assert(s.service.Installed(), Equals, true)
 
57
        err := os.Remove(filepath.Join(s.service.InitDir, "some-service.conf"))
 
58
        c.Assert(err, IsNil)
 
59
        c.Assert(s.service.Installed(), Equals, false)
 
60
}
 
61
 
 
62
func (s *UpstartSuite) TestRunning(c *C) {
 
63
        s.MakeTool(c, "status", "exit 1")
 
64
        c.Assert(s.service.Running(), Equals, false)
 
65
        s.MakeTool(c, "status", `echo "GIBBERISH NONSENSE"`)
 
66
        c.Assert(s.service.Running(), Equals, false)
 
67
        s.RunningStatus(c)
 
68
        c.Assert(s.service.Running(), Equals, true)
 
69
}
 
70
 
 
71
func (s *UpstartSuite) TestStart(c *C) {
 
72
        s.RunningStatus(c)
 
73
        s.MakeTool(c, "start", "exit 99")
 
74
        c.Assert(s.service.Start(), IsNil)
 
75
        s.StoppedStatus(c)
 
76
        c.Assert(s.service.Start(), ErrorMatches, "exit status 99")
 
77
        s.MakeTool(c, "start", "exit 0")
 
78
        c.Assert(s.service.Start(), IsNil)
 
79
}
 
80
 
 
81
func (s *UpstartSuite) TestStop(c *C) {
 
82
        s.StoppedStatus(c)
 
83
        s.MakeTool(c, "stop", "exit 99")
 
84
        c.Assert(s.service.Stop(), IsNil)
 
85
        s.RunningStatus(c)
 
86
        c.Assert(s.service.Stop(), ErrorMatches, "exit status 99")
 
87
        s.MakeTool(c, "stop", "exit 0")
 
88
        c.Assert(s.service.Stop(), IsNil)
 
89
}
 
90
 
 
91
func (s *UpstartSuite) TestRemoveMissing(c *C) {
 
92
        err := os.Remove(filepath.Join(s.service.InitDir, "some-service.conf"))
 
93
        c.Assert(err, IsNil)
 
94
        c.Assert(s.service.Remove(), IsNil)
 
95
}
 
96
 
 
97
func (s *UpstartSuite) TestRemoveStopped(c *C) {
 
98
        s.StoppedStatus(c)
 
99
        c.Assert(s.service.Remove(), IsNil)
 
100
        _, err := os.Stat(filepath.Join(s.service.InitDir, "some-service.conf"))
 
101
        c.Assert(os.IsNotExist(err), Equals, true)
 
102
}
 
103
 
 
104
func (s *UpstartSuite) TestRemoveRunning(c *C) {
 
105
        s.RunningStatus(c)
 
106
        s.MakeTool(c, "stop", "exit 99")
 
107
        c.Assert(s.service.Remove(), ErrorMatches, "exit status 99")
 
108
        _, err := os.Stat(filepath.Join(s.service.InitDir, "some-service.conf"))
 
109
        c.Assert(err, IsNil)
 
110
        s.MakeTool(c, "stop", "exit 0")
 
111
        c.Assert(s.service.Remove(), IsNil)
 
112
        _, err = os.Stat(filepath.Join(s.service.InitDir, "some-service.conf"))
 
113
        c.Assert(os.IsNotExist(err), Equals, true)
 
114
}
 
115
 
 
116
func (s *UpstartSuite) TestInstallErrors(c *C) {
 
117
        conf := &upstart.Conf{}
 
118
        check := func(msg string) {
 
119
                c.Assert(conf.Install(), ErrorMatches, msg)
 
120
                _, err := conf.InstallCommands()
 
121
                c.Assert(err, ErrorMatches, msg)
 
122
        }
 
123
        check("missing Name")
 
124
        conf.Name = "some-service"
 
125
        check("missing InitDir")
 
126
        conf.InitDir = c.MkDir()
 
127
        check("missing Desc")
 
128
        conf.Desc = "this is an upstart service"
 
129
        check("missing Cmd")
 
130
}
 
131
 
 
132
const expectStart = `description "this is an upstart service"
 
133
author "Juju Team <juju@lists.ubuntu.com>"
 
134
start on runlevel [2345]
 
135
stop on runlevel [!2345]
 
136
respawn
 
137
`
 
138
 
 
139
func (s *UpstartSuite) dummyConf(c *C) *upstart.Conf {
 
140
        return &upstart.Conf{
 
141
                Service: *s.service,
 
142
                Desc:    "this is an upstart service",
 
143
                Cmd:     "do something",
 
144
        }
 
145
}
 
146
 
 
147
func (s *UpstartSuite) assertInstall(c *C, conf *upstart.Conf, expectEnd string) {
 
148
        expectContent := expectStart + expectEnd
 
149
        expectPath := filepath.Join(conf.InitDir, "some-service.conf")
 
150
 
 
151
        cmds, err := conf.InstallCommands()
 
152
        c.Assert(err, IsNil)
 
153
        c.Assert(cmds, DeepEquals, []string{
 
154
                "cat >> " + expectPath + " << 'EOF'\n" + expectContent + "EOF\n",
 
155
                "start some-service",
 
156
        })
 
157
 
 
158
        s.MakeTool(c, "start", "exit 99")
 
159
        err = conf.Install()
 
160
        c.Assert(err, ErrorMatches, "exit status 99")
 
161
        s.MakeTool(c, "start", "exit 0")
 
162
        err = conf.Install()
 
163
        c.Assert(err, IsNil)
 
164
        content, err := ioutil.ReadFile(expectPath)
 
165
        c.Assert(err, IsNil)
 
166
        c.Assert(string(content), Equals, expectContent)
 
167
}
 
168
 
 
169
func (s *UpstartSuite) TestInstallSimple(c *C) {
 
170
        conf := s.dummyConf(c)
 
171
        s.assertInstall(c, conf, "\nexec do something\n")
 
172
}
 
173
 
 
174
func (s *UpstartSuite) TestInstallOutput(c *C) {
 
175
        conf := s.dummyConf(c)
 
176
        conf.Out = "/some/output/path"
 
177
        s.assertInstall(c, conf, "\nexec do something >> /some/output/path 2>&1\n")
 
178
}
 
179
 
 
180
func (s *UpstartSuite) TestInstallEnv(c *C) {
 
181
        conf := s.dummyConf(c)
 
182
        conf.Env = map[string]string{"FOO": "bar baz", "QUX": "ping pong"}
 
183
        s.assertInstall(c, conf, `env FOO="bar baz"
 
184
env QUX="ping pong"
 
185
 
 
186
exec do something
 
187
`)
 
188
}
 
189
 
 
190
func (s *UpstartSuite) TestInstallAlreadyRunning(c *C) {
 
191
        pathTo := func(name string) string {
 
192
                return filepath.Join(s.testPath, name)
 
193
        }
 
194
        s.MakeTool(c, "status-stopped", `echo "some-service stop/waiting"`)
 
195
        s.MakeTool(c, "status-started", `echo "some-service start/running, process 123"`)
 
196
        s.MakeTool(c, "stop", fmt.Sprintf(
 
197
                "rm %s; ln -s %s %s",
 
198
                pathTo("status"), pathTo("status-stopped"), pathTo("status"),
 
199
        ))
 
200
        s.MakeTool(c, "start", fmt.Sprintf(
 
201
                "rm %s; ln -s %s %s",
 
202
                pathTo("status"), pathTo("status-started"), pathTo("status"),
 
203
        ))
 
204
        err := os.Symlink(pathTo("status-started"), pathTo("status"))
 
205
        c.Assert(err, IsNil)
 
206
 
 
207
        conf := s.dummyConf(c)
 
208
        err = conf.Install()
 
209
        c.Assert(err, IsNil)
 
210
        c.Assert(conf.Running(), Equals, true)
 
211
}