53
53
type ConfigSuite struct{}
55
var _ = Suite(&ConfigSuite{})
55
var _ = gc.Suite(&ConfigSuite{})
57
func (s *ConfigSuite) TestReadConf(c *C) {
57
func (s *ConfigSuite) TestReadConf(c *gc.C) {
58
58
// Test reading the configuration.
59
59
cf := filepath.Join(c.MkDir(), "lxc-test")
60
c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), IsNil)
60
c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), gc.IsNil)
62
62
defer golxc.SetConfPath(golxc.SetConfPath(cf))
64
64
conf, err := golxc.ReadConf()
66
c.Assert(conf, DeepEquals, lxcconf)
65
c.Assert(err, gc.IsNil)
66
c.Assert(conf, gc.DeepEquals, lxcconf)
69
func (s *ConfigSuite) TestReadNotExistingDefaultEnvironment(c *C) {
69
func (s *ConfigSuite) TestReadNotExistingDefaultEnvironment(c *gc.C) {
70
70
// Test reading a not existing environment.
71
71
defer golxc.SetConfPath(golxc.SetConfPath(filepath.Join(c.MkDir(), "foo")))
73
73
_, err := golxc.ReadConf()
74
c.Assert(err, ErrorMatches, "open .*: no such file or directory")
74
c.Assert(err, gc.ErrorMatches, "open .*: no such file or directory")
77
func (s *ConfigSuite) TestNetworkAttributes(c *C) {
77
func (s *ConfigSuite) TestNetworkAttributes(c *gc.C) {
78
78
// Test reading the network attribute form an environment.
79
79
cf := filepath.Join(c.MkDir(), "lxc-test")
80
c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), IsNil)
80
c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), gc.IsNil)
82
82
defer golxc.SetConfPath(golxc.SetConfPath(cf))
84
84
addr, bridge, err := golxc.NetworkAttributes()
86
c.Assert(addr, Equals, "10.0.9.1")
87
c.Assert(bridge, Equals, "lxcbr9")
85
c.Assert(err, gc.IsNil)
86
c.Assert(addr, gc.Equals, "10.0.9.1")
87
c.Assert(bridge, gc.Equals, "lxcbr9")
90
90
type NetworkSuite struct{}
92
var _ = Suite(&NetworkSuite{})
92
var _ = gc.Suite(&NetworkSuite{})
94
func (s *NetworkSuite) SetUpSuite(c *C) {
94
func (s *NetworkSuite) SetUpSuite(c *gc.C) {
95
95
u, err := user.Current()
96
c.Assert(err, gc.IsNil)
98
98
// Has to be run as root!
99
99
c.Skip("tests must run as root")
103
func (s *NetworkSuite) TestStartStopNetwork(c *C) {
103
func (s *NetworkSuite) TestStartStopNetwork(c *gc.C) {
104
104
// Test starting and stoping of the LXC network.
105
105
initialRunning, err := golxc.IsNetworkRunning()
106
c.Assert(err, gc.IsNil)
108
108
if initialRunning {
109
c.Assert(golxc.StartNetwork(), IsNil)
109
c.Assert(golxc.StartNetwork(), gc.IsNil)
112
c.Assert(golxc.StartNetwork(), IsNil)
112
c.Assert(golxc.StartNetwork(), gc.IsNil)
113
113
running, err := golxc.IsNetworkRunning()
115
c.Assert(running, Equals, true)
116
c.Assert(golxc.StopNetwork(), IsNil)
114
c.Assert(err, gc.IsNil)
115
c.Assert(running, gc.Equals, true)
116
c.Assert(golxc.StopNetwork(), gc.IsNil)
117
117
running, err = golxc.IsNetworkRunning()
119
c.Assert(running, Equals, false)
118
c.Assert(err, gc.IsNil)
119
c.Assert(running, gc.Equals, false)
122
func (s *NetworkSuite) TestNotExistingNetworkAttributes(c *C) {
122
func (s *NetworkSuite) TestNotExistingNetworkAttributes(c *gc.C) {
123
123
// Test reading of network attributes from a not existing environment.
124
124
defer golxc.SetConfPath(golxc.SetConfPath(filepath.Join(c.MkDir(), "foo")))
126
126
_, _, err := golxc.NetworkAttributes()
127
c.Assert(err, ErrorMatches, "open .*: no such file or directory")
127
c.Assert(err, gc.ErrorMatches, "open .*: no such file or directory")
130
130
type LXCSuite struct {
131
131
factory golxc.ContainerFactory
134
var _ = Suite(&LXCSuite{golxc.Factory()})
134
var _ = gc.Suite(&LXCSuite{golxc.Factory()})
136
func (s *LXCSuite) SetUpSuite(c *C) {
136
func (s *LXCSuite) SetUpSuite(c *gc.C) {
137
137
u, err := user.Current()
138
c.Assert(err, gc.IsNil)
139
139
if u.Uid != "0" {
140
140
// Has to be run as root!
141
141
c.Skip("tests must run as root")
145
func (s *LXCSuite) createContainer(c *C) golxc.Container {
145
func (s *LXCSuite) createContainer(c *gc.C) golxc.Container {
146
146
container := s.factory.New("golxc")
147
c.Assert(container.IsConstructed(), Equals, false)
148
err := container.Create("", "ubuntu", nil, nil)
150
c.Assert(container.IsConstructed(), Equals, true)
147
c.Assert(container.IsConstructed(), gc.Equals, false)
148
err := container.Create("", "ubuntu", nil, nil, nil)
149
c.Assert(err, gc.IsNil)
150
c.Assert(container.IsConstructed(), gc.Equals, true)
154
func (s *LXCSuite) TestCreateDestroy(c *C) {
154
func (s *LXCSuite) TestCreateDestroy(c *gc.C) {
155
155
// Test clean creation and destroying of a container.
156
156
lc := s.factory.New("golxc")
157
c.Assert(lc.IsConstructed(), Equals, false)
157
c.Assert(lc.IsConstructed(), gc.Equals, false)
158
158
home := golxc.ContainerHome(lc)
159
159
_, err := os.Stat(home)
160
c.Assert(err, ErrorMatches, "stat .*: no such file or directory")
161
err = lc.Create("", "ubuntu", nil, nil)
163
c.Assert(lc.IsConstructed(), Equals, true)
160
c.Assert(err, gc.ErrorMatches, "stat .*: no such file or directory")
161
err = lc.Create("", "ubuntu", nil, nil, nil)
162
c.Assert(err, gc.IsNil)
163
c.Assert(lc.IsConstructed(), gc.Equals, true)
165
165
err = lc.Destroy()
166
c.Assert(err, gc.IsNil)
167
167
_, err = os.Stat(home)
168
c.Assert(err, ErrorMatches, "stat .*: no such file or directory")
168
c.Assert(err, gc.ErrorMatches, "stat .*: no such file or directory")
170
170
fi, err := os.Stat(golxc.ContainerHome(lc))
172
c.Assert(fi.IsDir(), Equals, true)
171
c.Assert(err, gc.IsNil)
172
c.Assert(fi.IsDir(), gc.Equals, true)
175
func (s *LXCSuite) TestCreateTwice(c *C) {
175
func (s *LXCSuite) TestCreateTwice(c *gc.C) {
176
176
// Test that a container cannot be created twice.
177
177
lc1 := s.createContainer(c)
178
c.Assert(lc1.IsConstructed(), Equals, true)
178
c.Assert(lc1.IsConstructed(), gc.Equals, true)
180
c.Assert(lc1.Destroy(), IsNil)
180
c.Assert(lc1.Destroy(), gc.IsNil)
182
182
lc2 := s.factory.New("golxc")
183
err := lc2.Create("", "ubuntu", nil, nil)
184
c.Assert(err, ErrorMatches, "container .* is already created")
183
err := lc2.Create("", "ubuntu", nil, nil, nil)
184
c.Assert(err, gc.ErrorMatches, "container .* is already created")
187
func (s *LXCSuite) TestCreateIllegalTemplate(c *C) {
187
func (s *LXCSuite) TestCreateIllegalTemplate(c *gc.C) {
188
188
// Test that a container creation fails correctly in
189
189
// case of an illegal template.
190
190
lc := s.factory.New("golxc")
191
c.Assert(lc.IsConstructed(), Equals, false)
192
err := lc.Create("", "name-of-a-not-existing-template-for-golxc", nil, nil)
193
c.Assert(err, ErrorMatches, `error executing "lxc-create": .*bad template.*`)
194
c.Assert(lc.IsConstructed(), Equals, false)
191
c.Assert(lc.IsConstructed(), gc.Equals, false)
192
err := lc.Create("", "name-of-a-not-existing-template-for-golxc", nil, nil, nil)
193
c.Assert(err, gc.ErrorMatches, `error executing "lxc-create": .*bad template.*`)
194
c.Assert(lc.IsConstructed(), gc.Equals, false)
197
func (s *LXCSuite) TestDestroyNotCreated(c *C) {
197
func (s *LXCSuite) TestDestroyNotCreated(c *gc.C) {
198
198
// Test that a non-existing container can't be destroyed.
199
199
lc := s.factory.New("golxc")
200
c.Assert(lc.IsConstructed(), Equals, false)
200
c.Assert(lc.IsConstructed(), gc.Equals, false)
201
201
err := lc.Destroy()
202
c.Assert(err, ErrorMatches, "container .* is not yet created")
202
c.Assert(err, gc.ErrorMatches, "container .* is not yet created")
205
205
func contains(lcs []golxc.Container, lc golxc.Container) bool {
214
func (s *LXCSuite) TestList(c *C) {
214
func (s *LXCSuite) TestList(c *gc.C) {
215
215
// Test the listing of created containers.
216
216
lcs, err := s.factory.List()
217
217
oldLen := len(lcs)
219
c.Assert(oldLen >= 0, Equals, true)
218
c.Assert(err, gc.IsNil)
219
c.Assert(oldLen >= 0, gc.Equals, true)
220
220
lc := s.createContainer(c)
222
c.Assert(lc.Destroy(), IsNil)
222
c.Assert(lc.Destroy(), gc.IsNil)
224
224
lcs, _ = s.factory.List()
225
225
newLen := len(lcs)
226
c.Assert(newLen == oldLen+1, Equals, true)
227
c.Assert(contains(lcs, lc), Equals, true)
226
c.Assert(newLen == oldLen+1, gc.Equals, true)
227
c.Assert(contains(lcs, lc), gc.Equals, true)
230
func (s *LXCSuite) TestClone(c *C) {
230
func (s *LXCSuite) TestClone(c *gc.C) {
231
231
// Test the cloning of an existing container.
232
232
lc1 := s.createContainer(c)
234
c.Assert(lc1.Destroy(), IsNil)
234
c.Assert(lc1.Destroy(), gc.IsNil)
236
236
lcs, _ := s.factory.List()
237
237
oldLen := len(lcs)
238
238
lc2, err := lc1.Clone("golxcclone", nil, nil)
240
c.Assert(lc2.IsConstructed(), Equals, true)
239
c.Assert(err, gc.IsNil)
240
c.Assert(lc2.IsConstructed(), gc.Equals, true)
242
c.Assert(lc2.Destroy(), IsNil)
242
c.Assert(lc2.Destroy(), gc.IsNil)
244
244
lcs, _ = s.factory.List()
245
245
newLen := len(lcs)
246
c.Assert(newLen == oldLen+1, Equals, true)
247
c.Assert(contains(lcs, lc1), Equals, true)
248
c.Assert(contains(lcs, lc2), Equals, true)
246
c.Assert(newLen == oldLen+1, gc.Equals, true)
247
c.Assert(contains(lcs, lc1), gc.Equals, true)
248
c.Assert(contains(lcs, lc2), gc.Equals, true)
251
func (s *LXCSuite) TestCloneNotCreated(c *C) {
251
func (s *LXCSuite) TestCloneNotCreated(c *gc.C) {
252
252
// Test the cloning of a non-existing container.
253
253
lc := s.factory.New("golxc")
254
c.Assert(lc.IsConstructed(), Equals, false)
254
c.Assert(lc.IsConstructed(), gc.Equals, false)
255
255
_, err := lc.Clone("golxcclone", nil, nil)
256
c.Assert(err, ErrorMatches, "container .* is not yet created")
256
c.Assert(err, gc.ErrorMatches, "container .* is not yet created")
259
func (s *LXCSuite) TestStartStop(c *C) {
259
func (s *LXCSuite) TestStartStop(c *gc.C) {
260
260
// Test starting and stopping a container.
261
261
lc := s.createContainer(c)
263
c.Assert(lc.Destroy(), IsNil)
263
c.Assert(lc.Destroy(), gc.IsNil)
265
c.Assert(lc.Start("", ""), IsNil)
266
c.Assert(lc.IsRunning(), Equals, true)
267
c.Assert(lc.Stop(), IsNil)
268
c.Assert(lc.IsRunning(), Equals, false)
265
c.Assert(lc.Start("", ""), gc.IsNil)
266
c.Assert(lc.IsRunning(), gc.Equals, true)
267
c.Assert(lc.Stop(), gc.IsNil)
268
c.Assert(lc.IsRunning(), gc.Equals, false)
271
func (s *LXCSuite) TestStartNotCreated(c *C) {
271
func (s *LXCSuite) TestStartNotCreated(c *gc.C) {
272
272
// Test that a non-existing container can't be started.
273
273
lc := s.factory.New("golxc")
274
c.Assert(lc.IsConstructed(), Equals, false)
275
c.Assert(lc.Start("", ""), ErrorMatches, "container .* is not yet created")
274
c.Assert(lc.IsConstructed(), gc.Equals, false)
275
c.Assert(lc.Start("", ""), gc.ErrorMatches, "container .* is not yet created")
278
func (s *LXCSuite) TestStopNotRunning(c *C) {
278
func (s *LXCSuite) TestStopNotRunning(c *gc.C) {
279
279
// Test that a not running container can't be stopped.
280
280
lc := s.createContainer(c)
282
c.Assert(lc.Destroy(), IsNil)
282
c.Assert(lc.Destroy(), gc.IsNil)
284
c.Assert(lc.Stop(), IsNil)
284
c.Assert(lc.Stop(), gc.IsNil)
287
func (s *LXCSuite) TestWait(c *C) {
287
func (s *LXCSuite) TestWait(c *gc.C) {
288
288
// Test waiting for one of a number of states of a container.
289
289
// ATTN: Using a not reached state blocks the test until timeout!
290
290
lc := s.factory.New("golxc")
291
c.Assert(lc.IsConstructed(), Equals, false)
292
c.Assert(lc.Wait(), ErrorMatches, "no states specified")
293
c.Assert(lc.Wait(golxc.StateStopped), IsNil)
294
c.Assert(lc.Wait(golxc.StateStopped, golxc.StateRunning), IsNil)
295
c.Assert(lc.Create("", "ubuntu", nil, nil), IsNil)
291
c.Assert(lc.IsConstructed(), gc.Equals, false)
292
c.Assert(lc.Wait(), gc.ErrorMatches, "no states specified")
293
c.Assert(lc.Wait(golxc.StateStopped), gc.IsNil)
294
c.Assert(lc.Wait(golxc.StateStopped, golxc.StateRunning), gc.IsNil)
295
c.Assert(lc.Create("", "ubuntu", nil, nil, nil), gc.IsNil)
297
c.Assert(lc.Destroy(), IsNil)
297
c.Assert(lc.Destroy(), gc.IsNil)
300
c.Assert(lc.Start("", ""), IsNil)
300
c.Assert(lc.Start("", ""), gc.IsNil)
302
c.Assert(lc.Wait(golxc.StateRunning), IsNil)
302
c.Assert(lc.Wait(golxc.StateRunning), gc.IsNil)
305
func (s *LXCSuite) TestFreezeUnfreeze(c *C) {
305
func (s *LXCSuite) TestFreezeUnfreeze(c *gc.C) {
306
306
// Test the freezing and unfreezing of a started container.
307
307
lc := s.createContainer(c)
309
c.Assert(lc.Destroy(), IsNil)
309
c.Assert(lc.Destroy(), gc.IsNil)
311
c.Assert(lc.Start("", ""), IsNil)
311
c.Assert(lc.Start("", ""), gc.IsNil)
313
c.Assert(lc.Stop(), IsNil)
313
c.Assert(lc.Stop(), gc.IsNil)
315
c.Assert(lc.IsRunning(), Equals, true)
316
c.Assert(lc.Freeze(), IsNil)
317
c.Assert(lc.IsRunning(), Equals, false)
318
c.Assert(lc.Unfreeze(), IsNil)
319
c.Assert(lc.IsRunning(), Equals, true)
315
c.Assert(lc.IsRunning(), gc.Equals, true)
316
c.Assert(lc.Freeze(), gc.IsNil)
317
c.Assert(lc.IsRunning(), gc.Equals, false)
318
c.Assert(lc.Unfreeze(), gc.IsNil)
319
c.Assert(lc.IsRunning(), gc.Equals, true)
322
func (s *LXCSuite) TestFreezeNotStarted(c *C) {
322
func (s *LXCSuite) TestFreezeNotStarted(c *gc.C) {
323
323
// Test that a not running container can't be frozen.
324
324
lc := s.createContainer(c)
326
c.Assert(lc.Destroy(), IsNil)
326
c.Assert(lc.Destroy(), gc.IsNil)
328
c.Assert(lc.Freeze(), ErrorMatches, "container .* is not running")
328
c.Assert(lc.Freeze(), gc.ErrorMatches, "container .* is not running")
331
func (s *LXCSuite) TestFreezeNotCreated(c *C) {
331
func (s *LXCSuite) TestFreezeNotCreated(c *gc.C) {
332
332
// Test that a non-existing container can't be frozen.
333
333
lc := s.factory.New("golxc")
334
c.Assert(lc.IsConstructed(), Equals, false)
335
c.Assert(lc.Freeze(), ErrorMatches, "container .* is not yet created")
334
c.Assert(lc.IsConstructed(), gc.Equals, false)
335
c.Assert(lc.Freeze(), gc.ErrorMatches, "container .* is not yet created")
338
func (s *LXCSuite) TestUnfreezeNotCreated(c *C) {
338
func (s *LXCSuite) TestUnfreezeNotCreated(c *gc.C) {
339
339
// Test that a non-existing container can't be unfrozen.
340
340
lc := s.factory.New("golxc")
341
c.Assert(lc.IsConstructed(), Equals, false)
342
c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not yet created")
341
c.Assert(lc.IsConstructed(), gc.Equals, false)
342
c.Assert(lc.Unfreeze(), gc.ErrorMatches, "container .* is not yet created")
345
func (s *LXCSuite) TestUnfreezeNotFrozen(c *C) {
345
func (s *LXCSuite) TestUnfreezeNotFrozen(c *gc.C) {
346
346
// Test that a running container can't be unfrozen.
347
347
lc := s.createContainer(c)
349
c.Assert(lc.Destroy(), IsNil)
349
c.Assert(lc.Destroy(), gc.IsNil)
351
c.Assert(lc.Start("", ""), IsNil)
351
c.Assert(lc.Start("", ""), gc.IsNil)
353
c.Assert(lc.Stop(), IsNil)
353
c.Assert(lc.Stop(), gc.IsNil)
355
c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen")
355
c.Assert(lc.Unfreeze(), gc.ErrorMatches, "container .* is not frozen")
358
358
type commandArgs struct {