~wallyworld/golxc/support-cmd-env

« back to all changes in this revision

Viewing changes to golxc_test.go

  • Committer: Ian Booth
  • Date: 2014-11-20 23:04:45 UTC
  • Revision ID: ian.booth@canonical.com-20141120230445-my2ge8skdq9yt9ll
Add support for passing env values to container Create

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
        "github.com/juju/testing"
13
13
        jc "github.com/juju/testing/checkers"
14
 
        . "launchpad.net/gocheck"
 
14
        gc "gopkg.in/check.v1"
15
15
 
16
16
        "launchpad.net/golxc"
17
17
)
52
52
 
53
53
type ConfigSuite struct{}
54
54
 
55
 
var _ = Suite(&ConfigSuite{})
 
55
var _ = gc.Suite(&ConfigSuite{})
56
56
 
57
 
func (s *ConfigSuite) TestReadConf(c *C) {
 
57
func (s *ConfigSuite) TestReadConf(c *gc.C) {
58
58
        // Test reading the configuration.
59
59
        cf := filepath.Join(c.MkDir(), "lxc-test")
60
 
        c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), IsNil)
 
60
        c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), gc.IsNil)
61
61
 
62
62
        defer golxc.SetConfPath(golxc.SetConfPath(cf))
63
63
 
64
64
        conf, err := golxc.ReadConf()
65
 
        c.Assert(err, IsNil)
66
 
        c.Assert(conf, DeepEquals, lxcconf)
 
65
        c.Assert(err, gc.IsNil)
 
66
        c.Assert(conf, gc.DeepEquals, lxcconf)
67
67
}
68
68
 
69
 
func (s *ConfigSuite) TestReadNotExistingDefaultEnvironment(c *C) {
 
69
func (s *ConfigSuite) TestReadNotExistingDefaultEnvironment(c *gc.C) {
70
70
        // Test reading a not existing environment.
71
71
        defer golxc.SetConfPath(golxc.SetConfPath(filepath.Join(c.MkDir(), "foo")))
72
72
 
73
73
        _, err := golxc.ReadConf()
74
 
        c.Assert(err, ErrorMatches, "open .*: no such file or directory")
 
74
        c.Assert(err, gc.ErrorMatches, "open .*: no such file or directory")
75
75
}
76
76
 
77
 
func (s *ConfigSuite) TestNetworkAttributes(c *C) {
 
77
func (s *ConfigSuite) TestNetworkAttributes(c *gc.C) {
78
78
        // Test reading the network attribute form an environment.
79
79
        cf := filepath.Join(c.MkDir(), "lxc-test")
80
 
        c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), IsNil)
 
80
        c.Assert(ioutil.WriteFile(cf, []byte(lxcfile), 0555), gc.IsNil)
81
81
 
82
82
        defer golxc.SetConfPath(golxc.SetConfPath(cf))
83
83
 
84
84
        addr, bridge, err := golxc.NetworkAttributes()
85
 
        c.Assert(err, IsNil)
86
 
        c.Assert(addr, Equals, "10.0.9.1")
87
 
        c.Assert(bridge, Equals, "lxcbr9")
 
85
        c.Assert(err, gc.IsNil)
 
86
        c.Assert(addr, gc.Equals, "10.0.9.1")
 
87
        c.Assert(bridge, gc.Equals, "lxcbr9")
88
88
}
89
89
 
90
90
type NetworkSuite struct{}
91
91
 
92
 
var _ = Suite(&NetworkSuite{})
 
92
var _ = gc.Suite(&NetworkSuite{})
93
93
 
94
 
