~thumper/golxc/mockable

« back to all changes in this revision

Viewing changes to golxc_test.go

  • Committer: Frank Mueller
  • Author(s): Frank Mueller
  • Date: 2012-11-23 15:59:15 UTC
  • mfrom: (1.1.13 golxc)
  • Revision ID: themue@gmail.com-20121123155915-rjw4kx08q6qahx2r
golxc: add first container tests

The package now has the first functions for the
testing of the container. Two external scripts
help to perform the tests as a root.

R=rog, fwereade
CC=
https://codereview.appspot.com/6852065

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package golxc_test
 
2
 
 
3
import (
 
4
        . "launchpad.net/gocheck"
 
5
        "launchpad.net/golxc"
 
6
        "os"
 
7
        "os/user"
 
8
        "testing"
 
9
)
 
10
 
 
11
func Test(t *testing.T) { TestingT(t) }
 
12
 
 
13
type LXCSuite struct{}
 
14
 
 
15
var _ = Suite(&LXCSuite{})
 
16
 
 
17
func (s *LXCSuite) SetUpSuite(c *C) {
 
18
        u, err := user.Current()
 
19
        c.Assert(err, IsNil)
 
20
        if u.Uid != "0" {
 
21
                // Has to be run as root!
 
22
                c.Skip("tests must run as root")
 
23
        }
 
24
}
 
25
 
 
26
func (s *LXCSuite) TestCreateDestroy(c *C) {
 
27
        // Test clean creation and destroying of a container.
 
28
        lc := golxc.New("golxc")
 
29
        c.Assert(lc.IsConstructed(), Equals, false)
 
30
        home := golxc.ContainerHome(lc)
 
31
        _, err := os.Stat(home)
 
32
        c.Assert(err, ErrorMatches, "stat .*: no such file or directory")
 
33
        err = lc.Create("ubuntu")
 
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
        }()
 
42
        fi, err := os.Stat(golxc.ContainerHome(lc))
 
43
        c.Assert(err, IsNil)
 
44
        c.Assert(fi.IsDir(), Equals, true)
 
45
}
 
46
 
 
47
func (s *LXCSuite) TestCreateTwice(c *C) {
 
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 file 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")
 
78
}
 
79
 
 
80
func contains(lcs []*golxc.Container, lc *golxc.Container) bool {
 
81
        for _, clc := range lcs {
 
82
                if clc.Name() == lc.Name() {
 
83
                        return true
 
84
                }
 
85
        }
 
86
        return false
 
87
}
 
88
 
 
89
func (s *LXCSuite) TestList(c *C) {
 
90
        // Test the listing of created containers.
 
91
        lcs, err := golxc.List()
 
92
        oldLen := len(lcs)
 
93
        c.Assert(err, IsNil)
 
94
        c.Assert(oldLen >= 0, Equals, true)
 
95
        lc := golxc.New("golxc")
 
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
        }()
 
102
        lcs, _ = golxc.List()
 
103
        newLen := len(lcs)
 
104
        c.Assert(newLen == oldLen+1, Equals, true)
 
105
        c.Assert(contains(lcs, lc), Equals, true)
 
106
}
 
107
 
 
108
func (s *LXCSuite) TestClone(c *C) {
 
109
        // Test the cloning of an existing container.
 
110
        lc1 := golxc.New("golxc")
 
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
        }()
 
117
        lcs, _ := golxc.List()
 
118
        oldLen := len(lcs)
 
119
        lc2, err := lc1.Clone("golxcclone")
 
120
        c.Assert(err, IsNil)
 
121
        c.Assert(lc2.IsConstructed(), Equals, true)
 
122
        defer func() {
 
123
                c.Assert(lc2.Destroy(), IsNil)
 
124
        }()
 
125
        lcs, _ = golxc.List()
 
126
        newLen := len(lcs)
 
127
        c.Assert(newLen == oldLen+1, Equals, true)
 
128
        c.Assert(contains(lcs, lc1), Equals, true)
 
129
        c.Assert(contains(lcs, lc2), Equals, true)
 
130
}
 
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")
 
138
}
 
139
 
 
140
func (s *LXCSuite) TestStartStop(c *C) {
 
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)
 
151
        c.Assert(lc.IsRunning(), Equals, false)
 
152
}
 
153
 
 
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 created")
 
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
        }()
 
202
        c.Assert(lc.IsRunning(), Equals, true)
 
203
        c.Assert(lc.Freeze(), IsNil)
 
204
        c.Assert(lc.IsRunning(), Equals, false)
 
205
        c.Assert(lc.Unfreeze(), IsNil)
 
206
        c.Assert(lc.IsRunning(), Equals, true)
 
207
}
 
208
 
 
209
func (l *LXCSuite) TestFreezeNotStarted(c *C) {
 
210
        // Test that a not running container can't be frozen.
 
211
        lc := golxc.New("golxc")
 
212
        c.Assert(lc.IsConstructed(), Equals, false)
 
213
        c.Assert(lc.Create("ubuntu"), IsNil)
 
214
        defer func() {
 
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
}