Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 package golxc_test | 1 package golxc_test |
2 | 2 |
3 import ( | 3 import ( |
4 . "launchpad.net/gocheck" | 4 . "launchpad.net/gocheck" |
5 "launchpad.net/golxc" | 5 "launchpad.net/golxc" |
6 "os" | 6 "os" |
7 "os/user" | |
7 "testing" | 8 "testing" |
8 ) | 9 ) |
9 | 10 |
10 func Test(t *testing.T) { TestingT(t) } | 11 func Test(t *testing.T) { TestingT(t) } |
11 | 12 |
12 type LXCSuite struct{} | 13 type LXCSuite struct{} |
13 | 14 |
14 var _ = Suite(&LXCSuite{}) | 15 var _ = Suite(&LXCSuite{}) |
15 | 16 |
16 func (s *LXCSuite) TearDownSuite(c *C) { | 17 func (s *LXCSuite) SetUpSuite(c *C) { |
17 » lc := golxc.New("golxc") | 18 » u, err := user.Current() |
18 » lc.Destroy() | 19 » c.Assert(err, IsNil) |
19 » lc = golxc.New("golxcclone") | 20 » if u.Uid != "0" { |
20 » lc.Destroy() | 21 » » // Has to be run as root! |
fwereade
2012/11/21 17:32:53
Shouldn't these be destroyed inline via defer?
TheMue
2012/11/22 12:53:23
Done.
| |
22 » » c.Skip("tests must run as root") | |
23 » } | |
21 } | 24 } |
22 | 25 |
23 func (s *LXCSuite) TestCreateDestroy(c *C) { | 26 func (s *LXCSuite) TestCreateDestroy(c *C) { |
24 » lc := golxc.New("golxc") | 27 » // Test clean creation and destroying of a container. |
25 » c.Assert(lc.IsRunning(), Equals, false) | 28 » lc := golxc.New("golxc") |
29 » c.Assert(lc.IsConstructed(), Equals, false) | |
26 home := golxc.ContainerHome(lc) | 30 home := golxc.ContainerHome(lc) |
27 _, err := os.Stat(home) | 31 _, err := os.Stat(home) |
28 c.Assert(err, ErrorMatches, "stat .*: no such file or directory") | 32 c.Assert(err, ErrorMatches, "stat .*: no such file or directory") |
29 err = lc.Create("ubuntu") | 33 err = lc.Create("ubuntu") |
30 c.Assert(err, IsNil) | 34 c.Assert(err, IsNil) |
35 c.Assert(lc.IsConstructed(), Equals, true) | |
36 defer func() { | |
37 err = lc.Destroy() | |
38 c.Assert(err, IsNil) | |
39 _, err = os.Stat(home) | |
40 c.Assert(err, ErrorMatches, "stat .*: no such file or directory" ) | |
41 }() | |
31 fi, err := os.Stat(golxc.ContainerHome(lc)) | 42 fi, err := os.Stat(golxc.ContainerHome(lc)) |
32 c.Assert(err, IsNil) | 43 c.Assert(err, IsNil) |
33 c.Assert(fi.IsDir(), Equals, true) | 44 c.Assert(fi.IsDir(), Equals, true) |
34 » err = lc.Destroy() | 45 } |
35 » c.Assert(err, IsNil) | 46 |
36 » _, err = os.Stat(home) | 47 func (s *LXCSuite) TestCreateTwice(c *C) { |
37 » c.Assert(err, ErrorMatches, "stat .*: no such file or directory") | 48 » // Test that a container cannot be created twice. |
49 » lc1 := golxc.New("golxc") | |
50 » c.Assert(lc1.IsConstructed(), Equals, false) | |
51 » err := lc1.Create("ubuntu") | |
52 » c.Assert(err, IsNil) | |
53 » c.Assert(lc1.IsConstructed(), Equals, true) | |
54 » defer func() { | |
55 » » c.Assert(lc1.Destroy(), IsNil) | |
56 » }() | |
57 » lc2 := golxc.New("golxc") | |
58 » err = lc2.Create("ubuntu") | |
59 » c.Assert(err, ErrorMatches, "container .* is already created") | |
60 } | |
61 | |
62 func (s *LXCSuite) TestCreateIllegalTemplate(c *C) { | |
63 » // Test that a container creation fails correctly in | |
64 » // case of an illegal template. | |
65 » lc := golxc.New("golxc") | |
66 » c.Assert(lc.IsConstructed(), Equals, false) | |
67 » err := lc.Create("name-of-a-not-existing-template-for-golxc") | |
68 » c.Assert(err, ErrorMatches, `error executing "lxc-create": No config fil e specified, .*`) | |
69 » c.Assert(lc.IsConstructed(), Equals, false) | |
70 } | |
71 | |
72 func (l *LXCSuite) TestDestroyNotCreated(c *C) { | |
73 » // Test that a non-existing container can't be destroyed. | |
74 » lc := golxc.New("golxc") | |
75 » c.Assert(lc.IsConstructed(), Equals, false) | |
76 » err := lc.Destroy() | |
77 » c.Assert(err, ErrorMatches, "container .* is not yet created") | |
38 } | 78 } |
39 | 79 |
40 func contains(lcs []*golxc.Container, lc *golxc.Container) bool { | 80 func contains(lcs []*golxc.Container, lc *golxc.Container) bool { |
41 for _, clc := range lcs { | 81 for _, clc := range lcs { |
42 if clc.Name() == lc.Name() { | 82 if clc.Name() == lc.Name() { |
43 return true | 83 return true |
44 } | 84 } |
45 } | 85 } |
46 return false | 86 return false |
47 } | 87 } |
48 | 88 |
49 func (s *LXCSuite) TestList(c *C) { | 89 func (s *LXCSuite) TestList(c *C) { |
90 // Test the listing of created containers. | |
50 lcs, err := golxc.List() | 91 lcs, err := golxc.List() |
51 oldLen := len(lcs) | 92 oldLen := len(lcs) |
52 c.Assert(err, IsNil) | 93 c.Assert(err, IsNil) |
53 c.Assert(oldLen >= 0, Equals, true) | 94 c.Assert(oldLen >= 0, Equals, true) |
54 lc := golxc.New("golxc") | 95 lc := golxc.New("golxc") |
55 » lc.Create("ubuntu") | 96 » c.Assert(lc.IsConstructed(), Equals, false) |
97 » c.Assert(lc.Create("ubuntu"), IsNil) | |
98 » c.Assert(lc.IsConstructed(), Equals, true) | |
99 » defer func() { | |
100 » » c.Assert(lc.Destroy(), IsNil) | |
101 » }() | |
56 lcs, _ = golxc.List() | 102 lcs, _ = golxc.List() |
57 newLen := len(lcs) | 103 newLen := len(lcs) |
58 c.Assert(newLen == oldLen+1, Equals, true) | 104 c.Assert(newLen == oldLen+1, Equals, true) |
59 c.Assert(contains(lcs, lc), Equals, true) | 105 c.Assert(contains(lcs, lc), Equals, true) |
60 lc.Destroy() | |
61 } | 106 } |
62 | 107 |
63 func (s *LXCSuite) TestClone(c *C) { | 108 func (s *LXCSuite) TestClone(c *C) { |
109 // Test the cloning of an existing container. | |
64 lc1 := golxc.New("golxc") | 110 lc1 := golxc.New("golxc") |
65 » lc1.Create("ubuntu") | 111 » c.Assert(lc1.IsConstructed(), Equals, false) |
112 » c.Assert(lc1.Create("ubuntu"), IsNil) | |
113 » c.Assert(lc1.IsConstructed(), Equals, true) | |
114 » defer func() { | |
115 » » c.Assert(lc1.Destroy(), IsNil) | |
116 » }() | |
66 lcs, _ := golxc.List() | 117 lcs, _ := golxc.List() |
67 oldLen := len(lcs) | 118 oldLen := len(lcs) |
68 lc2, err := lc1.Clone("golxcclone") | 119 lc2, err := lc1.Clone("golxcclone") |
69 c.Assert(err, IsNil) | 120 c.Assert(err, IsNil) |
121 c.Assert(lc2.IsConstructed(), Equals, true) | |
122 defer func() { | |
123 c.Assert(lc2.Destroy(), IsNil) | |
124 }() | |
70 lcs, _ = golxc.List() | 125 lcs, _ = golxc.List() |
71 newLen := len(lcs) | 126 newLen := len(lcs) |
72 c.Assert(newLen == oldLen+1, Equals, true) | 127 c.Assert(newLen == oldLen+1, Equals, true) |
73 c.Assert(contains(lcs, lc1), Equals, true) | 128 c.Assert(contains(lcs, lc1), Equals, true) |
74 c.Assert(contains(lcs, lc2), Equals, true) | 129 c.Assert(contains(lcs, lc2), Equals, true) |
75 » lc1.Destroy() | 130 } |
76 » lc2.Destroy() | 131 |
132 func (s *LXCSuite) TestCloneNotCreated(c *C) { | |
133 » // Test the cloning of a non-existing container. | |
134 » lc := golxc.New("golxc") | |
135 » c.Assert(lc.IsConstructed(), Equals, false) | |
136 » _, err := lc.Clone("golxcclone") | |
137 » c.Assert(err, ErrorMatches, "container .* is not yet created") | |
77 } | 138 } |
78 | 139 |
79 func (s *LXCSuite) TestStartStop(c *C) { | 140 func (s *LXCSuite) TestStartStop(c *C) { |
80 » lc := golxc.New("golxc") | 141 » // Test starting and stopping a container. |
142 » lc := golxc.New("golxc") | |
143 » c.Assert(lc.IsConstructed(), Equals, false) | |
144 » c.Assert(lc.Create("ubuntu"), IsNil) | |
145 » defer func() { | |
146 » » c.Assert(lc.Destroy(), IsNil) | |
147 » }() | |
148 » c.Assert(lc.Start("", ""), IsNil) | |
149 » c.Assert(lc.IsRunning(), Equals, true) | |
150 » c.Assert(lc.Stop(), IsNil) | |
81 c.Assert(lc.IsRunning(), Equals, false) | 151 c.Assert(lc.IsRunning(), Equals, false) |
82 » lc.Create("ubuntu") | 152 } |
83 » err := lc.Start("", "") | 153 |
84 » c.Assert(err, IsNil) | 154 func (l *LXCSuite) TestStartNotCreated(c *C) { |
155 » // Test that a non-existing container can't be started. | |
156 » lc := golxc.New("golxc") | |
157 » c.Assert(lc.IsConstructed(), Equals, false) | |
158 » c.Assert(lc.Start("", ""), ErrorMatches, "container .* is not yet create d") | |
159 } | |
160 | |
161 func (l *LXCSuite) TestStopNotRunning(c *C) { | |
162 » // Test that a not running container can't be stopped. | |
163 » lc := golxc.New("golxc") | |
164 » c.Assert(lc.IsConstructed(), Equals, false) | |
165 » c.Assert(lc.Create("ubuntu"), IsNil) | |
166 » defer func() { | |
167 » » c.Assert(lc.Destroy(), IsNil) | |
168 » }() | |
169 » c.Assert(lc.Stop(), IsNil) | |
170 } | |
171 | |
172 func (s *LXCSuite) TestWait(c *C) { | |
173 » // Test waiting for one of a number of states of a container. | |
174 » // ATTN: Using a not reached state blocks the test until timeout! | |
175 » lc := golxc.New("golxc") | |
176 » c.Assert(lc.IsConstructed(), Equals, false) | |
177 » c.Assert(lc.Wait(), ErrorMatches, "no states specified") | |
178 » c.Assert(lc.Wait(golxc.StateStopped), IsNil) | |
179 » c.Assert(lc.Wait(golxc.StateStopped, golxc.StateRunning), IsNil) | |
180 » c.Assert(lc.Create("ubuntu"), IsNil) | |
181 » defer func() { | |
182 » » c.Assert(lc.Destroy(), IsNil) | |
183 » }() | |
184 » go func() { | |
185 » » c.Assert(lc.Start("", ""), IsNil) | |
186 » }() | |
187 » c.Assert(lc.Wait(golxc.StateRunning), IsNil) | |
188 } | |
189 | |
190 func (l *LXCSuite) TestFreezeUnfreeze(c *C) { | |
191 » // Test the freezing and unfreezing of a started container. | |
192 » lc := golxc.New("golxc") | |
193 » c.Assert(lc.IsConstructed(), Equals, false) | |
194 » c.Assert(lc.Create("ubuntu"), IsNil) | |
195 » defer func() { | |
196 » » c.Assert(lc.Destroy(), IsNil) | |
197 » }() | |
198 » c.Assert(lc.Start("", ""), IsNil) | |
199 » defer func() { | |
200 » » c.Assert(lc.Stop(), IsNil) | |
201 » }() | |
85 c.Assert(lc.IsRunning(), Equals, true) | 202 c.Assert(lc.IsRunning(), Equals, true) |
86 » err = lc.Stop() | 203 » c.Assert(lc.Freeze(), IsNil) |
87 » c.Assert(err, IsNil) | |
88 c.Assert(lc.IsRunning(), Equals, false) | 204 c.Assert(lc.IsRunning(), Equals, false) |
89 » lc.Destroy() | 205 » c.Assert(lc.Unfreeze(), IsNil) |
90 } | |
91 | |
92 func (l *LXCSuite) TestFreezeUnfreeze(c *C) { | |
93 » lc := golxc.New("golxc") | |
94 » lc.Create("ubuntu") | |
95 » lc.Start("", "") | |
96 c.Assert(lc.IsRunning(), Equals, true) | 206 c.Assert(lc.IsRunning(), Equals, true) |
97 » err := lc.Freeze() | 207 } |
98 » c.Assert(err, IsNil) | 208 |
99 » c.Assert(lc.IsRunning(), Equals, false) | 209 func (l *LXCSuite) TestFreezeNotStarted(c *C) { |
100 » err = lc.Unfreeze() | 210 » // Test that a not running container can't be frozen. |
101 » c.Assert(err, IsNil) | 211 » lc := golxc.New("golxc") |
102 » c.Assert(lc.IsRunning(), Equals, true) | 212 » c.Assert(lc.IsConstructed(), Equals, false) |
103 » lc.Stop() | 213 » c.Assert(lc.Create("ubuntu"), IsNil) |
104 » lc.Destroy() | 214 » defer func() { |
105 } | 215 » » c.Assert(lc.Destroy(), IsNil) |
216 » }() | |
217 » c.Assert(lc.Freeze(), ErrorMatches, "container .* is not running") | |
218 } | |
219 | |
220 func (l *LXCSuite) TestFreezeNotCreated(c *C) { | |
221 » // Test that a non-existing container can't be frozen. | |
222 » lc := golxc.New("golxc") | |
223 » c.Assert(lc.IsConstructed(), Equals, false) | |
224 » c.Assert(lc.Freeze(), ErrorMatches, "container .* is not yet created") | |
225 } | |
226 | |
227 func (l *LXCSuite) TestUnfreezeNotCreated(c *C) { | |
228 » // Test that a non-existing container can't be unfrozen. | |
229 » lc := golxc.New("golxc") | |
230 » c.Assert(lc.IsConstructed(), Equals, false) | |
231 » c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not yet created") | |
232 } | |
233 | |
234 func (l *LXCSuite) TestUnfreezeNotFrozen(c *C) { | |
235 » // Test that a running container can't be unfrozen. | |
236 » lc := golxc.New("golxc") | |
237 » c.Assert(lc.IsConstructed(), Equals, false) | |
238 » c.Assert(lc.Create("ubuntu"), IsNil) | |
239 » defer func() { | |
240 » » c.Assert(lc.Destroy(), IsNil) | |
241 » }() | |
242 » c.Assert(lc.Start("", ""), IsNil) | |
243 » defer func() { | |
244 » » c.Assert(lc.Stop(), IsNil) | |
245 » }() | |
246 » c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen") | |
247 } | |
LEFT | RIGHT |