~xnox/ubuntu/saucy/lxc/dep8

« back to all changes in this revision

Viewing changes to src/lua-lxc/test/apitest.lua

  • Committer: Stéphane Graber
  • Date: 2013-02-18 15:20:18 UTC
  • mto: This revision was merged to the branch mainline in revision 190.
  • Revision ID: stgraber@ubuntu.com-20130218152018-ls2gi9hkqs2kuhj8
Tags: upstream-0.9.0~alpha3
Import upstream version 0.9.0~alpha3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env lua
 
2
--
 
3
-- test the lxc lua api
 
4
--
 
5
-- Copyright © 2012 Oracle.
 
6
--
 
7
-- Authors:
 
8
-- Dwight Engen <dwight.engen@oracle.com>
 
9
--
 
10
-- This library is free software; you can redistribute it and/or modify
 
11
-- it under the terms of the GNU General Public License version 2, as
 
12
-- published by the Free Software Foundation.
 
13
--
 
14
-- This program is distributed in the hope that it will be useful,
 
15
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
-- GNU General Public License for more details.
 
18
--
 
19
-- You should have received a copy of the GNU General Public License along
 
20
-- with this program; if not, write to the Free Software Foundation, Inc.,
 
21
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
22
--
 
23
 
 
24
local lxc     = require("lxc")
 
25
local lfs     = require("lfs")
 
26
local getopt  = require("alt_getopt")
 
27
 
 
28
local LXC_PATH          = lxc.default_config_path_get()
 
29
 
 
30
local container
 
31
local cfg_containers    = {}
 
32
local optarg            = {}
 
33
local optind            = {}
 
34
 
 
35
function printf(...)
 
36
    local function wrapper(...) io.write(string.format(...)) end
 
37
    local status, result = pcall(wrapper, ...)
 
38
    if not status then
 
39
        error(result, 2)
 
40
    end
 
41
end
 
42
 
 
43
function log(level, ...)
 
44
    if (optarg["v"] >= level) then
 
45
        printf(os.date("%Y-%m-%d %T "))
 
46
        printf(...)
 
47
        printf("\n")
 
48
    end
 
49
end
 
50
 
 
51
function die(...)
 
52
    printf(...)
 
53
    os.exit(1)
 
54
end
 
55
 
 
56
function test_global_info()
 
57
    local cfg_containers
 
58
    local run_containers
 
59
 
 
60
    log(0, "%-20s %s", "LXC version:", lxc.version_get())
 
61
    log(0, "%-20s %s", "Container name:", optarg["n"])
 
62
    if (optarg["c"]) then
 
63
        log(0, "%-20s %s", "Creating container:", "yes")
 
64
        log(0, "%-20s %s", "With template:", optarg["t"])
 
65
    end
 
66
    log(0, "%-20s %s", "Containers path:", LXC_PATH)
 
67
 
 
68
    cfg_containers = lxc.containers_configured()
 
69
    log(0, "%-20s", "Containers configured:")
 
70
    for _,v in ipairs(cfg_containers) do
 
71
        log(0, "  %s", v)
 
72
    end
 
73
 
 
74
    run_containers = lxc.containers_running(true)
 
75
    log(0, "%-20s", "Containers running:")
 
76
    for _,v in ipairs(run_containers) do
 
77
        log(0, "  %s", v)
 
78
    end
 
79
end
 
80
 
 
81
function test_container_new()
 
82
    container = lxc.container:new(optarg["n"])
 
83
    assert(container ~= nil)
 
84
    assert(container:config_file_name() == string.format("%s/%s/config", LXC_PATH, optarg["n"]))
 
85
end
 
86
 
 
87
function test_container_config_path()
 
88
    local cfgcontainer
 
89
    local cfgpath = "/tmp/" .. optarg["n"]
 
90
    local cfgname = cfgpath .. "/config"
 
91
 
 
92
    log(0, "Test container config path...")
 
93
 
 
94
    -- create a config file in the new location from container's config
 
95
    assert(lfs.mkdir(cfgpath))
 
96
    assert(container:save_config(cfgname))
 
97
    cfgcontainer = lxc.container:new(optarg["n"], "/tmp")
 
98
    assert(cfgcontainer ~= nil)
 
99
    log(0, "cfgname:%s cfgpath:%s", cfgcontainer:config_file_name(), cfgcontainer:get_config_path())
 
100
    assert(cfgcontainer:config_file_name() == cfgname)
 
101
    assert(cfgcontainer:get_config_path() == "/tmp")
 
