173 lines
5.0 KiB
C++
173 lines
5.0 KiB
C++
#include <stdint.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <stddef.h>
|
|
#include <getopt.h>
|
|
#include <stdbool.h>
|
|
#include <gtest/gtest.h>
|
|
#ifdef __cplusplus
|
|
extern "C"
|
|
{
|
|
#endif
|
|
#include "stellar/monitor.h"
|
|
#include "stellar/stellar.h"
|
|
#include "monitor/monitor_private.h"
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
static sds *stellar_cli_exec_cmd(const char *command_str, int *result_size)
|
|
{
|
|
int ret;
|
|
sds *cmd_result_array = NULL;
|
|
char result[4096] = {};
|
|
FILE *fp = popen(command_str, "r");
|
|
if (NULL == fp)
|
|
{
|
|
return NULL;
|
|
}
|
|
ret = fread(result, 1, sizeof(result), fp);
|
|
if (ret > 0)
|
|
{
|
|
cmd_result_array = sdssplitlen(result, strlen(result), "\r\n", 1, result_size);
|
|
}
|
|
pclose(fp);
|
|
return cmd_result_array;
|
|
}
|
|
|
|
#if 0 // for TEST_F
|
|
class MonitorServerMock : public testing::Test
|
|
{
|
|
public:
|
|
static struct stellar *st;
|
|
static pthread_t tid;
|
|
static int thread_count;
|
|
static int cmd_result_line_num;
|
|
static int cmd_array_size;
|
|
static sds *cmd_result_array;
|
|
|
|
protected:
|
|
static void SetUpTestCase()
|
|
{
|
|
printf("Gtest Stm Server: Setup Test Case Env...\n");
|
|
st = stellar_new("./conf/stellar.toml");
|
|
assert(st != NULL);
|
|
stellar_run(st);
|
|
}
|
|
static void TearDownTestCase()
|
|
{
|
|
printf("Gtest Stm Server: Tear Down Test Case Env...\n");
|
|
stellar_free(st);
|
|
}
|
|
};
|
|
pthread_t MonitorServerMock::tid;
|
|
int MonitorServerMock::thread_count;
|
|
int MonitorServerMock::cmd_result_line_num;
|
|
struct stellar *MonitorServerMock::st;
|
|
int MonitorServerMock::cmd_array_size;
|
|
sds *MonitorServerMock::cmd_result_array;
|
|
#endif
|
|
|
|
TEST(monitor_server, curl_ping)
|
|
{
|
|
int cmd_array_size;
|
|
sds *cmd_result_array;
|
|
struct stellar *st = stellar_new("./conf/stellar.toml");
|
|
assert(st != NULL);
|
|
stellar_run(st);
|
|
cmd_result_array = stellar_cli_exec_cmd("curl --silent http://127.0.0.1:80/v1/stellar_monitor?raw_cmd=ping", &cmd_array_size);
|
|
ASSERT_TRUE(cmd_array_size >= 1);
|
|
ASSERT_TRUE(cmd_result_array != NULL);
|
|
EXPECT_STREQ("pong", cmd_result_array[0]);
|
|
sdsfreesplitres(cmd_result_array, cmd_array_size);
|
|
stellar_free(st);
|
|
}
|
|
|
|
TEST(monitor_server, curl_ping_messge)
|
|
{
|
|
int cmd_array_size;
|
|
sds *cmd_result_array;
|
|
struct stellar *st = stellar_new("./conf/stellar.toml");
|
|
assert(st != NULL);
|
|
stellar_run(st);
|
|
cmd_result_array = stellar_cli_exec_cmd("curl --silent http://127.0.0.1:80/v1/stellar_monitor?raw_cmd=ping%20hello,world", &cmd_array_size);
|
|
ASSERT_TRUE(cmd_array_size >= 1);
|
|
ASSERT_TRUE(cmd_result_array != NULL);
|
|
EXPECT_STREQ("hello,world", cmd_result_array[0]);
|
|
sdsfreesplitres(cmd_result_array, cmd_array_size);
|
|
stellar_free(st);
|
|
}
|
|
|
|
TEST(monitor_server, stellar_cli_ping)
|
|
{
|
|
int cmd_array_size;
|
|
sds *cmd_result_array;
|
|
struct stellar *st = stellar_new("./conf/stellar.toml");
|
|
assert(st != NULL);
|
|
stellar_run(st);
|
|
cmd_result_array = stellar_cli_exec_cmd("./stellar-cli -e ping", &cmd_array_size);
|
|
ASSERT_TRUE(cmd_array_size >= 1);
|
|
ASSERT_TRUE(cmd_result_array != NULL);
|
|
EXPECT_STREQ("pong", cmd_result_array[0]);
|
|
sdsfreesplitres(cmd_result_array, cmd_array_size);
|
|
stellar_free(st);
|
|
}
|
|
|
|
TEST(monitor_server, stellar_cli_ping_messge)
|
|
{
|
|
int cmd_array_size;
|
|
sds *cmd_result_array;
|
|
struct stellar *st = stellar_new("./conf/stellar.toml");
|
|
assert(st != NULL);
|
|
stellar_run(st);
|
|
cmd_result_array = stellar_cli_exec_cmd("./stellar-cli -e ping hello,world", &cmd_array_size);
|
|
ASSERT_TRUE(cmd_array_size >= 1);
|
|
ASSERT_TRUE(cmd_result_array != NULL);
|
|
EXPECT_STREQ("hello,world", cmd_result_array[0]);
|
|
sdsfreesplitres(cmd_result_array, cmd_array_size);
|
|
stellar_free(st);
|
|
}
|
|
|
|
TEST(monitor_server, not_found)
|
|
{
|
|
int cmd_array_size;
|
|
sds *cmd_result_array;
|
|
struct stellar *st = stellar_new("./conf/stellar.toml");
|
|
assert(st != NULL);
|
|
stellar_run(st);
|
|
cmd_result_array = stellar_cli_exec_cmd("./stellar-cli -e xxxxxxx", &cmd_array_size);
|
|
ASSERT_TRUE(cmd_array_size >= 1);
|
|
ASSERT_TRUE(cmd_result_array != NULL);
|
|
printf("cmd_result_array[0]: %s\n", cmd_result_array[0]);
|
|
EXPECT_TRUE(strstr(cmd_result_array[0], "ERR unknown command") != NULL);
|
|
sdsfreesplitres(cmd_result_array, cmd_array_size);
|
|
stellar_free(st);
|
|
}
|
|
|
|
TEST(monitor_server, invalid_args)
|
|
{
|
|
int cmd_array_size;
|
|
sds *cmd_result_array;
|
|
struct stellar *st = stellar_new("./conf/stellar.toml");
|
|
assert(st != NULL);
|
|
stellar_run(st);
|
|
cmd_result_array = stellar_cli_exec_cmd("./stellar-cli -e ping x y z ", &cmd_array_size);
|
|
ASSERT_TRUE(cmd_array_size >= 1);
|
|
ASSERT_TRUE(cmd_result_array != NULL);
|
|
printf("cmd_result_array[0]: %s\n", cmd_result_array[0]);
|
|
EXPECT_TRUE(strstr(cmd_result_array[0], "ERR wrong number of arguments") != NULL);
|
|
sdsfreesplitres(cmd_result_array, cmd_array_size);
|
|
stellar_free(st);
|
|
}
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
testing::InitGoogleTest(&argc, argv);
|
|
int ret = RUN_ALL_TESTS();
|
|
return ret;
|
|
}
|