~frankban/golxc/fix-logging-suite

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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see COPYING and COPYING.LESSER file for details.

package golxc_test

import (
	"io/ioutil"
	"os"
	"os/user"
	"path/filepath"

	"github.com/juju/testing"
	jc "github.com/juju/testing/checkers"
	. "launchpad.net/gocheck"

	"launchpad.net/golxc"
)

var lxcfile = `# MIRROR to be used by ubuntu template at container creation:
# Leaving it undefined is fine
#MIRROR="http://archive.ubuntu.com/ubuntu"
# or
#MIRROR="http://<host-ip-addr>:3142/archive.ubuntu.com/ubuntu"

# LXC_AUTO - whether or not to start containers symlinked under
# /etc/lxc/auto
LXC_AUTO="true"

# Leave USE_LXC_BRIDGE as "true" if you want to use lxcbr0 for your
# containers.  Set to "false" if you'll use virbr0 or another existing
# bridge, or mavlan to your host's NIC.
USE_LXC_BRIDGE="true"

# LXC_BRIDGE and LXC_ADDR are changed against original for
# testing purposes.
# LXC_BRIDGE="lxcbr1"
LXC_BRIDGE="lxcbr9"
# LXC_ADDR="10.0.1.1"
LXC_ADDR="10.0.9.1"
LXC_NETMASK="255.255.255.0"
LXC_NETWORK="10.0.9.0/24"
LXC_DHCP_RANGE="10.0.9.2,10.0.9.254"
LXC_DHCP_MAX="253"
# And for testing LXC_BRIDGE="lxcbr99" and LXC_ADDR="10.0.99.1".

LXC_SHUTDOWN_TIMEOUT=120`

var lxcconf = map[string]string{
	"address": "10.0.9.1",
	"bridge":  "lxcbr9",
}

type ConfigSuite struct{}

var _ = Suite(&ConfigSuite{})

func (s *ConfigSuite) TestReadConf(c *C) {
	// Test reading the configuration.
	cf := filepath.Join(c.MkDir(), "lxc-test")
	c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), IsNil)

	defer golxc.SetConfPath(golxc.SetConfPath(cf))

	conf, err := golxc.ReadConf()
	c.Assert(err, IsNil)
	c.Assert(conf, DeepEquals, lxcconf)
}

func (s *ConfigSuite) TestReadNotExistingDefaultEnvironment(c *C) {
	// Test reading a not existing environment.
	defer golxc.SetConfPath(golxc.SetConfPath(filepath.Join(c.MkDir(), "foo")))

	_, err := golxc.ReadConf()
	c.Assert(err, ErrorMatches, "open .*: no such file or directory")
}

func (s *ConfigSuite) TestNetworkAttributes(c *C) {
	// Test reading the network attribute form an environment.
	cf := filepath.Join(c.MkDir(), "lxc-test")
	c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), IsNil)

	defer golxc.SetConfPath(golxc.SetConfPath(cf))

	addr, bridge, err := golxc.NetworkAttributes()
	c.Assert(err, IsNil)
	c.Assert(addr, Equals, "10.0.9.1")
	c.Assert(bridge, Equals, "lxcbr9")
}

type NetworkSuite struct{}

var _ = Suite(&NetworkSuite{})

func (s *NetworkSuite) SetUpSuite(c *C) {
	u, err := user.Current()
	c.Assert(err, IsNil)
	if u.Uid != "0" {
		// Has to be run as root!
		c.Skip("tests must run as root")
	}
}

func (s *NetworkSuite) TestStartStopNetwork(c *C) {
	// Test starting and stoping of the LXC network.
	initialRunning, err := golxc.IsNetworkRunning()
	c.Assert(err, IsNil)
	defer func() {
		if initialRunning {
			c.Assert(golxc.StartNetwork(), IsNil)
		}
	}()
	c.Assert(golxc.StartNetwork(), IsNil)
	running, err := golxc.IsNetworkRunning()
	c.Assert(err, IsNil)
	c.Assert(running, Equals, true)
	c.Assert(golxc.StopNetwork(), IsNil)
	running, err = golxc.IsNetworkRunning()
	c.Assert(err, IsNil)
	c.Assert(running, Equals, false)
}

