api_test.py revision 9c83a661397456e1455d739bcadfa38f05ce2fe6
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# api_test.py: Test/demo of the python3-lxc API
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# (C) Copyright Canonical Ltd. 2012
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# Stéphane Graber <stgraber@ubuntu.com>
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# This library is free software; you can redistribute it and/or
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# modify it under the terms of the GNU Lesser General Public
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# License as published by the Free Software Foundation; either
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# version 2.1 of the License, or (at your option) any later version.
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# This library is distributed in the hope that it will be useful,
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# but WITHOUT ANY WARRANTY; without even the implied warranty of
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# Lesser General Public License for more details.
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# You should have received a copy of the GNU Lesser General Public
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# License along with this library; if not, write to the Free Software
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinwarnings.filterwarnings("ignore", "The python-lxc API isn't yet stable")
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# Some constants
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# Let's pick a random name, avoiding clashes
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin## Instantiate the container instance
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinprint("Getting instance for '%s'" % CONTAINER_NAME)
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# A few basic checks of the current state
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinassert(container.config_file_name == "%s/%s/config" %
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin## Create a rootfs
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinprint("Creating rootfs using '%s'" % LXC_TEMPLATE)
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin == container.get_config_item("lxc.utsname"))
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinassert(container.name in lxc.list_containers())
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin## Test the config
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinprint("Testing the configuration")
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalincapdrop = container.get_config_item("lxc.cap.drop")
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalincontainer.set_config_item("lxc.cap.drop", capdrop[:-1])
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalincontainer.append_config_item("lxc.cap.drop", capdrop[-1])
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin# A few basic checks of the current state
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinassert(capdrop == container.get_config_item("lxc.cap.drop"))
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin## Test the networking
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinprint("Testing the networking")
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin# A few basic checks of the current state
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinassert("name" in container.get_keys("lxc.network.0"))
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinassert(container.network[0].hwaddr.startswith("00:16:3e"))
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin## Starting the container
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinprint("Starting the container")
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# A few basic checks of the current state
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin## Checking IP address
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinprint("Getting the IP addresses")
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalincontainer.attach("NETWORK|UTSNAME", "/sbin/ifconfig", "eth0")
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# A few basic checks of the current state
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin## Testing cgroups a bit
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinprint("Testing cgroup API")
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinmax_mem = container.get_cgroup_item("memory.max_usage_in_bytes")
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalincurrent_limit = container.get_cgroup_item("memory.limit_in_bytes")
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinassert(container.set_cgroup_item("memory.limit_in_bytes", max_mem))
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinassert(container.get_cgroup_item("memory.limit_in_bytes") != current_limit)
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin## Freezing the container
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinprint("Freezing the container")
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# A few basic checks of the current state
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin## Unfreezing the container
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinprint("Unfreezing the container")
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin# A few basic checks of the current state
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinif len(sys.argv) > 1 and sys.argv[1] == "--with-console":
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin ## Attaching to tty1
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin print("Attaching to tty1")
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin## Shutting down the container
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinprint("Shutting down the container")
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin print("Stopping the container")
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# A few basic checks of the current state
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin## Cloning the container
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinprint("Cloning the container")
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin## Destroy the container
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinprint("Destroying the container")