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