added grpc for a node command and fixed grpc unit tests
This commit is contained in:
parent
e063fcd4fe
commit
1890db5991
5 changed files with 52 additions and 2 deletions
|
@ -329,6 +329,19 @@ class CoreGrpcClient(object):
|
|||
request = core_pb2.DeleteNodeRequest(session_id=session_id, node_id=node_id)
|
||||
return self.stub.DeleteNode(request)
|
||||
|
||||
def node_command(self, session_id, node_id, command):
|
||||
"""
|
||||
Send command to a node and get the output.
|
||||
|
||||
:param int session_id: session id
|
||||
:param int node_id: node id
|
||||
:return: response with command combined stdout/stderr
|
||||
:rtype: core_pb2.NodeCommandResponse
|
||||
:raises grpc.RpcError: when session or node doesn't exist
|
||||
"""
|
||||
request = core_pb2.NodeCommandRequest(session_id=session_id, node_id=node_id, command=command)
|
||||
return self.stub.NodeCommand(request)
|
||||
|
||||
def get_node_links(self, session_id, node_id):
|
||||
"""
|
||||
Get current links for a node.
|
||||
|
|
|
@ -484,6 +484,13 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
|
|||
result = session.delete_node(request.node_id)
|
||||
return core_pb2.DeleteNodeResponse(result=result)
|
||||
|
||||
def NodeCommand(self, request, context):
|
||||
logging.debug("sending node command: %s", request)
|
||||
session = self.get_session(request.session_id, context)
|
||||
node = self.get_node(session, request.node_id, context)
|
||||
_, output = node.cmd_output(request.command)
|
||||
return core_pb2.NodeCommandResponse(output=output)
|
||||
|
||||
def GetNodeLinks(self, request, context):
|
||||
logging.debug("get node links: %s", request)
|
||||
session = self.get_session(request.session_id, context)
|
||||
|
|
|
@ -39,6 +39,8 @@ service CoreApi {
|
|||
}
|
||||
rpc DeleteNode (DeleteNodeRequest) returns (DeleteNodeResponse) {
|
||||
}
|
||||
rpc NodeCommand (NodeCommandRequest) returns (NodeCommandResponse) {
|
||||
}
|
||||
|
||||
// link rpc
|
||||
rpc GetNodeLinks (GetNodeLinksRequest) returns (GetNodeLinksResponse) {
|
||||
|
@ -300,6 +302,16 @@ message DeleteNodeResponse {
|
|||
bool result = 1;
|
||||
}
|
||||
|
||||
message NodeCommandRequest {
|
||||
int32 session_id = 1;
|
||||
int32 node_id = 2;
|
||||
string command = 3;
|
||||
}
|
||||
|
||||
message NodeCommandResponse {
|
||||
string output = 1;
|
||||
}
|
||||
|
||||
message GetNodeLinksRequest {
|
||||
int32 session_id = 1;
|
||||
int32 node_id = 2;
|
||||
|
|
|
@ -212,7 +212,7 @@ class CoreServerTest(object):
|
|||
def grpc_server():
|
||||
coremu = CoreEmu()
|
||||
grpc_server = CoreGrpcServer(coremu)
|
||||
thread = threading.Thread(target=grpc_server.listen)
|
||||
thread = threading.Thread(target=grpc_server.listen, args=("localhost:50051",))
|
||||
thread.daemon = True
|
||||
thread.start()
|
||||
time.sleep(0.1)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import time
|
||||
|
||||
from Queue import Queue
|
||||
|
||||
import grpc
|
||||
|
@ -8,6 +7,7 @@ import pytest
|
|||
from core.conf import ConfigShim
|
||||
from core.data import EventData
|
||||
from core.emane.ieee80211abg import EmaneIeee80211abgModel
|
||||
from core.emulator.emudata import NodeOptions
|
||||
from core.enumerations import NodeTypes, EventTypes, ConfigFlags, ExceptionLevels
|
||||
from core.grpc import core_pb2
|
||||
from core.grpc.client import CoreGrpcClient
|
||||
|
@ -239,6 +239,24 @@ class TestGrpc:
|
|||
with pytest.raises(KeyError):
|
||||
assert session.get_object(node.objid)
|
||||
|
||||
def test_node_command(self, grpc_server):
|
||||
# given
|
||||
client = CoreGrpcClient()
|
||||
session = grpc_server.coreemu.create_session()
|
||||
session.set_state(EventTypes.CONFIGURATION_STATE)
|
||||
node_options = NodeOptions(model="Host")
|
||||
node = session.add_node(node_options=node_options)
|
||||
session.instantiate()
|
||||
output = "hello world"
|
||||
|
||||
# then
|
||||
command = "echo %s" % output
|
||||
with client.context_connect():
|
||||
response = client.node_command(session.id, node.objid, command)
|
||||
|
||||
# then
|
||||
assert response.output == output
|
||||
|
||||
def test_get_hooks(self, grpc_server):
|
||||
# given
|
||||
client = CoreGrpcClient()
|
||||
|
|
Loading…
Reference in a new issue