def hil_client_connect(endpoint_ip, name, pw):
    '''
    '''
    hil_http_client = RequestsHTTPClient()
    hil_http_client.auth = (name, pw)

    return Client(endpoint_ip, hil_http_client)
Exemplo n.º 2
0
def hil_client_connect(endpoint_ip, name, pw):
    """Returns a HIL client object"""

    hil_http_client = RequestsHTTPClient()
    hil_http_client.auth = (name, pw)

    return Client(endpoint_ip, hil_http_client)
Exemplo n.º 3
0
def test_non_json_response():
    """The client library should raise an error when the response body is
    unexpectedly not JSON.
    """
    # Endpoint is arbitrary:
    endpoint = 'http:/127.0.0.1:9933'
    client = Client(endpoint, HybridHTTPClient(endpoint))

    # Override one of the API calls with a different implementation:
    # pylint: disable=unused-variable
    @rest.rest_call('GET', '/nodes/free', Schema({}))
    def list_free_nodes():
        """Mock API call for testing; always raises an error."""
        flask.abort(500)

    try:
        client.node.list('free')
        assert False, 'Client library did not report an error!'
    except FailedAPICallException as e:
        # Make sure it's the right error:
        assert e.error_type == 'unknown', 'Wrong error type.'
Exemplo n.º 4
0
        resp = self._flask_client.open(
            method=method,
            headers={'Authorization': auth_header},
            # flask expects just a path, and assumes
            # the host & scheme:
            path=urlparse(url).path,
            data=data,
            query_string=params,
        )
        return HTTPResponse(status_code=resp.status_code,
                            headers=resp.headers,
                            content=resp.get_data())


http_client = FlaskHTTPClient()
C = Client(ep, http_client)  # Initializing client library

fail_on_log_warnings = pytest.fixture(fail_on_log_warnings)
fresh_database = pytest.fixture(fresh_database)
server_init = pytest.fixture(server_init)


@pytest.fixture
def dummy_verify():
    """replace sha512_crypt.verify with something faster (albeit broken).

    The tests in this module mostly use the database auth backend. This was,
    frankly, a mistake. This module pulls in way too much "real" code (as
    opposed to mock objects) in general, and patches to improve the
    situation are welcome.
Exemplo n.º 5
0
def setup_http_client():
    """Set `http_client` to a valid instance of `HTTPClient`

    and pass it as parameter to initialize the client library.

    Sets http_client to an object which makes HTTP requests with
    authentication. It chooses an authentication backend as follows:

    1. If the environment variables HIL_USERNAME and HIL_PASSWORD
       are defined, it will use HTTP basic auth, with the corresponding
       user name and password.
    2. If the `python-keystoneclient` library is installed, and the
       environment variables:

           * OS_AUTH_URL
           * OS_USERNAME
           * OS_PASSWORD
           * OS_PROJECT_NAME

       are defined, Keystone is used.
    3. Oterwise, do not supply authentication information.

    This may be extended with other backends in the future.

    `http_client` is also passed as a parameter to the client library.
    Until all calls are moved to client library, this will support
    both ways of intereacting with HIL.
    """
    global http_client
    global C  # initiating the client library
    # First try basic auth:
    ep = (os.environ.get('HIL_ENDPOINT')
          or sys.stdout.write("Error: HIL_ENDPOINT not set \n"))
    basic_username = os.getenv('HIL_USERNAME')
    basic_password = os.getenv('HIL_PASSWORD')
    if basic_username is not None and basic_password is not None:
        # For calls with no client library support yet.
        # Includes all headnode calls; registration of nodes and switches.
        http_client = RequestsHTTPClient()
        http_client.auth = (basic_username, basic_password)
        # For calls using the client library
        C = Client(ep, http_client)
        return
    # Next try keystone:
    try:
        from keystoneauth1.identity import v3
        from keystoneauth1 import session
        os_auth_url = os.getenv('OS_AUTH_URL')
        os_password = os.getenv('OS_PASSWORD')
        os_username = os.getenv('OS_USERNAME')
        os_user_domain_id = os.getenv('OS_USER_DOMAIN_ID') or 'default'
        os_project_name = os.getenv('OS_PROJECT_NAME')
        os_project_domain_id = os.getenv('OS_PROJECT_DOMAIN_ID') or 'default'
        if None in (os_auth_url, os_username, os_password, os_project_name):
            raise KeyError("Required openstack environment variable not set.")
        auth = v3.Password(auth_url=os_auth_url,
                           username=os_username,
                           password=os_password,
                           project_name=os_project_name,
                           user_domain_id=os_user_domain_id,
                           project_domain_id=os_project_domain_id)
        sess = session.Session(auth=auth)
        http_client = KeystoneHTTPClient(sess)
        # For calls using the client library
        C = Client(ep, http_client)
        return
    except (ImportError, KeyError):
        pass
    # Finally, fall back to no authentication:
    http_client = requests.Session()
    C = Client(ep, http_client)