3
# This is an amulet test to test the haproxy functionality.
4
# Test to see if a web page comes through haproxy.
5
# Test to see if the haproxy charm relates to an apache2 charm.
6
# Test to see if multiple instances IP addresses are in the config file for haproxy
15
d = amulet.Deployment()
16
# Add the haproxy charm to the deployment.
18
# Add two units of apache2 charm to the deployment.
19
#d.add('apache2', units=units)
22
# Get the directory this way to load the file when CWD is different.
23
path = os.path.abspath(os.path.dirname(__file__))
24
template_path = os.path.join(path, 'default_apache.tmpl')
25
# Read in the Apache2 default template file.
26
with open(template_path) as f:
28
encodedTemplate = base64.b64encode(template.encode('ascii'))
29
configuration = {'vhost_https_template' : encodedTemplate.decode('ascii')}
30
# Apache2 needs a base64 encoded template to configure the web site.
31
d.configure('apache2', configuration)
33
# Relate the haproxy to apache2.
34
d.relate('haproxy:reverseproxy', 'apache2:website')
35
# Make the haproxy visible to the outside world.
38
# The number of seconds to wait for the environment to setup.
41
# Execute the deployer with the current mapping.
42
d.setup(timeout=seconds)
43
# Wait for the relation to finish the transations.
44
d.sentry.wait(seconds)
45
except amulet.helpers.TimeoutError:
46
message = 'The environment did not setup in %d seconds.', seconds
47
# The SKIP status enables skip or fail the test based on configuration.
48
amulet.raise_status(amulet.SKIP, msg=message)
52
# Get the haproxy unit.
53
haproxy_unit = d.sentry.unit['haproxy/0']
54
haproxy_address = haproxy_unit.info['public-address']
55
page = requests.get('http://%s/index.html' % haproxy_address)
56
# Raise an error if the page does not load through haproxy.
57
page.raise_for_status()
58
print('Successfully got the Apache2 web page through haproxy IP address.')
60
# Get the sentry for apache and get the private IP address
61
apache_unit = d.sentry.unit['apache2/0']
63
relation = apache_unit.relation('website', 'haproxy:reverseproxy')
64
# Get the private address from the relation.
65
print('Private address of the apache2 relation ', relation['private-address'])
66
apache_private = relation['private-address']
67
# Grep the configuration file for the private address
68
output, code = haproxy_unit.run('grep %s /etc/haproxy/haproxy.cfg' % apache_private)
70
print("Found the relation private address in the haproxy configuration file!")
74
message = "Unable to find the Apache IP address %s in the haproxy " \
75
"configuration file." % apache_private
76
amulet.raise_status(amulet.FAIL, msg=message)
78
# Send a message that the tests are complete.
79
print('The haproxy tests are complete.')