def test_vmware_resource_async_render_GET_section(): request = mock.Mock() request.args = { b'target': [b'this-is-ignored'], b'section': [b'mysection'], } args = mock.Mock() args.config_file = None resource = VMWareMetricsResource(args) resource.config = { 'default': { 'ignore_ssl': False, 'vsphere_host': '127.0.0.10', 'vsphere_user': '******', 'vsphere_password': '******', 'specs_size': 5000, 'fetch_custom_attributes': True, 'fetch_tags': True, 'collect_only': { 'datastores': True, 'hosts': True, 'snapshots': True, 'vmguests': True, 'vms': True, } }, 'mysection': { 'ignore_ssl': 'On', 'vsphere_host': '127.0.0.11', 'vsphere_user': '******', 'vsphere_password': '******', 'specs_size': 5000, 'fetch_custom_attributes': True, 'fetch_tags': True, 'collect_only': { 'datastores': True, 'hosts': True, 'snapshots': True, 'vmguests': True, 'vms': True, } } } with mock.patch( 'vmware_exporter.vmware_exporter.VmwareCollector') as Collector: Collector.return_value.collect.return_value = [] yield resource._async_render_GET(request) Collector.assert_called_with('127.0.0.11', 'username2', 'password2', resource.config['mysection']['collect_only'], 5000, True, 'On', True) request.setResponseCode.assert_called_with(200) request.write.assert_called_with(b'') request.finish.assert_called_with()
def test_vmware_resource(): request = mock.Mock() args = mock.Mock() args.config_file = None resource = VMWareMetricsResource(args) with mock.patch.object(resource, '_async_render_GET') as _async_render_GET: assert resource.render_GET(request) == NOT_DONE_YET _async_render_GET.assert_called_with(request)
def test_vmware_resource_async_render_GET_no_target(): request = mock.Mock() request.args = {} args = mock.Mock() args.config_file = None resource = VMWareMetricsResource(args) with mock.patch('vmware_exporter.vmware_exporter.VmwareCollector'): yield resource._async_render_GET(request) request.setResponseCode.assert_called_with(500) request.write.assert_called_with(b'No vsphere_host or target defined!\n') request.finish.assert_called_with()
def test_vmware_resource_async_render_GET_errback(): request = mock.Mock() request.args = { b'vsphere_host': [b'127.0.0.1'], } args = mock.Mock() args.config_file = None resource = VMWareMetricsResource(args) with mock.patch('vmware_exporter.vmware_exporter.VmwareCollector') as Collector: Collector.return_value.collect.side_effect = RuntimeError('Test exception') yield resource._async_render_GET(request) request.setResponseCode.assert_called_with(500) request.write.assert_called_with(b'# Collection failed') request.finish.assert_called_with()
def test_vmware_resource_async_render_GET(): request = mock.Mock() request.args = { b'vsphere_host': [b'127.0.0.1'], } args = mock.Mock() args.config_file = None resource = VMWareMetricsResource(args) with mock.patch('vmware_exporter.vmware_exporter.VmwareCollector') as Collector: Collector.return_value.collect.return_value = [] yield resource._async_render_GET(request) request.setResponseCode.assert_called_with(200) request.write.assert_called_with(b'') request.finish.assert_called_with()
def test_config_env_multiple_sections(): env = { 'VSPHERE_HOST': '127.0.0.10', 'VSPHERE_USER': '******', 'VSPHERE_PASSWORD': '******', 'VSPHERE_SPECS_SIZE': 5000, 'VSPHERE_FETCH_CUSTOM_ATTRIBUTES': True, 'VSPHERE_FETCH_TAGS': True, 'VSPHERE_MYSECTION_HOST': '127.0.0.11', 'VSPHERE_MYSECTION_USER': '******', 'VSPHERE_MYSECTION_PASSWORD': '******', 'VSPHERE_MYSECTION_SPECS_SIZE': 5000, } args = mock.Mock() args.config_file = None with mock.patch('vmware_exporter.vmware_exporter.os.environ', env): resource = VMWareMetricsResource(args) assert resource.config == { 'default': { 'ignore_ssl': False, 'vsphere_host': '127.0.0.10', 'vsphere_user': '******', 'vsphere_password': '******', 'specs_size': 5000, 'fetch_custom_attributes': True, 'fetch_tags': True, 'collect_only': { 'datastores': True, 'hosts': True, 'snapshots': True, 'vmguests': True, 'vms': True, } }, 'mysection': { 'ignore_ssl': False, 'vsphere_host': '127.0.0.11', 'vsphere_user': '******', 'vsphere_password': '******', 'specs_size': 5000, 'fetch_custom_attributes': False, 'fetch_tags': False, 'collect_only': { 'datastores': True, 'hosts': True, 'snapshots': True, 'vmguests': True, 'vms': True, } } }
def test_config_env_multiple_sections(): env = { 'VSPHERE_HOST': '127.0.0.10', 'VSPHERE_USER': '******', 'VSPHERE_PASSWORD': '******', 'VSPHERE_MYSECTION_HOST': '127.0.0.11', 'VSPHERE_MYSECTION_USER': '******', 'VSPHERE_MYSECTION_PASSWORD': '******', } args = mock.Mock() args.config_file = None with mock.patch('vmware_exporter.vmware_exporter.os.environ', env): resource = VMWareMetricsResource(args) assert resource.config == { 'default': { 'ignore_ssl': False, 'vsphere_host': '127.0.0.10', 'vsphere_user': '******', 'vsphere_password': '******', 'collect_only': { 'datastores': True, 'hosts': True, 'snapshots': True, 'vmguests': True, 'vms': True, } }, 'mysection': { 'ignore_ssl': False, 'vsphere_host': '127.0.0.11', 'vsphere_user': '******', 'vsphere_password': '******', 'collect_only': { 'datastores': True, 'hosts': True, 'snapshots': True, 'vmguests': True, 'vms': True, } } }