]>
Commit | Line | Data |
---|---|---|
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 |
27 | import lxc |
28 | import uuid | |
cd063f45 SG |
29 | import os |
30 | import subprocess | |
e0de36d7 | 31 | import sys |
9c83a661 | 32 | import time |
e0de36d7 | 33 | |
e0de36d7 SG |
34 | # Let's pick a random name, avoiding clashes |
35 | CONTAINER_NAME = str(uuid.uuid1()) | |
36 | CLONE_NAME = str(uuid.uuid1()) | |
cd063f45 | 37 | RENAME_NAME = str(uuid.uuid1()) |
e0de36d7 SG |
38 | |
39 | ## Instantiate the container instance | |
40 | print("Getting instance for '%s'" % CONTAINER_NAME) | |
41 | container = lxc.Container(CONTAINER_NAME) | |
42 | ||
43 | # A few basic checks of the current state | |
44 | assert(container.config_file_name == "%s/%s/config" % | |
1cb4260d | 45 | (lxc.default_config_path, CONTAINER_NAME)) |
bde18539 | 46 | assert(not container.defined) |
e0de36d7 SG |
47 | assert(container.init_pid == -1) |
48 | assert(container.name == CONTAINER_NAME) | |
bde18539 | 49 | assert(not container.running) |
e0de36d7 SG |
50 | assert(container.state == "STOPPED") |
51 | ||
cd063f45 SG |
52 | # Try to get the host architecture for dpkg systems |
53 | arch = "i386" | |
54 | try: | |
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() | |
62 | except: | |
63 | pass | |
64 | ||
e0de36d7 | 65 | ## Create a rootfs |
cd063f45 SG |
66 | print("Creating rootfs using 'download', arch=%s" % arch) |
67 | container.create("download", 0, | |
68 | {"dist": "ubuntu", | |
e1701068 | 69 | "release": "xenial", |
cd063f45 | 70 | "arch": arch}) |
e0de36d7 | 71 | |
bde18539 | 72 | assert(container.defined) |
e0de36d7 | 73 | assert(container.name == CONTAINER_NAME |
bde18539 | 74 | == container.get_config_item("lxc.utsname")) |
e0de36d7 SG |
75 | assert(container.name in lxc.list_containers()) |
76 | ||
77 | ## Test the config | |
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() | |
84 | ||
85 | # A few basic checks of the current state | |
86 | assert(isinstance(capdrop, list)) | |
87 | assert(capdrop == container.get_config_item("lxc.cap.drop")) | |
88 | ||
89 | ## Test the networking | |
90 | print("Testing the networking") | |
e0de36d7 SG |
91 | |
92 | # A few basic checks of the current state | |
7fa3f2e9 | 93 | assert("name" in container.get_keys("lxc.net.0")) |
e0de36d7 | 94 | assert(len(container.network) == 1) |
e0de36d7 SG |
95 | |
96 | ## Starting the container | |
97 | print("Starting the container") | |
98 | container.start() | |
99 | container.wait("RUNNING", 3) | |
100 | ||
101 | # A few basic checks of the current state | |
102 | assert(container.init_pid > 1) | |
bde18539 | 103 | assert(container.running) |
e0de36d7 SG |
104 | assert(container.state == "RUNNING") |
105 | ||
799f29ab ÇO |
106 | |
107 | ## Checking IP address | |
108 | print("Getting the interface names") | |
e6c6d622 | 109 | assert(set(container.get_interfaces()) == set(('lo', 'eth0'))) |
799f29ab | 110 | |
e0de36d7 SG |
111 | ## Checking IP address |
112 | print("Getting the IP addresses") | |
9c83a661 SG |
113 | |
114 | count = 0 | |
115 | ips = [] | |
116 | while not ips or count == 10: | |
117 | ips = container.get_ips() | |
118 | time.sleep(1) | |
119 | count += 1 | |
cd063f45 SG |
120 | |
121 | if os.geteuid(): | |
122 | container.attach_wait(lxc.attach_run_command, ["ifconfig", "eth0"], | |
123 | namespaces=(lxc.CLONE_NEWUSER + lxc.CLONE_NEWNET | |
124 | + lxc.CLONE_NEWUTS)) | |
125 | else: | |
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 | |
130 | assert(len(ips) > 0) | |
131 | ||
cd063f45 SG |
132 | ## Test running config |
133 | assert(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 |
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) | |
143 | ||
e0de36d7 SG |
144 | ## Freezing the container |
145 | print("Freezing the container") | |
146 | container.freeze() | |
147 | container.wait("FROZEN", 3) | |
148 | ||
149 | # A few basic checks of the current state | |
150 | assert(container.init_pid > 1) | |
bde18539 | 151 | assert(container.running) |
e0de36d7 SG |
152 | assert(container.state == "FROZEN") |
153 | ||
154 | ## Unfreezing the container | |
155 | print("Unfreezing the container") | |
156 | container.unfreeze() | |
157 | container.wait("RUNNING", 3) | |
158 | ||
159 | # A few basic checks of the current state | |
160 | assert(container.init_pid > 1) | |
bde18539 | 161 | assert(container.running) |
e0de36d7 SG |
162 | assert(container.state == "RUNNING") |
163 | ||
164 | if 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 | |
170 | print("Shutting down the container") | |
0464b881 SG |
171 | if not container.shutdown(3): |
172 | container.stop() | |
e0de36d7 SG |
173 | |
174 | if 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 | |
180 | assert(container.init_pid == -1) | |
bde18539 | 181 | assert(not container.running) |
e0de36d7 SG |
182 | assert(container.state == "STOPPED") |
183 | ||
4e31246a SG |
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) | |
191 | ||
e0de36d7 | 192 | ## Cloning the container |
cd063f45 SG |
193 | print("Cloning the container as '%s'" % CLONE_NAME) |
194 | clone = container.clone(CLONE_NAME) | |
195 | assert(clone is not False) | |
196 | ||
197 | print ("Renaming the clone to '%s'" % RENAME_NAME) | |
198 | rename = clone.rename(RENAME_NAME) | |
199 | rename.start() | |
200 | rename.stop() | |
201 | rename.destroy() | |
e0de36d7 SG |
202 | |
203 | ## Destroy the container | |
204 | print("Destroying the container") | |
205 | container.destroy() | |
206 | ||
bde18539 | 207 | assert(not container.defined) |