3
# This Amulet test deploys haproxy and related charms.
10
d = amulet.Deployment()
11
# Add the haproxy charm to the deployment.
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:
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)
27
# Relate the haproxy to apache2.
28
d.relate('haproxy:reverseproxy', 'apache2:website')
29
# Make the haproxy visible to the outside world.
32
# The number of seconds to wait for the environment to setup.
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)
46
# Test that haproxy is acting as the proxy for apache2.
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.')
56
# Test that the apache2 relation data is saved on the haproxy server.
58
# Get the sentry for apache and get the private IP address.
59
apache_unit = d.sentry.unit['apache2/0']
61
relation = apache_unit.relation('website', 'haproxy:reverseproxy')
62
# Get the private address from the relation.
63
apache_private = relation['private-address']
65
print('Private address of the apache2 relation ', apache_private)
67
# Grep the configuration file for the private address
68
output, code = haproxy_unit.run('grep %s /etc/haproxy/haproxy.cfg' %
71
print('Found the relation IP address in the haproxy configuration file!')
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)
79
# Send a message that the tests are complete.
80
print('The haproxy tests are complete.')