~paulgear/charms/trusty/quassel-core/trunk

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
#!/usr/bin/env python3

import amulet
import unittest


class TestDeployment(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        try:
            cls.d = amulet.Deployment(series='trusty')
            cls.d.add('quassel-core')
            cls.d.setup(timeout=1800)
            cls.d.sentry.wait()

            cls.u = cls.d.sentry.unit['quassel-core/0']
        except amulet.helpers.TimeoutError:
            amulet.raise_status(amulet.SKIP, msg="Environment wasn't stood up in time")
        except:
            raise

    # amulet.raise_status():
    #   - amulet.PASS
    #   - amulet.FAIL
    #   - amulet.SKIP
    # Each unit has the following methods:
    #   - .info - An array of the information of that unit from Juju
    #   - .file(PATH) - Get the details of a file on that unit
    #   - .file_contents(PATH) - Get plain text output of PATH file from that unit
    #   - .directory(PATH) - Get details of directory
    #   - .directory_contents(PATH) - List files and folders in PATH on that unit
    #   - .relation(relation, service:rel) - Get relation data from return service
    #          add tests here to confirm service is up and working properly
    #   - .run(something)
    # For example, to confirm that it has a functioning HTTP server:
    #     page = requests.get('http://{}'.format(self.unit.info['public-address']))
    #     page.raise_for_status()
    # More information on writing Amulet tests can be found at:
    #     https://juju.ubuntu.com/docs/tools-amulet.html

    def check_file_content(self, name, find):
        """Check that the named file exists and contains the find text."""
        stat = TestDeployment.u.file(name)
        if stat is None:
            amulet.raise_status(amulet.FAIL,
                    msg="Could not retrieve status of " + name)
        content = TestDeployment.u.file_contents(name)
        if not content.find(find):
            amulet.raise_status(amulet.FAIL,
                    msg=name + " does not contain the required text (" + find + ")")
        return content

    def test_defaults(self):
        """Check that /etc/default/quasselcore exists and contains a LOGLEVEL setting"""
        self.check_file_content(name='/etc/default/quasselcore', find='LOGLEVEL=')

    def test_logrotate(self):
        """Check that /etc/logrotate.d/quassel-core exists and contains
        /var/log/quassel/core.log"""
        self.check_file_content(name='/etc/logrotate.d/quassel-core',
            find='/var/log/quassel/core.log')

    def test_key_certificate(self):
        name = '/var/lib/quassel/quasselCert.pem'
        content = self.check_file_content(name, find='-----END PRIVATE KEY-----')

        # The file must also contain a certificate
        if not content.find('-----END CERTIFICATE-----'):
            amulet.raise_status(amulet.FAIL, msg="Certificate was not created correctly in " +
                name)

    def test_running_quassel(self):
        (output, exit) = TestDeployment.u.run('ps aux|grep -q "[q]uasselcore"')
        if exit != 0 or not output.find("quasselcore"):
            amulet.raise_status(amulet.FAIL, msg="quasselcore did not start correctly")


if __name__ == '__main__':
    unittest.main()