def test_refresh_tags_cache_should_not_raise_exception(aggregator, dd_run_check, realtime_instance): realtime_instance.update({'collect_tags': True}) check = VSphereCheck('vsphere', {}, [realtime_instance]) check.log = MagicMock() check.api_rest = MagicMock() check.api_rest.get_resource_tags_for_mors.side_effect = APIConnectionError("Some error") check.collect_tags({}) # Error logged, but `refresh_tags_cache` should NOT raise any exception check.log.error.assert_called_once_with("Failed to collect tags: %s", mock.ANY)
def test_renew_rest_api_session_on_failure(aggregator, dd_run_check, realtime_instance): realtime_instance.update({'collect_tags': True}) check = VSphereCheck('vsphere', {}, [realtime_instance]) config = VSphereConfig(realtime_instance, {}, MagicMock()) check.api_rest = VSphereRestAPI(config, MagicMock()) check.api_rest.make_batch = MagicMock(side_effect=[Exception, []]) check.api_rest.smart_connect = MagicMock() tags = check.collect_tags({}) assert tags assert check.api_rest.make_batch.call_count == 2 assert check.api_rest.smart_connect.call_count == 1
def test_external_host_tags(aggregator, realtime_instance): realtime_instance['collect_tags'] = True check = VSphereCheck('vsphere', {}, [realtime_instance]) config = VSphereConfig(realtime_instance, MagicMock()) check.api = MockedAPI(config) check.api_rest = VSphereRestAPI(config, MagicMock()) with check.tags_cache.update(): check.refresh_tags_cache() with check.infrastructure_cache.update(): check.refresh_infrastructure_cache() fixture_file = os.path.join(HERE, 'fixtures', 'host_tags_values.json') with open(fixture_file, 'r') as f: expected_tags = json.load(f) check.set_external_tags = MagicMock() check.submit_external_host_tags() submitted_tags = check.set_external_tags.mock_calls[0].args[0] submitted_tags.sort(key=lambda x: x[0]) for ex, sub in zip(expected_tags, submitted_tags): ex_host, sub_host = ex[0], sub[0] ex_tags, sub_tags = ex[1]['vsphere'], sub[1]['vsphere'] ex_tags = [ to_native_string(t) for t in ex_tags ] # json library loads data in unicode, let's convert back to native assert ex_host == sub_host assert ex_tags == sub_tags check.config.excluded_host_tags = ['vsphere_host'] check.set_external_tags = MagicMock() check.submit_external_host_tags() submitted_tags = check.set_external_tags.mock_calls[0].args[0] submitted_tags.sort(key=lambda x: x[0]) for ex, sub in zip(expected_tags, submitted_tags): ex_host, sub_host = ex[0], sub[0] ex_tags, sub_tags = ex[1]['vsphere'], sub[1]['vsphere'] ex_tags = [ to_native_string(t) for t in ex_tags if 'vsphere_host:' not in t ] assert ex_host == sub_host assert ex_tags == sub_tags check.set_external_tags = MagicMock() check.submit_external_host_tags()