blob: f68fa3ac498fd8e7953bec2e3287fa736b895fa4 [file] [log] [blame]
Adam Israel5e08a0e2018-09-06 19:22:47 -04001"""
2This test exercises LXD, to make sure that we can:
31. Create a container profile
42. Launch a container with a profile
53. Stop a container
64. Destroy a container
75. Delete a container profile
8
9"""
10import logging
11# import os
12import pytest
13from . import base
14import subprocess
15import shlex
16import tempfile
17
18
19@pytest.mark.asyncio
20async def test_lxd():
21
22 container = base.create_lxd_container(name="test-lxd")
23 assert container is not None
24
25 # Get the hostname of the container
26 hostname = container.name
27
28 # Delete the container
29 base.destroy_lxd_container(container)
30
31 # Verify the container is deleted
32 client = base.get_lxd_client()
33 assert client.containers.exists(hostname) is False
34
35
36@pytest.mark.asyncio
37async def test_lxd_ssh():
38
39 with tempfile.TemporaryDirectory() as tmp:
40 try:
41 # Create a temporary keypair
42 cmd = shlex.split(
43 "ssh-keygen -t rsa -b 4096 -N '' -f {}/id_lxd_rsa".format(
44 tmp,
45 )
46 )
47 subprocess.check_call(cmd)
48 except subprocess.CalledProcessError as e:
49 logging.debug(e)
50 assert False
51
52 # Slurp the public key
53 public_key = None
54 with open("{}/id_lxd_rsa.pub".format(tmp), "r") as f:
55 public_key = f.read()
56
57 assert public_key is not None
58
59 # Create the container with the keypair injected via profile
60 container = base.create_lxd_container(
61 public_key=public_key,
62 name="test-lxd"
63 )
64 assert container is not None
65
66 # Get the hostname of the container
67 hostname = container.name
68
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']
73
74 # Verify we can SSH into container
75 try:
76 cmd = shlex.split(
77 "ssh -i {}/id_lxd_rsa {} root@{} hostname".format(
78 tmp,
79 "-oStrictHostKeyChecking=no",
80 ipaddr,
81 )
82 )
83 subprocess.check_call(cmd)
84 except subprocess.CalledProcessError as e:
85 logging.debug(e)
86 assert False
87
88 # Delete the container
89 base.destroy_lxd_container(container)
90
91 # Verify the container is deleted
92 client = base.get_lxd_client()
93 assert client.containers.exists(hostname) is False
94
95 # Verify the container profile is deleted
96 assert client.profiles.exists(hostname) is False