Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 package golxc_test | 1 package golxc_test |
2 | 2 |
3 import ( | 3 import ( |
4 "io/ioutil" | 4 "io/ioutil" |
5 . "launchpad.net/gocheck" | 5 . "launchpad.net/gocheck" |
6 "launchpad.net/golxc" | |
7 "os" | 6 "os" |
8 "os/user" | 7 "os/user" |
9 "path/filepath" | 8 "path/filepath" |
10 "strings" | |
11 "testing" | 9 "testing" |
10 | |
11 "launchpad.net/golxc" | |
12 ) | 12 ) |
13 | 13 |
14 func Test(t *testing.T) { TestingT(t) } | 14 func Test(t *testing.T) { TestingT(t) } |
15 | 15 |
16 type ConfigurationSuite struct{} | 16 var lxcfile = `# MIRROR to be used by ubuntu template at container creation: |
17 | |
18 var _ = Suite(&ConfigurationSuite{}) | |
19 | |
20 var cfgs = `# Leading commment. | |
21 VALUE1 = 12345 | |
22 VALUE2="foo" | |
23 VALUE3 = "foo # bar" | |
24 VALUE4 = "foo = bar" | |
25 VALUE5 = "bar" # Trailing comment. | |
26 VALUE6 = bar # foo | |
27 | |
28 # Comment inbetween. | |
29 | |
30 VALUE7 = | |
31 VALUE8 = "" | |
32 VALUE9 =foo ` | |
33 | |
34 var icfgs = `some_invalid_kind_of_data_that_is_no_configuration` | |
35 | |
36 func (s *ConfigurationSuite) TestParseConfiguration(c *C) { | |
37 » // Test the parsing of a valid configuration. | |
38 » cr := strings.NewReader(cfgs) | |
39 » cfg, err := golxc.ParseConfiguration(cr) | |
40 » c.Assert(err, IsNil) | |
41 » c.Assert(len(cfg), Equals, 9) | |
42 » c.Assert(cfg["VALUE1"], Equals, "12345") | |
43 » c.Assert(cfg["VALUE2"], Equals, "foo") | |
44 » c.Assert(cfg["VALUE3"], Equals, "foo # bar") | |
45 » c.Assert(cfg["VALUE4"], Equals, "foo = bar") | |
46 » c.Assert(cfg["VALUE5"], Equals, "bar") | |
47 » c.Assert(cfg["VALUE6"], Equals, "bar") | |
48 » c.Assert(cfg["VALUE7"], Equals, "") | |
49 » c.Assert(cfg["VALUE8"], Equals, "") | |
50 » c.Assert(cfg["VALUE9"], Equals, "foo") | |
51 } | |
52 | |
53 func (s *ConfigurationSuite) TestReadInvalidConfiguration(c *C) { | |
54 » // Test the reading of an invalid configuration. | |
55 » cr := strings.NewReader(icfgs) | |
56 » _, err := golxc.ParseConfiguration(cr) | |
57 » c.Assert(err, ErrorMatches, "invalid config line: .*") | |
58 } | |
59 | |
60 var env = `# MIRROR to be used by ubuntu template at container creation: | |
61 # Leaving it undefined is fine | 17 # Leaving it undefined is fine |
62 #MIRROR="http://archive.ubuntu.com/ubuntu" | 18 #MIRROR="http://archive.ubuntu.com/ubuntu" |
63 # or· | 19 # or· |
64 #MIRROR="http://<host-ip-addr>:3142/archive.ubuntu.com/ubuntu" | 20 #MIRROR="http://<host-ip-addr>:3142/archive.ubuntu.com/ubuntu" |
65 | 21 |
66 # LXC_AUTO - whether or not to start containers symlinked under | 22 # LXC_AUTO - whether or not to start containers symlinked under |
67 # /etc/lxc/auto | 23 # /etc/lxc/auto |
68 LXC_AUTO="true" | 24 LXC_AUTO="true" |
69 | 25 |
70 # Leave USE_LXC_BRIDGE as "true" if you want to use lxcbr0 for your | 26 # Leave USE_LXC_BRIDGE as "true" if you want to use lxcbr0 for your |
71 # containers. Set to "false" if you'll use virbr0 or another existing | 27 # containers. Set to "false" if you'll use virbr0 or another existing |
72 # bridge, or mavlan to your host's NIC. | 28 # bridge, or mavlan to your host's NIC. |
73 USE_LXC_BRIDGE="true" | 29 USE_LXC_BRIDGE="true" |
74 | 30 |
75 # LXC_BRIDGE and LXC_ADDR are changed against original for | 31 # LXC_BRIDGE and LXC_ADDR are changed against original for |
76 # testing purposes. | 32 # testing purposes. |
33 # LXC_BRIDGE="lxcbr1" | |
77 LXC_BRIDGE="lxcbr9" | 34 LXC_BRIDGE="lxcbr9" |
35 # LXC_ADDR="10.0.1.1" | |
78 LXC_ADDR="10.0.9.1" | 36 LXC_ADDR="10.0.9.1" |
79 LXC_NETMASK="255.255.255.0" | 37 LXC_NETMASK="255.255.255.0" |
80 LXC_NETWORK="10.0.9.0/24" | 38 LXC_NETWORK="10.0.9.0/24" |
81 LXC_DHCP_RANGE="10.0.9.2,10.0.9.254" | 39 LXC_DHCP_RANGE="10.0.9.2,10.0.9.254" |
82 LXC_DHCP_MAX="253" | 40 LXC_DHCP_MAX="253" |
41 # And for testing LXC_BRIDGE="lxcbr99" and LXC_ADDR="10.0.99.1". | |
83 | 42 |
84 LXC_SHUTDOWN_TIMEOUT=120` | 43 LXC_SHUTDOWN_TIMEOUT=120` |
85 | 44 |
86 func (s *ConfigurationSuite) TestReadDefaultEnvironment(c *C) { | 45 var lxcconf = map[string]string{ |
87 » // Test reading the environment. | 46 » "address": "10.0.9.1", |
88 » f, err := ioutil.TempFile("/tmp", "lxc-test") | 47 » "bridge": "lxcbr9", |
89 » c.Assert(err, IsNil) | 48 } |
90 » defer func() { | 49 |
91 » » c.Assert(os.Remove(f.Name()), IsNil) | 50 type ConfigSuite struct{} |
92 » }() | 51 |
93 » _, err = f.WriteString(env) | 52 var _ = Suite(&ConfigSuite{}) |
94 » c.Assert(err, IsNil) | 53 |
95 » c.Assert(f.Close(), IsNil) | 54 func (s *ConfigSuite) TestReadConf(c *C) { |
96 | 55 » // Test reading the configuration. |
97 » orig := golxc.SetLXCDefaultFile(f.Name()) | 56 » cf := filepath.Join(c.MkDir(), "lxc-test") |
98 » defer golxc.SetLXCDefaultFile(orig) | 57 » c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), IsNil) |
99 | 58 |
100 » env, err := golxc.ReadDefaultEnvironment() | 59 » defer golxc.SetConfPath(golxc.SetConfPath(cf)) |
101 » c.Assert(err, IsNil) | 60 |
102 » c.Assert(env["LXC_BRIDGE"], Equals, "lxcbr9") | 61 » conf, err := golxc.ReadConf() |
103 » c.Assert(env["LXC_ADDR"], Equals, "10.0.9.1") | 62 » c.Assert(err, IsNil) |
104 » c.Assert(env["MIRROR"], Equals, "") | 63 » c.Assert(conf, DeepEquals, lxcconf) |
105 } | 64 } |
106 | 65 |
107 func (s *ConfigurationSuite) TestReadNotExistingDefaultEnvironment(c *C) { | 66 func (s *ConfigSuite) TestReadNotExistingDefaultEnvironment(c *C) { |
108 // Test reading a not existing environment. | 67 // Test reading a not existing environment. |
109 » orig := golxc.SetLXCDefaultFile("/tmp/not-existing-exc-test") | 68 » defer golxc.SetConfPath(golxc.SetConfPath(filepath.Join(c.MkDir(), "foo" ))) |
110 » defer golxc.SetLXCDefaultFile(orig) | 69 |
111 | 70 » _, err := golxc.ReadConf() |
112 » _, err := golxc.ReadDefaultEnvironment() | |
113 c.Assert(err, ErrorMatches, "open .*: no such file or directory") | 71 c.Assert(err, ErrorMatches, "open .*: no such file or directory") |
72 } | |
73 | |
74 func (s *ConfigSuite) TestNetworkAttributes(c *C) { | |
75 // Test reading the network attribute form an environment. | |
76 cf := filepath.Join(c.MkDir(), "lxc-test") | |
77 c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), IsNil) | |
78 | |
79 defer golxc.SetConfPath(golxc.SetConfPath(cf)) | |
80 | |
81 addr, bridge, err := golxc.NetworkAttributes() | |
82 c.Assert(err, IsNil) | |
83 c.Assert(addr, Equals, "10.0.9.1") | |
84 c.Assert(bridge, Equals, "lxcbr9") | |
114 } | 85 } |
115 | 86 |
116 type NetworkSuite struct{} | 87 type NetworkSuite struct{} |
117 | 88 |
118 var _ = Suite(&NetworkSuite{}) | 89 var _ = Suite(&NetworkSuite{}) |
119 | 90 |
120 func (s *NetworkSuite) SetUpSuite(c *C) { | 91 func (s *NetworkSuite) SetUpSuite(c *C) { |
121 u, err := user.Current() | 92 u, err := user.Current() |
122 c.Assert(err, IsNil) | 93 c.Assert(err, IsNil) |
123 if u.Uid != "0" { | 94 if u.Uid != "0" { |
(...skipping 14 matching lines...) Expand all Loading... | |
138 c.Assert(golxc.StartNetwork(), IsNil) | 109 c.Assert(golxc.StartNetwork(), IsNil) |
139 running, err := golxc.IsNetworkRunning() | 110 running, err := golxc.IsNetworkRunning() |
140 c.Assert(err, IsNil) | 111 c.Assert(err, IsNil) |
141 c.Assert(running, Equals, true) | 112 c.Assert(running, Equals, true) |
142 c.Assert(golxc.StopNetwork(), IsNil) | 113 c.Assert(golxc.StopNetwork(), IsNil) |
143 running, err = golxc.IsNetworkRunning() | 114 running, err = golxc.IsNetworkRunning() |
144 c.Assert(err, IsNil) | 115 c.Assert(err, IsNil) |
145 c.Assert(running, Equals, false) | 116 c.Assert(running, Equals, false) |
146 } | 117 } |
147 | 118 |
148 func (s *ConfigurationSuite) TestNetworkAttributes(c *C) { | |
149 // Test reading the network attribute form an environment. | |
150 cf := filepath.Join(c.MkDir(), "lxc-test") | |
151 c.Assert(ioutil.WriteFile(cf, []byte(env), 0555), IsNil) | |
152 | |
153 orig := golxc.SetLXCDefaultFile(cf) | |
154 defer golxc.SetLXCDefaultFile(orig) | |
155 | |
156 addr, bridge, err := golxc.NetworkAttributes() | |
157 c.Assert(err, IsNil) | |
158 c.Assert(addr, Equals, "10.0.9.1") | |
159 c.Assert(bridge, Equals, "lxcbr9") | |
160 } | |
161 | |
162 func (s *NetworkSuite) TestNotExistingNetworkAttributes(c *C) { | 119 func (s *NetworkSuite) TestNotExistingNetworkAttributes(c *C) { |
163 // Test reading of network attributes from a not existing environment. | 120 // Test reading of network attributes from a not existing environment. |
164 » orig := golxc.SetLXCDefaultFile("/any/path/to/non-existing/lxc-file") | 121 » defer golxc.SetConfPath(golxc.SetConfPath(filepath.Join(c.MkDir(), "foo" ))) |
fwereade
2013/02/04 14:51:30
I don't think you can guarantee this path doesn't
TheMue
2013/02/05 14:21:32
Done.
| |
165 » defer golxc.SetLXCDefaultFile(orig) | |
166 | 122 |
167 _, _, err := golxc.NetworkAttributes() | 123 _, _, err := golxc.NetworkAttributes() |
168 c.Assert(err, ErrorMatches, "open .*: no such file or directory") | 124 c.Assert(err, ErrorMatches, "open .*: no such file or directory") |
169 } | 125 } |
170 | 126 |
171 type LXCSuite struct{} | 127 type LXCSuite struct{} |
172 | 128 |
173 var _ = Suite(&LXCSuite{}) | 129 var _ = Suite(&LXCSuite{}) |
174 | 130 |
175 func (s *LXCSuite) SetUpSuite(c *C) { | 131 func (s *LXCSuite) SetUpSuite(c *C) { |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
396 c.Assert(lc.Create("ubuntu"), IsNil) | 352 c.Assert(lc.Create("ubuntu"), IsNil) |
397 defer func() { | 353 defer func() { |
398 c.Assert(lc.Destroy(), IsNil) | 354 c.Assert(lc.Destroy(), IsNil) |
399 }() | 355 }() |
400 c.Assert(lc.Start("", ""), IsNil) | 356 c.Assert(lc.Start("", ""), IsNil) |
401 defer func() { | 357 defer func() { |
402 c.Assert(lc.Stop(), IsNil) | 358 c.Assert(lc.Stop(), IsNil) |
403 }() | 359 }() |
404 c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen") | 360 c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen") |
405 } | 361 } |
LEFT | RIGHT |