Exemplo n.º 1
0
def test_broker_protocol_handler_call_waiting_for_getresourcelist_getresourcelist(
        ctx: Fixture):
    bph = BrokerProtocolHandler(ctx.mapper)

    resources = {
        "0": Resource("Kurt", "Gödel"),
        "1": Resource("Paul", "Dirac")
    }

    session_data, response = bph(
        GetResourceListRequest(),
        ProtocolSession(
            "Leonhard",
            "Euler",
            state=ProtocolState.WAITING_FOR_GETRESOURCELIST,
            resources=resources,
        ),
    )

    assert session_data.state == ProtocolState.WAITING_FOR_ALLOCATERESOURCE
    # print(response)
    assert isinstance(response, GetResourceListResponse)
    assert len(response.resources) == len(resources)
    assert response.resources[0].resource_name == resources["0"].name
    assert response.resources[0].resource_id == "0"
    assert response.resources[1].resource_name == resources["1"].name
    assert response.resources[1].resource_id == "1"

    assert isinstance(session_data, ProtocolSession)
    assert session_data.username == "Leonhard"
    assert session_data.password == "Euler"
    assert session_data.resources == resources
Exemplo n.º 2
0
def test_broker_protocol_handler_call_waiting_for_allocateresource_allocateresource_success(
    ctx: Fixture, ):
    agent_session = AgentSession("kolmogorov", "kolmogorov.edu", 666,
                                 "Catmull", "Rom", "0")

    def allocate_session(*args, **kwargs):
        return AllocateSessionStatus.SUCCESSFUL, agent_session

    protocol_session = ProtocolSession(
        "Leonhard",
        "Euler",
        state=ProtocolState.WAITING_FOR_ALLOCATERESOURCE,
        resources=[Resource("Hilbert", "hilbert.gov")],
    )

    bph = BrokerProtocolHandler(ctx.mapper, allocate_session=allocate_session)

    session_data, response = bph(AllocateResourceRequest(resource_id=0),
                                 protocol_session)

    assert isinstance(response, AllocateResourceSuccessResponse)
    assert response.sni == agent_session.sni
    assert response.port == agent_session.port
    assert response.session_id == agent_session.session_id
    assert response.connect_tag == agent_session.session_tag
    assert response.protocol == "PCOIP"

    assert session_data is not None
    assert session_data.state == ProtocolState.WAITING_FOR_BYE
Exemplo n.º 3
0
def test_broker_protocol_handler_call_waiting_for_authenticate_authenticate_success(
        ctx: Fixture):
    bph = BrokerProtocolHandler(ctx.mapper)

    ctx.mapper.resources = [Resource("Kurt", "Gödel")]

    session_data, response = bph(
        AuthenticateRequest(
            ctx.mapper.username,
            ctx.mapper.password,
            ctx.mapper.domain,
        ),
        ProtocolSession(state=ProtocolState.WAITING_FOR_AUTHENTICATE),
    )

    assert session_data.state == ProtocolState.WAITING_FOR_GETRESOURCELIST

    assert isinstance(response, AuthenticateSuccessResponse)

    assert isinstance(session_data, ProtocolSession)
    assert session_data.username == ctx.mapper.username
    assert session_data.password == ctx.mapper.password
    assert list(session_data.resources.values()) == ctx.mapper.resources

    assert ctx.mapper.map_called is True
Exemplo n.º 4
0
 def dummy_protocol(msg, data):
     return (
         ProtocolSession("leonhard",
                         "euler",
                         state=ProtocolState.WAITING_FOR_HELLO,
                         domain="example.com"),
         HelloResponse("lagrange", ["example.com"]),
     )
Exemplo n.º 5
0
def test_broker_protocol_handler_call_waiting_for_bye_bye(ctx: Fixture):
    bph = BrokerProtocolHandler(ctx.mapper)

    session_data, response = bph(
        ByeRequest(), ProtocolSession(state=ProtocolState.WAITING_FOR_BYE))

    assert session_data is None
    assert isinstance(response, ByeResponse)
Exemplo n.º 6
0
def test_broker_protocol_handler_call_waiting_for_authenticate_other_message(
        ctx: Fixture):
    bph = BrokerProtocolHandler(ctx.mapper)
    session_data, response = bph(
        HelloRequest(client_hostname="Euler", client_product_name="Abel"),
        ProtocolSession(state=ProtocolState.WAITING_FOR_AUTHENTICATE),
    )

    assert response is None

    assert session_data is None
