mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-07-14 12:47:41 +00:00
python3: Update apitest
This update will make it work unprivileged as well as testing a few of the new functions. Signed-off-by: Stéphane Graber <stgraber@ubuntu.com> Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
parent
c756a6e91b
commit
cd063f4524
@ -25,15 +25,15 @@
|
|||||||
|
|
||||||
import lxc
|
import lxc
|
||||||
import uuid
|
import uuid
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# Some constants
|
|
||||||
LXC_TEMPLATE = "ubuntu"
|
|
||||||
|
|
||||||
# Let's pick a random name, avoiding clashes
|
# Let's pick a random name, avoiding clashes
|
||||||
CONTAINER_NAME = str(uuid.uuid1())
|
CONTAINER_NAME = str(uuid.uuid1())
|
||||||
CLONE_NAME = str(uuid.uuid1())
|
CLONE_NAME = str(uuid.uuid1())
|
||||||
|
RENAME_NAME = str(uuid.uuid1())
|
||||||
|
|
||||||
## Instantiate the container instance
|
## Instantiate the container instance
|
||||||
print("Getting instance for '%s'" % CONTAINER_NAME)
|
print("Getting instance for '%s'" % CONTAINER_NAME)
|
||||||
@ -48,9 +48,25 @@ assert(container.name == CONTAINER_NAME)
|
|||||||
assert(not container.running)
|
assert(not container.running)
|
||||||
assert(container.state == "STOPPED")
|
assert(container.state == "STOPPED")
|
||||||
|
|
||||||
|
# Try to get the host architecture for dpkg systems
|
||||||
|
arch = "i386"
|
||||||
|
try:
|
||||||
|
with open(os.path.devnull, "w") as devnull:
|
||||||
|
dpkg = subprocess.Popen(['dpkg', '--print-architecture'],
|
||||||
|
stderr=devnull, stdout=subprocess.PIPE,
|
||||||
|
universal_newlines=True)
|
||||||
|
|
||||||
|
if dpkg.wait() == 0:
|
||||||
|
arch = dpkg.stdout.read().strip()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
## Create a rootfs
|
## Create a rootfs
|
||||||
print("Creating rootfs using '%s'" % LXC_TEMPLATE)
|
print("Creating rootfs using 'download', arch=%s" % arch)
|
||||||
container.create(LXC_TEMPLATE)
|
container.create("download", 0,
|
||||||
|
{"dist": "ubuntu",
|
||||||
|
"release": "trusty",
|
||||||
|
"arch": arch})
|
||||||
|
|
||||||
assert(container.defined)
|
assert(container.defined)
|
||||||
assert(container.name == CONTAINER_NAME
|
assert(container.name == CONTAINER_NAME
|
||||||
@ -101,12 +117,23 @@ while not ips or count == 10:
|
|||||||
ips = container.get_ips()
|
ips = container.get_ips()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
|
if os.geteuid():
|
||||||
|
container.attach_wait(lxc.attach_run_command, ["ifconfig", "eth0"],
|
||||||
|
namespaces=(lxc.CLONE_NEWUSER + lxc.CLONE_NEWNET
|
||||||
|
+ lxc.CLONE_NEWUTS))
|
||||||
|
else:
|
||||||
container.attach_wait(lxc.attach_run_command, ["ifconfig", "eth0"],
|
container.attach_wait(lxc.attach_run_command, ["ifconfig", "eth0"],
|
||||||
namespaces=(lxc.CLONE_NEWNET + lxc.CLONE_NEWUTS))
|
namespaces=(lxc.CLONE_NEWNET + lxc.CLONE_NEWUTS))
|
||||||
|
|
||||||
# A few basic checks of the current state
|
# A few basic checks of the current state
|
||||||
assert(len(ips) > 0)
|
assert(len(ips) > 0)
|
||||||
|
|
||||||
|
## Test running config
|
||||||
|
assert(container.name == CONTAINER_NAME
|
||||||
|
== container.get_config_item("lxc.utsname")
|
||||||
|
== container.get_running_config_item("lxc.utsname"))
|
||||||
|
|
||||||
## Testing cgroups a bit
|
## Testing cgroups a bit
|
||||||
print("Testing cgroup API")
|
print("Testing cgroup API")
|
||||||
max_mem = container.get_cgroup_item("memory.max_usage_in_bytes")
|
max_mem = container.get_cgroup_item("memory.max_usage_in_bytes")
|
||||||
@ -155,12 +182,15 @@ assert(not container.running)
|
|||||||
assert(container.state == "STOPPED")
|
assert(container.state == "STOPPED")
|
||||||
|
|
||||||
## Cloning the container
|
## Cloning the container
|
||||||
print("Cloning the container")
|
print("Cloning the container as '%s'" % CLONE_NAME)
|
||||||
clone = lxc.Container(CLONE_NAME)
|
clone = container.clone(CLONE_NAME)
|
||||||
clone.clone(container.name)
|
assert(clone is not False)
|
||||||
clone.start()
|
|
||||||
clone.stop()
|
print ("Renaming the clone to '%s'" % RENAME_NAME)
|
||||||
clone.destroy()
|
rename = clone.rename(RENAME_NAME)
|
||||||
|
rename.start()
|
||||||
|
rename.stop()
|
||||||
|
rename.destroy()
|
||||||
|
|
||||||
## Destroy the container
|
## Destroy the container
|
||||||
print("Destroying the container")
|
print("Destroying the container")
|
||||||
|
Loading…
Reference in New Issue
Block a user