def test_service_reload(self, monkeypatch): def mock_AnsibleRunner_constructor(obj, ansible_module_path, exec_path, **attr): assert attr == {"name": "collectd", "state": "reloaded"} return monkeypatch.setattr(AnsibleRunner, '__init__', mock_AnsibleRunner_constructor) def mock_runner_run(obj): result = { u'state': u'started', u'msg': u'', u'invocation': { u'module_args': { u'name': u'collectddd', u'enabled': None, u'daemon_reload': False, u'state': u'reloaded', u'user': False, u'masked': None } } } return result, "" monkeypatch.setattr(AnsibleRunner, 'run', mock_runner_run) service = Service("collectd", "/tmp/") message, success = service.reload() assert message == "" assert success
def test_restart(): init() service = Service("Test_service") with patch.object(ansible_module_runner, 'AnsibleRunner', ansible) as mock_ansible: with pytest.raises(ansible_module_runner.AnsibleModuleNotFound): ret = service.restart()
def test_service_reload(self, monkeypatch): def mock_AnsibleRunner_constructor(obj, asnible_module_path, **attr): assert attr == {"name": "collectd", "state": "reloaded"} return monkeypatch.setattr(AnsibleRunner, '__init__', mock_AnsibleRunner_constructor) def mock_runner_run(obj): result = { u'state': u'started', u'msg': u'', u'invocation': { u'module_args': { u'name': u'collectddd', u'enabled': None, u'daemon_reload': False, u'state': u'reloaded', u'user': False, u'masked': None } } } return result, "" monkeypatch.setattr(AnsibleRunner, 'run', mock_runner_run) service = Service("collectd") message, success = service.reload() assert message == "" assert success
def test_reload(): init() service = Service("Test_service") service.reload() with patch.object(ansible_module_runner.AnsibleRunner, 'run') as mock_run: mock_run.return_value = ansible_run(False) ret = service.reload() assert ret[1] is False
def test_reload(): init() service = Service("Test_service") service.reload() with patch.object(ansible_module_runner.AnsibleRunner, 'run') as mock_run: mock_run.return_value = ansible_run(False) ret = service.reload() assert ret[1] is False
def test_constructor(): service = Service("Test_service", "node_context", 1, True) assert service.publisher_id == "node_context" assert service.attributes["name"] == "Test_service" assert service.attributes["enabled"] is True init() service = Service("Test_service") assert service.node_id == 1
def run(self): self.parameters['Service.name'] = 'collectd' plugin_config_success = True graphite_host = (NS.config.data.get('graphite_host') or NS.config.data['etcd_connection']) graphite_port = (NS.config.data.get('graphite_port') or 2003) plugin_params = { "graphite_host": graphite_host, "graphite_port": graphite_port, "hostname": NS.node_context.fqdn, "integration_id": NS.tendrl_context.integration_id, "node_id": NS.node_context.node_id, "logging_socket_path": NS.config.data['logging_socket_path'], "interval": NS.config.data['sync_interval'], "interface": self.get_node_interface(NS.node_context.fqdn), "etcd_host": NS.config.data['etcd_connection'], "etcd_port": NS.config.data['etcd_port'] } etcd_ca_cert_file = NS.config.data.get("etcd_ca_cert_file") etcd_cert_file = NS.config.data.get("etcd_cert_file") etcd_key_file = NS.config.data.get("etcd_key_file") if etcd_ca_cert_file and str(etcd_ca_cert_file) != "" \ and etcd_cert_file and str(etcd_cert_file) != "" \ and etcd_key_file and str(etcd_key_file) != "": plugin_params.update({ "etcd_ca_cert_file": NS.config.data['etcd_ca_cert_file'], "etcd_cert_file": NS.config.data['etcd_cert_file'], "etcd_key_file": NS.config.data['etcd_key_file'] }) for node_plugin in NODE_PLUGINS: plugin_config_success &= self._configure_plugin( node_plugin, plugin_params) if NS.tendrl_context.sds_name == 'gluster': plugin_params['is_provisioner_node'] = False if "provisioner/%s" % ( NS.tendrl_context.integration_id) in NS.node_context.tags: plugin_params['is_provisioner_node'] = True for gluster_plugin in GLUSTER_CLUSTER_PLUGINS: plugin_config_success &= self._configure_plugin( gluster_plugin, plugin_params) if not plugin_config_success: raise AtomExecutionFailedError( "Collectd configuration failed for node %s from cluster %s" % (NS.node_context.fqdn, NS.tendrl_context.integration_id)) err, success = Service( 'collectd', publisher_id='node_agent', node_id=NS.node_context.node_id, socket_path=NS.config.data['logging_socket_path'], enabled=True).restart() _cluster = NS.tendrl.objects.Cluster( integration_id=NS.tendrl_context.integration_id).load() _cluster.import_status = "done" _cluster.save() return True
def test_service_error(self, monkeypatch): def mock_runner_run(obj): raise AnsibleExecutableGenerationFailed( "module_path", "arg", "err message" ) monkeypatch.setattr(AnsibleRunner, 'run', mock_runner_run) service = Service("collectd") message, success = service.start() assert not success assert message == "Executabe could not be generated for module" \ " module_path , with arguments arg. Error: err " \ "message"
def test_service_error(self, monkeypatch): def mock_runner_run(obj): raise AnsibleExecutableGenerationFailed( "module_path", "arg", "err message" ) monkeypatch.setattr(AnsibleRunner, 'run', mock_runner_run) service = Service("collectd", "/tmp") message, success = service.start() assert not success assert message == "Executabe could not be generated for module" \ " module_path , with arguments arg. Error: err " \ "message"
def run(self): self.parameters['Service.name'] = 'collectd' plugin_config_success = True graphite_host = ( NS.config.data.get('graphite_host') or NS.config.data['etcd_connection'] ) graphite_port = ( NS.config.data.get('graphite_port') or 2003 ) plugin_params = { "graphite_host": graphite_host, "graphite_port": graphite_port, "hostname": NS.node_context.fqdn, "integration_id": NS.tendrl_context.integration_id, "node_id": NS.node_context.node_id, "logging_socket_path": NS.config.data['logging_socket_path'], "interval": NS.config.data['sync_interval'], "interface": self.get_node_interface(NS.node_context.fqdn) } for node_plugin in NODE_PLUGINS: plugin_config_success &= self._configure_plugin( node_plugin, plugin_params ) if NS.tendrl_context.sds_name == 'gluster': plugins = GLUSTER_CLUSTER_PLUGINS.get('node_plugins', []) if "provisioner/%s" % ( NS.tendrl_context.integration_id ) in NS.node_context.tags: plugins.update( GLUSTER_CLUSTER_PLUGINS.get( 'cluster_plugins', [] ) ) for gluster_plugin in plugins: plugin_config_success &= self._configure_plugin( gluster_plugin, plugin_params ) if not plugin_config_success: raise AtomExecutionFailedError( "Collectd configuration failed for node %s from cluster %s" % ( NS.node_context.fqdn, NS.tendrl_context.integration_id ) ) err, success = Service( 'collectd', publisher_id='node_agent', node_id=NS.node_context.node_id, socket_path=NS.config.data['logging_socket_path'], enabled=True ).restart() return True
def run(self): self.parameters['Service.name'] = 'collectd' plugin_config_success = True graphite_host = (NS.config.data.get('graphite_host') or NS.config.data['etcd_connection']) graphite_port = (NS.config.data.get('graphite_port') or 2003) plugin_params = { "graphite_host": graphite_host, "graphite_port": graphite_port, "fqdn": NS.node_context.fqdn, "integration_id": NS.tendrl_context.integration_id, "node_id": NS.node_context.node_id, "logging_socket_path": NS.config.data['logging_socket_path'], "interval": NS.config.data['sync_interval'], "interface": self.get_node_interface(NS.node_context.fqdn), "etcd_host": NS.config.data['etcd_connection'], "etcd_port": NS.config.data['etcd_port'] } etcd_ca_cert_file = NS.config.data.get("etcd_ca_cert_file") etcd_cert_file = NS.config.data.get("etcd_cert_file") etcd_key_file = NS.config.data.get("etcd_key_file") if etcd_ca_cert_file and str(etcd_ca_cert_file) != "" \ and etcd_cert_file and str(etcd_cert_file) != "" \ and etcd_key_file and str(etcd_key_file) != "": plugin_params.update({ "etcd_ca_cert_file": NS.config.data['etcd_ca_cert_file'], "etcd_cert_file": NS.config.data['etcd_cert_file'], "etcd_key_file": NS.config.data['etcd_key_file'] }) for node_plugin in NODE_PLUGINS: plugin_config_success &= self._configure_plugin( node_plugin, plugin_params) if NS.tendrl_context.sds_name in ['gluster', 'RHGS']: for gluster_plugin in GLUSTER_CLUSTER_PLUGINS: plugin_config_success &= self._configure_plugin( gluster_plugin, plugin_params) if not plugin_config_success: logger.log( "error", NS.get("publisher_id", None), { "message": "Collectd configuration failed for node %s from" " cluster %s" % (NS.node_context.fqdn, NS.tendrl_context.integration_id) }, job_id=self.parameters['job_id'], flow_id=self.parameters['flow_id']) return False err, success = Service('collectd', publisher_id='node_agent', node_id=NS.node_context.node_id, enabled=True).restart() return True
def test_start(): init() service = Service("Test_service") service.start() with patch.object(ansible_module_runner, 'AnsibleRunner', ansible): with pytest.raises(ansible_module_runner.AnsibleModuleNotFound): ret = service.start() with patch.object(ansible_module_runner.AnsibleRunner, 'run', run): ret = service.start() assert ret[1] is False with patch.object(ansible_module_runner.AnsibleRunner, 'run') as mock_run: mock_run.return_value = ansible_run(True) ret = service.start() assert ret[1] is True assert ret[0] == "test_msg"
def main(): conf_name = argv[1] data = json.loads(argv[2]) ConfigManager(conf_name, data).generate_config_file() config = load_config( 'node-monitoring', '/etc/tendrl/node-monitoring/node-monitoring.conf.yaml') central_store = etcd_client(host=config['etcd_connection'], port=config['etcd_port']) with open('/etc/machine-id') as f: machine_id = f.read().strip('\n') node_id = central_store.read('/indexes/machine_id/%s' % machine_id).value return Service('collectd', publisher_id='node_monitoring', node_id=node_id, socket_path=config['logging_socket_path'], enabled=True).restart()
def test_start(): init() service = Service("Test_service") service.start() with patch.object(ansible_module_runner, 'AnsibleRunner', ansible): with pytest.raises(ansible_module_runner.AnsibleModuleNotFound): ret = service.start() with patch.object(ansible_module_runner.AnsibleRunner, 'run', run): ret = service.start() assert ret[1] is False with patch.object(ansible_module_runner.AnsibleRunner, 'run') as mock_run: mock_run.return_value = ansible_run(True) ret = service.start() assert ret[1] is True assert ret[0] == "test_msg"
def main(): conf_name = argv[2] data = json.loads(argv[3]) ConfigManager(conf_name, data).generate_config_file() return Service('collectd', argv[1]).restart()
def test_restart(): init() service = Service("Test_service") with patch.object(ansible_module_runner, 'AnsibleRunner', ansible): with pytest.raises(ansible_module_runner.AnsibleModuleNotFound): service.restart()
def test_service_constructor(self, monkeypatch): service = Service("collectd", "yes") expected_attr = {"name": "collectd", "enabled": "yes"} assert expected_attr == service.attributes