]>
git.proxmox.com Git - mirror_lxc.git/blob - src/python-lxc/examples/api_test.py
1934a238c5c00ab8de4f68cef35a3ff2b37d09e4
2 # -*- coding: utf-8 -*-
4 # api_test.py: Test/demo of the python3-lxc API
6 # (C) Copyright Canonical Ltd. 2012
9 # Stéphane Graber <stgraber@ubuntu.com>
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.
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.
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
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
34 # Let's pick a random name, avoiding clashes
35 CONTAINER_NAME
= str(uuid
.uuid1())
36 CLONE_NAME
= str(uuid
.uuid1())
37 RENAME_NAME
= str(uuid
.uuid1())
39 ## Instantiate the container instance
40 print("Getting instance for '%s'" % CONTAINER_NAME
)
41 container
= lxc
.Container(CONTAINER_NAME
)
43 # A few basic checks of the current state
44 assert(container
.config_file_name
== "%s/%s/config" %
45 (lxc
.default_config_path
, CONTAINER_NAME
))
46 assert(not container
.defined
)
47 assert(container
.init_pid
== -1)
48 assert(container
.name
== CONTAINER_NAME
)
49 assert(not container
.running
)
50 assert(container
.state
== "STOPPED")
52 # Try to get the host architecture for dpkg systems
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)
61 arch
= dpkg
.stdout
.read().strip()
66 print("Creating rootfs using 'download', arch=%s" % arch
)
67 container
.create("download", 0,
72 assert(container
.defined
)
73 assert(container
.name
== CONTAINER_NAME
74 == container
.get_config_item("lxc.uts.name"))
75 assert(container
.name
in lxc
.list_containers())
78 print("Testing the configuration")
79 capdrop
= container
.get_config_item("lxc.cap.drop")
80 container
.clear_config_item("lxc.cap.drop")
81 container
.set_config_item("lxc.cap.drop", capdrop
[:-1])
82 container
.append_config_item("lxc.cap.drop", capdrop
[-1])
83 container
.save_config()
85 # A few basic checks of the current state
86 assert(isinstance(capdrop
, list))
87 assert(capdrop
== container
.get_config_item("lxc.cap.drop"))
89 ## Test the networking
90 print("Testing the networking")
92 # A few basic checks of the current state
93 assert("name" in container
.get_keys("lxc.net.0"))
94 assert(len(container
.network
) == 1)
96 ## Starting the container
97 print("Starting the container")
99 container
.wait("RUNNING", 3)
101 # A few basic checks of the current state
102 assert(container
.init_pid
> 1)
103 assert(container
.running
)
104 assert(container
.state
== "RUNNING")
107 ## Checking IP address
108 print("Getting the interface names")
109 assert(set(container
.get_interfaces()) == set(('lo', 'eth0')))
111 ## Checking IP address
112 print("Getting the IP addresses")
116 while not ips
or count
== 10:
117 ips
= container
.get_ips()
122 container
.attach_wait(lxc
.attach_run_command
, ["ifconfig", "eth0"],
123 namespaces
=(lxc
.CLONE_NEWUSER
+ lxc
.CLONE_NEWNET
126 container
.attach_wait(lxc
.attach_run_command
, ["ifconfig", "eth0"],
127 namespaces
=(lxc
.CLONE_NEWNET
+ lxc
.CLONE_NEWUTS
))
129 # A few basic checks of the current state
132 ## Test running config
133 assert(container
.name
== CONTAINER_NAME
134 == container
.get_config_item("lxc.uts.name")
135 == container
.get_running_config_item("lxc.uts.name"))
137 ## Testing cgroups a bit
138 print("Testing cgroup API")
139 max_mem
= container
.get_cgroup_item("memory.max_usage_in_bytes")
140 current_limit
= container
.get_cgroup_item("memory.limit_in_bytes")
141 assert(container
.set_cgroup_item("memory.limit_in_bytes", max_mem
))
142 assert(container
.get_cgroup_item("memory.limit_in_bytes") != current_limit
)
144 ## Freezing the container
145 print("Freezing the container")
147 container
.wait("FROZEN", 3)
149 # A few basic checks of the current state
150 assert(container
.init_pid
> 1)
151 assert(container
.running
)
152 assert(container
.state
== "FROZEN")
154 ## Unfreezing the container
155 print("Unfreezing the container")
157 container
.wait("RUNNING", 3)
159 # A few basic checks of the current state
160 assert(container
.init_pid
> 1)
161 assert(container
.running
)
162 assert(container
.state
== "RUNNING")
164 if len(sys
.argv
) > 1 and sys
.argv
[1] == "--with-console":
166 print("Attaching to tty1")
167 container
.console(tty
=1)
169 ## Shutting down the container
170 print("Shutting down the container")
171 if not container
.shutdown(3):
174 if container
.running
:
175 print("Stopping the container")
177 container
.wait("STOPPED", 3)
179 # A few basic checks of the current state
180 assert(container
.init_pid
== -1)
181 assert(not container
.running
)
182 assert(container
.state
== "STOPPED")
184 ## Snapshotting the container
185 print("Snapshotting the container")
186 assert(not container
.snapshot_list())
187 assert(container
.snapshot() == "snap0")
188 assert(len(container
.snapshot_list()) == 1)
189 assert(container
.snapshot_restore("snap0") is True)
190 assert(container
.snapshot_destroy("snap0") is True)
192 ## Cloning the container
193 print("Cloning the container as '%s'" % CLONE_NAME
)
194 clone
= container
.clone(CLONE_NAME
)
195 assert(clone
is not False)
197 print ("Renaming the clone to '%s'" % RENAME_NAME
)
198 rename
= clone
.rename(RENAME_NAME
)
203 ## Destroy the container
204 print("Destroying the container")
207 assert(not container
.defined
)