~le-charmers/charms/trusty/rabbitmq-server/leadership-election

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python3

# This Amulet test performs a basic deploy and checks if rabbitmq is running.

import amulet
import os
import socket
import ssl
from deploy_common import CA

# The number of seconds to wait for the environment to setup.
seconds = 900
# Get the directory in this way to load the files from the tests directory.
path = os.path.abspath(os.path.dirname(__file__))

ca = CA()

# Create a dictionary for the rabbitmq configuration.
rabbitmq_configuration = {
    'ssl_enabled': True,
    'ssl_key': ca.get_key(),
    'ssl_cert': ca.get_cert(),
    'ssl_port': 5671
}

d = amulet.Deployment(series='trusty')
# Add the rabbitmq-server charm to the deployment.
d.add('rabbitmq-server')
# Configure options on the rabbitmq-server.
d.configure('rabbitmq-server', rabbitmq_configuration)
# Expose the server so we can connect.
d.expose('rabbitmq-server')

try:
    # Execute the deployer with the current mapping.
    d.setup(timeout=seconds)
except amulet.helpers.TimeoutError:
    message = 'The environment did not setup in %d seconds.' % seconds
    # The SKIP status enables skip or fail the test based on configuration.
    amulet.raise_status(amulet.SKIP, msg=message)
except:
    raise
print('The rabbitmq-server has been successfully deployed.')

###############################################################################
# Verify that the rabbit service is running on the deployed server.
###############################################################################
rabbitmq_sentry = d.sentry.unit['rabbitmq-server/0']
# Get the public address for rabbitmq-server instance.
server_address = rabbitmq_sentry.info['public-address']
# Create the command that checks if the rabbitmq-server service is running.
command = 'rabbitmqctl status'
print(command)
# Execute the command on the deployed service.
output, code = rabbitmq_sentry.run(command)
print(output)
# Check the return code for the success and failure of this test.
if (code != 0):
    message = 'The ' + command + ' did not return the expected code of 0.'
    amulet.raise_status(amulet.FAIL, msg=message)
else:
    print('The rabbitmq-server is running on %s' % server_address)

###############################################################################
# Test the ssl certificate.
###############################################################################
# Get the port for ssl_port instance.
server_port = rabbitmq_configuration['ssl_port']

print('Testing ssl connection to rabbitmq-server.')
try:
    # Create a normal socket.
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # Require a certificate from the server, since a self-signed certificate
    # was used, the ca_certs must be the server certificate file itself.
    ssl_sock = ssl.wrap_socket(s, ca_certs=ca.ca_cert_path(),
                               cert_reqs=ssl.CERT_REQUIRED)
    # Connect to the rabbitmq server using ssl.
    ssl_sock.connect((server_address, server_port))
    # Get the certificate.
    certificate = ssl_sock.getpeercert()
    # SSL socket connected and got the certificate, this passes the ssl test!
    print('Connected to the rabbitmq-server {0}:{1} using ssl!'.format(
          server_address, server_port))
except Exception as e:
    message = 'Failed to create an ssl connection to {0}:{1}\n{2}'.format(
              server_address, server_port, str(e))
    amulet.raise_status(amulet.FAIL, msg=message)
finally:
    ssl_sock.close()

print('The rabbitmq-server passed the basic deploy test!')