1 # Copyright 2019 Canonical Ltd.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 This test exercises LXD, to make sure that we can:
17 1. Create a container profile
18 2. Launch a container with a profile
20 4. Destroy a container
21 5. Delete a container profile
36 container
= base
.create_lxd_container(name
="test-lxd")
37 assert container
is not None
39 # Get the hostname of the container
40 hostname
= container
.name
42 # Delete the container
43 base
.destroy_lxd_container(container
)
45 # Verify the container is deleted
46 client
= base
.get_lxd_client()
47 assert client
.containers
.exists(hostname
) is False
51 async def test_lxd_ssh():
53 with tempfile
.TemporaryDirectory() as tmp
:
55 # Create a temporary keypair
57 "ssh-keygen -t rsa -b 4096 -N '' -f {}/id_lxd_rsa".format(
61 subprocess
.check_call(cmd
)
62 except subprocess
.CalledProcessError
as e
:
66 # Slurp the public key
68 with
open("{}/id_lxd_rsa.pub".format(tmp
), "r") as f
:
71 assert public_key
is not None
73 # Create the container with the keypair injected via profile
74 container
= base
.create_lxd_container(
75 public_key
=public_key
,
78 assert container
is not None
80 # Get the hostname of the container
81 hostname
= container
.name
83 addresses
= container
.state().network
['eth0']['addresses']
84 # The interface may have more than one address, but we only need
85 # the first one for testing purposes.
86 ipaddr
= addresses
[0]['address']
88 # Verify we can SSH into container
91 "ssh -i {}/id_lxd_rsa {} root@{} hostname".format(
93 "-oStrictHostKeyChecking=no",
97 subprocess
.check_call(cmd
)
98 except subprocess
.CalledProcessError
as e
:
102 # Delete the container
103 base
.destroy_lxd_container(container
)
105 # Verify the container is deleted
106 client
= base
.get_lxd_client()
107 assert client
.containers
.exists(hostname
) is False
109 # Verify the container profile is deleted
110 assert client
.profiles
.exists(hostname
) is False