func (s *NetworkSuite) TestNotExistingNetworkAttributes(c *C) {
	// Test reading of network attributes from a not existing environment.
	defer golxc.SetConfPath(golxc.SetConfPath(filepath.Join(c.MkDir(), "foo")))

	_, _, err := golxc.NetworkAttributes()
	c.Assert(err, ErrorMatches, "open .*: no such file or directory")
}

type LXCSuite struct {
	factory golxc.ContainerFactory
}

var _ = Suite(&LXCSuite{golxc.Factory()})

func (s *LXCSuite) SetUpSuite(c *C) {
	u, err := user.Current()
	c.Assert(err, IsNil)
	if u.Uid != "0" {
		// Has to be run as root!
		c.Skip("tests must run as root")
	}
}

func (s *LXCSuite) createContainer(c *C) golxc.Container {
	container := s.factory.New("golxc")
	c.Assert(container.IsConstructed(), Equals, false)
	err := container.Create("", "ubuntu", nil, nil)
	c.Assert(err, IsNil)
	c.Assert(container.IsConstructed(), Equals, true)
	return container
}

func (s *LXCSuite) TestCreateDestroy(c *C) {
	// Test clean creation and destroying of a container.
	lc := s.factory.New("golxc")
	c.Assert(lc.IsConstructed(), Equals, false)
	home := golxc.ContainerHome(lc)
	_, err := os.Stat(home)
	c.Assert(err, ErrorMatches, "stat .*: no such file or directory")
	err = lc.Create("", "ubuntu", nil, nil)
	c.Assert(err, IsNil)
	c.Assert(lc.IsConstructed(), Equals, true)
	defer func() {
		err = lc.Destroy()
		c.Assert(err, IsNil)
		_, err = os.Stat(home)
		c.Assert(err, ErrorMatches, "stat .*: no such file or directory")
	}()
	fi, err := os.Stat(golxc.ContainerHome(lc))
	c.Assert(err, IsNil)
	c.Assert(fi.IsDir(), Equals, true)
}

func (s *LXCSuite) TestCreateTwice(c *C) {
	// Test that a container cannot be created twice.
	lc1 := s.createContainer(c)
	c.Assert(lc1.IsConstructed(), Equals, true)
	defer func() {
		c.Assert(lc1.Destroy(), IsNil)
	}()
	lc2 := s.factory.New("golxc")
	err := lc2.Create("", "ubuntu", nil, nil)
	c.Assert(err, ErrorMatches, "container .* is already created")
}

func (s *LXCSuite) TestCreateIllegalTemplate(c *C) {
	// Test that a container creation fails correctly in
	// case of an illegal template.
	lc := s.factory.New("golxc")
	c.Assert(lc.IsConstructed(), Equals, false)
	err := lc.Create("", "name-of-a-not-existing-template-for-golxc", nil, nil)
	c.Assert(err, ErrorMatches, `error executing "lxc-create": .*bad template.*`)
	c.Assert(lc.IsConstructed(), Equals, false)
}

func (s *LXCSuite) TestDestroyNotCreated(c *C) {
	// Test that a non-existing container can't be destroyed.
	lc := s.factory.New("golxc")
	c.Assert(lc.IsConstructed(), Equals, false)
	err := lc.Destroy()
	c.Assert(err, ErrorMatches, "container .* is not yet created")
}

func contains(lcs []golxc.Container, lc golxc.Container) bool {
	for _, clc := range lcs {
		if clc.Name() == lc.Name() {
			return true
		}
	}
	return false
}

