Skip to content

Commit 2e45889

Browse files
hyperbolic2346Adam Stokes
authored and
Adam Stokes
committed
Fixing easyrsa test
Changing easyrsa test to no longer scrape the disk since the client cert isn't left on disk anymore. It looks at the relation data now. We could do this for the ca certificate, but we don't have the ca key in the relation data, so I'm leaving the ca test for now.
1 parent 2cfbffe commit 2e45889

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed

jobs/integration/charm/test_easyrsa.py

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
import pytest
44
import os
5+
import json
56
from pathlib import Path
67
from ..base import _juju_wait
78
from ..utils import asyncify
@@ -12,6 +13,8 @@
1213
# Locally built charm layer path
1314
CHARM_PATH = os.getenv('CHARM_PATH')
1415

16+
relation_data = None
17+
1518

1619
async def easyrsa_resource():
1720
URL = ('https://github.com/OpenVPN/easy-rsa/releases/download/'
@@ -30,6 +33,36 @@ async def deploy_easyrsa(controller, model):
3033
await asyncify(_juju_wait)(controller, model)
3134

3235

36+
async def deploy_test_app(controller, model):
37+
test_app = "etcd"
38+
39+
await asyncify(juju)(
40+
'deploy', '-m', '{}:{}'.format(controller, model),
41+
test_app)
42+
await asyncify(juju)(
43+
'relate', '-m', '{}:{}'.format(controller, model),
44+
test_app, 'easyrsa')
45+
await asyncify(_juju_wait)(controller, model)
46+
47+
48+
async def get_relation_data(controller, model):
49+
'''Gets data from the relation specified'''
50+
global relation_data
51+
52+
if relation_data is None:
53+
await deploy_easyrsa(controller, model.info.name)
54+
await deploy_test_app(controller, model.info.name)
55+
easyrsa = model.applications['easyrsa']
56+
easyrsa = easyrsa.units[0]
57+
58+
id = await easyrsa.run('relation-ids client') # magic :(
59+
id = id.results['Stdout'].strip()
60+
raw_json = await easyrsa.run('relation-get --format=json -r {} - {}'
61+
.format(id, easyrsa.name))
62+
relation_data = json.loads(raw_json.results['Stdout'])
63+
return relation_data
64+
65+
3366
async def test_easyrsa_installed(deploy, event_loop):
3467
'''Test that EasyRSA software is installed.'''
3568
controller, model = deploy
@@ -90,29 +123,27 @@ async def test_ca(deploy, event_loop):
90123

91124

92125
async def test_client(deploy, event_loop):
93-
'''Test that the client certificate and key were created.'''
126+
'''Test that the client certificate and key can be created.'''
94127
controller, model = deploy
95-
await deploy_easyrsa(controller, model.info.name)
96-
easyrsa = model.applications['easyrsa']
97-
easyrsa = easyrsa.units[0]
98-
charm_dir = Path('/var/lib/juju/agents/{tag}/charm'.format(
99-
tag=easyrsa.tag))
100-
easyrsa_dir = os.path.join(charm_dir, 'EasyRSA')
101-
# Create an absolute path to the client certificate.
102-
cert_path = os.path.join(easyrsa_dir, 'pki/issued/client.crt')
103-
client_cert = await asyncify(juju)(
104-
'ssh', '-m', '{}:{}'.format(controller, model.info.name),
105-
easyrsa.name,
106-
'sudo cat {}'.format(str(cert_path)))
107-
client_cert = client_cert.stdout.decode().strip()
108-
assert validate_certificate(client_cert)
109-
key_path = os.path.join(easyrsa_dir, 'pki/private/client.key')
110-
client_key = await asyncify(juju)(
111-
'ssh', '-m', '{}:{}'.format(controller, model.info.name),
112-
easyrsa.name,
113-
'sudo cat {}'.format(str(key_path)))
114-
client_key = client_key.stdout.decode().strip()
115-
assert validate_key(client_key)
128+
relation_data = await get_relation_data(controller, model)
129+
assert validate_certificate(relation_data['client.cert'])
130+
assert validate_key(relation_data['client.key'])
131+
132+
133+
async def test_server(deploy, event_loop):
134+
'''Test that the server certificate and key can be created.'''
135+
controller, model = deploy
136+
relation_data = await get_relation_data(controller, model)
137+
# find server certs and keys
138+
server_certs = {key: data for key, data in relation_data.items()
139+
if '.server.cert' in key}
140+
server_keys = {key: data for key, data in relation_data.items()
141+
if '.server.key' in key}
142+
143+
for _, cert in server_certs.items():
144+
assert validate_certificate(cert)
145+
for _, key in server_keys.items():
146+
assert validate_key(key)
116147

117148

118149
def validate_certificate(cert):

0 commit comments

Comments
 (0)