func (s *NetworkSuite) SetUpSuite(c *C) {
 
94
func (s *NetworkSuite) SetUpSuite(c *gc.C) {
95
95
        u, err := user.Current()
96
 
        c.Assert(err, IsNil)
 
96
        c.Assert(err, gc.IsNil)
97
97
        if u.Uid != "0" {
98
98
                // Has to be run as root!
99
99
                c.Skip("tests must run as root")
100
100
        }
101
101
}
102
102
 
103
 
func (s *NetworkSuite) TestStartStopNetwork(c *C) {
 
103
func (s *NetworkSuite) TestStartStopNetwork(c *gc.C) {
104
104
        // Test starting and stoping of the LXC network.
105
105
        initialRunning, err := golxc.IsNetworkRunning()
106
 
        c.Assert(err, IsNil)
 
106
        c.Assert(err, gc.IsNil)
107
107
        defer func() {
108
108
                if initialRunning {
109
 
                        c.Assert(golxc.StartNetwork(), IsNil)
 
109
                        c.Assert(golxc.StartNetwork(), gc.IsNil)
110
110
                }
111
111
        }()
112
 
        c.Assert(golxc.StartNetwork(), IsNil)
 
112
        c.Assert(golxc.StartNetwork(), gc.IsNil)
113
113
        running, err := golxc.IsNetworkRunning()
114
 
        c.Assert(err, IsNil)
115
 
        c.Assert(running, Equals, true)
116
 
        c.Assert(golxc.StopNetwork(), IsNil)
 
114
        c.Assert(err, gc.IsNil)
 
115
        c.Assert(running, gc.Equals, true)
 
116
        c.Assert(golxc.StopNetwork(), gc.IsNil)
117
117
        running, err = golxc.IsNetworkRunning()
118
 
        c.Assert(err, IsNil)
119
 
        c.Assert(running, Equals, false)
 
118
        c.Assert(err, gc.IsNil)
 
119
        c.Assert(running, gc.Equals, false)
120
120
}
121
121
 
122
 
func (s *NetworkSuite) TestNotExistingNetworkAttributes(c *C) {
 
122
func (s *NetworkSuite) TestNotExistingNetworkAttributes(c *gc.C) {
123
123
        // Test reading of network attributes from a not existing environment.
124
124
        defer golxc.SetConfPath(golxc.SetConfPath(filepath.Join(c.MkDir(), "foo")))
125
125
 
126
126
        _, _, err := golxc.NetworkAttributes()
127
 
        c.Assert(err, ErrorMatches, "open .*: no such file or directory")
 
127
        c.Assert(err, gc.ErrorMatches, "open .*: no such file or directory")
128
128
}
129
129
 
130
130
type LXCSuite struct {
131
131
        factory golxc.ContainerFactory
132
132
}
133
133
 
134
 
var _ = Suite(&LXCSuite{golxc.Factory()})
 
134
var _ = gc.Suite(&LXCSuite{golxc.Factory()})
135
135
 
136
 
func (s *LXCSuite) SetUpSuite(c *C) {
 
136
func (s *LXCSuite) SetUpSuite(c *gc.C) {
137
137
        u, err := user.Current()
138
 
        c.Assert(err, IsNil)
 
138
        c.Assert(err, gc.IsNil)
139
139
        if u.Uid != "0" {
140
140
                // Has to be run as root!
141
141
                c.Skip("tests must run as root")
142
142
        }
143
143
}
144
144
 
145
 
func (s *LXCSuite) createContainer(c *C) golxc.Container {
 
145
func (s *LXCSuite) createContainer(c *gc.C) golxc.Container {
146
146
        container := s.factory.New("golxc")
147
 
        c.Assert(container.IsConstructed(), Equals, false)
148
 
        err := container.Create("", "ubuntu", nil, nil)
149
 
        c.Assert(err, IsNil)
150
 
        c.Assert(container.IsConstructed(), Equals, true)
 
147
        c.Assert(container.IsConstructed(), gc.Equals, false)
 
148
        err := container.Create("", "ubuntu", nil, nil, nil)
 
149
        c.Assert(err, gc.IsNil)
 
150
        c.Assert(container.IsConstructed(), gc.Equals, true)
151
151
        return container
152
152
}
153
153
 
154
 
func (s *LXCSuite) TestCreateDestroy(c *C) {
 
154
func (s *LXCSuite) TestCreateDestroy(c *gc.C) {
155
155
        // Test clean creation and destroying of a container.
156
156
        lc := s.factory.New("golxc")
157
 
        c.Assert(lc.IsConstructed(), Equals, false)
 
157
        c.Assert(lc.IsConstructed(), gc.Equals, false)
158
158
        home := golxc.ContainerHome(lc)
159
159
        _, err := os.Stat(home)
160
 
        c.Assert(err, ErrorMatches, "stat .*: no such file or directory")
161
 
        err = lc.Create("", "ubuntu", nil, nil)
162
 
        c.Assert(err, IsNil)
163
 
        c.Assert(lc.IsConstructed(), Equals, true)
 
160
        c.Assert(err, gc.ErrorMatches, "stat .*: no such file or directory")
 
161
        err = lc.Create("", "ubuntu", nil, nil, nil)
 
162
        c.Assert(err, gc.IsNil)
 
163
        c.Assert(lc.IsConstructed(), gc.Equals, true)
164
164
        defer func() {
165
165
                err = lc.Destroy()
166
 
                c.Assert(err, IsNil)
 
166
                c.Assert(err, gc.IsNil)
167
167
                _, err = os.Stat(home)
168
 
                c.Assert(err, ErrorMatches, "stat .*: no such file or directory")
 
168
                c.Assert(err, gc.ErrorMatches, "stat .*: no such file or directory")
169
169
        }()
170
170
        fi, err := os.Stat(golxc.ContainerHome(lc))
171
 
        c.Assert(err, IsNil)
172
 
        c.Assert(fi.IsDir(), Equals, true)
 
171
        c.Assert(err, gc.IsNil)
 
172
        c.Assert(fi.IsDir(), gc.Equals, true)
173
173
}
174
174
 
175
 
func (s *LXCSuite) TestCreateTwice(c *C) {
 
175
func (s *LXCSuite) TestCreateTwice(c *gc.C) {
176
176
        // Test that a container cannot be created twice.
177
177
        lc1 := s.createContainer(c)
178
 
        c.Assert(lc1.IsConstructed(), Equals, true)
 
178
        c.Assert(lc1.IsConstructed(), gc.Equals, true)
179
179
        defer func() {
180
 
                c.Assert(lc1.Destroy(), IsNil)
 
180
                c.Assert(lc1.Destroy(), gc.IsNil)
181
181
        }()
182
182
        lc2 := s.factory.New("golxc")
183
 
        err := lc2.Create("", "ubuntu", nil, nil)
184
 
        c.Assert(err, ErrorMatches, "container .* is already created")
 
183
        err := lc2.Create("", "ubuntu", nil, nil, nil)
 
184
        c.Assert(err, gc.ErrorMatches, "container .* is already created")
185
185
}
186
186
 
187
 
func (s *LXCSuite) TestCreateIllegalTemplate(c *C) {
 
187
func (s *LXCSuite) TestCreateIllegalTemplate(c *gc.C) {
188
188
        // Test that a container creation fails correctly in
189
189
        // case of an illegal template.
190
190
        lc := s.factory.New("golxc")
191
 
        c.Assert(lc.IsConstructed(), Equals, false)
192
 
        err := lc.Create("", "name-of-a-not-existing-template-for-golxc", nil, nil)
193
 
        c.Assert(err, ErrorMatches, `error executing "lxc-create": .*bad template.*`)
194
 
        c.Assert(lc.IsConstructed(), Equals, false)
 
191
        c.Assert(lc.IsConstructed(), gc.Equals, false)
 
192
        err := lc.Create("", "name-of-a-not-existing-template-for-golxc", nil, nil, nil)
 
193
        c.Assert(err, gc.ErrorMatches, `error executing "lxc-create": .*bad template.*`)
 
194
        c.Assert(lc.IsConstructed(), gc.Equals, false)
195
195
}
196
196
 
197
 
func (s *LXCSuite) TestDestroyNotCreated(c *C) {
 
197
func (s *LXCSuite) TestDestroyNotCreated(c *gc.C) {
198
198
        // Test that a non-existing container can't be destroyed.
199
199
        lc := s.factory.New("golxc")
200
 
        c.Assert(lc.IsConstructed(), Equals, false)
 
200
        c.Assert(lc.IsConstructed(), gc.Equals, false)
201
201
        err := lc.Destroy()
202
 
        c.Assert(err, ErrorMatches, "container .* is not yet created")
 
202
        c.Assert(err, gc.ErrorMatches, "container .* is not yet created")
203
203
}
204
204
 
205
205
func contains(lcs []golxc.Container, lc golxc.Container) bool {
211
211
        return false
212
212
}
213
213
 
214
 
func (s *LXCSuite) TestList(c *C) {
 
214
func (s *LXCSuite) TestList(c *gc.C) {
215
215
        // Test the listing of created containers.
216
216
        lcs, err := s.factory.List()
217
217
        oldLen := len(lcs)
218
 
        c.Assert(err, IsNil)
219
 
        c.Assert(oldLen >= 0, Equals, true)
 
218
        c.Assert(err, gc.IsNil)
 
219
        c.Assert(oldLen >= 0, gc.Equals, true)
220
220
        lc := s.createContainer(c)
221
221
        defer func() {
222
 
                c.Assert(lc.Destroy(), IsNil)
 
222
                c.Assert(lc.Destroy(), gc.IsNil)
223
223
        }()
224
224
        lcs, _ = s.factory.List()
225
225
        newLen := len(lcs)
226
 
        c.Assert(newLen == oldLen+1, Equals, true)
227
 
        c.Assert(contains(lcs, lc), Equals, true)
 
226
        c.Assert(newLen == oldLen+1, gc.Equals, true)
 
227
        c.Assert(contains(lcs, lc), gc.Equals, true)
228
228
}
229
229
 
230
 
func (s *LXCSuite) TestClone(c *C) {
 
230
func (s *LXCSuite) TestClone(c *gc.C) {
231
231
        // Test the cloning of an existing container.
232
232
        lc1 := s.createContainer(c)
233
233
        defer func() {
234
 
                c.Assert(lc1.Destroy(), IsNil)
 
234
                c.Assert(lc1.Destroy(), gc.IsNil)
235
235
        }()
236
236
        lcs, _ := s.factory.List()
237
237
        oldLen := len(lcs)
238
238
        lc2, err := lc1.Clone("golxcclone", nil, nil)
239
 
        c.Assert(err, IsNil)
240
 
        c.Assert(lc2.IsConstructed(), Equals, true)
 
239
        c.Assert(err, gc.IsNil)
 
240
        c.Assert(lc2.IsConstructed(), gc.Equals, true)
241
241
        defer func() {
242
 
                c.Assert(lc2.Destroy(), IsNil)
 
242
                c.Assert(lc2.Destroy(), gc.IsNil)
243
243
        }()
244
244
        lcs, _ = s.factory.List()
245
245
        newLen := len(lcs)
246
 
        c.Assert(newLen == oldLen+1, Equals, true)
247
 
        c.Assert(contains(lcs, lc1), Equals, true)
248
 
        c.Assert(contains(lcs, lc2), Equals, true)
 
246
        c.Assert(newLen == oldLen+1, gc.Equals, true)
 
247
        c.Assert(contains(lcs, lc1), gc.Equals, true)
 
248
        c.Assert(contains(lcs, lc2), gc.Equals, true)
249
249
}
250
250
 
251
 
func (s *LXCSuite) TestCloneNotCreated(c *C) {
 
251
func (s *LXCSuite) TestCloneNotCreated(c *gc.C) {
252
252
        // Test the cloning of a non-existing container.
253
253
        lc := s.factory.New("golxc")
254
 
        c.Assert(lc.IsConstructed(), Equals, false)
 
254
        c.Assert(lc.IsConstructed(), gc.Equals, false)
255
255
        _, err := lc.Clone("golxcclone", nil, nil)
256
 
        c.Assert(err, ErrorMatches, "container .* is not yet created")
 
256
        c.Assert(err, gc.ErrorMatches, "container .* is not yet created")
257
257
}
258
258
 
259
 
func (s *LXCSuite) TestStartStop(c *C) {
 
259
func (s *LXCSuite) TestStartStop(c *gc.C) {
260
260
        // Test starting and stopping a container.
261
261
        lc := s.createContainer(c)
262
262
        defer func() {
263
 
                c.Assert(lc.Destroy(), IsNil)
 
263
                c.Assert(lc.Destroy(), gc.IsNil)
264
264
        }()
265
 
        c.Assert(lc.Start("", ""), IsNil)
266
 
        c.Assert(lc.IsRunning(), Equals, true)
267
 
        c.Assert(lc.Stop(), IsNil)
268
 
        c.Assert(lc.IsRunning(), Equals, false)
 
265
        c.Assert(lc.Start("", ""), gc.IsNil)
 
266
        c.Assert(lc.IsRunning(), gc.Equals, true)
 
267
        c.Assert(lc.Stop(), gc.IsNil)
 
268
        c.Assert(lc.IsRunning(), gc.Equals, false)
269
269
}
270
270
 
271
 
func (s *LXCSuite) TestStartNotCreated(c *C) {
 
271
func (s *LXCSuite) TestStartNotCreated(c *gc.C) {
272
272
        // Test that a non-existing container can't be started.
273
273
        lc := s.factory.New("golxc")
274
 
        c.Assert(lc.IsConstructed(), Equals, false)
275
 
        c.Assert(lc.Start("", ""), ErrorMatches, "container .* is not yet created")
 
274
        c.Assert(lc.IsConstructed(), gc.Equals, false)
 
275
        c.Assert(lc.Start("", ""), gc.ErrorMatches, "container .* is not yet created")
276
276
}
277
277
 
278
 
func (s *LXCSuite) TestStopNotRunning(c *C) {
 
278
func (s *LXCSuite) TestStopNotRunning(c *gc.C) {
279
279
        // Test that a not running container can't be stopped.
280
280
        lc := s.createContainer(c)
281
281
        defer func() {
282
 
                c.Assert(lc.Destroy(), IsNil)
 
282
                c.Assert(lc.Destroy(), gc.IsNil)
283
283
        }()
284
 
        c.Assert(lc.Stop(), IsNil)
 
284
        c.Assert(lc.Stop(), gc.IsNil)
285
285
}
286
286
 
287
 
func (s *LXCSuite) TestWait(c *C) {
 
287
func (s *LXCSuite) TestWait(c *gc.C) {
288
288
        // Test waiting for one of a number of states of a container.
289
289
        // ATTN: Using a not reached state blocks the test until timeout!
290
290
        lc := s.factory.New("golxc")
291
 
        c.Assert(lc.IsConstructed(), Equals, false)
292
 
        c.Assert(lc.Wait(), ErrorMatches, "no states specified")
293
 
        c.Assert(lc.Wait(golxc.StateStopped), IsNil)
294
 
        c.Assert(lc.Wait(golxc.StateStopped, golxc.StateRunning), IsNil)
295
 
        c.Assert(lc.Create("", "ubuntu", nil, nil), IsNil)
 
291
        c.Assert(lc.IsConstructed(), gc.Equals, false)
 
292
        c.Assert(lc.Wait(), gc.ErrorMatches, "no states specified")
 
293
        c.Assert(lc.Wait(golxc.StateStopped), gc.IsNil)
 
294
        c.Assert(lc.Wait(golxc.StateStopped, golxc.StateRunning), gc.IsNil)
 
295
        c.Assert(lc.Create("", "ubuntu", nil, nil, nil), gc.IsNil)
296
296
        defer func() {
297
 
                c.Assert(lc.Destroy(), IsNil)
 
297
                c.Assert(lc.Destroy(), gc.IsNil)
298
298
        }()
299
299
        go func() {
300
 
                c.Assert(lc.Start("", ""), IsNil)
 
300
                c.Assert(lc.Start("", ""), gc.IsNil)
301
301
        }()
302
 
        c.Assert(lc.Wait(golxc.StateRunning), IsNil)
 
302
        c.Assert(lc.Wait(golxc.StateRunning), gc.IsNil)
303
303
}
304
304
 
305
 
func (s *LXCSuite) TestFreezeUnfreeze(c *C) {
 
305
func (s *LXCSuite) TestFreezeUnfreeze(c *gc.C) {
306
306
        // Test the freezing and unfreezing of a started container.
307
307
        lc := s.createContainer(c)
308
308
        defer func() {
309
 
                c.Assert(lc.Destroy(), IsNil)
 
309
                c.Assert(lc.Destroy(), gc.IsNil)
310
310
        }()
311
 
        c.Assert(lc.Start("", ""), IsNil)
 
311
        c.Assert(lc.Start("", ""), gc.IsNil)
312
312
        defer func() {
313
 
                c.Assert(lc.Stop(), IsNil)
 
313
                c.Assert(lc.Stop(), gc.IsNil)
314
314
        }()
315
 
        c.Assert(lc.IsRunning(), Equals, true)
316
 
        c.Assert(lc.Freeze(), IsNil)
317
 
        c.Assert(lc.IsRunning(), Equals, false)
318
 
        c.Assert(lc.Unfreeze(), IsNil)
319
 
        c.Assert(lc.IsRunning(), Equals, true)
 
315
        c.Assert(lc.IsRunning(), gc.Equals, true)
 
316
        c.Assert(lc.Freeze(), gc.IsNil)
 
317
        c.Assert(lc.IsRunning(), gc.Equals, false)
 
318
        c.Assert(lc.Unfreeze(), gc.IsNil)
 
319
        c.Assert(lc.IsRunning(), gc.Equals, true)
320
320
}
321
321
 
322
 
func (s *LXCSuite) TestFreezeNotStarted(c *C) {
 
322
func (s *LXCSuite) TestFreezeNotStarted(c *gc.C) {
323
323
        // Test that a not running container can't be frozen.
324
324
        lc := s.createContainer(c)
325
325
        defer func() {
326
 
                c.Assert(lc.Destroy(), IsNil)
 
326
                c.Assert(lc.Destroy(), gc.IsNil)
327
327
        }()
328
 
        c.Assert(lc.Freeze(), ErrorMatches, "container .* is not running")
 
328
        c.Assert(lc.Freeze(), gc.ErrorMatches, "container .* is not running")
329
329
}
330
330
 
331
 
func (s *LXCSuite) TestFreezeNotCreated(c *C) {
 
331
func (s *LXCSuite) TestFreezeNotCreated(c *gc.C) {
332
332
        // Test that a non-existing container can't be frozen.
333
333
        lc := s.factory.New("golxc")
334
 
        c.Assert(lc.IsConstructed(), Equals, false)
335
 
        c.Assert(lc.Freeze(), ErrorMatches, "container .* is not yet created")
 
334
        c.Assert(lc.IsConstructed(), gc.Equals, false)
 
335
        c.Assert(lc.Freeze(), gc.ErrorMatches, "container .* is not yet created")
336
336
}
337
337
 
338
 
func (s *LXCSuite) TestUnfreezeNotCreated(c *C) {
 
338
func (s *LXCSuite) TestUnfreezeNotCreated(c *gc.C) {
339
339
        // Test that a non-existing container can't be unfrozen.
340
340
        lc := s.factory.New("golxc")
341
 
        c.Assert(lc.IsConstructed(), Equals, false)
342
 
        c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not yet created")
 
341
        c.Assert(lc.IsConstructed(), gc.Equals, false)
 
342
        c.Assert(lc.Unfreeze(), gc.ErrorMatches, "container .* is not yet created")
343
343
}
344
344
 
345
 
func (s *LXCSuite) TestUnfreezeNotFrozen(c *C) {
 
345
func (s *LXCSuite) TestUnfreezeNotFrozen(c *gc.C) {
346
346
        // Test that a running container can't be unfrozen.
347
347
        lc := s.createContainer(c)
348
348
        defer func() {
349
 
                c.Assert(lc.Destroy(), IsNil)
 
349
                c.Assert(lc.Destroy(), gc.IsNil)
350
350
        }()
351
 
        c.Assert(lc.Start("", ""), IsNil)
 
351
        c.Assert(lc.Start("", ""), gc.IsNil)
352
352
        defer func() {
353
 
                c.Assert(lc.Stop(), IsNil)
 
353
                c.Assert(lc.Stop(), gc.IsNil)
354
354
        }()
355
 
        c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen")
 
355
        c.Assert(lc.Unfreeze(), gc.ErrorMatches, "container .* is not frozen")
356
356
}
357
357
 
358
358
type commandArgs struct {
360
360
        originalPath string
361
361
}
362
362
 
363
 
var _ = Suite(&commandArgs{})
 
363
var _ = gc.Suite(&commandArgs{})
364
364
 
365
 
func (s *commandArgs) SetUpSuite(c *C) {
 
365
func (s *commandArgs) SetUpSuite(c *gc.C) {
366
366
        // lxc-create requires the PATH to be set.
367
367
        s.originalPath = os.Getenv("PATH")
368
368
        s.IsolationSuite.SetUpSuite(c)
369
369
}
370
370
 
371
 
func (s *commandArgs) setupLxcStart(c *C) {
 
371
func (s *commandArgs) setupLxcStart(c *gc.C) {
372
372
        dir := c.MkDir()
373
373
        // Make the rootfs for the "test" container so it thinks it is created.
374
374
        rootfs := filepath.Join(dir, "test", "rootfs")
375
375
        err := os.MkdirAll(rootfs, 0755)
376
 
        c.Assert(err, IsNil)
 
376
        c.Assert(err, gc.IsNil)
377
377
        c.Assert(rootfs, jc.IsDirectory)
378
378
 
379
379
        s.PatchValue(&golxc.ContainerDir, dir)
384
384
        testing.PatchExecutable(c, s, "lxc-info", "#!/bin/sh\necho '"+info+"'\n")
385
385
}
386
386
 
387
 
func (s *commandArgs) TestStartArgs(c *C) {
 
387
func (s *commandArgs) TestStartArgs(c *gc.C) {
388
388
        s.setupLxcStart(c)
389
389
        factory := golxc.Factory()
390
390
        container := factory.New("test")
391
391
        err := container.Start("config-file", "console-log")
392
 
        c.Assert(err, IsNil)
 
392
        c.Assert(err, gc.IsNil)
393
393
        testing.AssertEchoArgs(
394
394
                c, "lxc-start",
395
395
                "--daemon",
398
398
                "--console-log", "console-log")
399
399
}
400
400
 
401
 
func (s *commandArgs) TestStartArgsNoConsoleLog(c *C) {
 
401
func (s *commandArgs) TestStartArgsNoConsoleLog(c *gc.C) {
402
402
        s.setupLxcStart(c)
403
403
        factory := golxc.Factory()
404
404
        container := factory.New("test")
405
405
        err := container.Start("config-file", "")
406
 
        c.Assert(err, IsNil)
 
406
        c.Assert(err, gc.IsNil)
407
407
        testing.AssertEchoArgs(
408
408
                c, "lxc-start",
409
409
                "--daemon",
411
411
                "--rcfile", "config-file")
412
412
}
413
413
 
414
 
func (s *commandArgs) TestStartArgsFallback(c *C) {
 
414
func (s *commandArgs) TestStartArgsFallback(c *gc.C) {
415
415
        dir := c.MkDir()
416
416
        // Make the rootfs for the "test" container so it thinks it is created.
417
417
        rootfs := filepath.Join(dir, "test", "rootfs")
418
418
        err := os.MkdirAll(rootfs, 0755)
419
 
        c.Assert(err, IsNil)
 
419
        c.Assert(err, gc.IsNil)
420
420
        c.Assert(rootfs, jc.IsDirectory)
421
421
 
422
422
        s.PatchValue(&golxc.ContainerDir, dir)
442
442
        factory := golxc.Factory()
443
443
        container := factory.New("test")
444
444
        err = container.Start("config-file", "console-log")
445
 
        c.Assert(err, IsNil)
 
445
        c.Assert(err, gc.IsNil)
446
446
        testing.AssertEchoArgs(
447
447
                c, "lxc-start",
448
448
                "--daemon",
451
451
                "--console", "console-log")
452
452
}
453
453
 
454
 
func (s *commandArgs) TestCreateArgs(c *C) {
 
454
func (s *commandArgs) TestCreateArgs(c *gc.C) {
455
455
        s.PatchValue(&golxc.ContainerDir, c.MkDir())
456
456
        s.PatchEnvironment("PATH", s.originalPath)
457
457
        testing.PatchExecutableAsEchoArgs(c, s, "lxc-create")
462
462
                "config-file", "template",
463
463
                []string{"extra-1", "extra-2"},
464
464
                []string{"template-1", "template-2"},
 
465
                nil,
465
466
        )
466
 
        c.Assert(err, IsNil)
 
467
        c.Assert(err, gc.IsNil)
467
468
        testing.AssertEchoArgs(
468
469
                c, "lxc-create",
469
470
                "-n", "test",
473
474
                "--", "template-1", "template-2")
474
475
}
475
476
 
476
 
func (s *commandArgs) TestCloneArgs(c *C) {
 
477
func (s *commandArgs) TestCreateWithEnv(c *gc.C) {
 
478
        s.PatchValue(&golxc.ContainerDir, c.MkDir())
 
479
        s.PatchEnvironment("PATH", s.originalPath)
 
480
 
 
481
        EchoEnvHello := `#!/bin/bash
 
482
name=` + "`basename $0`" + `
 
483
argfile="$name.out"
 
484
rm -f $argfile
 
485
printf "%s" $name | tee -a $argfile
 
486
printf " \"%s\""  "$HELLO" | tee -a $argfile
 
487
printf "\n" | tee -a $argfile
 
488
`
 
489
 
 
490
        testing.PatchExecutable(
 
491
                c, s, "lxc-create", EchoEnvHello)
 
492
        s.AddCleanup(func(*gc.C) {
 
493
                os.Remove("lxc-create.out")
 
494
        })
 
495
 
 
496
        envArgs := []string{"HELLO=WORLD!"}
 
497
        factory := golxc.Factory()
 
498
        container := factory.New("test")
 
499
        err := container.Create(
 
500
                "config-file", "template",
 
501
                []string{"extra-1", "extra-2"},
 
502
                []string{"template-1", "template-2"},
 
503
                envArgs,
 
504
        )
 
505
        c.Assert(err, gc.IsNil)
 
506
        testing.AssertEchoArgs(c, "lxc-create", "WORLD!")
 
507
}
 
508
 
 
509
func (s *commandArgs) TestCloneArgs(c *gc.C) {
477
510
        dir := c.MkDir()
478
511
        s.PatchValue(&golxc.ContainerDir, dir)
479
512
        s.PatchEnvironment("PATH", s.originalPath)
486
519
        // Make the rootfs for the "test" container so it thinks it is created.
487
520
        rootfs := filepath.Join(dir, "test", "rootfs")
488
521
        err := os.MkdirAll(rootfs, 0755)
489
 
        c.Assert(err, IsNil)
 
522
        c.Assert(err, gc.IsNil)
490
523
        c.Assert(rootfs, jc.IsDirectory)
491
524
        c.Assert(container.IsConstructed(), jc.IsTrue)
492
525
        clone, err := container.Clone(
494
527
                []string{"extra-1", "extra-2"},
495
528
                []string{"template-1", "template-2"},
496
529
        )
497
 
        c.Assert(err, IsNil)
 
530
        c.Assert(err, gc.IsNil)
498
531
        testing.AssertEchoArgs(
499
532
                c, "lxc-clone",
500
533
                "-o", "test",
501
534
                "-n", "name",
502
535
                "extra-1", "extra-2",
503
536
                "--", "template-1", "template-2")
504
 
        c.Assert(clone.Name(), Equals, "name")
 
537
        c.Assert(clone.Name(), gc.Equals, "name")
505
538
}
506
539
 
507
540
type UtilsSuite struct {
508
541
        testing.IsolationSuite
509
542
}
510
543
 
511
 
var _ = Suite(&UtilsSuite{})
 
544
var _ = gc.Suite(&UtilsSuite{})
512
545
 
513
 
func (s *UtilsSuite) TestGetDefaultLXCContainerDirDefaultValue(c *C) {
 
546
func (s *UtilsSuite) TestGetDefaultLXCContainerDirDefaultValue(c *gc.C) {
514
547
        testing.PatchExecutable(c, s, "lxc-config", "#!/bin/sh\nexit -1\n")
515
 
        c.Assert(golxc.GetDefaultLXCContainerDir(), Equals, golxc.DefaultLXCDir)
 
548
        c.Assert(golxc.GetDefaultLXCContainerDir(), gc.Equals, golxc.DefaultLXCDir)
516
549
}
517
550
 
518
 
func (s *UtilsSuite) TestGetDefaultLXCContainerDir(c *C) {
 
551
func (s *UtilsSuite) TestGetDefaultLXCContainerDir(c *gc.C) {
519
552
        const path = "/var/lib/non-standard-lxc"
520
553
        testing.PatchExecutable(c, s, "lxc-config", "#!/bin/sh\necho '"+path+"'\n")
521
 
        c.Assert(golxc.GetDefaultLXCContainerDir(), Equals, path)
 
554
        c.Assert(golxc.GetDefaultLXCContainerDir(), gc.Equals, path)
522
555
}