Exemplo n.º 7
0
def test_broker_protocol_handler_call_waiting_for_hello_other_message(
        ctx: Fixture):
    bph = BrokerProtocolHandler(ctx.mapper)

    session_data, response = bph(
        AuthenticateRequest("Leonhard", "Euler", "example.com"),
        ProtocolSession(state=ProtocolState.WAITING_FOR_HELLO),
    )

    assert response is None

    assert session_data is None
Exemplo n.º 8
0
def test_broker_protocol_handler_call_waiting_for_allocateresource_allocateresource_session_in_use(
    ctx: Fixture, ):
    def allocate_session(*args, **kwargs):
        return AllocateSessionStatus.FAILED_ANOTHER_SESSION_STARTED, None

    protocol_session = ProtocolSession(
        "Leonhard",
        "Euler",
        state=ProtocolState.WAITING_FOR_ALLOCATERESOURCE,
        resources={"0": Resource("Hilbert", "hilbert.gov")},
    )

    bph = BrokerProtocolHandler(ctx.mapper, allocate_session=allocate_session)

    session_data, response = bph(AllocateResourceRequest(resource_id="0"),
                                 protocol_session)

    assert isinstance(response, AllocateResourceFailureResponse)
    assert response.result_id == "FAILED_ANOTHER_SESION_STARTED"

    assert session_data is not None
    assert session_data.state == ProtocolState.WAITING_FOR_ALLOCATERESOURCE
Exemplo n.º 9
0
def test_broker_protocol_handler_call_waiting_for_allocateresource_allocateresource_failure_to_connect(
    ctx: Fixture, ):
    def allocate_session(*args, **kwargs):
        return AllocateSessionStatus.CONNECTION_ERROR, None

    protocol_session = ProtocolSession(
        "Leonhard",
        "Euler",
        state=ProtocolState.WAITING_FOR_ALLOCATERESOURCE,
        resources={"0": Resource("Hilbert", "hilbert.gov")},
    )

    bph = BrokerProtocolHandler(ctx.mapper, allocate_session=allocate_session)

    session_data, response = bph(AllocateResourceRequest(resource_id="0"),
                                 protocol_session)

    assert isinstance(response, AllocateResourceFailureResponse)
    # TODO: We should use the proper response here.
    assert response.result_id == "FAILED_USER_AUTH"

    assert session_data is not None
    assert session_data.state == ProtocolState.WAITING_FOR_ALLOCATERESOURCE
Exemplo n.º 10
0
def test_broker_protocol_handler_call_waiting_for_authenticate_authenticate_failed(
        ctx: Fixture):
    bph = BrokerProtocolHandler(ctx.mapper)

    ctx.mapper.resources = {"0": Resource("Kurt", "Gödel")}

    session_data, response = bph(
        AuthenticateRequest(ctx.mapper.username, ctx.mapper.password + "wrong",
                            ctx.mapper.domain),
        ProtocolSession(state=ProtocolState.WAITING_FOR_AUTHENTICATE),
    )

    assert session_data.state == ProtocolState.WAITING_FOR_AUTHENTICATE

    assert isinstance(response, AuthenticateFailedResponse)

    assert isinstance(session_data, ProtocolSession)
    # The authentication failed so we shouldn't set these.
    assert session_data.username is None
    assert session_data.password is None
    assert list(session_data.resources) == []

    assert ctx.mapper.map_called is True
Exemplo n.º 11
0
def test_broker_protocol_handler_call_waiting_for_authenticate_authenticate_no_resources(
    ctx: Fixture, ):
    bph = BrokerProtocolHandler(ctx.mapper)

    ctx.mapper.resources = []

    session_data, response = bph(
        AuthenticateRequest(ctx.mapper.username, ctx.mapper.password,
                            ctx.mapper.domain),
        ProtocolSession(state=ProtocolState.WAITING_FOR_AUTHENTICATE),
    )

    assert session_data.state == ProtocolState.WAITING_FOR_AUTHENTICATE

    # In this case, authentication should be successful but there are no machines, so we have to fail
    # anyway, and not set the session data.
    assert isinstance(response, AuthenticateFailedResponse)

    assert isinstance(session_data, ProtocolSession)
    assert session_data.username is None
    assert session_data.password is None
    assert list(session_data.resources) == []

    assert ctx.mapper.map_called is True