api_test.py.in revision 2495cc911b6600521fd2dc735edba15f6fbb9081
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen#!/usr/bin/python3
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# api_test.py: Test/demo of the python3-lxc API
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# (C) Copyright Canonical Ltd. 2012
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Authors:
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Stéphane Graber <stgraber@ubuntu.com>
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# This library is free software; you can redistribute it and/or
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# modify it under the terms of the GNU Lesser General Public
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# License as published by the Free Software Foundation; either
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainen# version 2.1 of the License, or (at your option) any later version.
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# This library is distributed in the hope that it will be useful,
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# but WITHOUT ANY WARRANTY; without even the implied warranty of
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Lesser General Public License for more details.
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#
ae46f6ba5bb9eee8900254d3042e89d490023be0Timo Sirainen# You should have received a copy of the GNU Lesser General Public
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# License along with this library; if not, write to the Free Software
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3f3ad16ff74d694796d22501250a9a29997c0729Timo Sirainen#
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenimport warnings
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenwarnings.filterwarnings("ignore", "The python-lxc API isn't yet stable")
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenimport lxc
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenimport uuid
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenimport sys
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Some constants
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo SirainenLXC_PATH_LIB = "@LXCPATH@"
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo SirainenLXC_TEMPLATE = "ubuntu"
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Let's pick a random name, avoiding clashes
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo SirainenCONTAINER_NAME = str(uuid.uuid1())
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo SirainenCLONE_NAME = str(uuid.uuid1())
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen## Instantiate the container instance
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenprint("Getting instance for '%s'" % CONTAINER_NAME)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainencontainer = lxc.Container(CONTAINER_NAME)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen# A few basic checks of the current state
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(container.config_file_name == "%s/%s/config" %
5801ce4da7d807ab85d02051ece5969e7175eebaTimo Sirainen (LXC_PATH_LIB, CONTAINER_NAME))
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenassert(not container.defined)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(container.init_pid == -1)
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainenassert(container.name == CONTAINER_NAME)
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainenassert(not container.running)
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainenassert(container.state == "STOPPED")
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainen
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainen## Create a rootfs
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenprint("Creating rootfs using '%s'" % LXC_TEMPLATE)
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainencontainer.create(LXC_TEMPLATE)
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainen
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainenassert(container.defined)
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainenassert(container.name == CONTAINER_NAME
5801ce4da7d807ab85d02051ece5969e7175eebaTimo Sirainen == container.get_config_item("lxc.utsname"))
5801ce4da7d807ab85d02051ece5969e7175eebaTimo Sirainenassert(container.name in lxc.list_containers())
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainen## Test the config
5801ce4da7d807ab85d02051ece5969e7175eebaTimo Sirainenprint("Testing the configuration")
db693bf6fcae96d834567f1782257517b7207655Timo Sirainencapdrop = container.get_config_item("lxc.cap.drop")
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainencontainer.clear_config_item("lxc.cap.drop")
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainencontainer.set_config_item("lxc.cap.drop", capdrop[:-1])
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainencontainer.append_config_item("lxc.cap.drop", capdrop[-1])
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainencontainer.save_config()
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# A few basic checks of the current state
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(isinstance(capdrop, list))
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(capdrop == container.get_config_item("lxc.cap.drop"))
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen## Test the networking
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenprint("Testing the networking")
db693bf6fcae96d834567f1782257517b7207655Timo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# A few basic checks of the current state
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenassert("name" in container.get_keys("lxc.network.0"))
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenassert(len(container.network) == 1)
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenassert(container.network[0].hwaddr.startswith("00:16:3e"))
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen## Starting the container
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenprint("Starting the container")
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainencontainer.start()
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainencontainer.wait("RUNNING", 3)
fb35b9f2c80954da842c20d5128b5e506835d93eTimo Sirainen
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen# A few basic checks of the current state
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenassert(container.init_pid > 1)
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenassert(container.running)
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenassert(container.state == "RUNNING")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen## Checking IP address
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenprint("Getting the IP addresses")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenips = container.get_ips(timeout=10)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainencontainer.attach("NETWORK|UTSNAME", "/sbin/ifconfig", "eth0")
fa87f0ef3636fa52ce42513533ee500c0bf29910Timo Sirainen
fa87f0ef3636fa52ce42513533ee500c0bf29910Timo Sirainen# A few basic checks of the current state
fa87f0ef3636fa52ce42513533ee500c0bf29910Timo Sirainenassert(len(ips) > 0)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen## Freezing the container
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenprint("Freezing the container")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainencontainer.freeze()
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainencontainer.wait("FROZEN", 3)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# A few basic checks of the current state
fa87f0ef3636fa52ce42513533ee500c0bf29910Timo Sirainenassert(container.init_pid > 1)
fa87f0ef3636fa52ce42513533ee500c0bf29910Timo Sirainenassert(container.running)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(container.state == "FROZEN")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen## Unfreezing the container
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenprint("Unfreezing the container")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainencontainer.unfreeze()
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainencontainer.wait("RUNNING", 3)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# A few basic checks of the current state
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(container.init_pid > 1)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(container.running)
242abe3ad2423776e9cf05e1304eb8fda4831b23Timo Sirainenassert(container.state == "RUNNING")
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenif len(sys.argv) > 1 and sys.argv[1] == "--with-console":
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen ## Attaching to tty1
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen print("Attaching to tty1")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen container.console(tty=1)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen## Shutting down the container
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenprint("Shutting down the container")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainencontainer.shutdown(3)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenif container.running:
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen print("Stopping the container")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen container.stop()
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen container.wait("STOPPED", 3)
7ee226c2a66aa4dce7f13e8b17687db285c981bdTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# A few basic checks of the current state
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(container.init_pid == -1)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(not container.running)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(container.state == "STOPPED")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
7ee226c2a66aa4dce7f13e8b17687db285c981bdTimo Sirainen## Cloning the container
7ee226c2a66aa4dce7f13e8b17687db285c981bdTimo Sirainenprint("Cloning the container")
7ee226c2a66aa4dce7f13e8b17687db285c981bdTimo Sirainenclone = lxc.Container(CLONE_NAME)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenclone.clone(container)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenclone.start()
fa87f0ef3636fa52ce42513533ee500c0bf29910Timo Sirainenclone.stop()
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenclone.destroy()
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen## Destroy the container
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenprint("Destroying the container")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainencontainer.destroy()
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(not container.defined)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen