Added description to package
[osm/N2VC.git] / tests / test_lxd.py
1 """
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
5 3. Stop a container
6 4. Destroy a container
7 5. Delete a container profile
8
9 """
10 import logging
11 # import os
12 import pytest
13 from . import base
14 import subprocess
15 import shlex
16 import tempfile
17
18
19 @pytest.mark.asyncio
20 async 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
37 async 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