Skip to content

Commit

Permalink
Write command line execute command output to stdout.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Jun 10, 2020
1 parent ad4d7e5 commit fe5e35c
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 11 deletions.
4 changes: 3 additions & 1 deletion bunga/subparsers/execute.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

from ..client import Client
from ..client import ClientThread

Expand All @@ -8,7 +10,7 @@ def _do_execute(args):
client.wait_for_connection()

for command in args.commands:
client.execute_command(command)
sys.stdout.buffer.write(client.execute_command(command))


def add_subparser(subparsers):
Expand Down
2 changes: 1 addition & 1 deletion bunga/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.35.0'
__version__ = '0.36.0'
110 changes: 101 additions & 9 deletions lib/tst/test_server.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <errno.h>
#include <pthread.h>
#include <sys/eventfd.h>
#include <sys/epoll.h>
Expand Down Expand Up @@ -168,7 +169,7 @@ static void shell_execute_command_date(char *line_p, FILE *fout_p)
fprintf(fout_p, "Today!");
}

static void execute_command_process_on_execute_command_req(
static void execute_command_process_on_execute_command_req_date(
struct bunga_server_t *self_p,
int fd,
uint32_t events)
Expand Down Expand Up @@ -223,37 +224,55 @@ static void execute_command_send_result(struct bunga_server_t *self_p,
ASSERT_EQ(execute_command_rsp[1].output.size, 0);
}

TEST(execute_command)
static void mock_prepare_tcp_connect(void)
{
struct epoll_event event;

mock_prepare_server_main_until_epoll();

/* TCP connect. */
epoll_wait_mock_once(10, 1, -1, 1);
event.events = EPOLLIN;
event.data.fd = CLIENT_FD;
epoll_wait_mock_set_events_out(&event, sizeof(event));
bunga_server_process_mock_once(CLIENT_FD, event.events);
bunga_server_process_mock_set_callback(
execute_command_process_on_client_connected);
}

static void mock_prepare_connect(void)
{
struct epoll_event event;

/* Connect message. */
epoll_wait_mock_once(10, 1, -1, 1);
event.events = EPOLLIN;
event.data.fd = CLIENT_FD;
epoll_wait_mock_set_events_out(&event, sizeof(event));
bunga_server_process_mock_once(CLIENT_FD, event.events);
bunga_server_process_mock_set_callback(execute_command_process_on_connect_req);
}

static void mock_prepare_execute_command(
void (*callback)(struct bunga_server_t *self_p,
int fd,
uint32_t events))
{
struct epoll_event event;

/* Execute command message. */
epoll_wait_mock_once(10, 1, -1, 1);
event.events = EPOLLIN;
event.data.fd = CLIENT_FD;
epoll_wait_mock_set_events_out(&event, sizeof(event));
bunga_server_process_mock_once(CLIENT_FD, event.events);
bunga_server_process_mock_set_callback(
execute_command_process_on_execute_command_req);
bunga_server_process_mock_set_callback(callback);
}

TEST(execute_command_date_ok)
{
struct epoll_event event;

mock_prepare_server_main_until_epoll();
mock_prepare_tcp_connect();
mock_prepare_connect();
mock_prepare_execute_command(
execute_command_process_on_execute_command_req_date);

/* Execute command completion. */
epoll_wait_mock_once(10, 1, -1, 1);
Expand All @@ -280,3 +299,76 @@ TEST(execute_command)

call_server_main();
}

static void execute_command_process_on_execute_command_req_date_error(
struct bunga_server_t *self_p,
int fd,
uint32_t events)
{
(void)fd;
(void)events;

struct bunga_execute_command_req_t execute_command_req;
uint8_t *message_p;
int alloc_handle;
int spawn_handle;
struct nala_ml_message_alloc_params_t *alloc_params_p;
struct nala_ml_spawn_params_t *spawn_params_p;

message_p = nala_alloc(56);
alloc_handle = ml_message_alloc_mock_once(56, message_p);
spawn_handle = ml_spawn_mock_once();

execute_command_req.command_p = "date";
call_on_execute_command_req(self_p, &execute_command_req);
alloc_params_p = ml_message_alloc_mock_get_params_in(alloc_handle);

spawn_params_p = ml_spawn_mock_get_params_in(spawn_handle);
ml_shell_execute_command_mock_once("date", -EINVAL);
ml_queue_put_mock_once();

spawn_params_p->entry(spawn_params_p->arg_p);

ml_queue_get_mock_once(alloc_params_p->uid_p);
ml_queue_get_mock_set_message_pp_out((void **)&message_p, sizeof(message_p));
}

static void execute_command_send_error(struct bunga_server_t *self_p,
struct bunga_server_client_t *client_p)
{
(void)self_p;
(void)client_p;

ASSERT_EQ(execute_command_rsp[0].error_p, "Invalid argument");
ASSERT_EQ(execute_command_rsp[0].output.size, 0);
}

TEST(execute_command_date_error)
{
struct epoll_event event;

mock_prepare_server_main_until_epoll();
mock_prepare_tcp_connect();
mock_prepare_connect();
mock_prepare_execute_command(
execute_command_process_on_execute_command_req_date_error);

/* Execute command completion. */
epoll_wait_mock_once(10, 1, -1, 1);
event.events = EPOLLIN;
event.data.fd = PUT_FD;
epoll_wait_mock_set_events_out(&event, sizeof(event));

execute_command_rsp[0].error_p = "";
execute_command_rsp[0].output.size = 0;
bunga_server_init_execute_command_rsp_mock_once(&execute_command_rsp[0]);
bunga_server_send_mock_once();
bunga_server_send_mock_set_callback(execute_command_send_error);

ml_message_free_mock_once();

/* End loop. */
epoll_wait_mock_once(10, 1, -1, -1);

call_server_main();
}

0 comments on commit fe5e35c

Please sign in to comment.