예제 #1
0
def test_instance_ca_cert():
    """
    `instance_ca_cert` should default to the trusted ca_cert of the system
    if `tls_ca_cert` and `ca_certs` are unavailable.
    """
    # Ensure that 'tls_ca_cert' takes precedence
    params_with_all = from_instance(
        {
            'url': 'https://example2.com',
            'name': 'UpService',
            'tls_ca_cert': 'foobar',
            'ca_certs': 'barfoo'
        },
        'default_ca_cert',
    )
    assert params_with_all.instance_ca_certs == 'foobar'

    # Original config option for ca_certs
    params_only_ca_certs = from_instance({
        'url': 'https://example2.com',
        'name': 'UpService',
        'ca_certs': 'ca_cert'
    })
    assert params_only_ca_certs.instance_ca_certs == 'ca_cert'

    # Default if there is no cert path is configured
    params_no_certs = from_instance(
        {
            'url': 'https://example2.com',
            'name': 'UpService'
        }, 'default_ca_cert')
    assert params_no_certs.instance_ca_certs == 'default_ca_cert'

    # No default ca_cert
    params_no_default = from_instance({
        'url': 'https://example2.com',
        'name': 'UpService'
    })
    assert params_no_default.instance_ca_certs is None
예제 #2
0
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
예제 #3
0
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