Exemple #1
0
def test_rack():
    message = message_factory.create("RackCreateVMReq")

    # server_stack
    algo_server = Algorithm(HOST, PORT + 2000)
    node_server = Node(HOST, PORT + 1000)
    rack_server = Rack(HOST, PORT)

    # algo setting
    algo_server.node_addr = node_server.addr()

    # rack setting
    rack_server.algorithm_addr = algo_server.addr()

    server_stack = [algo_server, node_server, rack_server]

    for server in server_stack:
        def server_thread():
            server.run()

        t = threading.Thread(target = server_thread)
        t.start()

    # client
    try:
        sleep(3)
        client.sendonly_message((HOST, PORT), message)

    finally:
        for server in reversed(server_stack):
            server.shutdown()

    assert len(rack_server.request_context) == 0
Exemple #2
0
def test_node():
    message = message_factory.create("NodeCreateVMReq")

    # server
    server = Node(HOST, PORT)
    server.createVMReqHandler = MagicMock(return_value="hello world")

    # test start threading
    server.sayHello = MagicMock(return_value="hello")
    server.start_thread(target=server.sayHello)

    def server_thread():
        server.run()

    t = threading.Thread(target = server_thread)
    t.start()

    # client
    try:
        sleep(3)

        ret = client.send_message((HOST, PORT), message)
        assert ret == "hello world"

        client.sendonly_message((HOST, PORT), message)

    finally:
        server.shutdown()

    server.sayHello.assert_called_with()
    server.createVMReqHandler.assert_called_with(message, ('127.0.0.1', ANY))
Exemple #3
0
def test_rack():
    message = message_factory.create("RackCreateVMReq")

    # server_stack
    algo_server = Algorithm(HOST, PORT + 2000)
    node_server = Node(HOST, PORT + 1000)
    rack_server = Rack(HOST, PORT)

    # algo setting
    algo_server.node_addr = node_server.addr()

    # rack setting
    rack_server.algorithm_addr = algo_server.addr()

    server_stack = [algo_server, node_server, rack_server]

    for server in server_stack:

        def server_thread():
            server.run()

        t = threading.Thread(target=server_thread)
        t.start()

    # client
    try:
        sleep(3)
        client.sendonly_message((HOST, PORT), message)

    finally:
        for server in reversed(server_stack):
            server.shutdown()

    assert len(rack_server.request_context) == 0
Exemple #4
0
def test_node():
    message = message_factory.create("NodeCreateVMReq")

    # server
    server = Node(HOST, PORT)
    server.createVMReqHandler = MagicMock(return_value="hello world")

    # test start threading
    server.sayHello = MagicMock(return_value="hello")
    server.start_thread(target=server.sayHello)

    def server_thread():
        server.run()

    t = threading.Thread(target=server_thread)
    t.start()

    # client
    try:
        sleep(3)

        ret = client.send_message((HOST, PORT), message)
        assert ret == "hello world"

        client.sendonly_message((HOST, PORT), message)

    finally:
        server.shutdown()

    server.sayHello.assert_called_with()
    server.createVMReqHandler.assert_called_with(message, ('127.0.0.1', ANY))
Exemple #5
0
def test_base_server():

    # server
    server = BaseServer(HOST, PORT)
    server.register_handle_function("CmdHello", hello)

    holder = {'hello': ""}
    def sayHello():
        holder['hello'] = "world"

    server.register_start_function(sayHello)
    def server_thread():
        server.run()

    t = threading.Thread(target = server_thread)
    t.start()

    sleep(4)
    # client
    try:
        ret = client.send_message((HOST, PORT), CmdHello("world"))
        assert ret == "hello world"

        client.sendonly_message((HOST, PORT), CmdHello("world"))

    finally:
        server.shutdown()

    assert holder["hello"] == "world"
Exemple #6
0
def test_base_server():

    # server
    server = BaseServer(HOST, PORT)
    server.register_handle_function("CmdHello", hello)

    holder = {'hello': ""}

    def sayHello():
        holder['hello'] = "world"

    server.register_start_function(sayHello)

    def server_thread():
        server.run()

    t = threading.Thread(target=server_thread)
    t.start()

    sleep(4)
    # client
    try:
        ret = client.send_message((HOST, PORT), CmdHello("world"))
        assert ret == "hello world"

        client.sendonly_message((HOST, PORT), CmdHello("world"))

    finally:
        server.shutdown()

    assert holder["hello"] == "world"
Exemple #7
0
def test_cluster():
    message = message_factory.create("ClusterCreateVMReq")

    # server_stack
    algo_server = Algorithm(HOST, PORT + 3000)
    node_server = Node(HOST, PORT + 2000)
    rack_server = Rack(HOST, PORT + 1000)
    cluster_server = Cluster(HOST, PORT)

    # algo setting
    algo_server.node_addr = node_server.addr()
    algo_server.rack_addr = rack_server.addr()

    # cluster setting
    cluster_server.algorithm_addr = algo_server.addr()
    holder = {'createVMResHandler_get_called': False}

    def f(self, message):
        holder['createVMResHandler_get_called'] = True

    cluster_server.createVMResHandler = f

    # rack setting
    rack_server.algorithm_addr = algo_server.addr()

    server_stack = [algo_server, node_server, rack_server, cluster_server]

    for server in server_stack:
        def server_thread():
            server.run()

        t = threading.Thread(target = server_thread)
        t.start()

    # client
    try:
        sleep(3)
        client.sendonly_message((HOST, PORT), message)

        counter = 0
        while counter < 10 and holder['createVMResHandler_get_called'] == False:
            sleep(1)
            counter += 1
    except:
        print 'something wrong'
    finally:
        for server in reversed(server_stack):
            server.shutdown()

    assert holder['createVMResHandler_get_called'] == True