Пример #1
0
def test_defaults():
    api = BaseAPI(2)
    assert api.host == 'localhost'
    assert api.port == 8080
    assert api.ssl is False
    assert api.ssl_key is None
    assert api.ssl_cert is None
    assert api.timeout == 10
    assert api.protocol == 'http'
Пример #2
0
def connect(host='localhost', port=8080, ssl_verify=False, ssl_key=None,
            ssl_cert=None, timeout=10, protocol=None, url_path='/',
            username=None, password=None):
    """Connect with PuppetDB. This will return an object allowing you
    to query the API through its methods.

    :param host: (Default: 'localhost;) Hostname or IP of PuppetDB.
    :type host: :obj:`string`

    :param port: (Default: '8080') Port on which to talk to PuppetDB.
    :type port: :obj:`int`

    :param ssl_verify: (optional) Verify PuppetDB server certificate.
    :type ssl_verify: :obj:`bool` or :obj:`string` True, False or filesystem \
            path to CA certificate.

    :param ssl_key: (optional) Path to our client secret key.
    :type ssl_key: :obj:`None` or :obj:`string` representing a filesystem\
            path.

    :param ssl_cert: (optional) Path to our client certificate.
    :type ssl_cert: :obj:`None` or :obj:`string` representing a filesystem\
            path.

    :param timeout: (Default: 10) Number of seconds to wait for a response.
    :type timeout: :obj:`int`

    :param protocol: (optional) Explicitly specify the protocol to be used
            (especially handy when using HTTPS with ssl_verify=False and
            without certs)
    :type protocol: :obj:`None` or :obj:`string`

    :param url_path: (Default: '/') The URL path where PuppetDB is served
    :type url_path: :obj:`None` or :obj:`string`

    :param username: (optional) The username to use for HTTP basic
            authentication
    :type username: :obj:`None` or :obj:`string`

    :param password: (optional) The password to use for HTTP basic
            authentication
    :type password: :obj:`None` or :obj:`string`
    """
    return BaseAPI(host=host, port=port,
                   timeout=timeout, ssl_verify=ssl_verify, ssl_key=ssl_key,
                   ssl_cert=ssl_cert, protocol=protocol, url_path=url_path,
                   username=username, password=password)
Пример #3
0
def test_query_with_count_filter(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes?count-filter=""', body='[]')
    response = api._query('nodes', count_filter="")
    assert response == []
Пример #4
0
def test_url_with_endpoint_with_path():
    api = BaseAPI(2)
    assert api._url('facts', path='osfamily') == ('http://localhost:8080/'
                                                  'v2/facts/osfamily')
Пример #5
0
def test_query_with_order_by(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes?order-by=""', body='[]')
    response = api._query('nodes', order_by="")
    assert response == []
Пример #6
0
def test_api3_endpoints():
    api = BaseAPI(3)
    assert api.version == 'v3'
    assert api.endpoints == ENDPOINTS[3]
Пример #7
0
def test_query_connection():
    api = BaseAPI(2)
    with pytest.raises(requests.exceptions.ConnectionError):
        api._query('nodes')
Пример #8
0
def test_facts():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.facts()
Пример #9
0
def test_invalid_api_version():
    with pytest.raises(UnsupportedVersionError):
        api = BaseAPI(1)
Пример #10
0
def test_url_with_endpoint_with_path():
    api = BaseAPI(2)
    assert api._url('facts', path='osfamily') == ('http://localhost:8080/'
                                                  'v2/facts/osfamily')
Пример #11
0
def test_query_endpoint(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes', body='[]')
    response = api._query('nodes')
    assert response == []
Пример #12
0
def test_url_with_endpoint():
    api = BaseAPI(2)
    assert api._url('fact-names') == 'http://localhost:8080/v2/fact-names'
Пример #13
0
def test_url_with_unknown_endpoint():
    api = BaseAPI(2)
    with pytest.raises(APIError):
        api._url('bananas')
Пример #14
0
def test_resources():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.resources()
Пример #15
0
def test_facts():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.facts()
Пример #16
0
def test_node():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.node()
Пример #17
0
def test_query_connection():
    api = BaseAPI(2)
    with pytest.raises(requests.exceptions.ConnectionError):
        api._query('nodes')
Пример #18
0
def test_query_with_order_by(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes?order-by=""', body='[]')
    response = api._query('nodes', order_by="")
    assert response == []
Пример #19
0
def test_node():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.node()
Пример #20
0
def test_query_with_limit(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes?limit=""', body='[]')
    response = api._query('nodes', limit="")
    assert response == []
Пример #21
0
def test_resources():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.resources()
Пример #22
0
def test_with_invalid_ssl_config():
    with pytest.raises(ImproperlyConfiguredError):
        api = BaseAPI(3, ssl=True, ssl_cert='ssl.cert')
Пример #23
0
def test_api2_endpoints():
    api = BaseAPI(2)
    assert api.version == 'v2'
    assert api.endpoints == ENDPOINTS[2]
Пример #24
0
def test_url_with_endpoint():
    api = BaseAPI(2)
    assert api._url('fact-names') == 'http://localhost:8080/v2/fact-names'
Пример #25
0
def test_with_valid_ssl_config():
    api = BaseAPI(3, ssl=True, ssl_key='ssl.key', ssl_cert='ssl.cert')
    assert api.ssl is True
    assert api.ssl_key == 'ssl.key'
    assert api.ssl_cert == 'ssl.cert'
    assert api.protocol == 'https'
Пример #26
0
def test_query_empty_body(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes', body='null')
    with pytest.raises(EmptyResponseError):
        api._query('nodes')
Пример #27
0
def test_build_url():
    api = BaseAPI(2)
    assert api.base_url == 'http://localhost:8080'
Пример #28
0
def test_query_with_include_total(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes?include-total=""', body='[]')
    response = api._query('nodes', include_total="")
    assert response == []
Пример #29
0
def test_url_with_unknown_endpoint():
    api = BaseAPI(2)
    with pytest.raises(APIError):
        api._url('bananas')
Пример #30
0
def test_query_with_count_filter(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes?count-filter=""', body='[]')
    response = api._query('nodes', count_filter="")
    assert response == []
Пример #31
0
def test_query_endpoint(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes', body='[]')
    response = api._query('nodes')
    assert response == []
Пример #32
0
def test_query_empty_body(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes', body='null')
    with pytest.raises(EmptyResponseError):
        api._query('nodes')
Пример #33
0
def test_query_with_limit(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes?limit=""', body='[]')
    response = api._query('nodes', limit="")
    assert response == []
Пример #34
0
def test_query_with_include_total(stub_get):
    api = BaseAPI(2)
    stub_get('http://localhost:8080/v2/nodes?include-total=""', body='[]')
    response = api._query('nodes', include_total="")
    assert response == []