Esempio n. 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'
Esempio n. 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)
Esempio n. 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 == []
Esempio n. 4
0
def test_url_with_endpoint_with_path():
    api = BaseAPI(2)
    assert api._url('facts', path='osfamily') == ('http://localhost:8080/'
                                                  'v2/facts/osfamily')
Esempio n. 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 == []
Esempio n. 6
0
def test_api3_endpoints():
    api = BaseAPI(3)
    assert api.version == 'v3'
    assert api.endpoints == ENDPOINTS[3]
Esempio n. 7
0
def test_query_connection():
    api = BaseAPI(2)
    with pytest.raises(requests.exceptions.ConnectionError):
        api._query('nodes')
Esempio n. 8
0
def test_facts():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.facts()
Esempio n. 9
0
def test_invalid_api_version():
    with pytest.raises(UnsupportedVersionError):
        api = BaseAPI(1)
Esempio n. 10
0
def test_url_with_endpoint_with_path():
    api = BaseAPI(2)
    assert api._url('facts', path='osfamily') == ('http://localhost:8080/'
                                                  'v2/facts/osfamily')
Esempio n. 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 == []
Esempio n. 12
0
def test_url_with_endpoint():
    api = BaseAPI(2)
    assert api._url('fact-names') == 'http://localhost:8080/v2/fact-names'
Esempio n. 13
0
def test_url_with_unknown_endpoint():
    api = BaseAPI(2)
    with pytest.raises(APIError):
        api._url('bananas')
Esempio n. 14
0
def test_resources():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.resources()
Esempio n. 15
0
def test_facts():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.facts()
Esempio n. 16
0
def test_node():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.node()
Esempio n. 17
0
def test_query_connection():
    api = BaseAPI(2)
    with pytest.raises(requests.exceptions.ConnectionError):
        api._query('nodes')
Esempio n. 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 == []
Esempio n. 19
0
def test_node():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.node()
Esempio n. 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 == []
Esempio n. 21
0
def test_resources():
    api = BaseAPI(2)
    with pytest.raises(NotImplementedError):
        api.resources()
Esempio n. 22
0
def test_with_invalid_ssl_config():
    with pytest.raises(ImproperlyConfiguredError):
        api = BaseAPI(3, ssl=True, ssl_cert='ssl.cert')
Esempio n. 23
0
def test_api2_endpoints():
    api = BaseAPI(2)
    assert api.version == 'v2'
    assert api.endpoints == ENDPOINTS[2]
Esempio n. 24
0
def test_url_with_endpoint():
    api = BaseAPI(2)
    assert api._url('fact-names') == 'http://localhost:8080/v2/fact-names'
Esempio n. 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'
Esempio n. 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')
Esempio n. 27
0
def test_build_url():
    api = BaseAPI(2)
    assert api.base_url == 'http://localhost:8080'
Esempio n. 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 == []
Esempio n. 29
0
def test_url_with_unknown_endpoint():
    api = BaseAPI(2)
    with pytest.raises(APIError):
        api._url('bananas')
Esempio n. 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 == []
Esempio n. 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 == []
Esempio n. 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')
Esempio n. 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 == []
Esempio n. 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 == []