]> git.proxmox.com Git - mirror_lxc.git/blame - src/python-lxc/examples/api_test.py
replace all lxc.network* with lxc.net*
[mirror_lxc.git] / src / python-lxc / examples / api_test.py
CommitLineData
e2f91e34 1#!/usr/bin/env python3
59e8ed76 2# -*- coding: utf-8 -*-
e0de36d7
SG
3#
4# api_test.py: Test/demo of the python3-lxc API
5#
6# (C) Copyright Canonical Ltd. 2012
7#
8# Authors:
9# Stéphane Graber <stgraber@ubuntu.com>
10#
11# This library is free software; you can redistribute it and/or
12# modify it under the terms of the GNU Lesser General Public
13# License as published by the Free Software Foundation; either
14# version 2.1 of the License, or (at your option) any later version.
15#
16# This library is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19# Lesser General Public License for more details.
20#
21# You should have received a copy of the GNU Lesser General Public
22# License along with this library; if not, write to the Free Software
2edd7a88
SG
23# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24# USA
e0de36d7
SG
25#
26
e0de36d7
SG
27import lxc
28import uuid
cd063f45
SG
29import os
30import subprocess
e0de36d7 31import sys
9c83a661 32import time
e0de36d7 33
e0de36d7
SG
34# Let's pick a random name, avoiding clashes
35CONTAINER_NAME = str(uuid.uuid1())
36CLONE_NAME = str(uuid.uuid1())
cd063f45 37RENAME_NAME = str(uuid.uuid1())
e0de36d7
SG
38
39## Instantiate the container instance
40print("Getting instance for '%s'" % CONTAINER_NAME)
41container = lxc.Container(CONTAINER_NAME)
42
43# A few basic checks of the current state
44assert(container.config_file_name == "%s/%s/config" %
1cb4260d 45 (lxc.default_config_path, CONTAINER_NAME))
bde18539 46assert(not container.defined)
e0de36d7
SG
47assert(container.init_pid == -1)
48assert(container.name == CONTAINER_NAME)
bde18539 49assert(not container.running)
e0de36d7
SG
50assert(container.state == "STOPPED")
51
cd063f45
SG
52# Try to get the host architecture for dpkg systems
53arch = "i386"
54try:
55 with open(os.path.devnull, "w") as devnull:
56 dpkg = subprocess.Popen(['dpkg', '--print-architecture'],
57 stderr=devnull, stdout=subprocess.PIPE,
58 universal_newlines=True)
59
60 if dpkg.wait() == 0:
61 arch = dpkg.stdout.read().strip()
62except:
63 pass
64
e0de36d7 65## Create a rootfs
cd063f45
SG
66print("Creating rootfs using 'download', arch=%s" % arch)
67container.create("download", 0,
68 {"dist": "ubuntu",
e1701068 69 "release": "xenial",
cd063f45 70 "arch": arch})
e0de36d7 71
bde18539 72assert(container.defined)
e0de36d7 73assert(container.name == CONTAINER_NAME
bde18539 74 == container.get_config_item("lxc.utsname"))
e0de36d7
SG
75assert(container.name in lxc.list_containers())
76
77## Test the config
78print("Testing the configuration")
79capdrop = container.get_config_item("lxc.cap.drop")
80container.clear_config_item("lxc.cap.drop")
81container.set_config_item("lxc.cap.drop", capdrop[:-1])
82container.append_config_item("lxc.cap.drop", capdrop[-1])
83container.save_config()
84
85# A few basic checks of the current state
86assert(isinstance(capdrop, list))
87assert(capdrop == container.get_config_item("lxc.cap.drop"))
88
89## Test the networking
90print("Testing the networking")
e0de36d7
SG
91
92# A few basic checks of the current state
7fa3f2e9 93assert("name" in container.get_keys("lxc.net.0"))
e0de36d7 94assert(len(container.network) == 1)
e0de36d7
SG
95
96## Starting the container
97print("Starting the container")
98container.start()
99container.wait("RUNNING", 3)
100
101# A few basic checks of the current state
102assert(container.init_pid > 1)
bde18539 103assert(container.running)
e0de36d7
SG
104assert(container.state == "RUNNING")
105
799f29ab
ÇO
106
107## Checking IP address
108print("Getting the interface names")
e6c6d622 109assert(set(container.get_interfaces()) == set(('lo', 'eth0')))
799f29ab 110
e0de36d7
SG
111## Checking IP address
112print("Getting the IP addresses")
9c83a661
SG
113
114count = 0
115ips = []
116while not ips or count == 10:
117 ips = container.get_ips()
118 time.sleep(1)
119 count += 1
cd063f45
SG
120
121if os.geteuid():
122 container.attach_wait(lxc.attach_run_command, ["ifconfig", "eth0"],
123 namespaces=(lxc.CLONE_NEWUSER + lxc.CLONE_NEWNET
124 + lxc.CLONE_NEWUTS))
125else:
126 container.attach_wait(lxc.attach_run_command, ["ifconfig", "eth0"],
127 namespaces=(lxc.CLONE_NEWNET + lxc.CLONE_NEWUTS))
e0de36d7
SG
128
129# A few basic checks of the current state
130assert(len(ips) > 0)
131
cd063f45
SG
132## Test running config
133assert(container.name == CONTAINER_NAME
134 == container.get_config_item("lxc.utsname")
135 == container.get_running_config_item("lxc.utsname"))
136
f4d3a9fd
SG
137## Testing cgroups a bit
138print("Testing cgroup API")
139max_mem = container.get_cgroup_item("memory.max_usage_in_bytes")
140current_limit = container.get_cgroup_item("memory.limit_in_bytes")
141assert(container.set_cgroup_item("memory.limit_in_bytes", max_mem))
142assert(container.get_cgroup_item("memory.limit_in_bytes") != current_limit)
143
e0de36d7
SG
144## Freezing the container
145print("Freezing the container")
146container.freeze()
147container.wait("FROZEN", 3)
148
149# A few basic checks of the current state
150assert(container.init_pid > 1)
bde18539 151assert(container.running)
e0de36d7
SG
152assert(container.state == "FROZEN")
153
154## Unfreezing the container
155print("Unfreezing the container")
156container.unfreeze()
157container.wait("RUNNING", 3)
158
159# A few basic checks of the current state
160assert(container.init_pid > 1)
bde18539 161assert(container.running)
e0de36d7
SG
162assert(container.state == "RUNNING")
163
164if len(sys.argv) > 1 and sys.argv[1] == "--with-console":
165 ## Attaching to tty1
166 print("Attaching to tty1")
167 container.console(tty=1)
168
169## Shutting down the container
170print("Shutting down the container")
0464b881
SG
171if not container.shutdown(3):
172 container.stop()
e0de36d7
SG
173
174if container.running:
175 print("Stopping the container")
176 container.stop()
177 container.wait("STOPPED", 3)
178
179# A few basic checks of the current state
180assert(container.init_pid == -1)
bde18539 181assert(not container.running)
e0de36d7
SG
182assert(container.state == "STOPPED")
183
4e31246a
SG
184## Snapshotting the container
185print("Snapshotting the container")
186assert(not container.snapshot_list())
187assert(container.snapshot() == "snap0")
188assert(len(container.snapshot_list()) == 1)
189assert(container.snapshot_restore("snap0") is True)
190assert(container.snapshot_destroy("snap0") is True)
191
e0de36d7 192## Cloning the container
cd063f45
SG
193print("Cloning the container as '%s'" % CLONE_NAME)
194clone = container.clone(CLONE_NAME)
195assert(clone is not False)
196
197print ("Renaming the clone to '%s'" % RENAME_NAME)
198rename = clone.rename(RENAME_NAME)
199rename.start()
200rename.stop()
201rename.destroy()
e0de36d7
SG
202
203## Destroy the container
204print("Destroying the container")
205container.destroy()
206
bde18539 207assert(not container.defined)