func (s *LXCSuite) TestList(c *C) {
	// Test the listing of created containers.
	lcs, err := s.factory.List()
	oldLen := len(lcs)
	c.Assert(err, IsNil)
	c.Assert(oldLen >= 0, Equals, true)
	lc := s.createContainer(c)
	defer func() {
		c.Assert(lc.Destroy(), IsNil)
	}()
	lcs, _ = s.factory.List()
	newLen := len(lcs)
	c.Assert(newLen == oldLen+1, Equals, true)
	c.Assert(contains(lcs, lc), Equals, true)
}

func (s *LXCSuite) TestClone(c *C) {
	// Test the cloning of an existing container.
	lc1 := s.createContainer(c)
	defer func() {
		c.Assert(lc1.Destroy(), IsNil)
	}()
	lcs, _ := s.factory.List()
	oldLen := len(lcs)
	lc2, err := lc1.Clone("golxcclone", nil, nil)
	c.Assert(err, IsNil)
	c.Assert(lc2.IsConstructed(), Equals, true)
	defer func() {
		c.Assert(lc2.Destroy(), IsNil)
	}()
	lcs, _ = s.factory.List()
	newLen := len(lcs)
	c.Assert(newLen == oldLen+1, Equals, true)
	c.Assert(contains(lcs, lc1), Equals, true)
	c.Assert(contains(lcs, lc2), Equals, true)
}

func (s *LXCSuite) TestCloneNotCreated(c *C) {
	// Test the cloning of a non-existing container.
	lc := s.factory.New("golxc")
	c.Assert(lc.IsConstructed(), Equals, false)
	_, err := lc.Clone("golxcclone", nil, nil)
	c.Assert(err, ErrorMatches, "container .* is not yet created")
}

func (s *LXCSuite) TestStartStop(c *C) {
	// Test starting and stopping a container.
	lc := s.createContainer(c)
	defer func() {
		c.Assert(lc.Destroy(), IsNil)
	}()
	c.Assert(lc.Start("", ""), IsNil)
	c.Assert(lc.IsRunning(), Equals, true)
	c.Assert(lc.Stop(), IsNil)
	c.Assert(lc.IsRunning(), Equals, false)
}

func (s *LXCSuite) TestStartNotCreated(c *C) {
	// Test that a non-existing container can't be started.
	lc := s.factory.New("golxc")
	c.Assert(lc.IsConstructed(), Equals, false)
	c.Assert(lc.Start("", ""), ErrorMatches, "container .* is not yet created")
}

func (s *LXCSuite) TestStopNotRunning(c *C) {
	// Test that a not running container can't be stopped.
	lc := s.createContainer(c)
	defer func() {
		c.Assert(lc.Destroy(), IsNil)
	}()
	c.Assert(lc.Stop(), IsNil)
}

func (s *LXCSuite) TestWait(c *C) {
	// Test waiting for one of a number of states of a container.
	// ATTN: Using a not reached state blocks the test until timeout!
	lc := s.factory.New("golxc")
	c.Assert(lc.IsConstructed(), Equals, false)
	c.Assert(lc.Wait(), ErrorMatches, "no states specified")
	c.Assert(lc.Wait(golxc.StateStopped), IsNil)
	c.Assert(lc.Wait(golxc.StateStopped, golxc.StateRunning), IsNil)
	c.Assert(lc.Create("", "ubuntu", nil, nil), IsNil)
	defer func() {
		c.Assert(lc.Destroy(), IsNil)
	}()
	go func() {
		c.Assert(lc.Start("", ""), IsNil)
	}()
	c.Assert(lc.Wait(golxc.StateRunning), IsNil)
}

func (s *LXCSuite) TestFreezeUnfreeze(c *C) {
	// Test the freezing and unfreezing of a started container.
	lc := s.createContainer(c)
	defer func() {
		c.Assert(lc.Destroy(), IsNil)
	}()
	c.Assert(lc.Start("", ""), IsNil)
	defer func() {
		c.Assert(lc.Stop(), IsNil)
	}()
	c.Assert(lc.IsRunning(), Equals, true)
	c.Assert(lc.Freeze(), IsNil)
	c.Assert(lc.IsRunning(), Equals, false)
	c.Assert(lc.Unfreeze(), IsNil)
	c.Assert(lc.IsRunning(), Equals, true)
}

