def test_configuration_unchanged_master_password(self): config = { 'databases': 16, 'logfile': '/path/to/logfile', 'loglevel': 'debug', 'password': '******', 'port': 42, 'tcp-keepalive': 0, 'timeout': 0, } callback = serviceutils.write_config_file(config) with self.patch_all() as mocks: callback('foo') mocks.write.assert_called_once_with( { 'bind': '1.2.3.4', 'databases': 16, 'logfile': '/path/to/logfile', 'loglevel': 'debug', 'port': 42, 'requirepass': '******', 'tcp-keepalive': 0, 'timeout': 0, }, settings.REDIS_CONF) mocks.unit_get.assert_called_once_with('private-address') self.assertFalse(mocks.service_restart.called)
def test_configuration_unchanged_slave_password(self): config = { 'databases': 15, 'logfile': '/path/to/logs', 'loglevel': 'info', 'password': '', 'port': 4242, 'tcp-keepalive': 0, 'timeout': 0, } slave_relation = make_relation({ 'hostname': '4.3.2.1', 'password': '******', 'port': 90, }) callback = serviceutils.write_config_file( config, slave_relation=slave_relation) with self.patch_all() as mocks: callback('foo') mocks.write.assert_called_once_with( { 'bind': '1.2.3.4', 'databases': 15, 'logfile': '/path/to/logs', 'loglevel': 'info', 'masterauth': 'sercret!', 'port': 4242, 'slaveof': '4.3.2.1 90', 'tcp-keepalive': 0, 'timeout': 0, }, settings.REDIS_CONF) mocks.unit_get.assert_called_once_with('private-address') self.assertFalse(mocks.service_restart.called)
def test_configuration_unchanged_master(self): config = { 'databases': 3, 'logfile': '/path/to/logfile', 'loglevel': 'debug', 'password': '', 'port': 4242, 'tcp-keepalive': 60, 'timeout': 10, } callback = serviceutils.write_config_file(config) with self.patch_all() as mocks: callback('foo') mocks.write.assert_called_once_with( { 'bind': '1.2.3.4', 'databases': 3, 'logfile': '/path/to/logfile', 'loglevel': 'debug', 'port': 4242, 'tcp-keepalive': 60, 'timeout': 10, }, settings.REDIS_CONF) mocks.unit_get.assert_called_once_with('private-address') self.assertFalse(mocks.service_restart.called) self.assertEqual(3, mocks.log.call_count) mocks.log.assert_has_calls([ mock.call('Retrieving service options.'), mock.call('Writing configuration file for foo.'), mock.call('No changes detected in the configuration file.') ])
def test_configuration_changed_relations(self): config = { 'databases': 16, 'logfile': '/path/to/logfile', 'loglevel': 'debug', 'password': '******', 'port': 4242, 'tcp-keepalive': 0, 'timeout': 0, } data = { 'hostname': '4.3.2.1', 'port': 90, } db_relation = make_relation(data) callback = serviceutils.write_config_file(config, db_relation=db_relation) with self.patch_all(configuration_changed=True) as mocks: callback('foo') mocks.write.assert_called_once_with( { 'bind': '1.2.3.4', 'databases': 16, 'logfile': '/path/to/logfile', 'loglevel': 'debug', 'port': 4242, 'requirepass': '******', 'tcp-keepalive': 0, 'timeout': 0, }, settings.REDIS_CONF) mocks.unit_get.assert_called_once_with('private-address') mocks.service_restart.assert_called_once_with(settings.SERVICE_NAME) mocks.relation_ids.assert_called_once_with('testing') mocks.relation_set.assert_called_once_with('rel-id', data)
def test_configuration_changed(self): config = { 'databases': 16, 'logfile': '/path/to/logfile', 'loglevel': 'debug', 'password': '', 'port': 4242, 'tcp-keepalive': 0, 'timeout': 0, } callback = serviceutils.write_config_file(config) with self.patch_all(configuration_changed=True) as mocks: callback('foo') mocks.write.assert_called_once_with( { 'bind': '1.2.3.4', 'databases': 16, 'logfile': '/path/to/logfile', 'loglevel': 'debug', 'port': 4242, 'tcp-keepalive': 0, 'timeout': 0, }, settings.REDIS_CONF) mocks.unit_get.assert_called_once_with('private-address') mocks.service_restart.assert_called_once_with(settings.SERVICE_NAME) self.assertEqual(3, mocks.log.call_count) mocks.log.assert_has_calls([ mock.call('Retrieving service options.'), mock.call('Writing configuration file for foo.'), mock.call('Restarting service due to configuration change.') ])
def test_configuration_unchanged_slave(self): config = { 'databases': 10, 'logfile': '/path/to/logs', 'loglevel': 'info', 'password': '******', 'port': 4242, 'tcp-keepalive': 0, 'timeout': 0, } slave_relation = make_relation({ 'hostname': '4.3.2.1', 'port': 4747, }) callback = serviceutils.write_config_file( config, slave_relation=slave_relation) with self.patch_all() as mocks: callback('foo') mocks.write.assert_called_once_with( { 'bind': '1.2.3.4', 'databases': 10, 'logfile': '/path/to/logs', 'loglevel': 'info', 'port': 4242, 'slaveof': '4.3.2.1 4747', 'tcp-keepalive': 0, 'timeout': 0, }, settings.REDIS_CONF) mocks.unit_get.assert_called_once_with('private-address') self.assertFalse(mocks.service_restart.called) self.assertEqual(4, mocks.log.call_count) mocks.log.assert_has_calls([ mock.call('Retrieving service options.'), mock.call('Setting up slave relation.'), mock.call('Writing configuration file for foo.'), mock.call('No changes detected in the configuration file.') ])
def manage(): """Set up the service manager for redis.""" config = hookenv.config() service_start = functools.partial(serviceutils.service_start, config['port'], config.previous('port')) service_stop = functools.partial(serviceutils.service_stop, config['port']) # Handle relations. db_relation = relations.DbRelation() master_relation = relations.MasterRelation() slave_relation = relations.SlaveRelation() slave_relation_ready = slave_relation.is_ready() # Set up the service manager. manager = base.ServiceManager([ { # The name of the redis master service. 'service': 'redis-master', # Ports to open when the service starts. 'ports': [config['port']], # Context managers for provided relations. 'provided_data': [db_relation, master_relation], # Data (contexts) required to start the service. 'required_data': [config, not slave_relation_ready], # Callables called when required data is ready. 'data_ready': [ serviceutils.write_config_file( config, db_relation=db_relation, master_relation=master_relation), ], # Callables called when it is time to start the service. 'start': [service_start], # Callables called when it is time to stop the service. 'stop': [service_stop], }, { # The name of the redis slave service. 'service': 'redis-slave', # Ports to open when the service starts. 'ports': [config['port']], # Context managers for provided relations. 'provided_data': [db_relation], # Data (contexts) required to start the service. 'required_data': [config, slave_relation_ready], # Callables called when required data is ready. 'data_ready': [ serviceutils.write_config_file(config, db_relation=db_relation, slave_relation=slave_relation), ], # Callables called when it is time to start the service. 'start': [service_start], # Callables called when it is time to stop the service. 'stop': [service_stop], } ]) manager.manage()