~charmers/charms/precise/rabbitmq-server/trunk

« back to all changes in this revision

Viewing changes to tests/deprecated/30_configuration_test.py

  • Committer: James Page
  • Date: 2015-10-22 13:24:30 UTC
  • Revision ID: james.page@ubuntu.com-20151022132430-723cb00yprje2j1c
Tags: 15.10
15.10 Charm release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
# This Amulet test exercises the configuration options for rabbitmq-server.
 
4
 
 
5
import amulet
 
6
import os
 
7
import socket
 
8
import ssl
 
9
from deploy_common import CA
 
10
 
 
11
# The number of seconds to wait for the environment to setup.
 
12
seconds = 2700
 
13
# Get the directory in this way to load the files from the tests directory.
 
14
path = os.path.abspath(os.path.dirname(__file__))
 
15
 
 
16
ca = CA()
 
17
 
 
18
privateKey = ca.get_key()
 
19
certificate = ca.get_cert()
 
20
 
 
21
# Create a dictionary of all the configuration values.
 
22
rabbit_configuration = {
 
23
    'management_plugin': True,
 
24
    'ssl_enabled': True,
 
25
    'ssl_port': 5999,
 
26
    'ssl_key': privateKey,
 
27
    'ssl_cert': certificate,
 
28
}
 
29
 
 
30
d = amulet.Deployment(series='trusty')
 
31
# Add the rabbitmq-server charm to the deployment.
 
32
d.add('rabbitmq-server')
 
33
# Configure all the options on rabbitmq-server.
 
34
d.configure('rabbitmq-server', rabbit_configuration)
 
35
# Expose the rabbitmq-server.
 
36
d.expose('rabbitmq-server')
 
37
 
 
38
try:
 
39
    # Execute the deployer with the current mapping.
 
40
    d.setup(timeout=seconds)
 
41
    # Wait for the relation to finish the transations.
 
42
    d.sentry.wait(seconds)
 
43
except amulet.helpers.TimeoutError:
 
44
    message = 'The environment did not setup in %d seconds.' % seconds
 
45
    # The SKIP status enables skip or fail the test based on configuration.
 
46
    amulet.raise_status(amulet.SKIP, msg=message)
 
47
except:
 
48
    raise
 
49
 
 
50
rabbit_unit = d.sentry.unit['rabbitmq-server/0']
 
51
###############################################################################
 
52
# Verify that the rabbit service is running on the deployed server.
 
53
###############################################################################
 
54
# Create the command that checks if the rabbitmq-server service is running.
 
55
command = 'rabbitmqctl status'
 
56
print(command)
 
57
# Execute the command on the deployed service.
 
58
output, code = rabbit_unit.run(command)
 
59
print(output)
 
60
# Check the return code for the success and failure of this test.
 
61
if (code != 0):
 
62
    message = 'The ' + command + ' did not return the expected code of 0.'
 
63
    amulet.raise_status(amulet.FAIL, msg=message)
 
64
else:
 
65
    print('The rabbitmq-server is running.')
 
66
 
 
67
###############################################################################
 
68
# Verify the configuration values.
 
69
###############################################################################
 
70
# Get the contents of the private key from the rabbitmq-server
 
71
contents = rabbit_unit.file_contents('/etc/rabbitmq/rabbit-server-privkey.pem')
 
72
# Verify the private key was saved on the rabbitmq server correctly.
 
73
if contents != privateKey:
 
74
    message = 'The private keys did not match!'
 
75
    amulet.raise_status(amulet.FAIL, msg=message)
 
76
else:
 
77
    print('The private keys was configured properly on the rabbitmq server.')
 
78
 
 
79
# Get the contents of the certificate from the rabbitmq-server.
 
80
contents = rabbit_unit.file_contents('/etc/rabbitmq/rabbit-server-cert.pem')
 
81
# Verify the certificate was saved on the rabbitmq server correctly.
 
82
if contents != certificate:
 
83
    message = 'The certificates did not match!'
 
84
    amulet.raise_status(amulet.FAIL, msg=message)
 
85
else:
 
86
    print('The certificate was configured properly on the rabbitmq server.')
 
87
 
 
88
# Get the public address for rabbitmq-server instance.
 
89
rabbit_host = rabbit_unit.info['public-address']
 
90
 
 
91
###############################################################################
 
92
# Verify that SSL is set up on the non-default port.
 
93
###############################################################################
 
94
# Get the port for ssl_port instance.
 
95
ssl_port = rabbit_configuration['ssl_port']
 
96
 
 
97
try:
 
98
    # Create a normal socket.
 
99
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
100
    # Require a certificate from the server, since a self-signed certificate
 
101
    # was used, the ca_certs must be the server certificate file itself.
 
102
    ssl_sock = ssl.wrap_socket(s, ca_certs=ca.ca_cert_path(),
 
103
                               cert_reqs=ssl.CERT_REQUIRED)
 
104
    # Connect to the rabbitmq server using ssl.
 
105
    ssl_sock.connect((rabbit_host, ssl_port))
 
106
    # Get the certificate.
 
107
    certificate = ssl_sock.getpeercert()
 
108
    # SSL scoket connected and got the certificate, this passes the ssl test!
 
109
    print('Connected to the rabbitmq-server {0}:{1} using ssl!'.format(
 
110
          rabbit_host, ssl_port))
 
111
except Exception as e:
 
112
    message = 'Failed to create an ssl connection to {0}:{1}\n{2}'.format(
 
113
              rabbit_host, ssl_port, str(e))
 
114
    amulet.raise_status(amulet.FAIL, msg=message)
 
115
finally:
 
116
    ssl_sock.close()
 
117
 
 
118
print('The rabbitmq-server passed the configuration tests.')