~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

Viewing changes to tests/unittests/test_handler/test_handler_mcollective.py

  • Committer: Scott Moser
  • Date: 2016-07-19 18:46:14 UTC
  • Revision ID: smoser@ubuntu.com-20160719184614-tv2d9mvn1rbb3d2i
mcollective: add tests, cleanups and bug fix when no config in /etc.

Things here:
 - restart rather than 'start' the service, to pick up a config change
   that we would have written.
 - update the config and write cert files whether or not the file
   existed on the system.  Previously it would only write the cert
   files if /etc/mcollective/server.cfg already existed.
 - improve test coverage

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from cloudinit import (cloud, distros, helpers, util)
1
2
from cloudinit.config import cc_mcollective
2
 
from cloudinit import util
 
3
from cloudinit.sources import DataSourceNoCloud
3
4
 
4
 
from .. import helpers
 
5
from .. import helpers as t_help
5
6
 
6
7
import configobj
7
8
import logging
 
9
import os
8
10
import shutil
9
11
from six import BytesIO
10
12
import tempfile
12
14
LOG = logging.getLogger(__name__)
13
15
 
14
16
 
15
 
class TestConfig(helpers.FilesystemMockingTestCase):
 
17
STOCK_CONFIG = """\
 
18
main_collective = mcollective
 
19
collectives = mcollective
 
20
libdir = /usr/share/mcollective/plugins
 
21
logfile = /var/log/mcollective.log
 
22
loglevel = info
 
23
daemonize = 1
 
24
 
 
25
# Plugins
 
26
securityprovider = psk
 
27
plugin.psk = unset
 
28
 
 
29
connector = activemq
 
30
plugin.activemq.pool.size = 1
 
31
plugin.activemq.pool.1.host = stomp1
 
32
plugin.activemq.pool.1.port = 61613
 
33
plugin.activemq.pool.1.user = mcollective
 
34
plugin.activemq.pool.1.password = marionette
 
35
 
 
36
# Facts
 
37
factsource = yaml
 
38
plugin.yaml = /etc/mcollective/facts.yaml
 
39
"""
 
40
 
 
41
 
 
42
class TestConfig(t_help.FilesystemMockingTestCase):
16
43
    def setUp(self):
17
44
        super(TestConfig, self).setUp()
18
45
        self.tmp = tempfile.mkdtemp()
19
46
        self.addCleanup(shutil.rmtree, self.tmp)
 
47
        # "./": make os.path.join behave correctly with abs path as second arg
 
48
        self.server_cfg = os.path.join(
 
49
            self.tmp, "./" + cc_mcollective.SERVER_CFG)
 
50
        self.pubcert_file = os.path.join(
 
51
            self.tmp, "./" + cc_mcollective.PUBCERT_FILE)
 
52
        self.pricert_file= os.path.join(
 
53
            self.tmp, self.tmp, "./" + cc_mcollective.PRICERT_FILE)
20
54
 
21
55
    def test_basic_config(self):
22
56
        cfg = {
38
72
                },
39
73
            },
40
74
        }
 
75
        expected = cfg['mcollective']['conf']
 
76
 
41
77
        self.patchUtils(self.tmp)
42
78
        cc_mcollective.configure(cfg['mcollective']['conf'])
43
 
        contents = util.load_file("/etc/mcollective/server.cfg", decode=False)
 
79
        contents = util.load_file(cc_mcollective.SERVER_CFG, decode=False)
44
80
        contents = configobj.ConfigObj(BytesIO(contents))
45
 
        expected = {
46
 
            'loglevel': 'debug',
47
 
            'connector': 'rabbitmq',
48
 
            'logfile': '/var/log/mcollective.log',
49
 
            'ttl': '4294957',
50
 
            'collectives': 'mcollective',
51
 
            'main_collective': 'mcollective',
52
 
            'securityprovider': 'psk',
53
 
            'daemonize': '1',
54
 
            'factsource': 'yaml',
55
 
            'direct_addressing': '1',
56
 
            'plugin.psk': 'unset',
57
 
            'libdir': '/usr/share/mcollective/plugins',
58
 
            'identity': '1',
59
 
        }
