api_test.py.in revision 2495cc911b6600521fd2dc735edba15f6fbb9081
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# api_test.py: Test/demo of the python3-lxc API
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# (C) Copyright Canonical Ltd. 2012
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Stéphane Graber <stgraber@ubuntu.com>
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# 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.
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
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenwarnings.filterwarnings("ignore", "The python-lxc API isn't yet stable")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Some constants
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Let's pick a random name, avoiding clashes
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen## Instantiate the container instance
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenprint("Getting instance for '%s'" % CONTAINER_NAME)
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen# A few basic checks of the current state
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(container.config_file_name == "%s/%s/config" %
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainen## Create a rootfs
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenprint("Creating rootfs using '%s'" % LXC_TEMPLATE)
5801ce4da7d807ab85d02051ece5969e7175eebaTimo Sirainenassert(container.name in lxc.list_containers())
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainen## Test the config
db693bf6fcae96d834567f1782257517b7207655Timo Sirainencapdrop = container.get_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])
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# A few basic checks of the current state
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenassert(capdrop == container.get_config_item("lxc.cap.drop"))
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen## Test the networking
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# A few basic checks of the current state
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenassert("name" in container.get_keys("lxc.network.0"))
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenassert(container.network[0].hwaddr.startswith("00:16:3e"))
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen## Starting the container
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen# A few basic checks of the current state
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen## Checking IP address
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainencontainer.attach("NETWORK|UTSNAME", "/sbin/ifconfig", "eth0")
fa87f0ef3636fa52ce42513533ee500c0bf29910Timo Sirainen# A few basic checks of the current state
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen## Freezing the container
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# A few basic checks of the current state
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen## Unfreezing the container
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# A few basic checks of the current state
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenif len(sys.argv) > 1 and sys.argv[1] == "--with-console":
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen ## Attaching to tty1
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen## Shutting down the container
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# A few basic checks of the current state
7ee226c2a66aa4dce7f13e8b17687db285c981bdTimo Sirainen## Cloning the container
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen## Destroy the container