~dpb/charms/trusty/haproxy/merge-services-fix

« back to all changes in this revision

Viewing changes to tests/10_deploy_test.py

  • Committer: Marco Ceppi
  • Author(s): Matt Bruzek
  • Date: 2014-02-07 12:33:44 UTC
  • mfrom: (74.2.2 haproxy)
  • Revision ID: marco@ceppi.net-20140207123344-cs1rh9d5r02twdqz
Added amulet tests for haproxy charm.

R=
CC=
https://codereview.appspot.com/56140043

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
# This Amulet test deploys haproxy and related charms.
 
4
 
 
5
import os
 
6
import amulet
 
7
import requests
 
8
import base64
 
9
 
 
10
d = amulet.Deployment()
 
11
# Add the haproxy charm to the deployment.
 
12
d.add('haproxy')
 
13
d.add('apache2')
 
14
 
 
15
# Get the directory this way to load the file when CWD is different.
 
16
path = os.path.abspath(os.path.dirname(__file__))
 
17
template_path = os.path.join(path, 'default_apache.tmpl')
 
18
# Read in the Apache2 default template file.
 
19
with open(template_path) as f:
 
20
    template = f.read()
 
21
    encodedTemplate = base64.b64encode(template.encode('utf-8'))
 
22
# Create a dictionary with configuration values for apache2.
 
23
configuration = {'vhost_https_template': encodedTemplate.decode('ascii')}
 
24
# Apache2 needs a base64 encoded template to configure the web site.
 
25
d.configure('apache2', configuration)
 
26
 
 
27
# Relate the haproxy to apache2.
 
28
d.relate('haproxy:reverseproxy', 'apache2:website')
 
29
# Make the haproxy visible to the outside world.
 
30
d.expose('haproxy')
 
31
 
 
32
# The number of seconds to wait for the environment to setup.
 
33
seconds = 900
 
34
try:
 
35
    # Execute the deployer with the current mapping.
 
36
    d.setup(timeout=seconds)
 
37
    # Wait for the relation to finish the transations.
 
38
    d.sentry.wait(seconds)
 
39
except amulet.helpers.TimeoutError:
 
40
    message = 'The environment did not setup in %d seconds.' % seconds
 
41
    # The SKIP status enables skip or fail the test based on configuration.
 
42
    amulet.raise_status(amulet.SKIP, msg=message)
 
43
except:
 
44
    raise
 
45
 
 
46
# Test that haproxy is acting as the proxy for apache2.
 
47
 
 
48
# Get the haproxy unit.
 
49
haproxy_unit = d.sentry.unit['haproxy/0']
 
50
haproxy_address = haproxy_unit.info['public-address']
 
51
page = requests.get('http://%s/index.html' % haproxy_address)
 
52
# Raise an error if the page does not load through haproxy.
 
53
page.raise_for_status()
 
54
print('Successfully got the Apache2 web page through haproxy IP address.')
 
55
 
 
56
# Test that the apache2 relation data is saved on the haproxy server.
 
57
 
 
58
# Get the sentry for apache and get the private IP address.
 
59
apache_unit = d.sentry.unit['apache2/0']
 
60
# Get the relation.
 
61
relation = apache_unit.relation('website', 'haproxy:reverseproxy')
 
62
# Get the private address from the relation.
 
63
apache_private = relation['private-address']
 
64
 
 
65
print('Private address of the apache2 relation ', apache_private)
 
66
 
 
67
# Grep the configuration file for the private address
 
68
output, code = haproxy_unit.run('grep %s /etc/haproxy/haproxy.cfg' %
 
69
                                apache_private)
 
70
if code == 0:
 
71
    print('Found the relation IP address in the haproxy configuration file!')
 
72
    print(output)
 
73
else:
 
74
    print(output)
 
75
    message = 'Unable to find the Apache IP address %s in the haproxy ' \
 
76
              'configuration file.' % apache_private
 
77
    amulet.raise_status(amulet.FAIL, msg=message)
 
78
 
 
79
# Send a message that the tests are complete.
 
80
print('The haproxy tests are complete.')