Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 package golxc_test | |
2 | |
3 import ( | |
4 . "launchpad.net/gocheck" | |
5 "launchpad.net/golxc" | |
6 "os" | |
7 "testing" | |
8 ) | |
9 | |
10 func Test(t *testing.T) { TestingT(t) } | |
11 | |
12 type LXCSuite struct{} | |
rog
2012/11/22 15:38:31
i wonder if it might be worth Skipping the suite (
TheMue
2012/11/22 16:55:02
Done.
| |
13 | |
14 var _ = Suite(&LXCSuite{}) | |
15 | |
16 func (s *LXCSuite) TestCreateDestroy(c *C) { | |
17 // Test clean creation and destroying of a container. | |
18 lc := golxc.New("golxc") | |
19 c.Assert(lc.IsConstructed(), Equals, false) | |
20 home := golxc.ContainerHome(lc) | |
21 _, err := os.Stat(home) | |
22 c.Assert(err, ErrorMatches, "stat .*: no such file or directory") | |
23 err = lc.Create("ubuntu") | |
24 c.Assert(err, IsNil) | |
25 c.Assert(lc.IsConstructed(), Equals, true) | |
26 defer func() { | |
27 err = lc.Destroy() | |
28 c.Assert(err, IsNil) | |
29 _, err = os.Stat(home) | |
30 c.Assert(err, ErrorMatches, "stat .*: no such file or directory" ) | |
31 }() | |
32 fi, err := os.Stat(golxc.ContainerHome(lc)) | |
33 c.Assert(err, IsNil) | |
34 c.Assert(fi.IsDir(), Equals, true) | |
35 } | |
36 | |
37 func (s *LXCSuite) TestCreateTwice(c *C) { | |
38 // Test that a container cannot be created twice. | |
39 lc1 := golxc.New("golxc") | |
40 c.Assert(lc1.IsConstructed(), Equals, false) | |
41 err := lc1.Create("ubuntu") | |
42 c.Assert(err, IsNil) | |
43 c.Assert(lc1.IsConstructed(), Equals, true) | |
44 defer func() { | |
45 c.Assert(lc1.Destroy(), IsNil) | |
46 }() | |
47 lc2 := golxc.New("golxc") | |
48 err = lc2.Create("ubuntu") | |
49 c.Assert(err, ErrorMatches, "container .* is already created") | |
50 } | |
51 | |
52 func (l *LXCSuite) TestDestroyNotCreated(c *C) { | |
53 // Test that a non-existing container can't be destroyed. | |
54 lc := golxc.New("golxc") | |
55 c.Assert(lc.IsConstructed(), Equals, false) | |
56 err := lc.Destroy() | |
57 c.Assert(err, ErrorMatches, "container .* is not yet created") | |
58 } | |
59 | |
60 func contains(lcs []*golxc.Container, lc *golxc.Container) bool { | |
61 for _, clc := range lcs { | |
62 if clc.Name() == lc.Name() { | |
63 return true | |
64 } | |
65 } | |
66 return false | |
67 } | |
68 | |
69 func (s *LXCSuite) TestList(c *C) { | |
70 // Test the listing of created containers. | |
71 lcs, err := golxc.List() | |
72 oldLen := len(lcs) | |
73 c.Assert(err, IsNil) | |
74 c.Assert(oldLen >= 0, Equals, true) | |
75 lc := golxc.New("golxc") | |
76 c.Assert(lc.IsConstructed(), Equals, false) | |
77 c.Assert(lc.Create("ubuntu"), IsNil) | |
78 c.Assert(lc.IsConstructed(), Equals, true) | |
79 defer func() { | |
80 c.Assert(lc.Destroy(), IsNil) | |
81 }() | |
82 lcs, _ = golxc.List() | |
83 newLen := len(lcs) | |
84 c.Assert(newLen == oldLen+1, Equals, true) | |
85 c.Assert(contains(lcs, lc), Equals, true) | |
86 } | |
87 | |
88 func (s *LXCSuite) TestClone(c *C) { | |
89 // Test the cloning of an existing container. | |
90 lc1 := golxc.New("golxc") | |
91 c.Assert(lc1.IsConstructed(), Equals, false) | |
92 c.Assert(lc1.Create("ubuntu"), IsNil) | |
93 c.Assert(lc1.IsConstructed(), Equals, true) | |
94 defer func() { | |
95 c.Assert(lc1.Destroy(), IsNil) | |
96 }() | |
97 lcs, _ := golxc.List() | |
98 oldLen := len(lcs) | |
99 lc2, err := lc1.Clone("golxcclone") | |
100 c.Assert(err, IsNil) | |
101 c.Assert(lc2.IsConstructed(), Equals, true) | |
102 defer func() { | |
103 c.Assert(lc2.Destroy(), IsNil) | |
104 }() | |
105 lcs, _ = golxc.List() | |
106 newLen := len(lcs) | |
107 c.Assert(newLen == oldLen+1, Equals, true) | |
108 c.Assert(contains(lcs, lc1), Equals, true) | |
109 c.Assert(contains(lcs, lc2), Equals, true) | |
110 } | |
111 | |
112 func (s *LXCSuite) TestCloneNonExisting(c *C) { | |
113 // Test the cloning of a non-existing container. | |
114 lc := golxc.New("golxc") | |
115 c.Assert(lc.IsConstructed(), Equals, false) | |
116 _, err := lc.Clone("golxcclone") | |
117 c.Assert(err, ErrorMatches, "container .* is not yet created") | |
118 } | |
119 | |
120 func (s *LXCSuite) TestStartStop(c *C) { | |
121 // Test starting and stopping a container. | |
122 lc := golxc.New("golxc") | |
123 c.Assert(lc.IsConstructed(), Equals, false) | |
124 c.Assert(lc.Create("ubuntu"), IsNil) | |
125 defer func() { | |
126 c.Assert(lc.Destroy(), IsNil) | |
127 }() | |
128 c.Assert(lc.Start("", ""), IsNil) | |
129 c.Assert(lc.IsRunning(), Equals, true) | |
130 c.Assert(lc.Stop(), IsNil) | |
131 c.Assert(lc.IsRunning(), Equals, false) | |
132 } | |
133 | |
134 func (s *LXCSuite) TestWait(c *C) { | |
135 // Test waiting for one of a number of states of a container. | |
136 // ATTN: Using a not reached state blocks the test until timeout! | |
137 lc := golxc.New("golxc") | |
138 c.Assert(lc.IsConstructed(), Equals, false) | |
139 c.Assert(lc.Wait(), ErrorMatches, "no states specified") | |
140 c.Assert(lc.Wait(golxc.StateStopped), IsNil) | |
141 c.Assert(lc.Wait(golxc.StateStopped, golxc.StateRunning), IsNil) | |
142 c.Assert(lc.Create("ubuntu"), IsNil) | |
143 defer func() { | |
144 c.Assert(lc.Destroy(), IsNil) | |
145 }() | |
146 go func() { | |
147 c.Assert(lc.Start("", ""), IsNil) | |
148 }() | |
149 c.Assert(lc.Wait(golxc.StateRunning), IsNil) | |
150 } | |
151 | |
152 func (l *LXCSuite) TestFreezeUnfreeze(c *C) { | |
153 // Test the freezing and unfreezing of a started container. | |
154 lc := golxc.New("golxc") | |
155 c.Assert(lc.IsConstructed(), Equals, false) | |
156 c.Assert(lc.Create("ubuntu"), IsNil) | |
157 defer func() { | |
158 c.Assert(lc.Destroy(), IsNil) | |
159 }() | |
160 c.Assert(lc.Start("", ""), IsNil) | |
161 defer func() { | |
162 c.Assert(lc.Stop(), IsNil) | |
163 }() | |
164 c.Assert(lc.IsRunning(), Equals, true) | |
165 c.Assert(lc.Freeze(), IsNil) | |
166 c.Assert(lc.IsRunning(), Equals, false) | |
167 c.Assert(lc.Unfreeze(), IsNil) | |
168 c.Assert(lc.IsRunning(), Equals, true) | |
169 } | |
170 | |
171 func (l *LXCSuite) TestFreezeNotStarted(c *C) { | |
172 // Test that a not running container can't be frozen. | |
173 lc := golxc.New("golxc") | |
174 c.Assert(lc.IsConstructed(), Equals, false) | |
175 c.Assert(lc.Create("ubuntu"), IsNil) | |
176 defer func() { | |
177 c.Assert(lc.Destroy(), IsNil) | |
178 }() | |
179 c.Assert(lc.Freeze(), ErrorMatches, "container .* is not running") | |
180 } | |
181 | |
182 func (l *LXCSuite) TestFreezeNotCreated(c *C) { | |
183 // Test that a non-existing container can't be frozen. | |
184 lc := golxc.New("golxc") | |
185 c.Assert(lc.IsConstructed(), Equals, false) | |
186 c.Assert(lc.Freeze(), ErrorMatches, "container .* is not yet created") | |
187 } | |
188 | |
189 func (l *LXCSuite) TestUnfreezeNotCreated(c *C) { | |
190 // Test that a non-existing container can't be unfrozen. | |
191 lc := golxc.New("golxc") | |
192 c.Assert(lc.IsConstructed(), Equals, false) | |
193 c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not yet created") | |
194 } | |
195 | |
196 func (l *LXCSuite) TestUnfreezeNotFrozen(c *C) { | |
197 // Test that a running container can't be unfrozen. | |
198 lc := golxc.New("golxc") | |
199 c.Assert(lc.IsConstructed(), Equals, false) | |
200 c.Assert(lc.Create("ubuntu"), IsNil) | |
201 defer func() { | |
202 c.Assert(lc.Destroy(), IsNil) | |
203 }() | |
204 c.Assert(lc.Start("", ""), IsNil) | |
205 defer func() { | |
206 c.Assert(lc.Stop(), IsNil) | |
207 }() | |
208 c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen") | |
209 } | |
OLD | NEW |