60
81
        self.assertEqual(expected, dict(contents))
 
82
 
 
83
    def test_existing_config_is_saved(self):
 
84
        cfg = {'loglevel': 'warn'}
 
85
        util.write_file(self.server_cfg, STOCK_CONFIG)
 
86
        cc_mcollective.configure(config=cfg, server_cfg=self.server_cfg)
 
87
        self.assertTrue(os.path.exists(self.server_cfg))
 
88
        self.assertTrue(os.path.exists(self.server_cfg + ".old"))
 
89
        self.assertEqual(util.load_file(self.server_cfg + ".old"), STOCK_CONFIG)
 
90
 
 
91
    def test_existing_updated(self):
 
92
        cfg = {'loglevel': 'warn'}
 
93
        util.write_file(self.server_cfg, STOCK_CONFIG)
 
94
        cc_mcollective.configure(config=cfg, server_cfg=self.server_cfg)
 
95
        cfgobj = configobj.ConfigObj(self.server_cfg)
 
96
        self.assertEqual(cfg['loglevel'], cfgobj['loglevel'])
 
97
 
 
98
    def test_certificats_written(self):
 
99
        # check public-cert and private-cert keys in config get written
 
100
        cfg = {'loglevel': 'debug',
 
101
               'public-cert': "this is my public-certificate",
 
102
               'private-cert': "secret private certificate"}
 
103
 
 
104
        cc_mcollective.configure(config=cfg,
 
105
            server_cfg=self.server_cfg, pricert_file=self.pricert_file,
 
106
            pubcert_file=self.pubcert_file)
 
107
 
 
108
        found = configobj.ConfigObj(self.server_cfg)
 
109
 
 
110
        # make sure these didnt get written in
 
111
        self.assertFalse('public-cert' in found)
 
112
        self.assertFalse('private-cert' in found)
 
113
 
 
114
        # these need updating to the specified paths
 
115
        self.assertEqual(found['plugin.ssl_server_public'], self.pubcert_file)
 
116
        self.assertEqual(found['plugin.ssl_server_private'], self.pricert_file)
 
117
 
 
118
        # and the security provider should be ssl
 
119
        self.assertEqual(found['securityprovider'], 'ssl')
 
120
 
 
121
        self.assertEqual(
 
122
            util.load_file(self.pricert_file), cfg['private-cert'])
 
123
        self.assertEqual(
 
124
            util.load_file(self.pubcert_file), cfg['public-cert'])
 
125
 
 
126
 
 
127
class TestHandler(t_help.TestCase):
 
128
    def _get_cloud(self, distro):
 
129
        cls = distros.fetch(distro)
 
130
        paths = helpers.Paths({})
 
131
        d = cls(distro, {}, paths)
 
132
        ds = DataSourceNoCloud.DataSourceNoCloud({}, d, paths)
 
133
        cc = cloud.Cloud(ds, paths, {}, d, None)
 
134
        return cc
 
135
 
 
136
    @t_help.mock.patch("cloudinit.config.cc_mcollective.util")
 
137
    def test_mcollective_install(self, mock_util):
 
138
        cc = self._get_cloud('ubuntu')
 
139
        cc.distro = t_help.mock.MagicMock()
 
140
        mycfg = {'mcollective': {'conf': {'loglevel': 'debug'}}}
 
141
        cc_mcollective.handle('cc_mcollective', mycfg, cc, LOG, [])
 
142
        self.assertTrue(cc.distro.install_packages.called)
 
143
        install_pkg = cc.distro.install_packages.call_args_list[0][0][0]
 
144
        self.assertEqual(install_pkg, ('mcollective',))
 
145
 
 
146
        self.assertTrue(mock_util.subp.called)
 
147
        self.assertEqual(mock_util.subp.call_args_list[0][0][0],
 
148
                         ['service', 'mcollective', 'restart'])