提交 650170f0 编写于 作者: W wujing

add container list unit test

Signed-off-by: Nwujing <wujing50@huawei.com>
上级 46ff577a
......@@ -21,6 +21,10 @@
#include "host_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef COMMANDER_MAX_OPTIONS
#define COMMANDER_MAX_OPTIONS 64
#endif
......@@ -112,5 +116,9 @@ int check_default_ulimit_type(const char *type);
void free_default_ulimit(host_config_ulimits_element **default_ulimit);
#ifdef __cplusplus
}
#endif
#endif /* COMMANDER_H */
......@@ -26,6 +26,10 @@
#include "json_common.h"
#include "isula_connect.h"
#ifdef __cplusplus
extern "C" {
#endif
/* max arguments can be specify in client */
#define MAX_CLIENT_ARGS 1000
......@@ -350,5 +354,9 @@ extern void client_print_error(uint32_t cc, uint32_t server_errono, const char *
extern client_connect_config_t get_connect_config(const struct client_arguments *args);
#ifdef __cplusplus
}
#endif
#endif /* __ISULA_ARGUMENTS_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define CREATE_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_CALLBACK, false, "accel", 0, &(cmdargs).custom_conf.accel, \
"Accelerator bindings (format: [<name>=]<runtime>[@<driver>[,<options>]])", \
......@@ -169,5 +173,9 @@ int callback_annotation(command_option_t *option, const char *value);
int cmd_create_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_CREATE_H */
......@@ -18,6 +18,10 @@
#include "arguments.h"
#include "wait.h"
#ifdef __cplusplus
extern "C" {
#endif
#define KILL_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "signal", 's', &(cmdargs).signal, \
"Signal to send to the container (default \"SIGKILL\")", NULL }
......@@ -26,5 +30,10 @@ extern const char g_cmd_kill_desc[];
extern const char g_cmd_kill_usage[];
extern struct client_arguments g_cmd_kill_args;
int cmd_kill_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_rename_desc[];
extern const char g_cmd_rename_usage[];
extern struct client_arguments g_cmd_rename_args;
int cmd_rename_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -15,6 +15,10 @@
#ifndef __CMD_RESTART_H
#define __CMD_RESTART_H
#ifdef __cplusplus
extern "C" {
#endif
#define RESTART_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_CALLBACK, false, "time", 't', &(cmdargs).time, \
"Seconds to wait for stop before killing it (default 10)", command_convert_int }
......@@ -24,5 +28,9 @@ extern const char g_cmd_restart_usage[];
extern struct client_arguments g_cmd_restart_args;
int cmd_restart_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_RESTART_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define DELETE_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "force", 'f', &(cmdargs).force, \
"Force the removal of a running container (uses SIGKILL)", NULL }, \
......@@ -28,5 +32,9 @@ extern const char g_cmd_delete_usage[];
extern struct client_arguments g_cmd_delete_args;
int cmd_delete_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_DELETE_H */
......@@ -19,6 +19,10 @@
#include "start.h"
#include "wait.h"
#ifdef __cplusplus
extern "C" {
#endif
#define RUN_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "detach", 'd', &(cmdargs).detach, \
"Run container in background and print container ID", NULL }, \
......@@ -30,5 +34,9 @@ extern const char g_cmd_run_usage[];
extern struct client_arguments g_cmd_run_args;
int cmd_run_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_RUN_H */
......@@ -19,6 +19,10 @@
#include "commands.h"
#include <termios.h>
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_start_desc[];
extern struct client_arguments g_cmd_start_args;
......@@ -28,5 +32,10 @@ void client_restore_console(bool reset_tty, const struct termios *oldtios, struc
int client_start(const struct client_arguments *args, bool *reset_tty, struct termios *oldtios,
struct command_fifo_config **console_fifos);
int cmd_start_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_START_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define STOP_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "force", 'f', &(cmdargs).force, "Stop by force killing", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "time", 't', &(cmdargs).time, \
......@@ -28,5 +32,9 @@ extern struct client_arguments g_cmd_stop_args;
int cmd_stop_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_STOP_H */
......@@ -18,6 +18,10 @@
#include "arguments.h"
#include <semaphore.h>
#ifdef __cplusplus
extern "C" {
#endif
#define CLIENT_RUNDIR "/var/run/isula"
// A command is described by:
......@@ -69,5 +73,10 @@ int commmand_default_help(const char * const program_name,
const char **argv);
int run_command(struct command *commands, int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __COMMAND_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define EVENTS_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, \
"Name of the container", NULL }, \
......@@ -30,5 +34,9 @@ extern const char g_cmd_events_usage[];
extern struct client_arguments g_cmd_events_args;
int cmd_events_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_EVENT_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define EXPORT_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "output", 'o', &(cmdargs).file, "Write to a file", NULL }
......@@ -25,5 +29,9 @@ extern const char g_cmd_export_usage[];
extern struct client_arguments g_cmd_export_args;
int cmd_export_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_pause_desc[];
extern const char g_cmd_pause_usage[];
extern struct client_arguments g_cmd_pause_args;
int cmd_pause_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_resume_desc[];
extern const char g_cmd_resume_usage[];
extern struct client_arguments g_cmd_resume_args;
int cmd_resume_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define STATUS_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "all", 'a', &(cmdargs).showall, \
"Show all containers (default shows just running)", NULL }, \
......@@ -28,5 +32,9 @@ extern const char g_cmd_stats_usage[];
extern struct client_arguments g_cmd_stats_args;
int cmd_stats_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_STATS_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define UPDATE_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_CALLBACK, false, "cpu-shares", 0, &(cmdargs).cr.cpu_shares, \
"CPU shares (relative weight)", command_convert_llong }, \
......@@ -45,5 +49,9 @@ extern struct client_arguments g_cmd_update_args;
int cmd_update_main(int argc, const char **argv);
int update_checker(const struct client_arguments *args);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_UPDATE_H */
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_images_desc[];
extern const char g_cmd_images_usage[];
extern struct client_arguments g_cmd_images_args;
int cmd_images_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_IMAGES_LIST_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define LOAD_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "input", 'i', &(cmdargs).file, "Read from a manifest or an archive", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "tag", 0, &(cmdargs).tag, \
......@@ -29,5 +33,9 @@ extern const char g_cmd_load_desc[];
extern struct client_arguments g_cmd_load_args;
int cmd_load_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_LOAD_H */
......@@ -16,6 +16,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define LOGIN_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "username", 'u', &(cmdargs).username, "Username", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "password", 'p', &(cmdargs).password, "Password", NULL }, \
......@@ -28,5 +32,9 @@ extern const char g_cmd_login_usage[];
extern struct client_arguments g_cmd_login_args;
int cmd_login_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_LOGIN_H */
......@@ -16,10 +16,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_logout_desc[];
extern const char g_cmd_logout_usage[];
extern struct client_arguments g_cmd_logout_args;
int cmd_logout_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_LOGOUT_H */
......@@ -18,6 +18,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_pull_desc[];
extern const char g_cmd_pull_usage[];
extern struct client_arguments g_cmd_pull_args;
......@@ -25,5 +29,9 @@ int client_pull(const struct client_arguments *args);
int cmd_pull_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_PULL_IMAGE_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define RMI_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "force", 'f', &(cmdargs).force, "Force removal of the image", NULL }
......@@ -25,5 +29,9 @@ extern const char g_cmd_rmi_usage[];
extern struct client_arguments g_cmd_rmi_args;
int cmd_rmi_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_REMOVE_IMAGE_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define HEALTH_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "service", 'S', &(cmdargs).service, "GRPC service name", NULL }
......@@ -25,5 +29,9 @@ extern const char g_cmd_health_check_usage[];
extern struct client_arguments g_cmd_health_check_args;
int cmd_health_check_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_info_desc[];
extern const char g_cmd_info_usage[];
extern struct client_arguments g_cmd_info_args;
int cmd_info_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_INFO_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define INSPECT_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "format", 'f', &(cmdargs).format, \
"Format the output using the given go template", NULL }, \
......@@ -28,5 +32,9 @@ extern const char g_cmd_inspect_usage[];
extern struct client_arguments g_cmd_inspect_args;
int cmd_inspect_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_INSPECT_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define LOGS_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "follow", 'f', &(cmdargs).follow, "Follow log output", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "tail", 0, &(cmdargs).tail, \
......@@ -28,5 +32,10 @@ extern struct client_arguments g_cmd_logs_args;
int callback_tail(command_option_t *option, const char *arg);
int cmd_logs_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_LOGS_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define LIST_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "all", 'a', &(cmdargs).list_all, \
"Display all containers (default shows just running)", NULL }, \
......@@ -33,5 +37,9 @@ extern const char g_cmd_list_usage[];
extern struct client_arguments g_cmd_list_args;
int cmd_list_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_LIST_H */
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_top_desc[];
extern const char g_cmd_top_usage[];
extern struct client_arguments g_cmd_top_args;
int cmd_top_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_TOP_H */
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_version_desc[];
extern const char g_cmd_version_usage[];
extern struct client_arguments g_cmd_version_args;
int cmd_version_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_VERSION_H */
......@@ -17,11 +17,19 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_wait_desc[];
extern const char g_cmd_wait_usage[];
extern struct client_arguments g_cmd_wait_args;
int cmd_wait_main(int argc, const char **argv);
int client_wait(const struct client_arguments *args, unsigned int *exit_code);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_WAIT_H */
project(iSulad_LLT)
function(gmock_find_library _name)
find_library(${_name}
NAMES ${ARGN}
HINTS
$ENV{GMOCK_ROOT}
${GMOCK_ROOT}
)
mark_as_advanced(${_name})
endfunction()
find_path(GMOCK_INCLUDE_DIR gmock/gmock.h
HINTS
$ENV{GMOCK_ROOT}/include
${GMOCK_ROOT}/include
)
mark_as_advanced(GMOCK_INCLUDE_DIR)
gmock_find_library(GMOCK_LIBRARY gmock)
gmock_find_library(GMOCK_LIBRARY_DEBUG gmockd)
gmock_find_library(GMOCK_MAIN_LIBRARY gmock_main)
gmock_find_library(GMOCK_MAIN_LIBRARY_DEBUG gmock_maind)
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(GMock DEFAULT_MSG GMOCK_LIBRARY GMOCK_INCLUDE_DIR GMOCK_MAIN_LIBRARY)
if(GMOCK_FOUND)
set(GMOCK_INCLUDE_DIRS ${GMOCK_INCLUDE_DIR})
set(GMOCK_BOTH_LIBRARIES ${GMOCK_LIBRARIES} ${GMOCK_MAIN_LIBRARIES})
endif()
# setup testing
find_package(Threads REQUIRED)
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
include_directories(${GMOCK_INCLUDE_DIRS})
add_subdirectory(cutils)
add_subdirectory(image)
add_subdirectory(path)
add_subdirectory(cmd)
project(iSulad_LLT)
add_subdirectory(isula)
project(iSulad_LLT)
add_subdirectory(infomation)
project(iSulad_LLT)
add_subdirectory(ps)
project(iSulad_LLT)
SET(EXE ps_llt)
add_executable(${EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/log.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_string.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_array.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_file.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_convert.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_verify.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_regex.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/sha256/sha256.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/path.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/commander.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/console/console.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/arguments.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/libisulad.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/libisula.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/types_def.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/mainloop.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/container_def.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/error.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client/isula_connect.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json/schema/src/read_file.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/information/ps.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/grpc_client_mock.cc
${CMAKE_BINARY_DIR}/json/json_common.c
${CMAKE_BINARY_DIR}/json/host_config.c
${CMAKE_BINARY_DIR}/json/container_path_stat.c
${CMAKE_BINARY_DIR}/json/timestamp.c
ps_llt.cc)
target_include_directories(${EXE} PUBLIC
${GTEST_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../include
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/sha256
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/http
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/console
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/information
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client/grpc
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json/schema/src
${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks
${CMAKE_BINARY_DIR}/json
${CMAKE_BINARY_DIR}/conf
)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lsecurec -lz)
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
* Description: ps llt
* Author: wujing
* Create: 2019-12-19
*/
#include "ps.h"
#include <ctime>
#include <cmath>
#include <random>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "grpc_client_mock.h"
#include "utils.h"
using ::testing::Args;
using ::testing::ByRef;
using ::testing::SetArgPointee;
using ::testing::DoAll;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::NotNull;
using ::testing::AtLeast;
using ::testing::Invoke;
using ::testing::_;
using namespace std;
class ContainerListUnitTest : public testing::Test {
public:
void SetUp() override
{
GrpcClient_SetMock(&m_grpcClient);
::testing::Mock::AllowLeak(&m_grpcClient);
}
void TearDown() override
{
GrpcClient_SetMock(nullptr);
}
NiceMock<MockGrpcClient> m_grpcClient;
};
namespace {
unsigned generate_random_pid()
{
constexpr int pid_start = 10000;
constexpr int pid_end = 10000;
static default_random_engine e(time(0));
static uniform_int_distribution<unsigned> u(pid_start, pid_end);
return u(e);
}
long long generate_random_created()
{
// unix nanos: 2019-01-01T00:00:00.000000000+08:00
constexpr int64_t start = 1546272000000000000ll;
// unix nanos: 2019-12-31T00:00:00.000000000+08:00
constexpr int64_t end = 1577721600000000000ll;
static default_random_engine e(time(0));
static uniform_int_distribution<long long> u(start, end);
return u(e);
}
string generate_random_string(int length)
{
static string chset = "abcdefghijklmnopqrstuvwxyz1234567890";
string result;
result.resize(length);
srand(time(NULL));
for (int i = 0; i < length; i++) {
static default_random_engine e(time(0));
static uniform_int_distribution<unsigned> u(0, chset.size() - 1);
result[i] = chset[u(e) % chset.length()];
}
return result;
}
int set_container_summary(struct isula_list_response *response, int index)
{
constexpr int id_len = 64;
constexpr int name_len = 8;
response->container_summary[index] = (struct isula_container_summary_info *)util_common_calloc_s(
sizeof(struct isula_container_summary_info));
if (response->container_summary[index] == nullptr) {
return -1;
}
response->container_summary[index]->id = util_strdup_s(generate_random_string(id_len).c_str());
response->container_summary[index]->name = util_strdup_s(generate_random_string(name_len).c_str());
response->container_summary[index]->runtime = util_strdup_s("lcr");
response->container_summary[index]->pid = generate_random_pid();
response->container_summary[index]->status = (Container_Status)CONTAINER_STATUS_RUNNING;
response->container_summary[index]->image = util_strdup_s("busybox:latest");
response->container_summary[index]->command = util_strdup_s("/bin/sh");
response->container_summary[index]->startat = util_strdup_s("2019-12-31T23:55:50.867369507+08:00");
response->container_summary[index]->finishat = util_strdup_s("2020-01-01T23:55:50.867369507+08:00");
response->container_summary[index]->exit_code = 0;
response->container_summary[index]->restart_count = 0;
response->container_summary[index]->created = generate_random_created();
response->container_summary[index]->health_state = util_strdup_s("(healthy)");
response->container_num++;
return 0;
}
} // namespace
int ContainerList(const struct isula_list_request *request,
struct isula_list_response *response, void *arg)
{
(void)request;
(void)arg;
constexpr int container_cnt = 5;
response->cc = 0;
response->server_errono = 0;
response->errmsg = nullptr;
response->container_summary = (struct isula_container_summary_info **)util_common_calloc_s(
sizeof(struct isula_container_summary_info *) * container_cnt);
if (response->container_summary == nullptr) {
return -1;
}
for (size_t i {}; i < container_cnt; ++i) {
if (set_container_summary(response, i)) {
return -1;
}
}
return 0;
}
int invokeGrpcOpsInit(isula_connect_ops *ops)
{
if (ops == nullptr) {
return -1;
}
ops->container.list = &ContainerList;
return 0;
}
TEST_F(ContainerListUnitTest, test_cmd_list_main_all)
{
const char *argv[] = {"isula", "ps", "-a"};
const char *argv_failure[] = {"isula", "ps", "-k"};
isula_connect_ops ops;
ops.container.list = &ContainerList;
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_))
.WillOnce(Return(-1))
.WillOnce(DoAll(SetArgPointee<0>(ByRef(ops)), Return(0)));
ASSERT_EQ(connect_client_ops_init(), -1);
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_list_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_)).WillRepeatedly(Invoke(invokeGrpcOpsInit));
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_list_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
EXPECT_EXIT(cmd_list_main(sizeof(argv_failure) / sizeof(argv_failure[0]), const_cast<const char **>(argv_failure)),
testing::ExitedWithCode(125), "Unkown flag found");
testing::Mock::VerifyAndClearExpectations(&m_grpcClient);
}
TEST_F(ContainerListUnitTest, test_cmd_list_main_format)
{
const char *argv[] = {
"isula", "ps", "-a", "--format", "\"table XXX{{.ID}}AAA{{.Image}}"
" {{.Status}} {{.Pid}} {{.Command}} {{.Created}} {{.Ports}} {{.ExitCode}} "
"{{.RestartCount}} {{.StartAt}} {{.FinishAt}} {{.Runtime}} \t{{.Names}} \n{{.State}}\""
};
const char *argv_failure[] = {"isula", "ps", "--format", "\"{{.ID}} {{.XXX}}"};
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_)).WillRepeatedly(Invoke(invokeGrpcOpsInit));
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_list_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
EXPECT_EXIT(cmd_list_main(sizeof(argv_failure) / sizeof(argv_failure[0]), const_cast<const char **>(argv_failure)),
testing::ExitedWithCode(1), "not support the field");
testing::Mock::VerifyAndClearExpectations(&m_grpcClient);
}
TEST_F(ContainerListUnitTest, test_cmd_list_main_notrunc)
{
const char *argv[] = {"isula", "ps", "-q", "--no-trunc"};
testing::internal::CaptureStdout();
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_)).WillRepeatedly(Invoke(invokeGrpcOpsInit));
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_list_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
std::string output = testing::internal::GetCapturedStdout();
if (output.find("CONTAINER ID") != std::string::npos) {
ADD_FAILURE() << "the output of command('isula ps -q --no-trunc') should not include table headers";
}
std::cout << "Gtest Captured Stdout:" << std::endl << output;
testing::Mock::VerifyAndClearExpectations(&m_grpcClient);
}
TEST_F(ContainerListUnitTest, test_cmd_list_main_debug)
{
const char *argv[] = {"isula", "ps", "-a", "-D"};
testing::internal::CaptureStdout();
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_)).WillRepeatedly(Invoke(invokeGrpcOpsInit));
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_list_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
std::string output = testing::internal::GetCapturedStdout();
std::vector<std::string> tableItems {"CONTAINER ID", "IMAGE", "COMMAND", "CREATED", "STATUS", "PORTS", "NAMES"};
for (const auto &elem : tableItems) {
if (output.find(elem) == std::string::npos) {
ADD_FAILURE() << "container list info should include " << elem;
}
}
if (output.find("healthy") == std::string::npos) {
FAIL() << "container list info should include healthy";
}
std::cout << "Gtest Captured Stdout:" << std::endl << output;
testing::Mock::VerifyAndClearExpectations(&m_grpcClient);
SUCCEED() << "test isula ps --debug success";
}
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
* Description: grpc client mock
* Author: wujing
* Create: 2019-12-19
*/
#include "grpc_client_mock.h"
namespace {
MockGrpcClient *g_grpc_client_mock = NULL;
}
void GrpcClient_SetMock(MockGrpcClient* mock)
{
g_grpc_client_mock = mock;
}
int grpc_ops_init(isula_connect_ops *ops)
{
if (g_grpc_client_mock != nullptr) {
return g_grpc_client_mock->GrpcOpsInit(ops);
}
return 0;
}
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
* Description: grpc client mock
* Author: wujing
* Create: 2019-12-19
*/
#ifndef GRPC_CLIENT_MOCK_H_
#define GRPC_CLIENT_MOCK_H_
#include <gmock/gmock.h>
#include "grpc_client.h"
class MockGrpcClient {
public:
virtual ~MockGrpcClient() = default;
MOCK_METHOD1(GrpcOpsInit, int(isula_connect_ops *ops));
};
void GrpcClient_SetMock(MockGrpcClient* mock);
#endif // GRPC_CLIENT_MOCK_H_
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册