102
    assert(cfgcontainer:set_config_path(LXC_PATH))
 
103
    assert(cfgcontainer:get_config_path() == LXC_PATH)
 
104
 
 
105
    assert(os.remove(cfgname))
 
106
    assert(lfs.rmdir(cfgpath))
 
107
end
 
108
 
 
109
function test_container_create()
 
110
    if (optarg["c"]) then
 
111
        log(0, "%-20s %s", "Destroy existing container:", optarg["n"])
 
112
        container:destroy()
 
113
        assert(container:defined() == false)
 
114
    else
 
115
        local cfg_containers = lxc.containers_configured()
 
116
        if (cfg_containers[optarg["n"]]) then
 
117
            log(0, "%-20s %s", "Use existing container:", optarg["n"])
 
118
            return
 
119
        end
 
120
    end
 
121
    log(0, "%-20s %s", "Creating rootfs using:", optarg["t"])
 
122
    container:create(optarg["t"])
 
123
    assert(container:defined() == true)
 
124
    assert(container:name() == optarg["n"])
 
125
end
 
126
 
 
127
function test_container_started()
 
128
    local now_running
 
129
    log(2, "state:%s pid:%d\n", container:state(), container:init_pid())
 
130
    assert(container:init_pid() > 1)
 
131
    assert(container:running() == true)
 
132
    assert(container:state() == "RUNNING")
 
133
    now_running = lxc.containers_running(true)
 
134
    assert(now_running[optarg["n"]] ~= nil)
 
135
    log(1, "%-20s %s", "Running, init pid:", container:init_pid())
 
136
end
 
137
 
 
138
function test_container_stopped()
 
139
    local now_running
 
140
    assert(container:init_pid() == -1)
 
141
    assert(container:running() == false)
 
142
    assert(container:state() == "STOPPED")
 
143
    now_running = lxc.containers_running(true)
 
144
    assert(now_running[optarg["n"]] == nil)
 
145
end
 
146
 
 
147
function test_container_frozen()
 
148
    local now_running
 
149
    assert(container:init_pid() > 1)
 
150
    assert(container:running() == true)
 
151
    assert(container:state() == "FROZEN")
 
152
    now_running = lxc.containers_running(true)
 
153
    assert(now_running[optarg["n"]] ~= nil)
 
154
end
 
155
 
 
156
function test_container_start()
 
157
    log(0, "Starting...")
 
158
    if (not container:start()) then
 
159
        log(1, "Start returned failure, waiting another 10 seconds...")
 
160
        container:wait("RUNNING", 10)
 
161
    end
 
162
    container:wait("RUNNING", 1)
 
163
end
 
164
 
 
165
function test_container_stop()
 
166
    log(0, "Stopping...")
 
167
    if (not container:stop()) then
 
168
        log(1, "Stop returned failure, waiting another 10 seconds...")
 
169
        container:wait("STOPPED", 10)
 
170
    end
 
171
    container:wait("STOPPED", 1)
 
172
end
 
173
 
 
174
function test_container_freeze()
 
175
    log(0, "Freezing...")
 
176
    if (not container:freeze()) then
 
177
        log(1, "Freeze returned failure, waiting another 10 seconds...")
 
178
        container:wait("FROZEN", 10)
 
179
    end
 
180
end
 
181
 
 
182
function test_container_unfreeze()
 
183
    log(0, "Unfreezing...")
 
184
    if (not container:unfreeze()) then
 
185
        log(1, "Unfreeze returned failure, waiting another 10 seconds...")
 
186
        container:wait("RUNNING", 10)
 
187
    end
 
188
end
 
189
 
 
190
function test_container_shutdown()
 
191
    log(0, "Shutting down...")
 
192
    container:shutdown(5)
 
193
 
 
194
    if (container:running()) then
 
195
        test_container_stop()
 
196
    end
 
197
end
 
198
 
 
199
function test_container_in_cfglist(should_find)
 
200
    local cfg_containers = lxc.containers_configured()
 
201
 
 
202
    if (should_find) then
 
203
        assert(cfg_containers[container:name()] ~= nil)
 
204
    else
 
205
        assert(cfg_containers[container:name()] == nil)
 
206
    end
 
207
end
 
208
 
 
209
function test_config_items()
 
210
    log(0, "Test set/clear configuration items...")
 
211
 
 
212
    -- test setting a 'single type' item
 
213
    assert(container:get_config_item("lxc.utsname") == optarg["n"])
 
214
    container:set_config_item("lxc.utsname", "foobar")
 
