4
. "launchpad.net/gocheck"
11
func Test(t *testing.T) { TestingT(t) }
13
type LXCSuite struct{}
15
var _ = Suite(&LXCSuite{})
17
func (s *LXCSuite) SetUpSuite(c *C) {
18
u, err := user.Current()
21
// Has to be run as root!
22
c.Skip("tests must run as root")
26
func (s *LXCSuite) TestCreateDestroy(c *C) {
27
// Test clean creation and destroying of a container.
28
lc := golxc.New("golxc")
29
c.Assert(lc.IsConstructed(), Equals, false)
30
home := golxc.ContainerHome(lc)
31
_, err := os.Stat(home)
32
c.Assert(err, ErrorMatches, "stat .*: no such file or directory")
33
err = lc.Create("ubuntu")
35
c.Assert(lc.IsConstructed(), Equals, true)
39
_, err = os.Stat(home)
40
c.Assert(err, ErrorMatches, "stat .*: no such file or directory")
42
fi, err := os.Stat(golxc.ContainerHome(lc))
44
c.Assert(fi.IsDir(), Equals, true)
47
func (s *LXCSuite) TestCreateTwice(c *C) {
48
// Test that a container cannot be created twice.
49
lc1 := golxc.New("golxc")
50
c.Assert(lc1.IsConstructed(), Equals, false)
51
err := lc1.Create("ubuntu")
53
c.Assert(lc1.IsConstructed(), Equals, true)
55
c.Assert(lc1.Destroy(), IsNil)
57
lc2 := golxc.New("golxc")
58
err = lc2.Create("ubuntu")
59
c.Assert(err, ErrorMatches, "container .* is already created")
62
func (s *LXCSuite) TestCreateIllegalTemplate(c *C) {
63
// Test that a container creation fails correctly in
64
// case of an illegal template.
65
lc := golxc.New("golxc")
66
c.Assert(lc.IsConstructed(), Equals, false)
67
err := lc.Create("name-of-a-not-existing-template-for-golxc")
68
c.Assert(err, ErrorMatches, `error executing "lxc-create": No config file specified, .*`)
69
c.Assert(lc.IsConstructed(), Equals, false)
72
func (l *LXCSuite) TestDestroyNotCreated(c *C) {
73
// Test that a non-existing container can't be destroyed.
74
lc := golxc.New("golxc")
75
c.Assert(lc.IsConstructed(), Equals, false)
77
c.Assert(err, ErrorMatches, "container .* is not yet created")
80
func contains(lcs []*golxc.Container, lc *golxc.Container) bool {
81
for _, clc := range lcs {
82
if clc.Name() == lc.Name() {
89
func (s *LXCSuite) TestList(c *C) {
90
// Test the listing of created containers.
91
lcs, err := golxc.List()
94
c.Assert(oldLen >= 0, Equals, true)
95
lc := golxc.New("golxc")
96
c.Assert(lc.IsConstructed(), Equals, false)
97
c.Assert(lc.Create("ubuntu"), IsNil)
98
c.Assert(lc.IsConstructed(), Equals, true)
100
c.Assert(lc.Destroy(), IsNil)
102
lcs, _ = golxc.List()
104
c.Assert(newLen == oldLen+1, Equals, true)
105
c.Assert(contains(lcs, lc), Equals, true)
108
func (s *LXCSuite) TestClone(c *C) {
109
// Test the cloning of an existing container.
110
lc1 := golxc.New("golxc")
111
c.Assert(lc1.IsConstructed(), Equals, false)
112
c.Assert(lc1.Create("ubuntu"), IsNil)
113
c.Assert(lc1.IsConstructed(), Equals, true)
115
c.Assert(lc1.Destroy(), IsNil)
117
lcs, _ := golxc.List()
119
lc2, err := lc1.Clone("golxcclone")
121
c.Assert(lc2.IsConstructed(), Equals, true)
123
c.Assert(lc2.Destroy(), IsNil)
125
lcs, _ = golxc.List()
127
c.Assert(newLen == oldLen+1, Equals, true)
128
c.Assert(contains(lcs, lc1), Equals, true)
129
c.Assert(contains(lcs, lc2), Equals, true)
132
func (s *LXCSuite) TestCloneNotCreated(c *C) {
133
// Test the cloning of a non-existing container.
134
lc := golxc.New("golxc")
135
c.Assert(lc.IsConstructed(), Equals, false)
136
_, err := lc.Clone("golxcclone")
137
c.Assert(err, ErrorMatches, "container .* is not yet created")
140
func (s *LXCSuite) TestStartStop(c *C) {
141
// Test starting and stopping a container.
142
lc := golxc.New("golxc")
143
c.Assert(lc.IsConstructed(), Equals, false)
144
c.Assert(lc.Create("ubuntu"), IsNil)
146
c.Assert(lc.Destroy(), IsNil)
148
c.Assert(lc.Start("", ""), IsNil)
149
c.Assert(lc.IsRunning(), Equals, true)
150
c.Assert(lc.Stop(), IsNil)
151
c.Assert(lc.IsRunning(), Equals, false)
154
func (l *LXCSuite) TestStartNotCreated(c *C) {
155
// Test that a non-existing container can't be started.
156
lc := golxc.New("golxc")
157
c.Assert(lc.IsConstructed(), Equals, false)
158
c.Assert(lc.Start("", ""), ErrorMatches, "container .* is not yet created")
161
func (l *LXCSuite) TestStopNotRunning(c *C) {
162
// Test that a not running container can't be stopped.
163
lc := golxc.New("golxc")
164
c.Assert(lc.IsConstructed(), Equals, false)
165
c.Assert(lc.Create("ubuntu"), IsNil)
167
c.Assert(lc.Destroy(), IsNil)
169
c.Assert(lc.Stop(), IsNil)
172
func (s *LXCSuite) TestWait(c *C) {
173
// Test waiting for one of a number of states of a container.
174
// ATTN: Using a not reached state blocks the test until timeout!
175
lc := golxc.New("golxc")
176
c.Assert(lc.IsConstructed(), Equals, false)
177
c.Assert(lc.Wait(), ErrorMatches, "no states specified")
178
c.Assert(lc.Wait(golxc.StateStopped), IsNil)
179
c.Assert(lc.Wait(golxc.StateStopped, golxc.StateRunning), IsNil)
180
c.Assert(lc.Create("ubuntu"), IsNil)
182
c.Assert(lc.Destroy(), IsNil)
185
c.Assert(lc.Start("", ""), IsNil)
187
c.Assert(lc.Wait(golxc.StateRunning), IsNil)
190
func (l *LXCSuite) TestFreezeUnfreeze(c *C) {
191
// Test the freezing and unfreezing of a started container.
192
lc := golxc.New("golxc")
193
c.Assert(lc.IsConstructed(), Equals, false)
194
c.Assert(lc.Create("ubuntu"), IsNil)
196
c.Assert(lc.Destroy(), IsNil)
198
c.Assert(lc.Start("", ""), IsNil)
200
c.Assert(lc.Stop(), IsNil)
202
c.Assert(lc.IsRunning(), Equals, true)
203
c.Assert(lc.Freeze(), IsNil)
204
c.Assert(lc.IsRunning(), Equals, false)
205
c.Assert(lc.Unfreeze(), IsNil)
206
c.Assert(lc.IsRunning(), Equals, true)
209
func (l *LXCSuite) TestFreezeNotStarted(c *C) {
210
// Test that a not running container can't be frozen.
211
lc := golxc.New("golxc")
212
c.Assert(lc.IsConstructed(), Equals, false)
213
c.Assert(lc.Create("ubuntu"), IsNil)
215
c.Assert(lc.Destroy(), IsNil)
217
c.Assert(lc.Freeze(), ErrorMatches, "container .* is not running")
220
func (l *LXCSuite) TestFreezeNotCreated(c *C) {
221
// Test that a non-existing container can't be frozen.
222
lc := golxc.New("golxc")
223
c.Assert(lc.IsConstructed(), Equals, false)
224
c.Assert(lc.Freeze(), ErrorMatches, "container .* is not yet created")
227
func (l *LXCSuite) TestUnfreezeNotCreated(c *C) {
228
// Test that a non-existing container can't be unfrozen.
229
lc := golxc.New("golxc")
230
c.Assert(lc.IsConstructed(), Equals, false)
231
c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not yet created")
234
func (l *LXCSuite) TestUnfreezeNotFrozen(c *C) {
235
// Test that a running container can't be unfrozen.
236
lc := golxc.New("golxc")
237
c.Assert(lc.IsConstructed(), Equals, false)
238
c.Assert(lc.Create("ubuntu"), IsNil)
240
c.Assert(lc.Destroy(), IsNil)
242
c.Assert(lc.Start("", ""), IsNil)
244
c.Assert(lc.Stop(), IsNil)
246
c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen")