Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(884)

Delta Between Two Patch Sets: golxc_test.go

Issue 6853075: golxc: added configuration reading (Closed)
Left Patch Set: golxc: added configuration reading Created 11 years, 4 months ago
Right Patch Set: golxc: added configuration reading Created 11 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « export_test.go ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 package golxc_test 1 package golxc_test
2 2
3 import ( 3 import (
4 "io/ioutil"
4 . "launchpad.net/gocheck" 5 . "launchpad.net/gocheck"
5 "launchpad.net/golxc"
6 "os" 6 "os"
7 "os/user" 7 "os/user"
8 » "strings" 8 » "path/filepath"
9 "testing" 9 "testing"
10
11 "launchpad.net/golxc"
10 ) 12 )
11 13
12 func Test(t *testing.T) { TestingT(t) } 14 func Test(t *testing.T) { TestingT(t) }
13 15
14 type ConfigurationSuite struct{} 16 type ConfigSuite struct{}
15 17
16 var _ = Suite(&ConfigurationSuite{}) 18 var _ = Suite(&ConfigSuite{})
17 19
18 var cfgs = `# Leading commment. 20 var lxcfile = `# MIRROR to be used by ubuntu template at container creation:
19 VALUE1 = 12345 21 # Leaving it undefined is fine
20 VALUE2="foo" 22 #MIRROR="http://archive.ubuntu.com/ubuntu"
21 VALUE3 = "foo # bar" 23 # or·
22 VALUE4 = "foo = bar" 24 #MIRROR="http://<host-ip-addr>:3142/archive.ubuntu.com/ubuntu"
23 VALUE5 = "bar" # Trailing comment. 25
24 VALUE6 = bar # foo 26 # LXC_AUTO - whether or not to start containers symlinked under
25 27 # /etc/lxc/auto
26 # Comment inbetween. 28 LXC_AUTO="true"
27 29
28 VALUE7 = 30 # Leave USE_LXC_BRIDGE as "true" if you want to use lxcbr0 for your
29 VALUE8 = ""` 31 # containers. Set to "false" if you'll use virbr0 or another existing
30 32 # bridge, or mavlan to your host's NIC.
31 var icfgs = `some_invalid_kind_of_data_that_is_no_configuration` 33 USE_LXC_BRIDGE="true"
32 34
33 func (s *ConfigurationSuite) TestReadConfiguration(c *C) { 35 # LXC_BRIDGE and LXC_ADDR are changed against original for
34 » // Test the reading of a valid configuration. 36 # testing purposes.
35 » cr := strings.NewReader(cfgs) 37 # LXC_BRIDGE="lxcbr1"
36 » cfg, err := golxc.ReadConfiguration(cr) 38 LXC_BRIDGE="lxcbr9"
37 » c.Assert(err, IsNil) 39 # LXC_ADDR="10.0.1.1"
38 » c.Assert(len(cfg), Equals, 8) 40 LXC_ADDR="10.0.9.1"
39 » c.Assert(cfg["VALUE1"], Equals, "12345") 41 LXC_NETMASK="255.255.255.0"
40 » c.Assert(cfg["VALUE2"], Equals, "foo") 42 LXC_NETWORK="10.0.9.0/24"
41 » c.Assert(cfg["VALUE3"], Equals, "foo # bar") 43 LXC_DHCP_RANGE="10.0.9.2,10.0.9.254"
42 » c.Assert(cfg["VALUE4"], Equals, "foo = bar") 44 LXC_DHCP_MAX="253"
43 » c.Assert(cfg["VALUE5"], Equals, "bar") 45 # And for testing LXC_BRIDGE="lxcbr99" and LXC_ADDR="10.0.99.1".
44 » c.Assert(cfg["VALUE6"], Equals, "bar") 46
45 » c.Assert(cfg["VALUE7"], Equals, "") 47 LXC_SHUTDOWN_TIMEOUT=120`
46 » c.Assert(cfg["VALUE8"], Equals, "") 48
47 } 49 var lxcconf = map[string]string{
48 50 » "address": "10.0.9.1",
49 func (s *ConfigurationSuite) TestReadInvalidConfiguration(c *C) { 51 » "bridge": "lxcbr9",
50 » // Test the reading of an invalid configuration. 52 }
51 » cr := strings.NewReader(icfgs) 53
52 » _, err := golxc.ReadConfiguration(cr) 54 func (s *ConfigSuite) TestReadConf(c *C) {
53 » c.Assert(err, ErrorMatches, "invalid config line: .*") 55 » // Test reading the configuration.
54 } 56 » cf := filepath.Join(c.MkDir(), "lxc-test")
55 57 » c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), IsNil)
56 func (s *ConfigurationSuite) TestReadDefaultEnvironment(c *C) { 58
57 » // Test reading /etc/default/lxc. 59 » defer golxc.SetConfPath(golxc.SetConfPath(cf))
58 » env, err := golxc.ReadDefaultEnvironment() 60
59 » c.Assert(err, IsNil) 61 » conf, err := golxc.ReadConf()
60 » // LXC_ADDR and LXC_BRIDGE have to contain values. 62 » c.Assert(err, IsNil)
61 » c.Assert(env["LXC_ADDR"], Not(Equals), "") 63 » c.Assert(conf, DeepEquals, lxcconf)
62 » c.Assert(env["LXC_BRIDGE"], Not(Equals), "") 64 }
65
66 func (s *ConfigSuite) TestReadNotExistingDefaultEnviron(c *C) {
67 » // Test reading a not existing environment.
68 » defer golxc.SetConfPath(golxc.SetConfPath(filepath.Join(c.MkDir(), "foo" )))
69
70 » _, err := golxc.ReadConf()
71 » c.Assert(err, ErrorMatches, "open .*: no such file or directory")
63 } 72 }
64 73
65 type LXCSuite struct{} 74 type LXCSuite struct{}
66 75
67 var _ = Suite(&LXCSuite{}) 76 var _ = Suite(&LXCSuite{})
68 77
69 func (s *LXCSuite) SetUpSuite(c *C) { 78 func (s *LXCSuite) SetUpSuite(c *C) {
70 u, err := user.Current() 79 u, err := user.Current()
71 c.Assert(err, IsNil) 80 c.Assert(err, IsNil)
72 if u.Uid != "0" { 81 if u.Uid != "0" {
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 c.Assert(lc.Create("ubuntu"), IsNil) 299 c.Assert(lc.Create("ubuntu"), IsNil)
291 defer func() { 300 defer func() {
292 c.Assert(lc.Destroy(), IsNil) 301 c.Assert(lc.Destroy(), IsNil)
293 }() 302 }()
294 c.Assert(lc.Start("", ""), IsNil) 303 c.Assert(lc.Start("", ""), IsNil)
295 defer func() { 304 defer func() {
296 c.Assert(lc.Stop(), IsNil) 305 c.Assert(lc.Stop(), IsNil)
297 }() 306 }()
298 c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen") 307 c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen")
299 } 308 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b