3
-- test the lxc lua api
5
-- Copyright © 2012 Oracle.
8
-- Dwight Engen <dwight.engen@oracle.com>
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.
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.
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.
24
local lxc = require("lxc")
25
local lfs = require("lfs")
26
local getopt = require("alt_getopt")
28
local LXC_PATH = lxc.default_config_path_get()
31
local cfg_containers = {}
36
local function wrapper(...) io.write(string.format(...)) end
37
local status, result = pcall(wrapper, ...)
43
function log(level, ...)
44
if (optarg["v"] >= level) then
45
printf(os.date("%Y-%m-%d %T "))
56
function test_global_info()
60
log(0, "%-20s %s", "LXC version:", lxc.version_get())
61
log(0, "%-20s %s", "Container name:", optarg["n"])
63
log(0, "%-20s %s", "Creating container:", "yes")
64
log(0, "%-20s %s", "With template:", optarg["t"])
66
log(0, "%-20s %s", "Containers path:", LXC_PATH)
68
cfg_containers = lxc.containers_configured()
69
log(0, "%-20s", "Containers configured:")
70
for _,v in ipairs(cfg_containers) do
74
run_containers = lxc.containers_running(true)
75
log(0, "%-20s", "Containers running:")
76
for _,v in ipairs(run_containers) do
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"]))
87
function test_container_config_path()
89
local cfgpath = "/tmp/" .. optarg["n"]
90
local cfgname = cfgpath .. "/config"
92
log(0, "Test container config path...")
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)
105
assert(os.remove(cfgname))
106
assert(lfs.rmdir(cfgpath))
109
function test_container_create()
110
if (optarg["c"]) then
111
log(0, "%-20s %s", "Destroy existing container:", optarg["n"])
113
assert(container:defined() == false)
115
local cfg_containers = lxc.containers_configured()
116
if (cfg_containers[optarg["n"]]) then
117
log(0, "%-20s %s", "Use existing container:", optarg["n"])
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"])
127
function test_container_started()
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())
138
function test_container_stopped()
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)
147
function test_container_frozen()
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)
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)
162
container:wait("RUNNING", 1)
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)
171
container:wait("STOPPED", 1)
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)
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)
190
function test_container_shutdown()
191
log(0, "Shutting down...")
192
container:shutdown(5)
194
if (container:running()) then
195
test_container_stop()
199
function test_container_in_cfglist(should_find)
200
local cfg_containers = lxc.containers_configured()
202
if (should_find) then
203
assert(cfg_containers[container:name()] ~= nil)
205
assert(cfg_containers[container:name()] == nil)
209
function test_config_items()
210
log(0, "Test set/clear configuration items...")
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"])
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)
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))
236
function test_config_mount_entries()
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
247
function test_config_keys()
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 "")
257
function test_config_network(net_nr)
258
log(0, "Test network %d config...", net_nr)
261
netcfg = container:get_keys("lxc.network." .. net_nr)
262
if (netcfg == nil) then
265
for k,v in pairs(netcfg) do
266
log(0, " %s = %s", k, v or "")
268
assert(netcfg["flags"] == "up")
269
assert(container:get_config_item("lxc.network."..net_nr..".type") == "veth")
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"
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
303
test_container_create()
304
test_container_stopped()
305
test_container_in_cfglist(true)
306
test_container_config_path()
310
test_config_mount_entries()
311
test_config_network(0)
313
test_container_start()
314
test_container_started()
316
test_container_freeze()
317
test_container_frozen()
318
test_container_unfreeze()
319
test_container_started()
321
test_container_shutdown()
322
test_container_stopped()
324
test_container_in_cfglist(false)
326
log(0, "All tests passed")