2 This test exercises LXD, to make sure that we can:
3 1. Create a container profile
4 2. Launch a container with a profile
7 5. Delete a container profile
22 container
= base
.create_lxd_container(name
="test-lxd")
23 assert container
is not None
25 # Get the hostname of the container
26 hostname
= container
.name
28 # Delete the container
29 base
.destroy_lxd_container(container
)
31 # Verify the container is deleted
32 client
= base
.get_lxd_client()
33 assert client
.containers
.exists(hostname
) is False
37 async def test_lxd_ssh():
39 with tempfile
.TemporaryDirectory() as tmp
:
41 # Create a temporary keypair
43 "ssh-keygen -t rsa -b 4096 -N '' -f {}/id_lxd_rsa".format(
47 subprocess
.check_call(cmd
)
48 except subprocess
.CalledProcessError
as e
:
52 # Slurp the public key
54 with
open("{}/id_lxd_rsa.pub".format(tmp
), "r") as f
:
57 assert public_key
is not None
59 # Create the container with the keypair injected via profile
60 container
= base
.create_lxd_container(
61 public_key
=public_key
,
64 assert container
is not None
66 # Get the hostname of the container
67 hostname
= container
.name
69 addresses
= container
.state().network
['eth0']['addresses']
70 # The interface may have more than one address, but we only need
71 # the first one for testing purposes.
72 ipaddr
= addresses
[0]['address']
74 # Verify we can SSH into container
77 "ssh -i {}/id_lxd_rsa {} root@{} hostname".format(
79 "-oStrictHostKeyChecking=no",
83 subprocess
.check_call(cmd
)
84 except subprocess
.CalledProcessError
as e
:
88 # Delete the container
89 base
.destroy_lxd_container(container
)
91 # Verify the container is deleted
92 client
= base
.get_lxd_client()
93 assert client
.containers
.exists(hostname
) is False
95 # Verify the container profile is deleted
96 assert client
.profiles
.exists(hostname
) is False