def test_from_instance(): """ Test the defaults and the pieces of _load_conf that actually perform some logic """ # misconfiguration with pytest.raises(ConfigurationError) as e: from_instance({}) assert 'Bad configuration' in str(e) # misconfiguration with pytest.raises(ConfigurationError) as e: from_instance({'url': 'example.com'}) assert 'scheme' in str(e) # defaults config = from_instance({'url': 'https://example.com', 'name': 'UpService'}) assert len(config) == 18 # `url` is mandatory assert config.url == 'https://example.com' # assert defaults assert config.client_cert is None assert config.client_key is None assert config.method == 'get' assert config.data == {} assert config.http_response_status_code == DEFAULT_EXPECTED_CODE assert config.include_content is False assert config.headers == agent_headers({}) assert config.response_time is True assert config.content_match is None assert config.reverse_content_match is False assert config.tags == [] assert config.ssl_expire is True assert config.instance_ca_certs != '' # `ca_certs`, it's mocked we don't care assert config.weakcipher is False assert config.check_hostname is True assert config.allow_redirects is True assert config.stream is False # headers config = from_instance({ 'url': 'https://example.com', 'name': 'UpService', 'headers': { "X-Auth-Token": "SOME-AUTH-TOKEN" } }) headers = config.headers expected_headers = agent_headers({}).get('User-Agent') assert headers["X-Auth-Token"] == "SOME-AUTH-TOKEN", headers assert expected_headers == headers.get('User-Agent'), headers
def from_instance(instance, default_ca_certs=None): """ Create a config object from an instance dictionary """ method = instance.get('method', 'get') data = instance.get('data', {}) tags = instance.get('tags', []) ntlm_domain = instance.get('ntlm_domain') username = instance.get('username') password = instance.get('password') client_cert = instance.get('client_cert') client_key = instance.get('client_key') http_response_status_code = str( instance.get('http_response_status_code', DEFAULT_EXPECTED_CODE)) timeout = int(instance.get('timeout', 10)) config_headers = instance.get('headers', {}) default_headers = is_affirmative( instance.get("include_default_headers", True)) if default_headers: headers = agent_headers({}) else: headers = {} headers.update(config_headers) url = instance.get('url') if url is not None: url = ensure_unicode(url) content_match = instance.get('content_match') if content_match is not None: content_match = ensure_unicode(content_match) reverse_content_match = is_affirmative( instance.get('reverse_content_match', False)) response_time = is_affirmative(instance.get('collect_response_time', True)) if not url: raise ConfigurationError("Bad configuration. You must specify a url") if not url.startswith("http"): raise ConfigurationError( "The url {} must start with the scheme http or https".format(url)) include_content = is_affirmative(instance.get('include_content', False)) disable_ssl_validation = is_affirmative( instance.get('disable_ssl_validation', True)) ssl_expire = is_affirmative( instance.get('check_certificate_expiration', True)) instance_ca_certs = instance.get('ca_certs', default_ca_certs) weakcipher = is_affirmative(instance.get('weakciphers', False)) ignore_ssl_warning = is_affirmative( instance.get('ignore_ssl_warning', False)) check_hostname = is_affirmative(instance.get('check_hostname', True)) skip_proxy = is_affirmative( instance.get('skip_proxy', instance.get('no_proxy', False))) allow_redirects = is_affirmative(instance.get('allow_redirects', True)) stream = is_affirmative(instance.get('stream', False)) return Config(url, ntlm_domain, username, password, client_cert, client_key, method, data, http_response_status_code, timeout, include_content, headers, response_time, content_match, reverse_content_match, tags, disable_ssl_validation, ssl_expire, instance_ca_certs, weakcipher, check_hostname, ignore_ssl_warning, skip_proxy, allow_redirects, stream)
def test_http_headers(http_check): """ Headers format. """ # Get just the headers from http_check._load_conf(...), which happens to be at index 10 headers = http_check._load_conf(CONFIG_HTTP_HEADERS['instances'][0])[10] expected_headers = agent_headers({}).get('User-Agent') assert headers["X-Auth-Token"] == "SOME-AUTH-TOKEN", headers assert expected_headers == headers.get('User-Agent'), headers
def _load_conf(self, instance): # Fetches the conf method = instance.get('method', 'get') data = instance.get('data', {}) tags = instance.get('tags', []) ntlm_domain = instance.get('ntlm_domain') username = instance.get('username') password = instance.get('password') client_cert = instance.get('client_cert') client_key = instance.get('client_key') http_response_status_code = str( instance.get('http_response_status_code', DEFAULT_EXPECTED_CODE)) timeout = int(instance.get('timeout', 10)) config_headers = instance.get('headers', {}) default_headers = _is_affirmative( instance.get("include_default_headers", True)) if default_headers: headers = agent_headers(self.agentConfig) else: headers = {} headers.update(config_headers) url = instance.get('url') content_match = instance.get('content_match') reverse_content_match = _is_affirmative( instance.get('reverse_content_match', False)) response_time = _is_affirmative( instance.get('collect_response_time', True)) if not url: raise Exception("Bad configuration. You must specify a url") include_content = _is_affirmative( instance.get('include_content', False)) disable_ssl_validation = _is_affirmative( instance.get('disable_ssl_validation', True)) ssl_expire = _is_affirmative( instance.get('check_certificate_expiration', True)) instance_ca_certs = instance.get('ca_certs', self.ca_certs) weakcipher = _is_affirmative(instance.get('weakciphers', False)) ignore_ssl_warning = _is_affirmative( instance.get('ignore_ssl_warning', False)) check_hostname = _is_affirmative(instance.get('check_hostname', True)) skip_proxy = _is_affirmative( instance.get('skip_proxy', instance.get('no_proxy', False))) allow_redirects = _is_affirmative(instance.get('allow_redirects', True)) return url, ntlm_domain, username, password, client_cert, client_key, method, data, http_response_status_code, \ timeout, include_content, headers, response_time, content_match, reverse_content_match, tags, \ disable_ssl_validation, ssl_expire, instance_ca_certs, weakcipher, check_hostname, ignore_ssl_warning, \ skip_proxy, allow_redirects
def test_from_instance(): """ Test the defaults and the pieces of _load_conf that actually perform some logic """ # misconfiguration with pytest.raises(ConfigurationError) as e: from_instance({}) assert 'Bad configuration' in str(e) # misconfiguration with pytest.raises(ConfigurationError) as e: from_instance({'url': 'example.com'}) assert 'scheme' in str(e) # defaults params = from_instance({'url': 'https://example.com', 'name': 'UpService'}) assert len(params) == 25 # `url` is mandatory assert params[0] == 'https://example.com' # default `ntlm_domain` is None assert params[1] is None # default `username` is None assert params[2] is None # default `password` is None assert params[3] is None # defualt `client_cert` is None assert params[4] is None # defualt `client_key` is None assert params[5] is None # default `method` is get assert params[6] == 'get' # default `data` is an empty dict assert params[7] == {} # default `http_response_status_code` assert params[8] == DEFAULT_EXPECTED_CODE # default `timeout` is 10 assert params[9] == 10 # default `include_content` is False assert params[10] is False # default headers assert params[11] == agent_headers({}) # default `collect_response_time` is True assert params[12] is True # default `content_match` is None assert params[13] is None # default `reverse_content_match` is False assert params[14] is False # default `tags` is an empty list assert params[15] == [] # default `disable_ssl_validation` is True assert params[16] is True # default `check_certificate_expiration` is True assert params[17] is True # default `ca_certs`, it's mocked we don't care assert params[18] != '' # default `weakciphers` is False assert params[19] is False # default `check_hostname` is True assert params[20] is True # default `ignore_ssl_warning` is False assert params[21] is False # default `skip_proxy` is False assert params[22] is False # default `allow_redirects` is True assert params[23] is True # default `stream` is False assert params[24] is False # headers params = from_instance({ 'url': 'https://example.com', 'name': 'UpService', 'headers': { "X-Auth-Token": "SOME-AUTH-TOKEN" } }) headers = params[11] expected_headers = agent_headers({}).get('User-Agent') assert headers["X-Auth-Token"] == "SOME-AUTH-TOKEN", headers assert expected_headers == headers.get('User-Agent'), headers # proxy params = from_instance({ 'url': 'https://example.com', 'name': 'UpService', 'no_proxy': True }) assert params[22] is True params = from_instance({ 'url': 'https://example.com', 'name': 'UpService', 'no_proxy': False, 'skip_proxy': True }) assert params[22] is True