def test_get_keystone_url_from_implicit_openstack_config(): # This test is for documentation purposes because it is really testing OpenStackConfig instance = copy.deepcopy(common.CONFIG_FILE_INSTANCE) instance['openstack_cloud_name'] = 'rackspace' check = OpenStackControllerCheck("test", {}, {}, instances=[instance]) keystone_server_url = check._get_keystone_server_url(instance) assert keystone_server_url == 'https://identity.api.rackspacecloud.com/v2.0/'
def test_get_paginated_server(servers_detail, aggregator): """ Ensure the server cache is updated while using pagination """ check = OpenStackControllerCheck( "test", { 'keystone_server_url': 'http://10.0.2.15:5000', 'ssl_verify': False, 'exclude_server_ids': common.EXCLUDED_SERVER_IDS, 'paginated_server_limit': 1, }, {}, instances=instances, ) check.populate_servers_cache( { 'testproj': { "id": "6f70656e737461636b20342065766572", "name": "testproj" } }, []) servers = check.servers_cache['servers'] assert 'server-1' in servers assert 'other-1' not in servers assert 'other-2' not in servers
def test_populate_servers_cache_with_project_name_none(servers_detail, aggregator): """ Ensure the cache contains the expected VMs between check runs. """ check = OpenStackControllerCheck("test", {'ssl_verify': False}, [copy.deepcopy(common.KEYSTONE_INSTANCE)]) # Start off with a list of servers check.servers_cache = copy.deepcopy(common.SERVERS_CACHE_MOCK) # Update the cached list of servers based on what the endpoint returns check.populate_servers_cache( { '': { "id": '6f70656e737461636b20342065766572', "name": None }, 'blacklist_1': { "id": 'blacklist_1', "name": 'blacklist_1' }, 'blacklist_2': { "id": 'blacklist_2', "name": 'blacklist_2' }, }, [], ) servers = check.servers_cache['servers'] assert 'server_newly_added' not in servers assert 'server-1' not in servers assert 'other-1' in servers assert 'other-2' in servers
def test_get_all_servers_with_project_name_none(servers_detail, aggregator): """ Ensure the cache contains the expected VMs between check runs. """ check = OpenStackControllerCheck( "test", { 'keystone_server_url': 'http://10.0.2.15:5000', 'ssl_verify': False, 'exclude_server_ids': common.EXCLUDED_SERVER_IDS }, {}, instances=instances) # Start off with a list of servers check.servers_cache = copy.deepcopy(common.SERVERS_CACHE_MOCK) # Update the cached list of servers based on what the endpoint returns check.get_all_servers( { '6f70656e737461636b20342065766572': None, 'blacklist_1': 'blacklist_1', 'blacklist_2': 'blacklist_2' }, "test_name", []) servers = check.servers_cache['servers'] assert 'server_newly_added' not in servers assert 'server-1' not in servers assert 'other-1' in servers assert 'other-2' in servers
def test_parse_uptime_string(aggregator): instance = copy.deepcopy(common.KEYSTONE_INSTACE) instance['tags'] = ['optional:tag1'] init_config = common.MOCK_CONFIG['init_config'] check = OpenStackControllerCheck('openstack_controller', init_config, {}, instances=[instance]) response = u' 16:53:48 up 1 day, 21:34, 3 users, load average: 0.04, 0.14, 0.19\n' uptime_parsed = check._parse_uptime_string(response) assert uptime_parsed == [0.04, 0.14, 0.19]
def test_get_keystone_url_from_datadog_config(): check = OpenStackControllerCheck("test", { 'ssl_verify': False, 'paginated_server_limit': 1 }, [common.KEYSTONE_INSTANCE]) keystone_server_url = check._get_keystone_server_url( common.KEYSTONE_INSTANCE) assert keystone_server_url == 'http://10.0.2.15:5000'
def test_get_keystone_url_from_openstack_config(): check = OpenStackControllerCheck("test", { 'ssl_verify': False, 'paginated_server_limit': 1 }, [common.CONFIG_FILE_INSTANCE]) keystone_server_url = check._get_keystone_server_url( common.CONFIG_FILE_INSTANCE) assert keystone_server_url == 'http://xxx.xxx.xxx.xxx:5000/v2.0/'
def test_missing_keystone_server_url(): # This test is for documentation purposes because it is really testing OpenStackConfig instance = copy.deepcopy(common.KEYSTONE_INSTACE) instance['keystone_server_url'] = None check = OpenStackControllerCheck("test", {}, {}, instances=[instance]) with pytest.raises(IncompleteConfig): check._get_keystone_server_url(instance)
def test_check_with_config_file(mock_api, aggregator): check = OpenStackControllerCheck("test", {'ssl_verify': False}, {}, instances=[common.CONFIG_FILE_INSTANCE]) check.check(common.CONFIG_FILE_INSTANCE) aggregator.assert_service_check('openstack.keystone.api.up', AgentCheck.OK) aggregator.assert_service_check('openstack.nova.api.up', AgentCheck.OK) aggregator.assert_service_check('openstack.neutron.api.up', AgentCheck.OK) mock_api.assert_called_with(ANY, ANY, common.CONFIG_FILE_INSTANCE)
def test_check(mock_api, aggregator): check = OpenStackControllerCheck("test", {'ssl_verify': False}, [common.KEYSTONE_INSTANCE]) check.check(common.KEYSTONE_INSTANCE) aggregator.assert_service_check('openstack.keystone.api.up', AgentCheck.OK) aggregator.assert_service_check('openstack.nova.api.up', AgentCheck.OK) aggregator.assert_service_check('openstack.neutron.api.up', AgentCheck.OK) mock_api.assert_called_with(ANY, common.KEYSTONE_INSTANCE, ANY)
def test_cache_utils(aggregator): instance = common.MOCK_CONFIG["instances"][0] instance['tags'] = ['optional:tag1'] init_config = common.MOCK_CONFIG['init_config'] check = OpenStackControllerCheck('openstack_controller', init_config, {}, instances=[instance]) check.CACHE_TTL['aggregates'] = 1 expected_aggregates = {'hyp_1': ['aggregate:staging', 'availability_zone:test']} with mock.patch('datadog_checks.openstack_controller.OpenStackControllerCheck.get_all_aggregate_hypervisors', return_value=expected_aggregates): assert check._get_and_set_aggregate_list() == expected_aggregates time.sleep(1.5) assert check._is_expired('aggregates')
def test_get_paginated_server(servers_detail, aggregator): """ Ensure the server cache is updated while using pagination """ check = OpenStackControllerCheck( "test", {'ssl_verify': False, 'paginated_server_limit': 1}, {}, instances=[common.KEYSTONE_INSTACE] ) check.populate_servers_cache({'testproj': {"id": "6f70656e737461636b20342065766572", "name": "testproj"}}, []) servers = check.servers_cache['servers'] assert 'server-1' in servers assert 'other-1' not in servers assert 'other-2' not in servers
def test_get_all_servers_with_paginated_server(servers_detail, aggregator): """ Ensure the server cache is updated while using pagination """ check = OpenStackControllerCheck("test", { 'keystone_server_url': 'http://10.0.2.15:5000', 'ssl_verify': False, 'exclude_server_ids': common.EXCLUDED_SERVER_IDS, 'paginated_server_limit': 1 }, {}, instances=common.MOCK_CONFIG) check.get_all_servers({"6f70656e737461636b20342065766572": "testproj"}, "test_instance") assert len(check.server_details_by_id) == 1 assert 'server-1' in check.server_details_by_id assert 'other-1' not in check.server_details_by_id assert 'other-2' not in check.server_details_by_id
def test_retry(aggregator): instance = copy.deepcopy(common.MOCK_CONFIG["instances"][0]) instance['tags'] = ['optional:tag1'] init_config = copy.deepcopy(common.MOCK_CONFIG['init_config']) check = OpenStackControllerCheck('openstack', init_config, {}, instances=[instance]) retry = BackOffRetry(check) assert retry.should_run(instance) is True assert retry.backoff["test_name"]['retries'] == 0 # Make sure it is idempotent assert retry.should_run(instance) is True assert retry.backoff["test_name"]['retries'] == 0 retry.do_backoff(instance) assert retry.should_run(instance) is False assert retry.backoff["test_name"]['retries'] == 1 scheduled_1 = retry.backoff["test_name"]['scheduled'] retry.do_backoff(instance) scheduled_2 = retry.backoff["test_name"]['scheduled'] retry.do_backoff(instance) scheduled_3 = retry.backoff["test_name"]['scheduled'] retry.do_backoff(instance) scheduled_4 = retry.backoff["test_name"]['scheduled'] assert retry.backoff["test_name"]['retries'] == 4 assert time.time() < scheduled_1 < scheduled_2 < scheduled_3 < scheduled_4 for i in range(4): aggregator.assert_metric("openstack.backoff.retries", value=i + 1, tags=['optional:tag1']) aggregator.assert_metric("openstack.backoff.interval", tags=['optional:tag1']) aggregator.assert_all_metrics_covered()
def test_get_paginated_server(*args): """ Ensure the server cache is updated while using pagination """ openstackCheck = OpenStackControllerCheck("test", { 'keystone_server_url': 'http://10.0.2.15:5000', 'ssl_verify': False, 'exclude_server_ids': common.EXCLUDED_SERVER_IDS, 'paginated_server_limit': 1 }, {}, instances=common.MOCK_CONFIG) i_key = "test_instance" openstackCheck.get_all_servers(i_key) assert len(openstackCheck.server_details_by_id) == 1 assert 'server-1' in openstackCheck.server_details_by_id
def test_server_include_all_by_default(*args): """ Exclude servers using regular expressions. """ openstackCheck = OpenStackControllerCheck("test", { 'keystone_server_url': 'http://10.0.2.15:5000', 'ssl_verify': False }, {}, instances=common.MOCK_CONFIG) # Retrieve servers openstackCheck.server_details_by_id = copy.deepcopy( common.ALL_SERVER_DETAILS) openstackCheck.filter_excluded_servers() server_ids = openstackCheck.server_details_by_id # Assert # All 4 servers should still be monitored assert len(server_ids) == 4
def test_config(test_case, extra_config, expected_http_kwargs): instance = deepcopy(common.KEYSTONE_INSTANCE) instance.update(extra_config) check = OpenStackControllerCheck('openstack_controller', {}, instances=[instance]) for key, value in expected_http_kwargs.items(): assert check.http.options[ key] == value, "Expected '{}' to be {} but was {}".format( key, value, check.http.options[key])
def test_project_name_none(*args): """ Ensure the cache contains the expected VMs between check runs. """ openstackCheck = OpenStackControllerCheck( "test", { 'keystone_server_url': 'http://10.0.2.15:5000', 'ssl_verify': False, 'exclude_server_ids': common.EXCLUDED_SERVER_IDS }, {}, instances=common.MOCK_CONFIG) # Start off with a list of servers openstackCheck.server_details_by_id = copy.deepcopy( common.ALL_SERVER_DETAILS) i_key = "test_instance" # Update the cached list of servers based on what the endpoint returns openstackCheck.get_all_servers(i_key) assert 'server_newly_added' in openstackCheck.server_details_by_id assert 'server-1' not in openstackCheck.server_details_by_id
def test_server_exclusion_by_project(*args): """ Exclude servers using regular expressions. """ openstackCheck = OpenStackControllerCheck( "test", { 'keystone_server_url': 'http://10.0.2.15:5000', 'ssl_verify': False, 'blacklist_project_names': ["blacklist*"] }, {}, instances=common.MOCK_CONFIG) # Retrieve servers openstackCheck.server_details_by_id = copy.deepcopy( common.ALL_SERVER_DETAILS) openstackCheck.filter_excluded_servers() server_ids = openstackCheck.server_details_by_id # Assert # .. 2 out of 4 server ids filtered assert len(server_ids) == 2 # Ensure the server IDs filtered are the ones expected for server_id in server_ids: assert server_id in common.FILTERED_BY_PROJ_SERVER_ID
def test_get_all_servers_between_runs(servers_detail, aggregator): """ Ensure the cache contains the expected VMs between check runs. """ check = OpenStackControllerCheck("test", { 'keystone_server_url': 'http://10.0.2.15:5000', 'ssl_verify': False, 'exclude_server_ids': common.EXCLUDED_SERVER_IDS }, {}, instances=common.MOCK_CONFIG) # Start off with a list of servers check.server_details_by_id = copy.deepcopy(common.ALL_SERVER_DETAILS) # Update the cached list of servers based on what the endpoint returns check.get_all_servers({'6f70656e737461636b20342065766572': 'testproj', 'blacklist_1': 'blacklist_1', 'blacklist_2': 'blacklist_2'}, "test_instance") cached_servers = check.server_details_by_id print(cached_servers) assert 'server-1' not in cached_servers assert 'server_newly_added' in cached_servers assert 'other-1' in cached_servers assert 'other-2' in cached_servers
# Licensed under Simplified BSD License (see LICENSE) import copy import re import time import mock import pytest from . import common from datadog_checks.openstack_controller import OpenStackControllerCheck instance = common.MOCK_CONFIG["instances"][0] instance['tags'] = ['optional:tag1'] init_config = common.MOCK_CONFIG['init_config'] openstack_check = OpenStackControllerCheck('openstack', init_config, {}, instances=[instance]) @pytest.fixture def aggregator(): from datadog_checks.stubs import aggregator aggregator.reset() return aggregator def test_parse_uptime_string(): uptime_parsed = openstack_check._parse_uptime_string( u' 16:53:48 up 1 day, 21:34, 3 users, load average: 0.04, 0.14, 0.19\n' ) assert uptime_parsed.get('loads') == [0.04, 0.14, 0.19]
def test_collect_server_metrics_pre_2_48(server_diagnostics, os_aggregates, aggregator): check = OpenStackControllerCheck("test", { 'keystone_server_url': 'http://10.0.2.15:5000', 'ssl_verify': False, 'exclude_server_ids': common.EXCLUDED_SERVER_IDS, 'paginated_server_limit': 1 }, {}, instances=common.MOCK_CONFIG) check.collect_server_metrics({}) aggregator.assert_metric('openstack.nova.server.vda_read_req', value=112.0, tags=['availability_zone:NA'], hostname='') aggregator.assert_metric('openstack.nova.server.vda_read', value=262144.0, tags=['availability_zone:NA'], hostname='') aggregator.assert_metric('openstack.nova.server.memory', value=524288.0, tags=['availability_zone:NA'], hostname='') aggregator.assert_metric('openstack.nova.server.cpu0_time', value=17300000000.0, tags=['availability_zone:NA'], hostname='') aggregator.assert_metric('openstack.nova.server.vda_errors', value=-1.0, tags=['availability_zone:NA'], hostname='') aggregator.assert_metric('openstack.nova.server.vda_write_req', value=488.0, tags=['availability_zone:NA'], hostname='') aggregator.assert_metric('openstack.nova.server.vda_write', value=5778432.0, tags=['availability_zone:NA'], hostname='') aggregator.assert_metric('openstack.nova.server.tx_drop', value=0.0, tags=['availability_zone:NA', 'interface:vnet1'], hostname='') aggregator.assert_metric('openstack.nova.server.tx', value=140208.0, tags=['availability_zone:NA', 'interface:vnet1'], hostname='') aggregator.assert_metric('openstack.nova.server.rx_drop', value=0.0, tags=['availability_zone:NA', 'interface:vnet1'], hostname='') aggregator.assert_metric('openstack.nova.server.rx', value=2070139.0, tags=['availability_zone:NA', 'interface:vnet1'], hostname='') aggregator.assert_metric('openstack.nova.server.tx_packets', value=662.0, tags=['availability_zone:NA', 'interface:vnet1'], hostname='') aggregator.assert_metric('openstack.nova.server.tx_errors', value=0.0, tags=['availability_zone:NA', 'interface:vnet1'], hostname='') aggregator.assert_metric('openstack.nova.server.rx_packets', value=26701.0, tags=['availability_zone:NA', 'interface:vnet1'], hostname='') aggregator.assert_metric('openstack.nova.server.rx_errors', value=0.0, tags=['availability_zone:NA', 'interface:vnet1'], hostname='') aggregator.assert_metric('openstack.nova.server.tx_drop', value=0.0, tags=['availability_zone:NA', 'interface:vnet2'], hostname='') aggregator.assert_metric('openstack.nova.server.tx', value=140208.0, tags=['availability_zone:NA', 'interface:vnet2'], hostname='') aggregator.assert_metric('openstack.nova.server.rx_drop', value=0.0, tags=['availability_zone:NA', 'interface:vnet2'], hostname='') aggregator.assert_metric('openstack.nova.server.rx', value=2070139.0, tags=['availability_zone:NA', 'interface:vnet2'], hostname='') aggregator.assert_metric('openstack.nova.server.tx_packets', value=662.0, tags=['availability_zone:NA', 'interface:vnet2'], hostname='') aggregator.assert_metric('openstack.nova.server.tx_errors', value=0.0, tags=['availability_zone:NA', 'interface:vnet2'], hostname='') aggregator.assert_metric('openstack.nova.server.rx_packets', value=26701.0, tags=['availability_zone:NA', 'interface:vnet2'], hostname='') aggregator.assert_metric('openstack.nova.server.rx_errors', value=0.0, tags=['availability_zone:NA', 'interface:vnet2'], hostname='') aggregator.assert_all_metrics_covered()
def test_collect_server_metrics_pre_2_48(server_diagnostics, os_aggregates, aggregator): check = OpenStackControllerCheck( "test", {'ssl_verify': False, 'paginated_server_limit': 1}, {}, instances=[common.KEYSTONE_INSTACE] ) check.collect_server_diagnostic_metrics({}) aggregator.assert_metric( 'openstack.nova.server.vda_read_req', value=112.0, tags=['nova_managed_server', 'availability_zone:NA'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.vda_read', value=262144.0, tags=['nova_managed_server', 'availability_zone:NA'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.memory', value=524288.0, tags=['nova_managed_server', 'availability_zone:NA'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.cpu0_time', value=17300000000.0, tags=['nova_managed_server', 'availability_zone:NA'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.vda_errors', value=-1.0, tags=['nova_managed_server', 'availability_zone:NA'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.vda_write_req', value=488.0, tags=['nova_managed_server', 'availability_zone:NA'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.vda_write', value=5778432.0, tags=['nova_managed_server', 'availability_zone:NA'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.tx_drop', value=0.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet1'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.tx', value=140208.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet1'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.rx_drop', value=0.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet1'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.rx', value=2070139.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet1'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.tx_packets', value=662.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet1'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.tx_errors', value=0.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet1'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.rx_packets', value=26701.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet1'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.rx_errors', value=0.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet1'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.tx_drop', value=0.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet2'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.tx', value=140208.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet2'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.rx_drop', value=0.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet2'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.rx', value=2070139.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet2'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.tx_packets', value=662.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet2'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.tx_errors', value=0.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet2'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.rx_packets', value=26701.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet2'], hostname='', ) aggregator.assert_metric( 'openstack.nova.server.rx_errors', value=0.0, tags=['nova_managed_server', 'availability_zone:NA', 'interface:vnet2'], hostname='', ) aggregator.assert_all_metrics_covered()
def check(instance): return OpenStackControllerCheck(CHECK_NAME, {}, [instance])