215
    assert(container:get_config_item("lxc.utsname") == "foobar")
 
216
    container:set_config_item("lxc.utsname", optarg["n"])
 
217
    assert(container:get_config_item("lxc.utsname") == optarg["n"])
 
218
 
 
219
    -- test clearing/setting a 'list type' item
 
220
    container:clear_config_item("lxc.cap.drop")
 
221
    container:set_config_item("lxc.cap.drop", "new_cap1")
 
222
    container:set_config_item("lxc.cap.drop", "new_cap2")
 
223
    local cap_drop = container:get_config_item("lxc.cap.drop")
 
224
    assert(cap_drop["new_cap1"] ~= nil)
 
225
    assert(cap_drop["new_cap2"] ~= nil)
 
226
    -- note: clear_config_item only works on list type items
 
227
    container:clear_config_item("lxc.cap.drop")
 
228
    assert(container:get_config_item("lxc.cap.drop") == nil)
 
229
 
 
230
    local altname = "/tmp/" .. optarg["n"] .. ".altconfig"
 
231
    log(0, "Test saving to an alternate (%s) config file...", altname)
 
232
    assert(container:save_config(altname))
 
233
    assert(os.remove(altname))
 
234
end
 
235
 
 
236
function test_config_mount_entries()
 
237
    local mntents
 
238
 
 
239
    -- mount entries are a list type item
 
240
    mntents = container:get_config_item("lxc.mount.entry")
 
241
    log(0, "Mount entries:")
 
242
    for _,v in ipairs(mntents) do
 
243
        log(0, "  %s", v)
 
244
    end
 
245
end
 
246
 
 
247
function test_config_keys()
 
248
    local keys
 
249
 
 
250
    keys = container:get_keys()
 
251
    log(0, "Top level keys:")
 
252
    for k,v in pairs(keys) do
 
253
        log(0, "  %s = %s", k, v or "")
 
254
    end
 
255
end
 
256
 
 
257
function test_config_network(net_nr)
 
258
    log(0, "Test network %d config...", net_nr)
 
259
    local netcfg
 
260
 
 
261
    netcfg = container:get_keys("lxc.network." .. net_nr)
 
262
    if (netcfg == nil) then
 
263
        return
 
264
    end
 
265
    for k,v in pairs(netcfg) do
 
266
        log(0, "  %s = %s", k, v or "")
 
267
    end
 
268
    assert(netcfg["flags"] == "up")
 
269
    assert(container:get_config_item("lxc.network."..net_nr..".type") == "veth")
 
270
end
 
271
 
 
272
 
 
273
function usage()
 
274
    die("Usage: apitest <options>\n" ..
 
275
        "  -v|--verbose        increase verbosity with each -v\n" ..
 
276
        "  -h|--help           print help message\n" ..
 
277
        "  -n|--name           name of container to use for testing\n" ..
 
278
        "  -c|--create         create the test container anew\n" ..
 
279
        "  -l|--login          do interactive login test\n" ..
 
280
        "  -t|--template       template to use when creating test container\n"
 
281
    )
 
282
end
 
283
 
 
284
local long_opts = {
 
285
    verbose       = "v",
 
286
    help          = "h",
 
287
    name          = "n",
 
288
    create        = "c",
 
289
    template      = "t",
 
290
}
 
291
 
 
292
optarg,optind = alt_getopt.get_opts (arg, "hvn:ct:", long_opts)
 
293
optarg["v"] = tonumber(optarg["v"]) or 0
 
294
optarg["n"] = optarg["n"] or "lua-apitest"
 
295
optarg["c"] = optarg["c"] or nil
 
296
optarg["t"] = optarg["t"] or "busybox"
 
297
if (optarg["h"] ~= nil) then
 
298
    usage()
 
299
end
 
300
 
 
301
test_global_info()
 
302
test_container_new()
 
303
test_container_create()
 
304
test_container_stopped()
 
305
test_container_in_cfglist(true)
 
306
test_container_config_path()
 
307
 
 
308
test_config_items()
 
309
test_config_keys()
 
310
test_config_mount_entries()
 
311
test_config_network(0)
 
312
 
 
313
test_container_start()
 
314
test_container_started()
 
315
 
 
316
test_container_freeze()
 
317
test_container_frozen()
 
318
test_container_unfreeze()
 
319
test_container_started()
 
320
 
 
321
test_container_shutdown()
 
322
test_container_stopped()
 
323
container:destroy()
 
324
test_container_in_cfglist(false)
 
325
 
 
326
log(0, "All tests passed")