~mbruzek/charms/precise/haproxy/trunk

« back to all changes in this revision

Viewing changes to tests/10_deploy_test.py

  • Committer: matthew.bruzek at canonical
  • Date: 2014-01-23 19:57:25 UTC
  • Revision ID: matthew.bruzek@canonical.com-20140123195725-1502a3o23hbd0px1
Added amulet test for haproxy

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
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
 
7
 
 
8
import os
 
9
import amulet
 
10
import requests
 
11
import base64
 
12
 
 
13
units=2
 
14
 
 
15
d = amulet.Deployment()
 
16
# Add the haproxy charm to the deployment.
 
17
d.add('haproxy')
 
18
# Add two units of apache2 charm to the deployment.
 
19
#d.add('apache2', units=units)
 
20
d.add('apache2')
 
21
 
 
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:
 
27
    template = f.read()
 
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)
 
32
 
 
33
# Relate the haproxy to apache2.
 
34
d.relate('haproxy:reverseproxy', 'apache2:website')
 
35
# Make the haproxy visible to the outside world.
 
36
d.expose('haproxy')
 
37
 
 
38
# The number of seconds to wait for the environment to setup.
 
39
seconds = 900
 
40
try:
 
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)
 
49
except:
 
50
    raise
 
51
 
 
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.')
 
59
 
 
60
# Get the sentry for apache and get the private IP address 
 
61
apache_unit = d.sentry.unit['apache2/0']
 
62
# Get the relation.
 
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)
 
69
if code == 0:
 
70
    print("Found the relation private address in the haproxy configuration file!")
 
71
    print(output)
 
72
else:
 
73
    print(output)
 
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)
 
77
 
 
78
# Send a message that the tests are complete.
 
79
print('The haproxy tests are complete.')
 
80