func (s *LXCSuite) TestFreezeNotStarted(c *C) {
	// Test that a not running container can't be frozen.
	lc := s.createContainer(c)
	defer func() {
		c.Assert(lc.Destroy(), IsNil)
	}()
	c.Assert(lc.Freeze(), ErrorMatches, "container .* is not running")
}

func (s *LXCSuite) TestFreezeNotCreated(c *C) {
	// Test that a non-existing container can't be frozen.
	lc := s.factory.New("golxc")
	c.Assert(lc.IsConstructed(), Equals, false)
	c.Assert(lc.Freeze(), ErrorMatches, "container .* is not yet created")
}

func (s *LXCSuite) TestUnfreezeNotCreated(c *C) {
	// Test that a non-existing container can't be unfrozen.
	lc := s.factory.New("golxc")
	c.Assert(lc.IsConstructed(), Equals, false)
	c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not yet created")
}

func (s *LXCSuite) TestUnfreezeNotFrozen(c *C) {
	// Test that a running container can't be unfrozen.
	lc := s.createContainer(c)
	defer func() {
		c.Assert(lc.Destroy(), IsNil)
	}()
	c.Assert(lc.Start("", ""), IsNil)
	defer func() {
		c.Assert(lc.Stop(), IsNil)
	}()
	c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen")
}

type commandArgs struct {
	testing.IsolationSuite
	originalPath string
}

var _ = Suite(&commandArgs{})

func (s *commandArgs) SetUpSuite(c *C) {
	// lxc-create requires the PATH to be set.
	s.originalPath = os.Getenv("PATH")
	s.IsolationSuite.SetUpSuite(c)
}

func (s *commandArgs) TestCreateArgs(c *C) {
	s.PatchValue(&golxc.ContainerDir, c.MkDir())
	s.PatchEnvironment("PATH", s.originalPath)
	testing.PatchExecutableAsEchoArgs(c, s, "lxc-create")

	factory := golxc.Factory()
	container := factory.New("test")
	err := container.Create(
		"config-file", "template",
		[]string{"extra-1", "extra-2"},
		[]string{"template-1", "template-2"},
	)
	c.Assert(err, IsNil)
	testing.AssertEchoArgs(
		c, "lxc-create",
		"-n", "test",
		"-t", "template",
		"-f", "config-file",
		"extra-1", "extra-2",
		"--", "template-1", "template-2")
}

func (s *commandArgs) TestCloneArgs(c *C) {
	dir := c.MkDir()
	s.PatchValue(&golxc.ContainerDir, dir)
	s.PatchEnvironment("PATH", s.originalPath)
	// Patch lxc-info too as clone checks to see if it is running.
	testing.PatchExecutableAsEchoArgs(c, s, "lxc-info")
	testing.PatchExecutableAsEchoArgs(c, s, "lxc-clone")

	factory := golxc.Factory()
	container := factory.New("test")
	// Make the rootfs for the "test" container so it thinks it is created.
	rootfs := filepath.Join(dir, "test", "rootfs")
	err := os.MkdirAll(rootfs, 0755)
	c.Assert(err, IsNil)
	c.Assert(rootfs, jc.IsDirectory)
	c.Assert(container.IsConstructed(), jc.IsTrue)
	clone, err := container.Clone(
		"name",
		[]string{"extra-1", "extra-2"},
		[]string{"template-1", "template-2"},
	)
	c.Assert(err, IsNil)
	testing.AssertEchoArgs(
		c, "lxc-clone",
		"-o", "test",
		"-n", "name",
		"extra-1", "extra-2",
		"--", "template-1", "template-2")
	c.Assert(clone.Name(), Equals, "name")
}