]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
add [gs]et_cgroup_item to lua api
authorDwight Engen <dwight.engen@gmail.com>
Thu, 31 Oct 2013 20:38:22 +0000 (16:38 -0400)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Mon, 4 Nov 2013 12:37:08 +0000 (06:37 -0600)
fix up api test to run and add test for new [gs]et_cgroup_item

Signed-off-by: Dwight Engen <dwight.engen@oracle.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
src/lua-lxc/core.c
src/lua-lxc/lxc.lua
src/lua-lxc/test/apitest.lua

index 002e8bf6666bed247f2dae05000a27dbc6ed87c7..ea19cc3550768025317166f129e305af3ffa0025 100644 (file)
@@ -282,6 +282,29 @@ static int container_clear_config_item(lua_State *L)
     return 1;
 }
 
+static int container_get_cgroup_item(lua_State *L)
+{
+    struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
+    const char *key = luaL_checkstring(L, 2);
+    int len;
+    char *value;
+
+    len = c->get_cgroup_item(c, key, NULL, 0);
+    if (len <= 0)
+       goto not_found;
+
+    value = alloca(sizeof(char)*len + 1);
+    if (c->get_cgroup_item(c, key, value, len + 1) != len)
+       goto not_found;
+
+    lua_pushstring(L, value);
+    return 1;
+
+not_found:
+    lua_pushnil(L);
+    return 1;
+}
+
 static int container_get_config_item(lua_State *L)
 {
     struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
@@ -305,6 +328,16 @@ not_found:
     return 1;
 }
 
+static int container_set_cgroup_item(lua_State *L)
+{
+    struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
+    const char *key = luaL_checkstring(L, 2);
+    const char *value = luaL_checkstring(L, 3);
+
+    lua_pushboolean(L, !!c->set_cgroup_item(c, key, value));
+    return 1;
+}
+
 static int container_set_config_item(lua_State *L)
 {
     struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
@@ -361,6 +394,8 @@ static luaL_Reg lxc_container_methods[] =
     {"config_file_name",       container_config_file_name},
     {"load_config",            container_load_config},
     {"save_config",            container_save_config},
+    {"get_cgroup_item",                container_get_cgroup_item},
+    {"set_cgroup_item",                container_set_cgroup_item},
     {"get_config_path",                container_get_config_path},
     {"set_config_path",                container_set_config_path},
     {"get_config_item",                container_get_config_item},
index aa80a95c99df752c596d9f487fa74f40854b3242..7c9580e01708074b47c05102ef17d620faf8eb1d 100755 (executable)
@@ -189,6 +189,10 @@ function container:clear_config_item(key)
     return self.core:clear_config_item(key)
 end
 
+function container:get_cgroup_item(key)
+    return self.core:get_cgroup_item(key)
+end
+
 function container:get_config_item(key)
     local value
     local vals = {}
@@ -209,6 +213,10 @@ function container:get_config_item(key)
     return vals
 end
 
+function container:set_cgroup_item(key, value)
+    return self.core:set_cgroup_item(key, value)
+end
+
 function container:set_config_item(key, value)
     return self.core:set_config_item(key, value)
 end
@@ -410,6 +418,14 @@ function M.containers_running(names_only)
     return containers
 end
 
+function M.version_get()
+    return core.version_get()
+end
+
+function M.default_config_path_get()
+    return core.default_config_path_get()
+end
+
 lxc_path = core.default_config_path_get()
 cgroup_path = cgroup_path_get()
 
index 1365f9100582a6eb5c845a1fe038872c4b7b00da..f957ca4a11325bc45cb48ca508b9cbcd543b650d 100755 (executable)
@@ -206,6 +206,17 @@ function test_container_in_cfglist(should_find)
     end
 end
 
+function test_container_cgroup()
+    log(0, "Test get/set cgroup items...")
+
+    max_mem = container:get_cgroup_item("memory.max_usage_in_bytes")
+    saved_limit = container:get_cgroup_item("memory.limit_in_bytes")
+    assert(saved_limit ~= max_mem)
+    assert(container:set_cgroup_item("memory.limit_in_bytes", max_mem))
+    assert(container:get_cgroup_item("memory.limit_in_bytes") ~= saved_limit)
+    assert(container:set_cgroup_item("memory.limit_in_bytes", "-1"))
+end
+
 function test_config_items()
     log(0, "Test set/clear configuration items...")
 
@@ -313,6 +324,8 @@ test_config_network(0)
 test_container_start()
 test_container_started()
 
+test_container_cgroup()
+
 test_container_freeze()
 test_container_frozen()
 test_container_unfreeze()