github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/trusty/haproxy/hooks/tests/test_statistics_hooks.py (about) 1 2 from testtools import TestCase 3 from mock import patch 4 from charmhelpers.core.hookenv import Config 5 6 import hooks 7 8 9 class StatisticsRelationTest(TestCase): 10 11 def setUp(self): 12 super(StatisticsRelationTest, self).setUp() 13 config = Config(**{"monitoring_port": 10001, 14 "monitoring_username": "mon_user", 15 "enable_monitoring": True}) 16 self.config_get = self.patch_hook("config_get") 17 self.config_get.return_value = config 18 # patch changed and save methods to do nothing 19 self.config_get().changed = lambda x: False 20 self.config_get().save = lambda: None 21 self.get_monitoring_password = \ 22 self.patch_hook("get_monitoring_password") 23 self.get_monitoring_password.return_value = "this-is-a-secret" 24 self.relation_set = self.patch_hook("relation_set") 25 self.get_relation_ids = self.patch_hook("get_relation_ids") 26 self.get_relation_ids.return_value = ['__stats-rel-id__'] 27 self.log = self.patch_hook("log") 28 29 def patch_hook(self, hook_name): 30 mock_controller = patch.object(hooks, hook_name) 31 mock = mock_controller.start() 32 self.addCleanup(mock_controller.stop) 33 return mock 34 35 def test_relation_joined(self): 36 hooks.statistics_interface() 37 self.get_relation_ids.assert_called_once_with('statistics') 38 self.relation_set.assert_called_once_with( 39 relation_id=self.get_relation_ids()[0], 40 enabled=True, 41 port=10001, 42 password="this-is-a-secret", 43 user="mon_user") 44 45 def test_relation_joined_monitoring_disabled(self): 46 self.config_get.return_value['enable_monitoring'] = False 47 hooks.statistics_interface() 48 self.get_relation_ids.assert_called_once_with('statistics') 49 self.relation_set.assert_called_once_with( 50 relation_id=self.get_relation_ids()[0], 51 enabled=False) 52 53 def test_called_on_config_change(self): 54 config_changed = self.patch_hook('config_changed') 55 update_nrpe_config = self.patch_hook('update_nrpe_config') 56 statistics_interface = self.patch_hook('statistics_interface') 57 hooks.main('config-changed') 58 config_changed.assert_called_once_with() 59 update_nrpe_config.assert_called_once_with() 60 statistics_interface.assert_called_once_with()